T09 · Insecure Skill Coding Practices
- Location
scripts/clip_audio.py:47- Finding
Path Traversal Through Unvalidated Highlight IDs
- Content
View full analysis
Vulnerability Details
File Location:
scripts/clip_audio.py, lines 47–73
Vulnerability Type: Path traversal and arbitrary file creation
Risk Level: MediumVulnerable Code
python for h in data.get("highlights", []): hid = h["id"] start = max(0.0, float(h["start"]) - args.pad_before) end = float(h["end"]) + args.pad_after dur = max(0.1, end - start) out = out_audio_dir / f"{hid}.mp3" cmd = [ "ffmpeg", "-hide_banner", "-loglevel", "error", "-ss", f"{start:.3f}", "-i", str(audio), "-t", f"{dur:.3f}", "-c:a", "libmp3lame", "-q:a", str(args.q), str(out), ] run(cmd) h.setdefault("clip", {}) h["clip"]["file"] = f"audio/{hid}.mp3" h["clip"]["start"] = start h["clip"]["end"] = endTechnical Analysis
The
idproperty is read fromhighlights.jsonand incorporated into an output pathname without validation or canonical-path containment checks. A value containing traversal components such as../can cause the resolved output path to escapeout_audio_dir.Although the
ffmpegprocess is safely started with an argument array rather than through a shell, this only prevents shell metacharacter injection. It does not prevent path traversal. The untrusted path is passed toffmpegas its output destination, enabling file creation outside the intendedsite_assets/audiodirectory where the operating-system permissions andffmpegbehavior permit it.The malicious ID is also written into
h["clip"]["file"], allowing the unsafe path to persist in the updated JSON data.Attack Path
- An attacker supplies or modifies the
highlights.jsonfile used by the documented clipping workflow. - The attacker assigns a highlight an ID containing traversal components, for example:
json
...[truncated 1316 chars]
- An attacker supplies or modifies the
- Remediation
View remediation
Remediation Suggestions
-
Enforce a strict identifier allowlist before using an ID in a pathname:
python import re SAFE_ID = re.compile(r"^[A-Za-z0-9_-]+$") hid = h["id"] if not isinstance(hid, str) or not SAFE_ID.fullmatch(hid): raise ValueError(f"Invalid highlight id: {hid!r}") -
Resolve the destination and verify that it remains within the intended directory:
python base = out_audio_dir.resolve() out = (base / f"{hid}.mp3").resolve() if out.parent != base: raise ValueError("Output path escapes the audio directory") -
Reject absolute paths, path separators,
.and..components explicitly, even if an allowlist is also present. -
Define and validate the complete JSON schema before processing. Require finite, non-negative timestamps and ensure
endis greater thanstart. -
Decide on a safe overwrite policy. For example, fail if an output already exists unless an explicit
--overwriteoption is supplied.
-
