T05 · Unauthorized Access and Privilege Escalation
Error
- Location
- scripts/mint_pack_v2.py:29
- Finding
- Incorrect Workspace Root Resolution Causes Out-of-Project Filesystem Writes<![CDATA[ ## Vulnerability Details **File Locations**: - `scripts/mint_pack_v2.py:29-32, 149-180` - `scripts/mint_pack_local.py:25-27, 101-110` - `scripts/backfill_anchors.py:13-14, 37-39` - `scripts/make_anchor_snippet.py:13-14, 29-30` **Vulnerability Type**: Incorrect path trust boundary and unauthorized filesystem access **Risk Level**: High ### Vulnerable Code From `scripts/mint_pack_v2.py`: ```python WS = Path(__file__).resolve().parents[4] # .../workspace STATE_DIR = WS / "state" REF_DIR = WS / "reference" OUT_DIR = REF_DIR / "minted_v2" ``` ```python # write outputs STATE_DIR.mkdir(parents=True, exist_ok=True) OUT_DIR.mkdir(parents=True, exist_ok=True) manifest_path = OUT_DIR / f"{pack_sha}_manifest.json" manifest_path.write_text(canon_manifest, encoding="utf-8") ``` ```python ledger_path = STATE_DIR / "lygo_mint_v2_ledger.jsonl" with ledger_path.open("a", encoding="utf-8") as f: f.write(json.dumps(record, ensure_ascii=False) + "\n") canon_path = STATE_DIR / "lygo_mint_v2_ledger_canonical.json" canon_map = load_json(canon_path, {}) canon_map[pack_sha] = record canon_path.write_text(json.dumps(canon_map, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") ``` From `scripts/mint_pack_local.py`: ```python ROOT = Path(__file__).resolve().parents[4] # workspace root LEDGER = ROOT / "state" / "lygo_mint_ledger.jsonl" CANON = ROOT / "state" / "lygo_mint_ledger_canonical.json" ``` ```python LEDGER.parent.mkdir(parents=True, exist_ok=True) with LEDGER.open("a", encoding="utf-8") as f: f.write(json.dumps(minted, ensure_ascii=False) + "\n") ``` From `scripts/backfill_anchors.py`: ```python ROOT = Path(__file__).resolve().parents[4] # workspace root LEDGER = ROOT / "state" / "lygo_mint_ledger.jsonl" ``` ```python LEDGER.parent.mkdir(parents=True, exist_ok=True) with LEDGER.open("a", encoding="utf-8") as f: f.write(json.dumps(rec, ensure_ascii=False) + "\n") ``` From `scripts/make_anchor_snippet.py`: ```python ROOT = Path(__file__).re ...[truncated 2097 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Replace fixed-depth parent traversal with a project-relative root appropriate to the packaged layout: ```python ROOT = Path(__file__).resolve().parents[1] ``` 2. Prefer an explicit `--workspace` argument or a narrowly defined environment variable when integration with an external workspace is required. 3. Resolve the selected workspace strictly and validate every output path before writing: ```python workspace = Path(args.workspace).expanduser().resolve(strict=True) destination = (workspace / "state" / "lygo_mint_v2_ledger.jsonl").resolve() destination.relative_to(workspace) ``` 4. Reject output destinations that escape the authorized workspace. 5. Use atomic replacement for canonical JSON files by writing to a temporary file in the same directory and then calling `Path.replace()`. 6. Add installation-layout tests confirming that all generated files remain under the selected workspace when the Skill is placed at different directory depths. 7. Document every filesystem destination and avoid creating root-level directories implicitly. ]]>
