diff --git a/app.py b/app.py index 1ea5fd5..9aabdbb 100644 --- a/app.py +++ b/app.py @@ -1734,8 +1734,10 @@ app.add_middleware( # of a reopened project used to 404 while the transcript requests were still # restoring it. Late-bound lambda: the restorer is defined further down. from restoring_static import RestoringStaticFiles +import media_auth app.mount("/videos", RestoringStaticFiles( directory=OUTPUT_DIR, + guard=media_auth.is_servable, restorer=lambda job_id: _restore_for_public_path(job_id)), name="videos") # Mount static files for serving thumbnails diff --git a/restoring_static.py b/restoring_static.py index f8ed1f0..8018783 100644 --- a/restoring_static.py +++ b/restoring_static.py @@ -22,14 +22,38 @@ from starlette.exceptions import HTTPException from starlette.staticfiles import StaticFiles Restorer = Callable[[str], Awaitable[bool]] +Guard = Callable[[str], bool] class RestoringStaticFiles(StaticFiles): - def __init__(self, *args, restorer: Optional[Restorer] = None, **kwargs): + """StaticFiles that can restore a missing job, and refuses non-deliverables. + + ``guard`` is the allowlist (``media_auth.is_servable``). Without it this + mount hands out the whole working directory to anyone holding the job id: + ``.owner`` (a user uuid), ``.resume.json`` (the customer's own + ``webhook_secret``), ``.transcript_checkpoint.json``, and the + ``*_metadata.json`` carrying the full transcript of the user's video. + Verified served, unauthenticated, on 7-sep-2026. + + This is only the path half of media_auth: the clips themselves, and the + untouched source video sitting in the same directory, are still public to + anyone who has the job id. Closing that needs the capability tokens the + module was written for, which is a bigger change (request handlers, an + /api/media-token endpoint, and the dashboard appending the token to every + media URL) and must ship with its frontend half. + """ + + def __init__(self, *args, restorer: Optional[Restorer] = None, + guard: Optional[Guard] = None, **kwargs): super().__init__(*args, **kwargs) self.restorer = restorer + self.guard = guard async def get_response(self, path: str, scope): + # Before the filesystem: a refused path must look exactly like a + # missing one, or the 404-vs-403 difference confirms the file is there. + if self.guard is not None and not self.guard(path): + raise HTTPException(status_code=404) try: return await super().get_response(path, scope) except HTTPException as exc: diff --git a/tests/test_media_path_guard.py b/tests/test_media_path_guard.py new file mode 100644 index 0000000..af1a6ff --- /dev/null +++ b/tests/test_media_path_guard.py @@ -0,0 +1,49 @@ +"""/videos must hand out deliverables, not the whole working directory. + +The mount was a plain StaticFiles over OUTPUT_DIR, so anyone holding a job id +(it travels in every clip URL) could also fetch `.owner` (a user uuid), +`.resume.json` (the customer's own webhook_secret), `.transcript_checkpoint.json` +and `