181 行
5.6 KiB
Python
181 行
5.6 KiB
Python
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from backend.dto.common import ApiError, ErrorCode
|
|
from backend.services.resource_access_service import ResourceAccessService
|
|
from backend.services.route_ownership_guard import enforce_route_ownership
|
|
from core.identity import RequestContext
|
|
from database.db_helper import Database, DatabaseSettings, SqliteSettings
|
|
from database.repositories.image_repository import ImageRepository
|
|
from database.repositories.job_repository import JobRepository
|
|
from database.repositories.profile_repository import ProfileRepository
|
|
from database.repositories.workflow_repository import WorkflowRepository
|
|
|
|
|
|
@pytest.fixture()
|
|
def access_service(tmp_path):
|
|
database = Database(
|
|
DatabaseSettings(
|
|
backend="sqlite",
|
|
sqlite=SqliteSettings(path=str(tmp_path / "idor.sqlite3")),
|
|
)
|
|
)
|
|
database.initialize()
|
|
profiles = ProfileRepository(database)
|
|
workflows = WorkflowRepository(database)
|
|
images = ImageRepository(database)
|
|
jobs = JobRepository(database)
|
|
|
|
profiles.create(
|
|
{
|
|
"profile_id": "profile-a",
|
|
"name": "Alice's character",
|
|
"owner_platform": "qq",
|
|
"owner_user_id": "user-a",
|
|
"status": "active",
|
|
"metadata": {},
|
|
}
|
|
)
|
|
workflows.create_workflow(
|
|
{
|
|
"workflow_id": "workflow-a",
|
|
"profile_id": "profile-a",
|
|
"workflow_type": "single_image",
|
|
"display_name": "Alice's flow",
|
|
"status": "draft",
|
|
"metadata": {},
|
|
}
|
|
)
|
|
workflows.create_node(
|
|
{
|
|
"node_id": "node-a",
|
|
"workflow_id": "workflow-a",
|
|
"node_key": "virtual_single_image",
|
|
"title": "Image",
|
|
"status": "draft",
|
|
"branch_name": "main",
|
|
"sort_order": 10,
|
|
"metadata": {},
|
|
}
|
|
)
|
|
return ResourceAccessService(profiles, workflows, images, jobs)
|
|
|
|
|
|
def _ctx(user: str, permissions: frozenset[str] = frozenset()) -> RequestContext:
|
|
return RequestContext.from_astrbot(
|
|
username=user,
|
|
user_id=user,
|
|
client_ip="127.0.0.1",
|
|
source="test",
|
|
permissions=permissions,
|
|
)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("param", "value"),
|
|
[
|
|
("profile_id", "profile-a"),
|
|
("workflow_id", "workflow-a"),
|
|
("node_id", "node-a"),
|
|
],
|
|
)
|
|
def test_other_user_cannot_reach_the_resource(access_service, param, value) -> None:
|
|
"""The IDOR the audit found: a guessed ID reached another user's data."""
|
|
|
|
with pytest.raises(ApiError) as excinfo:
|
|
enforce_route_ownership(_ctx("user-b"), "read", {param: value}, lambda: access_service)
|
|
|
|
# Not-found semantics, so IDs cannot be enumerated by probing for 403s.
|
|
assert excinfo.value.error_code == ErrorCode.RESOURCE_NOT_FOUND
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("param", "value"),
|
|
[
|
|
("profile_id", "profile-a"),
|
|
("workflow_id", "workflow-a"),
|
|
("node_id", "node-a"),
|
|
],
|
|
)
|
|
def test_owner_is_allowed_through(access_service, param, value) -> None:
|
|
enforce_route_ownership(_ctx("user-a"), "read", {param: value}, lambda: access_service)
|
|
|
|
|
|
def test_manage_all_profiles_grants_access(access_service) -> None:
|
|
"""Administrators keep working, via the shared ownership predicate."""
|
|
|
|
enforce_route_ownership(
|
|
_ctx("admin", frozenset({"can_manage_all_profiles"})),
|
|
"read",
|
|
{"workflow_id": "workflow-a"},
|
|
lambda: access_service,
|
|
)
|
|
|
|
|
|
def test_public_token_routes_skip_the_owner_chain(access_service) -> None:
|
|
"""media/fetch has no session to compare an owner against."""
|
|
|
|
enforce_route_ownership(
|
|
_ctx("", frozenset()),
|
|
"public_token",
|
|
{"image_id": "anything"},
|
|
lambda: access_service,
|
|
)
|
|
|
|
|
|
def test_routes_without_owned_parameters_do_no_database_work() -> None:
|
|
"""Ownership must not add a query to routes that address nothing owned."""
|
|
|
|
def _explode(): # pragma: no cover - must never be called
|
|
raise AssertionError("access service should not be constructed")
|
|
|
|
enforce_route_ownership(_ctx("user-a"), "read", {}, _explode)
|
|
enforce_route_ownership(_ctx("user-a"), "read", {"category_id": "cat-1"}, _explode)
|
|
|
|
|
|
def test_most_specific_identifier_wins(access_service) -> None:
|
|
"""node_id already walks up to the profile; do not re-check every level."""
|
|
|
|
calls: list[str] = []
|
|
|
|
class _Counting:
|
|
def __getattr__(self, name):
|
|
def _call(value, ctx):
|
|
calls.append(name)
|
|
return getattr(access_service, name)(value, ctx)
|
|
|
|
return _call
|
|
|
|
enforce_route_ownership(
|
|
_ctx("user-a"),
|
|
"read",
|
|
{"workflow_id": "workflow-a", "node_id": "node-a"},
|
|
lambda: _Counting(),
|
|
)
|
|
assert calls == ["require_node"]
|
|
|
|
|
|
def test_dispatcher_actually_invokes_the_ownership_guard(monkeypatch, access_service) -> None:
|
|
"""End-to-end: the wiring in dispatch_extension_request must be live.
|
|
|
|
The unit tests above prove the guard is correct; this proves it runs.
|
|
"""
|
|
|
|
from adapters import astrbot_adapter
|
|
|
|
monkeypatch.setattr(
|
|
astrbot_adapter, "create_resource_access_service", lambda: access_service
|
|
)
|
|
|
|
denied = astrbot_adapter.dispatch_extension_request(
|
|
_ctx("user-b"), "GET", "workflows/workflow-a"
|
|
)
|
|
assert denied["ok"] is False
|
|
assert denied["error_code"] == ErrorCode.RESOURCE_NOT_FOUND
|
|
|
|
# The owner gets past the guard (whatever the handler then does).
|
|
allowed = astrbot_adapter.dispatch_extension_request(
|
|
_ctx("user-a"), "GET", "workflows/workflow-a"
|
|
)
|
|
assert allowed.get("error_code") != ErrorCode.RESOURCE_NOT_FOUND
|