From 979c1371b4fdb05a57ddf28ea44ec439d132a8b3 Mon Sep 17 00:00:00 2001 From: Zachary Chua Date: Fri, 20 Nov 2026 12:03:41 -0800 Subject: [PATCH] Reject "." and ".." path segments before sending requests ApiResource.urlEncodeId leaves dots alone, so an ID of "." or ".." produces a dot segment that a custom HttpClient, proxy, or server could remove, sending the request to a different endpoint. validatePath now rejects these segments, including percent-encoded forms, with an InvalidRequestException. Co-Authored-By: Claude Opus 5.5 Committed-By-Agent: claude --- ...1-20_zacchua_reject-dot-segments.change.md | 6 ++ .../stripe/net/LiveStripeResponseGetter.java | 25 ++++- .../stripe/net/OriginRelativePathTest.java | 94 +++++++++++++++++++ 3 files changed, 123 insertions(+), 2 deletions(-) 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 000000000000..41a7140822f3 --- /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 ".", before sending the request +semver_level: patch +jira_tickets_closed: +- RUN_DEVSDK-3747 +--- diff --git a/src/main/java/com/stripe/net/LiveStripeResponseGetter.java b/src/main/java/com/stripe/net/LiveStripeResponseGetter.java index 66b3f0b766c2..c1aea72635eb 100644 --- a/src/main/java/com/stripe/net/LiveStripeResponseGetter.java +++ b/src/main/java/com/stripe/net/LiveStripeResponseGetter.java @@ -19,6 +19,7 @@ import java.io.InputStream; import java.lang.reflect.Type; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.Optional; import java.util.function.Function; @@ -515,15 +516,35 @@ public void validateRequestOptions(RequestOptions options) { *

So, we require that a path starts with a leading slash. Deliberately not using {@link * java.net.URI} to parse -- it enforces RFC 2396 strictly and would reject paths containing * characters that callers have always been able to send. + * + *

We also reject {@code "."} and {@code ".."} segments, which an ID of {@code "."} or {@code + * ".."} produces because {@link ApiResource#urlEncodeId} leaves dots alone. {@link + * HttpURLConnectionClient} sends these segments literally, but a custom {@link HttpClient}, a + * proxy, or a server may remove them, some after decoding {@code "%2E"}, so the request would + * reach a different endpoint. It throws {@link InvalidRequestException} so that callers' existing + * handling of API errors keeps working. */ - static void validatePath(String path) { + static void validatePath(String path) throws InvalidRequestException { if (path == null || !path.startsWith("/") || path.startsWith("//")) { throw new IllegalArgumentException( "Request path must begin with a single \"/\", got: " + path); } + + for (String segment : path.split("[?#]", 2)[0].split("/", -1)) { + String decoded = segment.toLowerCase(Locale.ROOT).replace("%2e", "."); + if (decoded.equals(".") || decoded.equals("..")) { + throw new InvalidRequestException( + "Request path may not contain a \".\" or \"..\" segment, got: " + path, + null, + null, + null, + 0, + null); + } + } } - private String fullUrl(BaseApiRequest apiRequest) { + private String fullUrl(BaseApiRequest apiRequest) throws InvalidRequestException { BaseAddress baseAddress = apiRequest.getBaseAddress(); RequestOptions options = apiRequest.getOptions(); String relativeUrl = apiRequest.getPath(); diff --git a/src/test/java/com/stripe/net/OriginRelativePathTest.java b/src/test/java/com/stripe/net/OriginRelativePathTest.java index 9e4f8c5eac4f..eeec627ee6df 100644 --- a/src/test/java/com/stripe/net/OriginRelativePathTest.java +++ b/src/test/java/com/stripe/net/OriginRelativePathTest.java @@ -3,7 +3,11 @@ import static org.junit.jupiter.api.Assertions.*; import com.stripe.BaseStripeTest; +import com.stripe.exception.InvalidRequestException; +import com.stripe.model.Customer; +import com.stripe.model.checkout.Session; import org.junit.jupiter.api.Test; +import org.mockito.Mockito; public class OriginRelativePathTest extends BaseStripeTest { @@ -17,6 +21,29 @@ public class OriginRelativePathTest extends BaseStripeTest { "/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", + }; + + private static final String[] DOT_SEGMENT_PATHS = { + // A client, proxy, or server that removes these segments would send each + // request to 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 of them decode "%2E" to "." before removing dot segments. + "/v1/checkout/sessions/%2E", + "/v1/checkout/sessions/%2e%2E", + "/v1/checkout/sessions/.%2E", }; private static final String[] HOSTILE_PATHS = { @@ -62,4 +89,71 @@ public void testRejectionMessageNamesThePath() { () -> LiveStripeResponseGetter.validatePath("@evil.example/v1/leak")); assertTrue(e.getMessage().contains("@evil.example/v1/leak")); } + + @Test + public void testRejectsDotSegmentPaths() { + for (String path : DOT_SEGMENT_PATHS) { + InvalidRequestException e = + assertThrows( + InvalidRequestException.class, + () -> LiveStripeResponseGetter.validatePath(path), + "expected to reject: " + path); + assertEquals( + "Request path may not contain a \".\" or \"..\" segment, got: " + path, e.getMessage()); + } + } + + @Test + public void testRequestRejectsDotSegmentPathsWithoutSendingRequest() { + for (String path : DOT_SEGMENT_PATHS) { + ApiRequest request = + new ApiRequest(BaseAddress.API, ApiResource.RequestMethod.GET, path, null, null); + assertThrows( + InvalidRequestException.class, + () -> networkSpy.request(request, Customer.class), + "expected to reject: " + path); + } + + Mockito.verifyNoInteractions(httpClientSpy); + } + + @Test + public void testRawRequestRejectsDotSegmentPathsWithoutSendingRequest() { + for (String path : DOT_SEGMENT_PATHS) { + assertThrows( + InvalidRequestException.class, + () -> mockClient.rawRequest(ApiResource.RequestMethod.GET, path, null), + "expected to reject: " + path); + } + + Mockito.verifyNoInteractions(httpClientSpy); + } + + @Test + public void testServiceRetrieveRejectsDotIdWithoutSendingRequest() { + for (String id : new String[] {".", ".."}) { + assertThrows( + InvalidRequestException.class, () -> mockClient.v1().checkout().sessions().retrieve(id)); + } + + Mockito.verifyNoInteractions(httpClientSpy); + } + + @Test + public void testNestedServiceRetrieveRejectsDotParentIdWithoutSendingRequest() { + assertThrows( + InvalidRequestException.class, + () -> mockClient.v1().customers().paymentMethods().retrieve("..", "pm_123")); + + Mockito.verifyNoInteractions(httpClientSpy); + } + + @Test + public void testLegacyRetrieveRejectsDotIdWithoutSendingRequest() { + for (String id : new String[] {".", ".."}) { + assertThrows(InvalidRequestException.class, () -> Session.retrieve(id)); + } + + Mockito.verifyNoInteractions(httpClientSpy); + } }