clipwright/clipwright/api/restoring_static.py
pedro-bento 1dec0dd1af
All checks were successful
CI / backend-tests (push) Successful in 28s
CI / frontend-build (push) Successful in 20s
chore/dead-code-removal (#4)
Reviewed-on: #4
Co-authored-by: pedro-bento <mail@pbento.pt>
Co-committed-by: pedro-bento <mail@pbento.pt>
2026-10-04 22:11:11 +01:00

48 lines
2.1 KiB
Python

"""Static file serving for ``/videos`` that can refuse non-deliverables.
``/videos`` used to be a plain ``StaticFiles`` mount over ``OUTPUT_DIR``, which
served the whole working directory to anyone holding a 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. All four were verified served, unauthenticated,
on 7-sep-2026.
This subclass keeps everything ``StaticFiles`` does (Range requests, ETags,
HEAD) and adds one step before the filesystem: ``guard`` (the allowlist,
``media_auth.is_servable``, wrapped by ``app._media_guard``) refuses a path, and
a refused path looks exactly like a missing one. ``app.py`` mounts it with the
guard only.
"""
from typing import Callable, Optional
from starlette.exceptions import HTTPException
from starlette.staticfiles import StaticFiles
Guard = Callable[[str], bool]
class RestoringStaticFiles(StaticFiles):
"""StaticFiles that 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.
"""
def __init__(self, *args, guard: Optional[Guard] = None, **kwargs):
super().__init__(*args, **kwargs)
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)
return await super().get_response(path, scope)