Repository navigation
Reject "." and ".." path segments before sending requests - #2001
zacchua-stripe wants to merge 5 commits into
Conversation
HTTP clients such as httpx, requests and aiohttp remove "." and ".." segments from a URL before sending it. Since quote_plus leaves dots alone, an ID of "." or ".." changed which endpoint a method called. validate_path now rejects these segments, including percent-encoded forms, with an InvalidRequestError. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Committed-By-Agent: claude
There was a problem hiding this comment.
🟢 Approval recommended
The review reports no active issues with the request-path validation change.
No open issues 🎉
What changed in this PR
This PR hardens the shared request-path validation to prevent HTTP clients from redirecting requests to unintended endpoints by normalizing dot segments.
Changes:
- Reject path segments that decode to
.or.., raisingInvalidRequestErrorbefore sending a request. - Add rejection and acceptance tests covering sync/async requests, generated services, nested IDs, and legacy resources.
- Add a patch release change note.
| File | Description |
|---|---|
tests/test_api_requestor.py |
Tests dot-segment rejection without sending requests and preserves valid dotted paths. |
stripe/_util.py |
Rejects literal and percent-encoded dot segments in shared path validation. |
.hark/changes/2026-11-20_zacchua_reject-dot-segments.change.md |
Documents the request-path safety fix for release. |
Review effort: Balanced
Paths can carry query parameters such as a client_secret, which then ended up in the exception message and any logs that record it. The message now shows only the path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Committed-By-Agent: claude
The API only rejects these paths when the dot segment is last (it becomes a trailing slash). A ".." parent ID collapses into a valid path with the default sync client, so the API does not reject it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Committed-By-Agent: claude
jar-stripe
left a comment
There was a problem hiding this comment.
Generally looks good. I had a question about the specific error that is raised, and something else to consider (maybe as a followup?)
|
|
||
| # The message leaves out the query string, which can carry | ||
| # secrets such as a client_secret. | ||
| raise InvalidRequestError( |
There was a problem hiding this comment.
the above checks raise a ValueError for invalid characters; why does this need to be different?
There was a problem hiding this comment.
Fair enough! I've switched it to ValueError. Initially used InvalidRequestError because before this change, a trailing . with the default sync client reached the API and came back as an InvalidRequestError, but now nothing reaches the API when this check fails, so an API error type was misleading.
| ) | ||
|
|
||
| for segment in parts.path.split("/"): | ||
| if unquote(segment) in (".", ".."): |
There was a problem hiding this comment.
maybe out of scope for this PR, but shouldn't we be rejecting any quoted segments?
There was a problem hiding this comment.
Hm I don't think so, percent-encoding is how sanitize_id safely sends an ID with characters like : or é, as data inside a single segment. Some IDs are user-chosen (coupons, plans, products), so a valid request can contain quoted segments. But lmk if you think otherwise!
There was a problem hiding this comment.
Follow up from slack thread, since slash IDs are still in use we can't reject %2F outright, but the check now treats %2F as a separator when looking for dot segments (2f6b8cb). So an encoded .. can't be turned into a traversal by a proxy that decodes the path, and a plain encoded slash in an ID still works. Same change is in the other SDKs' PRs.
validate_path already raises ValueError for paths it won't send. Nothing reaches the API when this check fails, so an InvalidRequestError (which normally describes an API response) was misleading. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Committed-By-Agent: claude
An ID like "x/../../customers/cus_B" is sent as one segment, x%2F..%2F..%2Fcustomers%2Fcus_B, which passed the check. A proxy that decodes the path before normalizing it (nginx's proxy_pass, for example) would still turn it into a request for a different object. Splitting on "%2F" as well as "/" rejects it, while IDs that merely contain a slash keep working. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Committed-By-Agent: claude
Why?
Some HTTP clients, including httpx/httpx2 (the default async client), requests, and aiohttp, remove
.and..segments from a URL path before sending it.sanitize_id(and the legacy resources, which callquote_plusdirectly) escape/in IDs but leave dots alone, so an ID of.or..changes which endpoint a method calls.For example, with httpx,
client.v1.checkout.sessions.retrieve_async(".")sendsGET /v1/checkout/sessionsand returns a list of sessions, andclient.v1.customers.payment_methods.retrieve("..", "pm_123")sendsGET /v1/payment_methods/pm_123with any of those clients.If an integration passes an untrusted ID to one of these methods, the request can read or change a different object than intended.
What?
validate_pathrejects any path segment that is.or..after percent-decoding (some clients decode%2Ebefore removing dot segments), before any request is sent%2F) as a separator for this check, since a proxy that decodes the path before normalizing it would turn it into a real onevalidate_pathruns on every request, so this covers generated services, legacy resources,raw_request, andfetch_related_object, regardless of HTTP clientStripeClientservices, nested IDs, and the legacy APISee Also
https://go/j/RUN_DEVSDK-3747
Configuration
skip-changefile: This PR is not a user-facing change, so there's no changefile.