Files
codebase-memory-mcp/pkg
Dustin c552db6447 fix: harden PyPI installer against tar-slip and scheme attacks (#246)
Closes #246.

Three security improvements to pkg/pypi/src/codebase_memory_mcp/_cli.py:

1. tar-slip mitigation (S202) — the previous `tf.extractall(tmp)` /
   `zf.extractall(tmp)` calls trusted the archive not to contain absolute
   paths or `..` components. Replaced with _safe_extract_tar and
   _safe_extract_zip:
   - On Python 3.12+, tarfile uses the new `filter='data'` (PEP 706).
   - On Python 3.8–3.11 and for zipfile (no built-in filter), each member
     path is validated against the destination via os.path.abspath checks;
     symlinks and hardlinks in tarballs are rejected outright.

2. URL scheme allowlist (S310) — _validate_url_scheme rejects anything
   that isn't https before urlretrieve runs. The URL is constructed from
   hardcoded constants today, but this is a cheap defense-in-depth check
   against future config sources, redirects, or typos.

3. Justified noqa suppressions on the exec path — os.execv and
   subprocess.run receive args as a list (not a shell string) and neither
   uses shell=True, so there is no injection vector. Comments document
   the invariant so future changes don't reintroduce a shell form.

Verification:
- `ruff check _cli.py --select S` — passes (was 5 findings, now 0)
- Syntax check passes under Python 3.14
- Behavior preserved: download/extract/exec flow is unchanged on happy path
- New failure modes: clear `sys.exit` with actionable message when a
  malicious member path or non-https URL is detected

No new dependencies; all changes stdlib-only and compatible with the
project's declared `requires-python = ">=3.8"`.
2026-04-12 23:52:46 -07:00
..