clipwright/media_auth.py
pedro-bento 4e82d60cbc fix: harden Postiz integration and align docs/CLI/n8n after review
- Postiz URL is now required (no built-in host); a key can never be sent to a default instance
- /api/postiz/scheduled normalizes scheduled_date and widens the default window
- Disabled channels are refused for posting; frontend excludes them from connected platforms
- 4xx instead of 500 on missing clip URL / bad schedule date; Postiz error bodies truncated
- Add tests/test_postiz.py (24 tests); fix remaining cloud-removal test drift
- CLI defaults to localhost:8000; n8n workflows fixed against the real routes
- Remove dead media-token auth code, stale copy (download_url/402) and doc drift
2026-10-04 17:25:17 +01:00

67 lines
2.8 KiB
Python

"""Path rules for the ``/videos`` bytes: what these prefixes may ever serve.
This fork is single-tenant / self-host with no accounts, so there is no token
or URL-signing layer here: the only access rule is this allowlist.
``/videos`` used to be a plain ``StaticFiles`` mount over the working
directory, so anyone holding a job id (it travels in every clip URL) could also
fetch the ``.resume.json`` manifest (which carries the customer's own
``webhook_secret``), the ``.owner`` sidecar (a user uuid), the ``.instance``
deploy marker, the face crops under ``thumbnails/`` and ``<title>_metadata.json``
(the full transcript of the user's video).
``normalise_relpath`` rejects absolute paths, traversal and every dot-segment;
``is_servable`` additionally requires a deliverable extension. ``app.py`` wraps
both in ``_media_guard`` and installs it as the ``guard`` on the ``/videos``
mount.
"""
import posixpath
# Nothing outside this list is a deliverable: the working directories also hold
# transcripts, metadata JSON, resume manifests and ownership sidecars, and an
# allowlist is the only version of this rule that stays correct when the
# pipeline starts writing a new kind of scratch file.
SERVABLE_EXTENSIONS = frozenset({
".mp4", ".webm", ".mov", ".m4v", ".mkv", ".ogv",
".mp3", ".m4a", ".wav", ".aac", ".flac", ".opus",
".jpg", ".jpeg", ".png", ".webp", ".gif", ".avif",
".srt", ".vtt", ".ass", ".zip",
})
class MediaPathError(ValueError):
"""The requested path is not something these prefixes may ever serve."""
def normalise_relpath(rel_path: str) -> str:
"""Return ``rel_path`` as a clean forward-slash relative path.
Raises ``MediaPathError`` for absolute paths, traversal, and any segment
starting with a dot — which is what keeps ``.resume.json`` (webhook secret),
``.owner`` (user uuid), ``.instance`` and ``.transcript_checkpoint.json``
unreachable no matter who is asking.
"""
if not rel_path or not isinstance(rel_path, str):
raise MediaPathError("Empty path")
cleaned = rel_path.replace("\\", "/").strip("/")
if not cleaned:
raise MediaPathError("Empty path")
if "\x00" in cleaned:
raise MediaPathError("Invalid path")
segments = [s for s in cleaned.split("/") if s != ""]
if not segments:
raise MediaPathError("Empty path")
for segment in segments:
if segment.startswith("."):
# Covers "." and ".." as well as every dotfile the pipeline writes.
raise MediaPathError(f"Hidden or relative segment: {segment!r}")
return posixpath.join(*segments)
def is_servable(rel_path: str) -> bool:
"""True if this relative path is both visible and of a deliverable type."""
try:
cleaned = normalise_relpath(rel_path)
except MediaPathError:
return False
return posixpath.splitext(cleaned)[1].lower() in SERVABLE_EXTENSIONS