Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
---
21 changes: 20 additions & 1 deletion stripe/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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(
Expand All @@ -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"):
Expand Down
95 changes: 95 additions & 0 deletions tests/test_api_requestor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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
):
Expand Down