diff --git a/app/api/endpoints/site.py b/app/api/endpoints/site.py index 2c677e942..9dc415706 100644 --- a/app/api/endpoints/site.py +++ b/app/api/endpoints/site.py @@ -92,6 +92,15 @@ def _project_agent_site(site: Any, *, include_secrets: bool) -> dict[str, JsonDa return projected +def _project_readable_site( + site: _SchemaSite, token_payload: _SchemaTokenPayload +) -> _SchemaSite | dict[str, JsonData]: + """普通登录用户可读的站点信息不返回认证凭据,超级管理员保持完整字段。""" + if token_payload.super_user: + return site + return _project_agent_site(site, include_secrets=False) + + def _indexer_supports_media_type(indexer: dict, media_type: MediaType) -> bool: """ 判断站点索引器是否支持指定媒体类型。 @@ -686,7 +695,7 @@ async def site_resource( async def read_site_by_domain( site_url: str, query: SiteQueryService = Depends(get_site_query_service), - _: _SchemaTokenPayload = Depends(verify_token), + token_payload: _SchemaTokenPayload = Depends(verify_token), ) -> Any: """ 通过域名获取站点信息 @@ -698,7 +707,7 @@ async def read_site_by_domain( status_code=404, detail=f"站点 {domain} 不存在", ) - return site + return _project_readable_site(site, token_payload) @router.get( @@ -751,10 +760,10 @@ async def read_statistics( async def read_rss_sites( response: Response = None, query: SiteQueryService = Depends(get_site_query_service), - _: _SchemaTokenPayload = Depends(verify_token), + token_payload: _SchemaTokenPayload = Depends(verify_token), page: CompatiblePageParam = None, count: CompatibleCountParam = None, -) -> List[dict]: +) -> List[_SchemaSite | dict[str, JsonData]]: """ 获取站点列表 """ @@ -767,11 +776,12 @@ async def read_rss_sites( response.headers[COLLECTION_TOTAL_HEADER] = str( await query.count_ordered(site_ids=site_ids) ) - return await query.list_ordered( + sites = await query.list_ordered( site_ids=site_ids, page=page, count=count, ) + return [_project_readable_site(site, token_payload) for site in sites] @router.get("/auth", summary="查询认证站点", response_model=_SchemaJsonObject) diff --git a/docs/refactor/optimization-checklist.md b/docs/refactor/optimization-checklist.md index bc703c43d..1ea646db7 100644 --- a/docs/refactor/optimization-checklist.md +++ b/docs/refactor/optimization-checklist.md @@ -106,7 +106,7 @@ ARCH-201 至 ARCH-204 均达到实现、验证、提交、推送和远端门禁 | Event Contract | 53 | 均已有 payload model,但当前全部是 diagnostic enforcement | | Python 源码量 | 305,884 行 | 排除 `app/plugins/**`;61 个文件超过 1,000 行,11 个超过 2,000 行 | | 长方法 | 290 个超过 80 行 | AST 统计排除 `app/plugins/**`;65 个超过 150 行,21 个超过 250 行 | -| 全量 mypy 历史债务 | 9,176 / 508 文件 | 系统版本查询补齐类型后的现状基线;消息媒体交互类型债务已清零,canonical Facade 与 endpoint 类型边界已补齐,低水位只允许继续下降 | +| 全量 mypy 历史债务 | 9,174 / 508 文件 | 系统版本查询补齐类型后的现状基线;消息媒体交互类型债务已清零,canonical Facade 与 endpoint 类型边界已补齐,低水位只允许继续下降 | | Ruff 历史诊断 | 505 | 低水位门禁通过,但规则集只覆盖 `E4/E7/E9/F/I` | | 覆盖率固定基线 | Application 80.00%,Domain 80.00% | Chain、Runtime、Agent、Adapter、Startup 未进入包级覆盖率门禁 | diff --git a/tests/fixtures/architecture/mypy-baseline.json b/tests/fixtures/architecture/mypy-baseline.json index bece4a999..d2ace77cf 100644 --- a/tests/fixtures/architecture/mypy-baseline.json +++ b/tests/fixtures/architecture/mypy-baseline.json @@ -517,8 +517,8 @@ "attr-defined": 2, "misc": 26, "no-untyped-def": 2, - "return-value": 2, - "type-arg": 6 + "return-value": 1, + "type-arg": 5 }, "app/api/endpoints/storage.py": { "assignment": 1, diff --git a/tests/test_agent_api_projection_endpoints.py b/tests/test_agent_api_projection_endpoints.py index a97ffb583..bb909c923 100644 --- a/tests/test_agent_api_projection_endpoints.py +++ b/tests/test_agent_api_projection_endpoints.py @@ -7,6 +7,7 @@ from app.api.endpoints import site as site_endpoint from app.api.endpoints import storage as storage_endpoint from app.api.endpoints import workflow as workflow_endpoint from app.schemas.file import FileItem +from app.schemas.token import TokenPayload class _SiteQuery: @@ -17,6 +18,7 @@ class _SiteQuery: *, is_active=None, name=None, + site_ids=None, page=None, count=None, ): @@ -49,11 +51,18 @@ class _SiteQuery: sites = [site for site in sites if site.is_active is is_active] if name: sites = [site for site in sites if name.lower() in site.name.lower()] + if site_ids is not None: + sites = [site for site in sites if site.id in site_ids] if page is not None and count is not None: offset = (page - 1) * count sites = sites[offset:offset + count] return sites + async def get_by_domain(self, domain): + """按域名返回第一个固定站点。""" + sites = await self.list_ordered() + return next((site for site in sites if site.domain == domain), None) + class _WorkflowQuery: """Return workflows with private action context that the Agent projection must omit.""" @@ -150,6 +159,68 @@ def test_site_agent_projection_returns_auth_fields_only_to_superusers() -> None: assert result[0]["apikey"] == "secret-key" +def _token(*, super_user: bool) -> TokenPayload: + return TokenPayload(sub=1, username="tester", super_user=super_user, purpose="authentication") + + +def test_site_rss_list_hides_secrets_for_normal_users(monkeypatch) -> None: + """订阅站点列表对普通用户只返回选择站点所需的字段,不返回认证凭据。""" + monkeypatch.setattr( + site_endpoint, + "get_configured_system_config", + lambda: SimpleNamespace(get=lambda _key: []), + ) + + result = asyncio.run( + site_endpoint.read_rss_sites(query=_SiteQuery(), token_payload=_token(super_user=False)) + ) + + assert [item["name"] for item in result] == ["Active Site", "Inactive Site"] + assert all( + key not in item + for item in result + for key in ("rss", "cookie", "apikey", "token") + ) + + +def test_site_rss_list_keeps_secrets_for_superusers(monkeypatch) -> None: + """超级管理员读取订阅站点列表时保持原有完整字段。""" + monkeypatch.setattr( + site_endpoint, + "get_configured_system_config", + lambda: SimpleNamespace(get=lambda _key: ["1"]), + ) + + result = asyncio.run( + site_endpoint.read_rss_sites(query=_SiteQuery(), token_payload=_token(super_user=True)) + ) + + assert [site.name for site in result] == ["Active Site"] + assert result[0].cookie == "secret-cookie" + + +def test_site_by_domain_hides_secrets_for_normal_users() -> None: + """按域名查询站点时,普通用户拿不到认证凭据,超级管理员保持完整字段。""" + normal = asyncio.run( + site_endpoint.read_site_by_domain( + site_url="https://example.invalid/", + query=_SiteQuery(), + token_payload=_token(super_user=False), + ) + ) + admin = asyncio.run( + site_endpoint.read_site_by_domain( + site_url="https://example.invalid/", + query=_SiteQuery(), + token_payload=_token(super_user=True), + ) + ) + + assert normal["domain"] == "example.invalid" + assert all(key not in normal for key in ("rss", "cookie", "apikey", "token")) + assert admin.apikey == "secret-key" + + def test_workflow_agent_projection_filters_without_returning_action_context() -> None: """User-level workflow discovery must expose state without private execution payloads.""" result = asyncio.run(