Skip to content
Draft
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 ".", before sending the request
semver_level: patch
jira_tickets_closed:
- RUN_DEVSDK-3747
---
25 changes: 23 additions & 2 deletions src/main/java/com/stripe/net/LiveStripeResponseGetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -515,15 +516,35 @@ public void validateRequestOptions(RequestOptions options) {
* <p>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.
*
* <p>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();
Expand Down
94 changes: 94 additions & 0 deletions src/test/java/com/stripe/net/OriginRelativePathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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 = {
Expand Down Expand Up @@ -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);
}
}
Loading