clipwright/tests/e2e/api/test_security_paths.py
pedro-bento 026f3ff271
Some checks failed
CI / unit (pull_request) Failing after 24s
CI / backend-e2e (pull_request) Successful in 1m18s
CI / dashboard-e2e (pull_request) Successful in 1m44s
test: add full E2E coverage (API, pipeline, browser)
Three-level end-to-end suite, all deterministic (no retries, no sleeps, no
unmocked network):

- tests/e2e/api (209 tests): every route in clipwright/api/app.py +
  free_tools — health/config, uploads, process submit matrix + worker/webhook/
  auto-retry/resume, status/source/download-all, edit/effects, transcript/EDL/
  rerender, scenes/reframe, subtitle, hook, Postiz, thumbnails, free tools,
  and the security/path guards.
- tests/e2e/pipeline (136 tests): every stage with real ffmpeg and stubbed
  Gemini/yt-dlp/ASR/ML — download, transcribe, scene detect, selection,
  layout, reframe/render, subtitles, hooks, watermark, thumbnails, plus the
  real CLI full pipeline via stub_env.
- dashboard/e2e (19 Playwright specs): route-interception state machine
  covering every dashboard flow; one real-backend spec left test.fixme.
- Foundation: tests/e2e/conftest.py (state isolation, no-network guard, ffmpeg
  media factory, disk-seeded jobs), FakePopen worker harness, child-process
  stub_env, architecture-consistent fixtures.
- CI: unit + backend-e2e + dashboard-e2e jobs in the python+node container;
  ffmpeg + Playwright installed in-job; no setup-node/cache (act runner).

Verified: unit 790 passed; API E2E 209 passed; pipeline E2E 136 passed;
Playwright 19 passed / 1 skipped.
2026-10-04 21:58:28 +01:00

125 lines
5.2 KiB
Python

"""L3 E2E: security cross-cuts shared by the API surface.
Pins the path allowlist on the ``/videos`` mount, the traversal refusal on the
``/thumbnails`` mount, the basename sanitisation of client-supplied filenames,
the credential scrubbing of logs, and the deliberate absence of an auth gate in
this single-tenant fork (so a future accidental gate fails loudly here).
"""
import os
import pytest
pytestmark = pytest.mark.e2e
# --------------------------------------------------------------------------- #
# /videos allowlist
# --------------------------------------------------------------------------- #
async def test_videos_serves_clips_but_refuses_sidecars(client, app_state, seeded_job):
job_id = seeded_job(job_id="job-sec", base="pw", clips=1)
job_dir = app_state.out / job_id
# The sensitive sidecars the old plain-StaticFiles mount handed out.
(job_dir / ".owner").write_text("user-uuid")
(job_dir / ".resume.json").write_text('{"webhook_secret": "s"}')
(job_dir / ".instance").write_text("instance-id")
(job_dir / ".transcript_checkpoint.json").write_text("{}")
(job_dir / "notes.json").write_text("{}")
served = await client.get(f"/videos/{job_id}/pw_clip_1.mp4")
assert served.status_code == 200
assert served.content
for name in (".owner", ".resume.json", ".instance",
".transcript_checkpoint.json", "pw_metadata.json", "notes.json"):
resp = await client.get(f"/videos/{job_id}/{name}")
assert resp.status_code == 404, f"{name} leaked from /videos"
async def test_media_guard_rejects_traversal_and_hidden_paths(app_state):
app = app_state.app
assert app._media_guard("job/pw_clip_1.mp4")
for bad in ("../app.py", "job/../../app.py", "job/../.resume.json",
"job/.owner", "job/pw_metadata.json", "/etc/passwd"):
assert not app._media_guard(bad), bad
async def test_videos_encoded_traversal_is_404(client, app_state, seeded_job):
seeded_job(job_id="job-trav", base="pw")
for path in ("/videos/%2e%2e/%2e%2e/etc/passwd",
"/videos/job-trav/..%2f..%2fetc%2fpasswd.mp4",
"/videos/job-trav/%2e%2e/%2e%2e/etc/passwd.mp4"):
resp = await client.get(path)
assert resp.status_code == 404, path
# --------------------------------------------------------------------------- #
# /thumbnails mount
# --------------------------------------------------------------------------- #
async def test_thumbnails_serves_files_but_blocks_traversal(client, app_state):
(app_state.out / "thumbnails" / "ok.jpg").write_bytes(b"jpeg")
ok = await client.get("/thumbnails/ok.jpg")
assert ok.status_code == 200
assert ok.content == b"jpeg"
for path in ("/thumbnails/%2e%2e/%2e%2e/etc/passwd",
"/thumbnails/..%2f..%2fetc%2fpasswd",
"/thumbnails/%2e%2e/secret.txt"):
resp = await client.get(path)
assert resp.status_code == 404, path
# --------------------------------------------------------------------------- #
# Client-supplied filenames are basenamed
# --------------------------------------------------------------------------- #
async def test_edit_input_filename_traversal_is_basenamed(client, app_state, seeded_job):
job_id = seeded_job(job_id="job-edit", base="pw", clips=1)
# A traversal name must resolve under OUTPUT_DIR/<job>/ after basename().
resp = await client.post(
"/api/edit",
json={"job_id": job_id, "clip_index": 0, "api_key": "k",
"input_filename": "../../outside.mp4"},
)
# The route's broad except turns the missing-file 404 into a 500, but the
# detail still shows which path it resolved: the basenamed one.
detail = resp.json()["detail"]
assert detail.endswith(os.path.join(job_id, "outside.mp4"))
assert ".." not in detail
# --------------------------------------------------------------------------- #
# Log credential scrubbing
# --------------------------------------------------------------------------- #
def test_scrub_secrets_masks_embedded_credentials(app_state):
scrub = app_state.app._scrub_secrets
assert scrub("proxy http://user:pass@host:8080/x") == \
"proxy http://***:***@host:8080/x"
assert scrub("socks5://alice:s3cr3t@proxy.example:1080") == \
"socks5://***:***@proxy.example:1080"
assert scrub("no credentials here") == "no credentials here"
assert scrub("https://host/path") == "https://host/path"
# --------------------------------------------------------------------------- #
# No auth gate (single-tenant fork) — pin the intentional openness
# --------------------------------------------------------------------------- #
async def test_public_endpoints_need_no_auth(client, app_state, seeded_job):
# No Authorization / API-key header on any of these.
cfg = await client.get("/api/config")
assert cfg.status_code == 200
job_id = seeded_job(job_id="job-open", base="pw", clips=1)
status = await client.get(f"/api/status/{job_id}")
assert status.status_code == 200
clip = await client.get(f"/videos/{job_id}/pw_clip_1.mp4")
assert clip.status_code == 200
bundle = await client.get(f"/api/jobs/{job_id}/download-all")
assert bundle.status_code == 200
assert bundle.headers["content-type"] == "application/zip"