S117 权限矩阵审计
这个提交存在于:
父节点
1054787846
当前提交
a1916f9955
共有 3 个文件被更改,包括 63 次插入 和 2 次删除
|
|
@ -13,7 +13,7 @@ from dataclasses import dataclass
|
||||||
from typing import Any, Callable
|
from typing import Any, Callable
|
||||||
|
|
||||||
from backend.dto.common import ErrorCode, error_response
|
from backend.dto.common import ErrorCode, error_response
|
||||||
from backend.route_registry import ROUTES, RouteSpec, assert_unique_routes
|
from backend.route_registry import ROUTES, RouteSpec, assert_known_permissions, assert_unique_routes
|
||||||
from backend.services.ui_service import API_BASE
|
from backend.services.ui_service import API_BASE
|
||||||
from core.identity import RequestContext
|
from core.identity import RequestContext
|
||||||
|
|
||||||
|
|
@ -98,6 +98,10 @@ def runtime_health() -> dict[str, Any]:
|
||||||
assert_unique_routes()
|
assert_unique_routes()
|
||||||
except RuntimeError as exc:
|
except RuntimeError as exc:
|
||||||
errors.append({"code": "DUPLICATE_ROUTE", "message": str(exc)})
|
errors.append({"code": "DUPLICATE_ROUTE", "message": str(exc)})
|
||||||
|
try:
|
||||||
|
assert_known_permissions()
|
||||||
|
except RuntimeError as exc:
|
||||||
|
errors.append({"code": "UNKNOWN_PERMISSION", "message": str(exc)})
|
||||||
importable = 0
|
importable = 0
|
||||||
for route in ROUTES:
|
for route in ROUTES:
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,23 @@ from dataclasses import dataclass
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
|
PERMISSION_MATRIX: dict[str, dict[str, Any]] = {
|
||||||
|
"read": {"auth_required": True, "required_permission": None, "description": "authenticated read"},
|
||||||
|
"write": {"auth_required": True, "required_permission": "can_write", "description": "profile/workflow write"},
|
||||||
|
"config": {
|
||||||
|
"auth_required": True,
|
||||||
|
"required_permission": "can_manage_config",
|
||||||
|
"description": "configuration management",
|
||||||
|
},
|
||||||
|
"materials": {
|
||||||
|
"auth_required": True,
|
||||||
|
"required_permission": "can_manage_materials",
|
||||||
|
"description": "material library management",
|
||||||
|
},
|
||||||
|
"public_token": {"auth_required": False, "required_permission": None, "description": "signed public token"},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@dataclass(frozen=True)
|
@dataclass(frozen=True)
|
||||||
class RouteSpec:
|
class RouteSpec:
|
||||||
method: str
|
method: str
|
||||||
|
|
@ -14,11 +31,14 @@ class RouteSpec:
|
||||||
permission: str
|
permission: str
|
||||||
|
|
||||||
def to_dict(self) -> dict[str, Any]:
|
def to_dict(self) -> dict[str, Any]:
|
||||||
|
matrix = permission_requirement(self.permission)
|
||||||
return {
|
return {
|
||||||
"method": self.method,
|
"method": self.method,
|
||||||
"endpoint": self.endpoint,
|
"endpoint": self.endpoint,
|
||||||
"handler": self.handler,
|
"handler": self.handler,
|
||||||
"permission": self.permission,
|
"permission": self.permission,
|
||||||
|
"auth_required": matrix["auth_required"],
|
||||||
|
"required_permission": matrix["required_permission"],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -159,6 +179,22 @@ def route_manifest() -> list[dict[str, Any]]:
|
||||||
return [route.to_dict() for route in ROUTES]
|
return [route.to_dict() for route in ROUTES]
|
||||||
|
|
||||||
|
|
||||||
|
def permission_requirement(permission: str) -> dict[str, Any]:
|
||||||
|
normalized = str(permission or "").strip()
|
||||||
|
return dict(PERMISSION_MATRIX.get(normalized) or {"auth_required": True, "required_permission": normalized})
|
||||||
|
|
||||||
|
|
||||||
|
def permission_matrix() -> list[dict[str, Any]]:
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
"permission": permission,
|
||||||
|
**definition,
|
||||||
|
"route_count": sum(1 for route in ROUTES if route.permission == permission),
|
||||||
|
}
|
||||||
|
for permission, definition in sorted(PERMISSION_MATRIX.items())
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def assert_unique_routes() -> None:
|
def assert_unique_routes() -> None:
|
||||||
seen: set[tuple[str, str]] = set()
|
seen: set[tuple[str, str]] = set()
|
||||||
for route in ROUTES:
|
for route in ROUTES:
|
||||||
|
|
@ -166,3 +202,9 @@ def assert_unique_routes() -> None:
|
||||||
if key in seen:
|
if key in seen:
|
||||||
raise RuntimeError(f"duplicate route: {route.method} {route.endpoint}")
|
raise RuntimeError(f"duplicate route: {route.method} {route.endpoint}")
|
||||||
seen.add(key)
|
seen.add(key)
|
||||||
|
|
||||||
|
|
||||||
|
def assert_known_permissions() -> None:
|
||||||
|
unknown = sorted({route.permission for route in ROUTES if route.permission not in PERMISSION_MATRIX})
|
||||||
|
if unknown:
|
||||||
|
raise RuntimeError(f"unknown route permissions: {', '.join(unknown)}")
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ from adapters.astrbot_adapter import (
|
||||||
runtime_route_table,
|
runtime_route_table,
|
||||||
)
|
)
|
||||||
from adapters.webui_adapter import get_web_mount_manifest, resolve_web_asset
|
from adapters.webui_adapter import get_web_mount_manifest, resolve_web_asset
|
||||||
from backend.route_registry import ROUTES, assert_unique_routes, route_manifest
|
from backend.route_registry import ROUTES, assert_known_permissions, assert_unique_routes, permission_matrix, route_manifest
|
||||||
from backend.services.web_hosting_service import WebHostingService
|
from backend.services.web_hosting_service import WebHostingService
|
||||||
from core.identity import RequestContext
|
from core.identity import RequestContext
|
||||||
|
|
||||||
|
|
@ -69,6 +69,21 @@ class RouteRegistryTests(unittest.TestCase):
|
||||||
self.assertIn(("POST", "costs/guard"), keys)
|
self.assertIn(("POST", "costs/guard"), keys)
|
||||||
self.assertEqual(len(routes), len(ROUTES))
|
self.assertEqual(len(routes), len(ROUTES))
|
||||||
|
|
||||||
|
def test_permission_matrix_covers_all_routes(self):
|
||||||
|
assert_known_permissions()
|
||||||
|
routes = route_manifest()
|
||||||
|
matrix = {item["permission"]: item for item in permission_matrix()}
|
||||||
|
|
||||||
|
self.assertTrue(all("auth_required" in route for route in routes))
|
||||||
|
self.assertTrue(all("required_permission" in route for route in routes))
|
||||||
|
self.assertEqual(matrix["write"]["required_permission"], "can_write")
|
||||||
|
self.assertEqual(matrix["config"]["required_permission"], "can_manage_config")
|
||||||
|
self.assertEqual(matrix["materials"]["required_permission"], "can_manage_materials")
|
||||||
|
|
||||||
|
public_routes = [route for route in routes if route["permission"] == "public_token"]
|
||||||
|
self.assertEqual([route["endpoint"] for route in public_routes], ["media/fetch"])
|
||||||
|
self.assertFalse(public_routes[0]["auth_required"])
|
||||||
|
|
||||||
def test_webui_adapter_manifest_includes_api_routes(self):
|
def test_webui_adapter_manifest_includes_api_routes(self):
|
||||||
manifest = get_web_mount_manifest()
|
manifest = get_web_mount_manifest()
|
||||||
|
|
||||||
|
|
|
||||||
正在加载…
添加表格
在新工单中引用