Skip to content

Reject "." and ".." path segments before sending requests - #2001

Open
zacchua-stripe wants to merge 5 commits into
masterfrom
zacchua/reject-dot-segments
Open

zacchua-stripe wants to merge 5 commits into
masterfrom
zacchua/reject-dot-segments

Conversation

@zacchua-stripe

@zacchua-stripe zacchua-stripe commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

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 call quote_plus directly) 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(".") sends GET /v1/checkout/sessions and returns a list of sessions, and client.v1.customers.payment_methods.retrieve("..", "pm_123") sends GET /v1/payment_methods/pm_123 with 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_path rejects any path segment that is . or .. after percent-decoding (some clients decode %2E before removing dot segments), before any request is sent
  • also treat an encoded slash (%2F) as a separator for this check, since a proxy that decodes the path before normalizing it would turn it into a real one
  • raise ValueError for this check, like the other checks in validate_path; nothing reaches the API when it fails
  • validate_path runs on every request, so this covers generated services, legacy resources, raw_request, and fetch_related_object, regardless of HTTP client
  • add tests for rejected and still-accepted paths across sync/async requests, StripeClient services, nested IDs, and the legacy API
  • the error message shows only the path, leaving out query params that can carry secrets such as a client_secret

See Also

https://go/j/RUN_DEVSDK-3747

Configuration

  • skip-changefile: This PR is not a user-facing change, so there's no changefile.

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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 .., raising InvalidRequestError before 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

@zacchua-stripe
zacchua-stripe marked this pull request as ready for review November 20, 2026 19:09
@zacchua-stripe
zacchua-stripe requested a review from a team as a code owner November 20, 2026 19:09
@zacchua-stripe
zacchua-stripe requested review from jar-stripe, mark-kogan-stripe and mnorth-stripe and removed request for a team November 20, 2026 19:09
zacchua-stripe and others added 2 commits November 23, 2026 09:05
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 jar-stripe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good. I had a question about the specific error that is raised, and something else to consider (maybe as a followup?)

Comment thread stripe/_util.py Outdated

# The message leaves out the query string, which can carry
# secrets such as a client_secret.
raise InvalidRequestError(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the above checks raise a ValueError for invalid characters; why does this need to be different?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread stripe/_util.py
)

for segment in parts.path.split("/"):
if unquote(segment) in (".", ".."):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe out of scope for this PR, but shouldn't we be rejecting any quoted segments?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

zacchua-stripe and others added 2 commits November 23, 2026 11:15
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

This branch has not been deployed

No deployments
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.

3 participants