validation: reject unsafe slugs in 'socrates init <slug>'#3
Open
CryptoJones wants to merge 1 commit into
Open
Conversation
`Path(base) / args.project` had three foot-guns when args.project
was untrusted:
1. Absolute slug: Path("base") / "/etc/passwd" returns "/etc/passwd"
in Python pathlib. `socrates init /etc/passwd --base ~/foo` would
try to scaffold under /etc/passwd, not under ~/foo. Even though
the user owns the CLI invocation, this violates the mental model
("init creates a project named <slug> UNDER --base").
2. Path-separator slug: `socrates init a/b` quietly creates nested
structure; `socrates init ../tmp/bad` escapes --base.
3. Empty slug: `socrates init ""` resolves to the base dir itself,
risking damage to existing siblings.
Added a `_validate_slug()` helper that rejects:
- empty / whitespace-only
- containing '/' or '\\' (single path component only)
- absolute paths
- '..' or any '..' segment
- '.' or '..' literal
- NUL bytes (defensive)
Accepts alphanumerics, dash, underscore, dot — covers normal slugs
like `quarterly-rebates`, `v0.8.0`, `.hidden`.
Tests (new tests/test_init_slug_safety.py, 22 tests):
- 11 parametrized rejection cases
- 7 parametrized acceptance cases (normal slugs unchanged)
- 4 end-to-end CLI integration tests proving:
- absolute slug exits 2, /etc/passwd/docs/ never created
- traversal slug exits 2, sibling dir never created
- nested slug exits 2 with helpful message
- empty slug exits 2 with helpful message
169/169 tests pass; ruff + mypy clean.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Path(base) / args.projecthad three foot-guns when args.projectwas untrusted:
Absolute slug: Path("base") / "/etc/passwd" returns "/etc/passwd"
in Python pathlib.
socrates init /etc/passwd --base ~/foowouldtry to scaffold under /etc/passwd, not under ~/foo. Even though
the user owns the CLI invocation, this violates the mental model
("init creates a project named UNDER --base").
Path-separator slug:
socrates init a/bquietly creates nestedstructure;
socrates init ../tmp/badescapes --base.Empty slug:
socrates init ""resolves to the base dir itself,risking damage to existing siblings.
Added a
_validate_slug()helper that rejects:Accepts alphanumerics, dash, underscore, dot — covers normal slugs
like
quarterly-rebates,v0.8.0,.hidden.Tests (new tests/test_init_slug_safety.py, 22 tests):
169/169 tests pass; ruff + mypy clean.
Self-review caveat: doesn't reject Windows reserved names (CON, NUL, PRN, AUX, COM1-9, LPT1-9) or trailing dots/spaces (Windows strips silently). Unix-only safety; Windows operators can still trip themselves up.