Add URL.joinpath_safe to prevent path traversal from untrusted segments#1673
Add URL.joinpath_safe to prevent path traversal from untrusted segments#1673aiolibsbot wants to merge 7 commits into
Conversation
|
@aiolibsbot rr |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1673 +/- ##
==========================================
+ Coverage 99.47% 99.80% +0.32%
==========================================
Files 30 21 -9
Lines 5960 4156 -1804
Branches 285 245 -40
==========================================
- Hits 5929 4148 -1781
+ Misses 22 5 -17
+ Partials 9 3 -6
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
PR Review — Add URL.joinpath_safe to prevent path traversal from untrusted segmentsSolid security-hardening addition. The implementation is small, focused, and the input-side guard for 🟢 Suggestions1. Consider handling empty-string segments explicitly (`yarl/_url.py`, L1485-1494)Empty-string arguments fall through to 2. Add a few more boundary cases (`tests/test_url.py`, L1119-1175)The matrix is good for the documented threats, but a couple of cheap additions would harden it:
3. Mention the encoded form in the docstring (`yarl/_url.py`, L1475-1494)The docstring tells the reader that Checklist
SummarySolid security-hardening addition. The implementation is small, focused, and the input-side guard for |
|
You must use the aio-libs PR template when opening prs |
|
@aiolibsbot rr |
Rebase with requested adjustmentsBranch Changes applied
StatsActions performed
CI statusCI will be checked asynchronously. Automated by Kōan |
|
@aiolibsbot gh_request Update the PR to use the PR template, add to memory to always use the aio-libs PR template for this repo |
|
@aiolibsbot rr |
Closes aio-libs#1631. Each argument is treated as a single opaque path segment: '/' is percent-encoded so it cannot introduce additional segments, and whole-segment '.' / '..' are percent-encoded so they cannot be interpreted as relative-path indicators by normalize_path_segments. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Rebase with requested adjustmentsBranch StatsActions performed
CI statusCI will be checked asynchronously. Automated by Kōan |
22066e1 to
8ba9743
Compare
|
@lexrobin-te Is that what you were hoping for ? |
What do these changes do?
Add
URL.joinpath_safe(*segments)— likejoinpath, but every argument is treated as a single opaque path segment. Each segment is quoted with a newPATH_SAFE_QUOTER(identical toPATH_QUOTERbut does not protect/, so embedded slashes get percent-encoded). Literal"."/".."inputs are replaced with"%2E"/"%2E%2E"sonormalize_path_segmentscannot interpret them as relative-path indicators. The call then delegates to_make_child(..., encoded=True).Are there changes in behavior for the user?
Yes — new public method on
URL. No change to existing methods.Untrusted input dropped into
joinpath(or/) could previously traverse the base path.joinpath_safeis the explicit-opt-in API for that case. Non-strarguments raiseTypeError.Related issue number
Fixes #1631
Checklist