88 行
3.5 KiB
Python
88 行
3.5 KiB
Python
"""Central owner-chain enforcement for dispatched routes.
|
|
|
|
``ResourceAccessService`` already implements the full profile -> workflow ->
|
|
node -> image -> task ownership chain, but only three call sites used it. The
|
|
web routes resolved a path parameter straight into a service call after nothing
|
|
more than ``require_authenticated``, so any logged-in user could read or mutate
|
|
another user's workflow, node, image, or generation task by guessing an ID
|
|
(``GET jobs`` even returned the full generation request body).
|
|
|
|
Enforcing per-handler would mean touching ~60 handlers and would silently miss
|
|
every route added later. Instead this runs once in the dispatcher, keyed on the
|
|
path parameter name, which makes ownership a property of the route table rather
|
|
than of whoever wrote the handler.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any, Callable
|
|
|
|
from core.identity import RequestContext
|
|
|
|
# Path parameters that name an owned resource, mapped to the
|
|
# ``ResourceAccessService`` method that walks its chain up to a profile owner.
|
|
#
|
|
# Deliberately limited to the five chains that terminate at ``profile.owner_
|
|
# user_id``. Material-domain identifiers (``material_id``, ``category_id``,
|
|
# ``import_id``, ...) follow a different, library-wide sharing model and are
|
|
# gated by the ``materials`` permission tier instead.
|
|
OWNED_PARAM_RESOLVERS: dict[str, str] = {
|
|
"profile_id": "require_profile",
|
|
"workflow_id": "require_workflow",
|
|
"node_id": "require_node",
|
|
"image_id": "require_image",
|
|
"task_id": "require_task",
|
|
}
|
|
|
|
# Routes whose declared permission tier means "no session", so there is no
|
|
# actor to compare an owner against.
|
|
UNAUTHENTICATED_PERMISSIONS = frozenset({"public_token"})
|
|
|
|
|
|
def enforce_route_ownership(
|
|
ctx: RequestContext,
|
|
permission: str,
|
|
path_params: dict[str, str],
|
|
access_service_factory: Callable[[], Any],
|
|
) -> None:
|
|
"""Raise ``RESOURCE_NOT_FOUND`` if ``ctx`` does not own the addressed resource.
|
|
|
|
Only the most specific identifier present is checked: ``require_node``
|
|
already walks up through the workflow to the profile, so checking every
|
|
parameter would issue redundant queries for the same chain.
|
|
"""
|
|
|
|
if permission in UNAUTHENTICATED_PERMISSIONS:
|
|
return
|
|
if not path_params:
|
|
return
|
|
|
|
for param in ("image_id", "task_id", "node_id", "workflow_id", "profile_id"):
|
|
value = path_params.get(param)
|
|
if not value:
|
|
continue
|
|
resolver_name = OWNED_PARAM_RESOLVERS[param]
|
|
service = access_service_factory()
|
|
getattr(service, resolver_name)(value, ctx)
|
|
return
|
|
|
|
|
|
def create_resource_access_service() -> Any:
|
|
"""Build a ``ResourceAccessService`` against the default database."""
|
|
|
|
# Imported lazily so the dispatcher does not pay for the database stack on
|
|
# routes that carry no owned identifier.
|
|
from backend.services.resource_access_service import ResourceAccessService
|
|
from database.db_helper import Database, load_database_settings
|
|
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
|
|
|
|
database = Database(load_database_settings())
|
|
return ResourceAccessService(
|
|
ProfileRepository(database),
|
|
WorkflowRepository(database),
|
|
ImageRepository(database),
|
|
JobRepository(database),
|
|
)
|