Fix design validation, canonical routes, media headers and favicon

This commit is contained in:
2026-11-23 18:52:26 +08:00
parent 01ede89dcd
commit 584455c66d
18 changed files with 153 additions and 65 deletions
+22 -18
View File
@@ -93,14 +93,14 @@ final class DesignLibrary
public static function normalize(string $kind, string $preset, array $data): array
{
if (!isset(self::PRESETS[$kind][$preset])) {
throw new RuntimeException('请选择可用的组件模板。');
throw new DesignFieldError('preset', '请选择可用的组件模板。');
}
if ($kind === 'palette') {
$colors = [];
foreach (self::COLORS as $key => [, $default]) {
$color = $data[$key] ?? $default;
if (!is_string($color) || !preg_match('/^#[\da-f]{6}$/iD', $color)) {
throw new RuntimeException('颜色必须使用六位十六进制表示。');
throw new DesignFieldError($key, '颜色必须使用六位十六进制表示。');
}
$colors[$key] = strtolower($color);
}
@@ -110,23 +110,27 @@ final class DesignLibrary
$clean = self::defaults($kind);
foreach (['brand' => 100, 'text' => 1200, 'legal' => 300, 'logo' => 1000] as $field => $limit) {
$clean[$field] = self::string($data[$field] ?? '', $limit);
$clean[$field] = self::string($data[$field] ?? '', $limit, $field);
}
if ($clean['logo'] !== '' && !Html::url($clean['logo'], true)) {
throw new RuntimeException('请检查站点标识地址。');
throw new DesignFieldError('logo', '请检查站点标识地址。');
}
$components = $data['components'] ?? $clean['components'];
if (!is_array($components) || !array_is_list($components)) {
throw new RuntimeException('组件顺序无效。');
throw new DesignFieldError('components', '组件顺序无效。');
}
foreach ($components as $component) {
if (!is_string($component) || !isset(self::COMPONENTS[$kind][$component])) {
throw new RuntimeException('布局包含未知组件。');
throw new DesignFieldError('components', '布局包含未知组件。');
}
}
$clean['components'] = array_values(array_unique($components));
$clean['links'] = Buttons::validate($data['links'] ?? $clean['links'], 20);
$clean['action'] = Buttons::validate($data['action'] ?? []);
try {
$clean['action'] = Buttons::validate($data['action'] ?? []);
} catch (RuntimeException $error) {
throw new DesignFieldError('action', $error->getMessage());
}
$clean['provider'] = $kind === 'header' && $preset === 'member'
? self::provider($data['provider'] ?? []) : [];
@@ -140,27 +144,27 @@ final class DesignLibrary
return [];
}
if (!in_array($mode, ['oidc', 'oauth2'], true)) {
throw new RuntimeException('请选择 OIDC 或 OAuth 2.0。');
throw new DesignFieldError('provider_mode', '请选择 OIDC 或 OAuth 2.0。');
}
$result = ['mode' => $mode];
foreach (['client_id', 'scope', 'issuer', 'authorization', 'token', 'userinfo', 'subject_field', 'name_field', 'avatar_field', 'token_auth'] as $field) {
$result[$field] = self::string($data[$field] ?? '', 600);
$result[$field] = self::string($data[$field] ?? '', 600, 'provider_' . $field);
}
if (!in_array($result['token_auth'], ['', 'client_secret_basic', 'client_secret_post'], true)) {
throw new RuntimeException('令牌端点认证方式无效。');
throw new DesignFieldError('provider_token_auth', '令牌端点认证方式无效。');
}
if ($result['client_id'] === '') {
throw new RuntimeException('请填写客户端 ID。');
throw new DesignFieldError('provider_client_id', '请填写客户端 ID。');
}
foreach ($mode === 'oidc' ? ['issuer'] : ['authorization', 'token', 'userinfo'] as $field) {
$url = parse_url($result[$field]);
if (!$url || ($url['scheme'] ?? '') !== 'https' || empty($url['host'])
|| isset($url['user']) || isset($url['pass']) || isset($url['fragment']) || ($field === 'issuer' && isset($url['query'])) || !Html::url($result[$field], true)) {
throw new RuntimeException('认证端点必须使用完整的 HTTPS 地址。');
throw new DesignFieldError('provider_' . $field, '认证端点必须使用完整的 HTTPS 地址。');
}
}
if ($mode === 'oauth2' && $result['subject_field'] === '') {
throw new RuntimeException('OAuth 2.0 用户资料需要指定稳定的用户 ID 字段。');
throw new DesignFieldError('provider_subject_field', 'OAuth 2.0 用户资料需要指定稳定的用户 ID 字段。');
}
return $result;
@@ -183,9 +187,9 @@ final class DesignLibrary
public static function save(string $kind, ?int $id, string $name, string $preset, array $data, int $version, bool $default, string $secret = ''): int
{
$name = self::string($name, 100);
$name = self::string($name, 100, 'name');
if ($name === '') {
throw new RuntimeException('请填写配置名称。');
throw new DesignFieldError('name', '请填写配置名称。');
}
$clean = self::normalize($kind, $preset, $data);
$payload = Database::json($clean);
@@ -198,7 +202,7 @@ final class DesignLibrary
$fields = array_flip(['mode', 'issuer', 'authorization', 'token', 'userinfo', 'client_id']);
if (empty($saved['credential_revision'])
|| array_intersect_key($provider, $fields) !== array_intersect_key($saved['payload']['provider'], $fields)) {
throw new RuntimeException('启用登录或变更身份提供商时,请填写客户端密钥。');
throw new DesignFieldError('provider_secret', '启用登录或变更身份提供商时,请填写客户端密钥。');
}
}
if ($id === null) {
@@ -232,10 +236,10 @@ final class DesignLibrary
});
}
private static function string(mixed $value, int $limit): string
private static function string(mixed $value, int $limit, string $field): string
{
if (!is_string($value) || !mb_check_encoding($value, 'UTF-8') || mb_strlen($value) > $limit) {
throw new RuntimeException('配置包含无效或过长的文字。');
throw new DesignFieldError($field, '配置包含无效或过长的文字。');
}
return trim($value);