Reviewed-on: #4 Co-authored-by: pedro-bento <mail@pbento.pt> Co-committed-by: pedro-bento <mail@pbento.pt>
48 lines
2.1 KiB
Python
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)
|