Skip to content

Reject diff/file paths that escape the project root - #31

Merged
mikkel merged 1 commit into
mainfrom
fix/path-traversal
Oct 24, 2026
Merged

mikkel merged 1 commit into
mainfrom
fix/path-traversal

Conversation

@mikkel

@mikkel mikkel commented Oct 24, 2026

Copy link
Copy Markdown
Member

Found during a Vulnops dogfood run, by manual review. Vulnops has no SAST stage yet, so its scanner didn't catch this.

save_files() in patchling/core.py, apply_diff() in patchling/applydiff.py, and the per-file path in smart_apply_patch all joined model-generated paths onto the project dir without checking they stay inside it. A diff containing +++ b/../x or an absolute path wrote a file outside the project. Prompt-injected repo content could trigger this.

Fix: a new resolve_within(root, path) resolves the path, following symlinks, and raises UnsafePathError for absolute paths, .. escapes and symlink escapes. apply_diff checks every path before it touches disk and returns False if any is unsafe. save_files raises the error. smart_apply_patch marks the file as failed.

Tests: tests/test_path_traversal.py covers ../, nested ../, absolute paths, symlink escape, and checks that in-root writes still work. Local run with mock keys: full suite 217 passed, 2 skipped.

This fix awaits Ember exact-head review before merge.

@mikkel
mikkel marked this pull request as ready for review October 24, 2026 13:37
@mikkel
mikkel merged commit ffeb4c7 into main Oct 24, 2026
2 checks passed
@mikkel mikkel mentioned this pull request Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant