From 5e9e09f81c492d8a2da899bf51ccaaec74ca4716 Mon Sep 17 00:00:00 2001 From: Zachary Chua Date: Fri, 20 Nov 2026 10:52:17 -0800 Subject: [PATCH 1/5] Reject "." and ".." path segments before sending requests 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 Committed-By-Agent: claude --- ...1-20_zacchua_reject-dot-segments.change.md | 6 ++ stripe/_util.py | 21 +++- tests/test_api_requestor.py | 95 +++++++++++++++++++ 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 .hark/changes/2026-11-20_zacchua_reject-dot-segments.change.md diff --git a/.hark/changes/2026-11-20_zacchua_reject-dot-segments.change.md b/.hark/changes/2026-11-20_zacchua_reject-dot-segments.change.md new file mode 100644 index 000000000..099011a51 --- /dev/null +++ b/.hark/changes/2026-11-20_zacchua_reject-dot-segments.change.md @@ -0,0 +1,6 @@ +--- +title: Reject request paths with a "." or ".." segment, such as an ID of ".", instead of letting HTTP clients send the request to a different endpoint +semver_level: patch +jira_tickets_closed: +- RUN_DEVSDK-3747 +--- diff --git a/stripe/_util.py b/stripe/_util.py index 995967aa5..fff0e1c9d 100644 --- a/stripe/_util.py +++ b/stripe/_util.py @@ -7,7 +7,7 @@ from stripe._api_mode import ApiMode -from urllib.parse import quote_plus, urlsplit +from urllib.parse import quote_plus, unquote, urlsplit from typing_extensions import Type, TYPE_CHECKING from typing import ( @@ -379,6 +379,13 @@ def validate_path(path: str) -> None: So, we require that a path starts with a leading slash and that urlsplit finds no scheme or authority in it. + + We also reject "." and ".." path segments. HTTP clients such as httpx, + requests and aiohttp remove them before sending, so an ID of "." or ".." + changes which endpoint is called: retrieving "." from + /v1/checkout/sessions/{session} lists every session instead. IDs are + escaped with quote_plus, which leaves dots alone, and some clients decode + "%2E" back to "." before removing it, so we check the decoded segment. """ if not path.startswith("/") or path.startswith("//"): raise ValueError( @@ -391,6 +398,18 @@ def validate_path(path: str) -> None: f"Request path may not contain a scheme or authority, got: {path!r}" ) + for segment in parts.path.split("/"): + if unquote(segment) in (".", ".."): + # Raised as an InvalidRequestError rather than a ValueError because + # with the default sync client the API already rejects these paths + # with one, so callers' existing error handling keeps working. + from stripe._error import InvalidRequestError + + raise InvalidRequestError( + f'Request path may not contain a "." or ".." segment, got: {path!r}', + None, + ) + def get_api_mode(url: str) -> ApiMode: if url.startswith("/v2"): diff --git a/tests/test_api_requestor.py b/tests/test_api_requestor.py index b8adae3a3..97b935a90 100644 --- a/tests/test_api_requestor.py +++ b/tests/test_api_requestor.py @@ -1241,6 +1241,29 @@ def test_raw_request_with_file_param(self, requestor, http_client_mock): "/v1/invoices/in_123@456", # A backslash does not open an authority: the "/" already closed it. "/v1/\\evil.example", + # Dots are fine anywhere other than a whole "." or ".." segment. + "/v1/reporting/report_types/balance.summary.1", + "/v1/customers/...", + "/v1/customers/.cus_123", + "/v1/customers/cus_123..", + "/v1/customers?email=a..b%40example.com", + # A double-encoded dot reaches the API as the literal "%2E". + "/v1/customers/%252E", + ] + + DOT_SEGMENT_PATHS = [ + # HTTP clients remove these segments, so each request would reach a + # different endpoint than the one the path was built for. + "/v1/checkout/sessions/.", + "/v1/checkout/sessions/..", + "/v1/customers/../payment_methods/pm_123", + "/v1/customers/./payment_methods/pm_123", + "/v1/customers/cus_123/sources/..?expand[]=customer", + "/v2/core/accounts/.", + # Some clients decode "%2E" to "." before removing dot segments. + "/v1/checkout/sessions/%2E", + "/v1/checkout/sessions/%2e%2E", + "/v1/checkout/sessions/.%2E", ] HOSTILE_PATHS = [ @@ -1277,6 +1300,78 @@ def test_request_rejects_hostile_path_without_issuing_request( http_client_mock.assert_no_request() + @pytest.mark.parametrize("path", DOT_SEGMENT_PATHS) + def test_rejects_dot_segment_path(self, path): + with pytest.raises(stripe.InvalidRequestError): + validate_path(path) + + @pytest.mark.parametrize("path", DOT_SEGMENT_PATHS) + def test_request_rejects_dot_segment_path_without_issuing_request( + self, path, requestor, http_client_mock + ): + with pytest.raises(stripe.InvalidRequestError): + requestor.request("get", path, base_address="api") + + http_client_mock.assert_no_request() + + @pytest.mark.anyio + @pytest.mark.parametrize("path", DOT_SEGMENT_PATHS) + async def test_request_async_rejects_dot_segment_path_without_issuing_request( + self, path, requestor, http_client_mock + ): + with pytest.raises(stripe.InvalidRequestError): + await requestor.request_async("get", path, base_address="api") + + http_client_mock.assert_no_request() + + @pytest.mark.parametrize("session_id", [".", ".."]) + def test_service_retrieve_rejects_dot_id_without_issuing_request( + self, session_id, http_client_mock + ): + client = stripe.StripeClient( + "sk_test_123", http_client=http_client_mock.get_mock_http_client() + ) + + with pytest.raises(stripe.InvalidRequestError): + client.v1.checkout.sessions.retrieve(session_id) + + http_client_mock.assert_no_request() + + @pytest.mark.anyio + @pytest.mark.parametrize("session_id", [".", ".."]) + async def test_service_retrieve_async_rejects_dot_id_without_issuing_request( + self, session_id, http_client_mock + ): + client = stripe.StripeClient( + "sk_test_123", http_client=http_client_mock.get_mock_http_client() + ) + + with pytest.raises(stripe.InvalidRequestError): + await client.v1.checkout.sessions.retrieve_async(session_id) + + http_client_mock.assert_no_request() + + def test_service_nested_rejects_dot_parent_id_without_issuing_request( + self, http_client_mock + ): + client = stripe.StripeClient( + "sk_test_123", http_client=http_client_mock.get_mock_http_client() + ) + + with pytest.raises(stripe.InvalidRequestError): + client.v1.customers.payment_methods.retrieve("..", "pm_123") + + http_client_mock.assert_no_request() + + @pytest.mark.parametrize("session_id", [".", ".."]) + def test_legacy_retrieve_rejects_dot_id_without_issuing_request( + self, session_id, http_client_mock + ): + with pytest.raises(stripe.InvalidRequestError): + stripe.checkout.Session.retrieve(session_id) + + http_client_mock.assert_no_request() + def test_raw_request_rejects_hostile_path_without_issuing_request( self, http_client_mock ): From e89443759f14d145c422089ca9320bab083f1f5f Mon Sep 17 00:00:00 2001 From: Zachary Chua Date: Mon, 23 Nov 2026 09:05:50 -0800 Subject: [PATCH 2/5] Omit the query string from the dot-segment error message 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 Committed-By-Agent: claude --- stripe/_util.py | 4 +++- tests/test_api_requestor.py | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/stripe/_util.py b/stripe/_util.py index fff0e1c9d..a7ef9fd2b 100644 --- a/stripe/_util.py +++ b/stripe/_util.py @@ -405,8 +405,10 @@ def validate_path(path: str) -> None: # with one, so callers' existing error handling keeps working. from stripe._error import InvalidRequestError + # The message leaves out the query string, which can carry + # secrets such as a client_secret. raise InvalidRequestError( - f'Request path may not contain a "." or ".." segment, got: {path!r}', + f'Request path may not contain a "." or ".." segment, got: {parts.path!r}', None, ) diff --git a/tests/test_api_requestor.py b/tests/test_api_requestor.py index 97b935a90..03ab884fa 100644 --- a/tests/test_api_requestor.py +++ b/tests/test_api_requestor.py @@ -1305,6 +1305,15 @@ def test_rejects_dot_segment_path(self, path): with pytest.raises(stripe.InvalidRequestError): validate_path(path) + def test_dot_segment_error_omits_query_string(self): + with pytest.raises(stripe.InvalidRequestError) as exc_info: + validate_path( + "/v1/payment_intents/..?client_secret=pi_123_secret_456" + ) + + assert "/v1/payment_intents/.." in str(exc_info.value) + assert "pi_123_secret_456" not in str(exc_info.value) + @pytest.mark.parametrize("path", DOT_SEGMENT_PATHS) def test_request_rejects_dot_segment_path_without_issuing_request( self, path, requestor, http_client_mock From 19d6ac80c9d47a52ad2918951abeb3ce77870662 Mon Sep 17 00:00:00 2001 From: Zachary Chua Date: Mon, 23 Nov 2026 09:48:09 -0800 Subject: [PATCH 3/5] Correct the comment on why validate_path raises InvalidRequestError 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 Committed-By-Agent: claude --- stripe/_util.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/stripe/_util.py b/stripe/_util.py index a7ef9fd2b..255c17f7a 100644 --- a/stripe/_util.py +++ b/stripe/_util.py @@ -400,9 +400,8 @@ def validate_path(path: str) -> None: for segment in parts.path.split("/"): if unquote(segment) in (".", ".."): - # Raised as an InvalidRequestError rather than a ValueError because - # with the default sync client the API already rejects these paths - # with one, so callers' existing error handling keeps working. + # Raised as an InvalidRequestError rather than a ValueError, so + # callers' existing handling of API errors keeps working. from stripe._error import InvalidRequestError # The message leaves out the query string, which can carry From 77d21924f46903e02c1534bcf6fe4dea207d20c4 Mon Sep 17 00:00:00 2001 From: Zachary Chua Date: Mon, 23 Nov 2026 11:15:22 -0800 Subject: [PATCH 4/5] Raise ValueError for dot-segment paths, like the other path checks 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 Committed-By-Agent: claude --- stripe/_util.py | 9 ++------- tests/test_api_requestor.py | 16 ++++++++-------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/stripe/_util.py b/stripe/_util.py index 255c17f7a..f20088f11 100644 --- a/stripe/_util.py +++ b/stripe/_util.py @@ -400,15 +400,10 @@ def validate_path(path: str) -> None: for segment in parts.path.split("/"): if unquote(segment) in (".", ".."): - # Raised as an InvalidRequestError rather than a ValueError, so - # callers' existing handling of API errors keeps working. - from stripe._error import InvalidRequestError - # The message leaves out the query string, which can carry # secrets such as a client_secret. - raise InvalidRequestError( - f'Request path may not contain a "." or ".." segment, got: {parts.path!r}', - None, + raise ValueError( + f'Request path may not contain a "." or ".." segment, got: {parts.path!r}' ) diff --git a/tests/test_api_requestor.py b/tests/test_api_requestor.py index 03ab884fa..802e97896 100644 --- a/tests/test_api_requestor.py +++ b/tests/test_api_requestor.py @@ -1302,11 +1302,11 @@ def test_request_rejects_hostile_path_without_issuing_request( @pytest.mark.parametrize("path", DOT_SEGMENT_PATHS) def test_rejects_dot_segment_path(self, path): - with pytest.raises(stripe.InvalidRequestError): + with pytest.raises(ValueError): validate_path(path) def test_dot_segment_error_omits_query_string(self): - with pytest.raises(stripe.InvalidRequestError) as exc_info: + with pytest.raises(ValueError) as exc_info: validate_path( "/v1/payment_intents/..?client_secret=pi_123_secret_456" ) @@ -1318,7 +1318,7 @@ def test_dot_segment_error_omits_query_string(self): def test_request_rejects_dot_segment_path_without_issuing_request( self, path, requestor, http_client_mock ): - with pytest.raises(stripe.InvalidRequestError): + with pytest.raises(ValueError): requestor.request("get", path, base_address="api") http_client_mock.assert_no_request() @@ -1328,7 +1328,7 @@ def test_request_rejects_dot_segment_path_without_issuing_request( async def test_request_async_rejects_dot_segment_path_without_issuing_request( self, path, requestor, http_client_mock ): - with pytest.raises(stripe.InvalidRequestError): + with pytest.raises(ValueError): await requestor.request_async("get", path, base_address="api") http_client_mock.assert_no_request() @@ -1341,7 +1341,7 @@ def test_service_retrieve_rejects_dot_id_without_issuing_request( "sk_test_123", http_client=http_client_mock.get_mock_http_client() ) - with pytest.raises(stripe.InvalidRequestError): + with pytest.raises(ValueError): client.v1.checkout.sessions.retrieve(session_id) http_client_mock.assert_no_request() @@ -1355,7 +1355,7 @@ async def test_service_retrieve_async_rejects_dot_id_without_issuing_request( "sk_test_123", http_client=http_client_mock.get_mock_http_client() ) - with pytest.raises(stripe.InvalidRequestError): + with pytest.raises(ValueError): await client.v1.checkout.sessions.retrieve_async(session_id) http_client_mock.assert_no_request() @@ -1367,7 +1367,7 @@ def test_service_nested_rejects_dot_parent_id_without_issuing_request( "sk_test_123", http_client=http_client_mock.get_mock_http_client() ) - with pytest.raises(stripe.InvalidRequestError): + with pytest.raises(ValueError): client.v1.customers.payment_methods.retrieve("..", "pm_123") http_client_mock.assert_no_request() @@ -1376,7 +1376,7 @@ def test_service_nested_rejects_dot_parent_id_without_issuing_request( def test_legacy_retrieve_rejects_dot_id_without_issuing_request( self, session_id, http_client_mock ): - with pytest.raises(stripe.InvalidRequestError): + with pytest.raises(ValueError): stripe.checkout.Session.retrieve(session_id) http_client_mock.assert_no_request() From 2f6b8cb12fb81961519e25d6ad0dda1aa9f3dc87 Mon Sep 17 00:00:00 2001 From: Zachary Chua Date: Mon, 23 Nov 2026 15:09:19 -0800 Subject: [PATCH 5/5] Treat an encoded slash as a separator when checking for dot segments 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 Committed-By-Agent: claude --- stripe/_util.py | 7 +++++-- tests/test_api_requestor.py | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/stripe/_util.py b/stripe/_util.py index f20088f11..5de42531f 100644 --- a/stripe/_util.py +++ b/stripe/_util.py @@ -385,7 +385,10 @@ def validate_path(path: str) -> None: changes which endpoint is called: retrieving "." from /v1/checkout/sessions/{session} lists every session instead. IDs are escaped with quote_plus, which leaves dots alone, and some clients decode - "%2E" back to "." before removing it, so we check the decoded segment. + "%2E" back to "." before removing it, so we check the decoded segment. An + encoded slash ("%2F") also counts as a separator for this check, because a + proxy that decodes the path before normalizing it (nginx's proxy_pass, for + example) turns it into a real one. """ if not path.startswith("/") or path.startswith("//"): raise ValueError( @@ -398,7 +401,7 @@ def validate_path(path: str) -> None: f"Request path may not contain a scheme or authority, got: {path!r}" ) - for segment in parts.path.split("/"): + for segment in re.split(r"/|%2[fF]", parts.path): if unquote(segment) in (".", ".."): # The message leaves out the query string, which can carry # secrets such as a client_secret. diff --git a/tests/test_api_requestor.py b/tests/test_api_requestor.py index 802e97896..e3184334a 100644 --- a/tests/test_api_requestor.py +++ b/tests/test_api_requestor.py @@ -1249,6 +1249,8 @@ def test_raw_request_with_file_param(self, requestor, http_client_mock): "/v1/customers?email=a..b%40example.com", # A double-encoded dot reaches the API as the literal "%2E". "/v1/customers/%252E", + # An encoded slash is fine as long as no piece around it is a dot segment. + "/v1/coupons/summer%2F2017", ] DOT_SEGMENT_PATHS = [ @@ -1264,6 +1266,11 @@ def test_raw_request_with_file_param(self, requestor, http_client_mock): "/v1/checkout/sessions/%2E", "/v1/checkout/sessions/%2e%2E", "/v1/checkout/sessions/.%2E", + # A proxy that decodes "%2F" before removing dot segments turns these + # into real path separators. + "/v1/coupons/x%2F..%2F..%2Fcustomers%2Fcus_B", + "/v1/customers/cus_123/sources/..%2F..%2Fcus_B", + "/v1/coupons/..%2fsummer", ] HOSTILE_PATHS = [ @@ -1372,6 +1379,18 @@ def test_service_nested_rejects_dot_parent_id_without_issuing_request( http_client_mock.assert_no_request() + def test_service_rejects_id_with_encoded_dot_segments_without_issuing_request( + self, http_client_mock + ): + client = stripe.StripeClient( + "sk_test_123", http_client=http_client_mock.get_mock_http_client() + ) + + with pytest.raises(ValueError): + client.v1.coupons.retrieve("x/../../customers/cus_B") + + http_client_mock.assert_no_request() + @pytest.mark.parametrize("session_id", [".", ".."]) def test_legacy_retrieve_rejects_dot_id_without_issuing_request( self, session_id, http_client_mock