fix: validate platform keys at the gate and stop auto-register
All checks were successful
技能自动化发布 / release (push) Successful in 6s

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-22 15:40:30 +08:00
parent 9a20e7fc69
commit 3f1cde1f99
15 changed files with 543 additions and 214 deletions

View File

@@ -10,10 +10,8 @@ from typing import Optional
from db.accounts_repo import (
insert_account,
resolve_auth_strategy_for_platform,
ensure_platform_from_caller_meta,
)
from db.auth_strategy import (
ALL_AUTH_STRATEGIES,
AUTH_STRATEGY_CLIENT_CERTIFICATE,
AUTH_STRATEGY_DEVICE_BOUND,
AUTH_STRATEGY_PASSWORD_PLUS_2FA,
@@ -22,14 +20,10 @@ from db.auth_strategy import (
AUTH_STRATEGY_SSO_REDIRECT,
)
from db.connection import get_conn, init_db
from util.logging_config import get_skill_logger
from util.platforms import (
DEFAULT_CAPABILITIES_JSON,
PlatformResolveResult,
_platform_list_cn_for_help,
is_valid_platform_key_format,
resolve_platform_key_dynamic,
seed_platforms,
)
@@ -137,7 +131,7 @@ def _remove_profile_dir(path: str) -> None:
@dataclass
class PlatformCallerMeta:
"""调用方为未注册自定义平台提供的自动注册元数据(pick-web / add-web)。"""
"""调用方可附带的平台展示信息。不再用于自动插入 platforms 行。"""
display_name: Optional[str] = None
url: Optional[str] = None
@@ -145,8 +139,17 @@ class PlatformCallerMeta:
auth_strategy: Optional[str] = None
aliases: list[str] = field(default_factory=list)
def is_sufficient_for_auto_register(self) -> bool:
return bool((self.display_name or "").strip() and (self.url or "").strip())
_RESOLVE_CODES_WITHOUT_PLATFORM_LIST = frozenset(
{
"PLATFORM_NOT_REGISTERED",
"INVALID_PLATFORM_KEY",
"PLATFORM_DISABLED",
"AMBIGUOUS_PLATFORM",
"PLATFORM_ALIAS_CONFLICT",
"INVALID_PLATFORM_DISPLAY_NAME",
}
)
def _emit_platform_resolve_error(result: PlatformResolveResult, hint: str = "") -> None:
@@ -164,77 +167,9 @@ def _resolve_or_auto_register(
*,
hint: str = "",
) -> Optional[str]:
"""解析平台;未注册时可在元数据充足时自动 ensure(幂等、仅填空字段)。"""
init_db()
conn = get_conn()
try:
result = resolve_platform_key_dynamic(platform_input, conn=conn)
if result.ok:
return result.platform_key
if result.error_code in ("PLATFORM_DISABLED", "INVALID_PLATFORM_KEY", "INVALID_PLATFORM"):
_emit_platform_resolve_error(result, hint=hint)
return None
if result.error_code != "PLATFORM_NOT_REGISTERED":
_emit_platform_resolve_error(result, hint=hint)
if result.error_code not in (
"PLATFORM_NOT_REGISTERED",
"INVALID_PLATFORM_KEY",
"PLATFORM_DISABLED",
):
_say("支持:" + _platform_list_cn_for_help())
return None
raw_key = (platform_input or "").strip().lower()
if not is_valid_platform_key_format(raw_key):
_emit_platform_resolve_error(
PlatformResolveResult(
error_code="INVALID_PLATFORM_KEY",
message=result.message or f"平台 key 格式非法:「{platform_input}」。",
),
hint=hint,
)
return None
if not meta or not meta.is_sufficient_for_auto_register():
_emit_platform_resolve_error(
PlatformResolveResult(
error_code="PLATFORM_NOT_REGISTERED",
message=(
f"平台 {raw_key} 尚未注册。"
"请传入 --platform-display-name 与 --platform-url,"
"或先执行 account platform ensure。"
),
),
hint=hint,
)
return None
if meta.auth_strategy and meta.auth_strategy not in ALL_AUTH_STRATEGIES:
_say(_err_json(
"ERR_AUTH_STRATEGY_NOT_SUPPORTED",
f"不支持的 --platform-auth-strategy:{meta.auth_strategy}",
))
return None
seed_platforms(conn)
action = ensure_platform_from_caller_meta(
conn,
raw_key,
display_name=(meta.display_name or "").strip(),
domain=(meta.domain or "generic").strip() or "generic",
default_url=(meta.url or "").strip(),
aliases=meta.aliases,
default_auth_strategy=meta.auth_strategy,
capabilities_json=DEFAULT_CAPABILITIES_JSON,
)
get_skill_logger().info(
"platform_auto_registered key=%s action=%s", raw_key, action
)
return raw_key
finally:
conn.close()
"""解析已注册平台;未注册则拒绝。meta 仅兼容旧调用方,不再自动建平台。"""
del meta
return _resolve_or_fail(platform_input, hint=hint)
def _resolve_or_fail(input_name: str, hint: str = "") -> Optional[str]:
@@ -250,6 +185,6 @@ def _resolve_or_fail(input_name: str, hint: str = "") -> Optional[str]:
return result.platform_key
_emit_platform_resolve_error(result, hint=hint)
if result.error_code not in ("PLATFORM_NOT_REGISTERED", "INVALID_PLATFORM_KEY", "PLATFORM_DISABLED"):
if result.error_code not in _RESOLVE_CODES_WITHOUT_PLATFORM_LIST:
_say("支持:" + _platform_list_cn_for_help())
return None

View File

@@ -8,7 +8,7 @@ from db.connection import get_conn, init_db
from service._svc_common import _err_json, _ok_json, _resolve_or_fail, _say
from util.constants import SKILL_SLUG
from util.logging_config import get_skill_logger
from util.platforms import DEFAULT_CAPABILITIES_JSON, is_valid_platform_key_format, seed_platforms
from util.platforms import DEFAULT_CAPABILITIES_JSON, seed_platforms
from util.runtime_paths import get_db_path
@@ -76,6 +76,20 @@ def cmd_platform_get(input_key: str) -> None:
))
def _aliases_for_ensure(aliases: Optional[str], key: str, display_name: str) -> tuple[list[str], str]:
from util.platforms import _parse_aliases_json
claimed: list[str] = []
extra = _parse_aliases_json(aliases) if aliases else []
for item in (display_name, key, *extra):
text = (item or "").strip()
if text and text not in claimed:
claimed.append(text)
if aliases:
return claimed, aliases
return claimed, json.dumps(claimed, ensure_ascii=False)
def cmd_platform_ensure(
key: str,
display_name: str,
@@ -89,25 +103,33 @@ def cmd_platform_ensure(
幂等地注册或更新平台到 DB。
输出单行 JSON:{"ok": true, "platform_key": "xxx", "action": "created|updated"}
"""
from util.platform_validate import validate_platform_registration
log = get_skill_logger()
key = (key or "").strip()
display_name = (display_name or key).strip()
display_name = (display_name or "").strip()
if not key:
_say('ERROR:INVALID_ARGS platform ensure 需要 --key 参数')
return 1
if not is_valid_platform_key_format(key):
_say(_err_json(
"ERROR:INVALID_PLATFORM_KEY",
f"平台 key 格式非法:「{key}」。建议使用小写字母、数字、下划线或连字符。",
))
return 1
aliases_json = aliases if aliases else json.dumps([key, display_name], ensure_ascii=False)
capabilities_json = capabilities if capabilities else DEFAULT_CAPABILITIES_JSON
init_db()
conn = get_conn()
try:
alias_list, aliases_json = _aliases_for_ensure(aliases, key, display_name)
check = validate_platform_registration(
key,
display_name,
aliases=alias_list,
conn=conn,
)
if not check.ok:
_say(_err_json(
f"ERROR:{check.error_code}",
check.message or "平台登记校验失败。",
))
return 1
capabilities_json = capabilities if capabilities else DEFAULT_CAPABILITIES_JSON
existed = bool(
conn.execute(
"SELECT 1 FROM platforms WHERE platform_key = ?", (key,)