Spaces:
Sleeping
Sleeping
Claude commited on
Commit ·
a037749
1
Parent(s): 2ebc042
Validate segment filenames in /jobs/{id}/zip to block path traversal
Browse filesThe endpoint trusted state.segments[*].image_filename and audio_filename
when joining them to frames_dir, so a crafted name like ../../etc/hosts
would resolve outside the temp dir and bundle_zip would write that file
into the archive as static/<basename>. Filenames now must match the
same regex patterns the pipeline produces (primary, alt, or audio) or
they are dropped, keeping resolved paths under frames_dir.
- app/main.py +16 -2
- tests/test_progress.py +73 -0
app/main.py
CHANGED
|
@@ -584,9 +584,23 @@ async def fetch_job_zip(job_id: str, request: Request) -> Response:
|
|
| 584 |
except Exception as exc:
|
| 585 |
raise HTTPException(500, f"Render failed: {exc}")
|
| 586 |
|
| 587 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 588 |
audio_paths = [
|
| 589 |
-
frames_dir / s.audio_filename
|
|
|
|
|
|
|
| 590 |
]
|
| 591 |
zip_bytes = bundle_zip(
|
| 592 |
html,
|
|
|
|
| 584 |
except Exception as exc:
|
| 585 |
raise HTTPException(500, f"Render failed: {exc}")
|
| 586 |
|
| 587 |
+
# Filenames come from client-supplied state — only accept the
|
| 588 |
+
# exact patterns the pipeline produces so a crafted name with
|
| 589 |
+
# path separators or `..` can't escape `frames_dir` and pull
|
| 590 |
+
# arbitrary readable files into the bundle.
|
| 591 |
+
frame_paths = [
|
| 592 |
+
frames_dir / s.image_filename
|
| 593 |
+
for s in page.segments
|
| 594 |
+
if s.image_filename
|
| 595 |
+
and (
|
| 596 |
+
_PRIMARY_FILENAME_RE.match(s.image_filename)
|
| 597 |
+
or _ALT_FILENAME_RE.match(s.image_filename)
|
| 598 |
+
)
|
| 599 |
+
]
|
| 600 |
audio_paths = [
|
| 601 |
+
frames_dir / s.audio_filename
|
| 602 |
+
for s in page.segments
|
| 603 |
+
if s.audio_filename and _AUDIO_FILENAME_RE.match(s.audio_filename)
|
| 604 |
]
|
| 605 |
zip_bytes = bundle_zip(
|
| 606 |
html,
|
tests/test_progress.py
CHANGED
|
@@ -225,3 +225,76 @@ def test_full_sse_job_lifecycle(tmp_path):
|
|
| 225 |
assert resp.status_code == 200
|
| 226 |
assert resp.headers["content-type"].startswith("text/html")
|
| 227 |
assert b"<title>SSE Test</title>" in resp.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
assert resp.status_code == 200
|
| 226 |
assert resp.headers["content-type"].startswith("text/html")
|
| 227 |
assert b"<title>SSE Test</title>" in resp.content
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
def test_zip_endpoint_rejects_path_traversal_filenames(tmp_path):
|
| 231 |
+
"""A crafted state.segments[*].{image,audio}_filename must not pull
|
| 232 |
+
arbitrary readable files into the bundle. The endpoint should drop
|
| 233 |
+
any filename that doesn't match the pipeline's own naming patterns
|
| 234 |
+
so `frames_dir / filename` can never resolve outside the temp dir.
|
| 235 |
+
"""
|
| 236 |
+
import io
|
| 237 |
+
import zipfile
|
| 238 |
+
|
| 239 |
+
from app.main import registry
|
| 240 |
+
|
| 241 |
+
# Plant a sentinel file outside the temp `static/` workdir that a
|
| 242 |
+
# traversal payload would otherwise read.
|
| 243 |
+
secret = tmp_path / "outside_secret.txt"
|
| 244 |
+
secret.write_text("sentinel-secret-bytes")
|
| 245 |
+
|
| 246 |
+
job = registry.create()
|
| 247 |
+
try:
|
| 248 |
+
client = TestClient(app)
|
| 249 |
+
# No `primary_image_data_uris` entries are written for these
|
| 250 |
+
# bogus filenames (they fail the upload-time regex), so the only
|
| 251 |
+
# way the bundler could include them is via the unvalidated
|
| 252 |
+
# `frames_dir / s.image_filename` join.
|
| 253 |
+
# Two `..` segments to climb out of both `static/` and the
|
| 254 |
+
# `vgm_zip_*` workdir mkdtemp picks under TMPDIR.
|
| 255 |
+
traversal_image = f"../../{secret.name}"
|
| 256 |
+
traversal_audio = f"../../{secret.name}"
|
| 257 |
+
body = {
|
| 258 |
+
"state": {
|
| 259 |
+
"title": "Traversal Test",
|
| 260 |
+
"frames_dir": "static",
|
| 261 |
+
"segments": [
|
| 262 |
+
{
|
| 263 |
+
"index": 0,
|
| 264 |
+
"start_seconds": 0.0,
|
| 265 |
+
"end_seconds": 1.0,
|
| 266 |
+
"image_filename": traversal_image,
|
| 267 |
+
"section_title": "S",
|
| 268 |
+
"alt_text": "",
|
| 269 |
+
"audio_filename": traversal_audio,
|
| 270 |
+
}
|
| 271 |
+
],
|
| 272 |
+
},
|
| 273 |
+
"primary_image_data_uris": {},
|
| 274 |
+
}
|
| 275 |
+
|
| 276 |
+
# We need `frames_dir` in the handler to live under `tmp_path`
|
| 277 |
+
# so the `..` payload would actually resolve to our sentinel.
|
| 278 |
+
# The handler uses `tempfile.mkdtemp(prefix="vgm_zip_")`, which
|
| 279 |
+
# honors $TMPDIR — point it at tmp_path for this test.
|
| 280 |
+
import os
|
| 281 |
+
old_tmpdir = os.environ.get("TMPDIR")
|
| 282 |
+
os.environ["TMPDIR"] = str(tmp_path)
|
| 283 |
+
try:
|
| 284 |
+
import tempfile as _tempfile
|
| 285 |
+
_tempfile.tempdir = str(tmp_path)
|
| 286 |
+
resp = client.post(f"/jobs/{job.id}/zip", json=body)
|
| 287 |
+
finally:
|
| 288 |
+
_tempfile.tempdir = None
|
| 289 |
+
if old_tmpdir is None:
|
| 290 |
+
os.environ.pop("TMPDIR", None)
|
| 291 |
+
else:
|
| 292 |
+
os.environ["TMPDIR"] = old_tmpdir
|
| 293 |
+
|
| 294 |
+
assert resp.status_code == 200, resp.text
|
| 295 |
+
with zipfile.ZipFile(io.BytesIO(resp.content)) as zf:
|
| 296 |
+
names = zf.namelist()
|
| 297 |
+
# The sentinel file's basename must not appear anywhere in the bundle.
|
| 298 |
+
assert not any(secret.name in n for n in names), names
|
| 299 |
+
finally:
|
| 300 |
+
registry.pop(job.id)
|