fix(videos): stop serving the working directory, only deliverables
media_auth landed in 96f0139 with the allowlist and the capability tokens,
but nothing imported it: app.py still mounted /videos as a plain
StaticFiles over OUTPUT_DIR. The module was deployed as dead code and the
hole it was written to close stayed open.
Verified against production on 7-sep-2026, unauthenticated, with nothing
but a job id (which travels in every clip URL the dashboard builds):
/videos/<job>/.owner -> 200, a user uuid
/videos/<job>/<title>_metadata.json -> 200, the full transcript
/videos/<job>/<title>.mp4 -> 200, the untouched source
Wiring media_auth.is_servable into the mount closes the first two, plus
.resume.json (which carries the customer's own webhook_secret),
.transcript_checkpoint.json, .instance and the .layout.json sidecars. A
refused path 404s before touching the filesystem, so it is indistinguishable
from a missing one.
Safe for every consumer: the dashboard reads transcripts through the
authenticated /api/clip/{job}/{i}/transcript, never from /videos, and every
/videos URL the backend builds is a clip, preview (.mp4), scene thumbnail
(.jpg), actor upload (.png) or subtitle (.ass) — all allowlisted.
This is the path half only. The clips themselves and the source video in
the same directory are still public to anyone holding the job id; closing
that needs the capability tokens media_auth also provides, which means
request handlers, an /api/media-token endpoint and the dashboard appending
the token in getApiUrl. That ships with its frontend half, not here.
Claude-Session: https://claude.ai/code/session_01UZGqjYcZyfmKww1gcyKrp5
This commit is contained in:
parent
978e549c2e
commit
134ad5431e
3 changed files with 76 additions and 1 deletions
2
app.py
2
app.py
|
|
@ -1734,8 +1734,10 @@ app.add_middleware(
|
||||||
# of a reopened project used to 404 while the transcript requests were still
|
# of a reopened project used to 404 while the transcript requests were still
|
||||||
# restoring it. Late-bound lambda: the restorer is defined further down.
|
# restoring it. Late-bound lambda: the restorer is defined further down.
|
||||||
from restoring_static import RestoringStaticFiles
|
from restoring_static import RestoringStaticFiles
|
||||||
|
import media_auth
|
||||||
app.mount("/videos", RestoringStaticFiles(
|
app.mount("/videos", RestoringStaticFiles(
|
||||||
directory=OUTPUT_DIR,
|
directory=OUTPUT_DIR,
|
||||||
|
guard=media_auth.is_servable,
|
||||||
restorer=lambda job_id: _restore_for_public_path(job_id)), name="videos")
|
restorer=lambda job_id: _restore_for_public_path(job_id)), name="videos")
|
||||||
|
|
||||||
# Mount static files for serving thumbnails
|
# Mount static files for serving thumbnails
|
||||||
|
|
|
||||||
|
|
@ -22,14 +22,38 @@ from starlette.exceptions import HTTPException
|
||||||
from starlette.staticfiles import StaticFiles
|
from starlette.staticfiles import StaticFiles
|
||||||
|
|
||||||
Restorer = Callable[[str], Awaitable[bool]]
|
Restorer = Callable[[str], Awaitable[bool]]
|
||||||
|
Guard = Callable[[str], bool]
|
||||||
|
|
||||||
|
|
||||||
class RestoringStaticFiles(StaticFiles):
|
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)
|
super().__init__(*args, **kwargs)
|
||||||
self.restorer = restorer
|
self.restorer = restorer
|
||||||
|
self.guard = guard
|
||||||
|
|
||||||
async def get_response(self, path: str, scope):
|
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:
|
try:
|
||||||
return await super().get_response(path, scope)
|
return await super().get_response(path, scope)
|
||||||
except HTTPException as exc:
|
except HTTPException as exc:
|
||||||
|
|
|
||||||
49
tests/test_media_path_guard.py
Normal file
49
tests/test_media_path_guard.py
Normal file
|
|
@ -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 `<title>_metadata.json`, which carries the full transcript of their video.
|
||||||
|
All four were verified served, unauthenticated, against production on
|
||||||
|
7-sep-2026. media_auth shipped the allowlist in 96f0139 but nothing imported it.
|
||||||
|
|
||||||
|
This is the path half only: the clips and the untouched source video in the
|
||||||
|
same directory are still public to whoever has the job id.
|
||||||
|
"""
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
media_auth = pytest.importorskip("media_auth")
|
||||||
|
restoring_static = pytest.importorskip("restoring_static")
|
||||||
|
|
||||||
|
|
||||||
|
class TestTheGuardIsWiredIn:
|
||||||
|
def test_the_videos_mount_carries_the_allowlist(self):
|
||||||
|
app = pytest.importorskip("app")
|
||||||
|
mount = next(r for r in app.app.routes
|
||||||
|
if getattr(r, "name", None) == "videos")
|
||||||
|
assert mount.app.guard is media_auth.is_servable, \
|
||||||
|
"/videos is serving the working directory with no allowlist"
|
||||||
|
|
||||||
|
|
||||||
|
class TestWhatMayLeave:
|
||||||
|
@pytest.mark.parametrize("rel", [
|
||||||
|
"job/clip_1.mp4", "job/subtitled_1_clip_1.mp4", "job/a.webm",
|
||||||
|
"job/autosubs_1.ass", "job/subs.srt", "job/thumb.jpg", "job/clips.zip",
|
||||||
|
])
|
||||||
|
def test_deliverables_pass(self, rel):
|
||||||
|
assert media_auth.is_servable(rel)
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("rel", [
|
||||||
|
"job/.owner",
|
||||||
|
"job/.resume.json",
|
||||||
|
"job/.transcript_checkpoint.json",
|
||||||
|
"job/My_Video_metadata.json",
|
||||||
|
"job/clip_1.mp4.layout.json",
|
||||||
|
"job/.instance",
|
||||||
|
"../app.py",
|
||||||
|
"job/../../app.py",
|
||||||
|
"/etc/passwd",
|
||||||
|
"",
|
||||||
|
])
|
||||||
|
def test_everything_else_is_refused(self, rel):
|
||||||
|
assert not media_auth.is_servable(rel)
|
||||||
Loading…
Reference in a new issue