Skip to content
Merged
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
Expand Up @@ -72,9 +72,13 @@ public CognitoCustomDomainFilter(CognitoService cognitoService) {

@Override
public void filter(ContainerRequestContext requestContext) {
// Every request of every service passes here, so the URI is parsed only for a path this filter may serve.
if (!mayServe(requestContext.getUriInfo().getPath())) {
return;
Comment on lines +76 to +77

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Action required

1. Slashless paths miss custom-domain login 🐞 Bug ≡ Correctness

CognitoCustomDomainFilter.filter passes UriInfo.getPath() to mayServe, whose accepted paths
all begin with /, without first accounting for the slashless relative path that getPath() can
return. When it returns oauth2/token, login, or logout, the filter exits before domain lookup
and rewrite, leaving those custom-domain requests to other routes instead of their Cognito handlers.
Agent Prompt
## Issue description
The early Cognito path check can reject a served path when `UriInfo.getPath()` omits its leading slash.
## Fix Focus Areas
- src/main/java/io/floci/aws/services/cognito/CognitoCustomDomainFilter.java[75-82]
- src/main/java/io/floci/aws/services/cognito/CognitoCustomDomainFilter.java[123-125]
- src/test/java/io/floci/aws/services/cognito/CognitoCustomDomainFilterTest.java[204-208]
## Recommended Fix
Handle both relative and slash-prefixed `UriInfo` paths before applying the preliminary predicate. Test served paths with a slashless `getPath()` value rather than deriving every stubbed path from `URI.getPath()`. Preserve the separate raw-path check.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗

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.

UriInfo.getPath() keeps the leading slash here. RESTEasy Reactive returns the decoded request path with its leading slash, and Floci sets no application path, so nothing is cut from it. SignedServiceRoute and the SQS queue URL filter already compare this value with slash-prefixed paths. CognitoCustomDomainOAuthContractIntegrationTest sends /oauth2/token to a custom domain over HTTP and passes on this branch.

Comment on lines +76 to +77

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Action required

2. Encoded paths bypass domain pool pinning 🐞 Bug ⛨ Security

CognitoCustomDomainFilter.filter now gates its raw-path check on UriInfo.getPath(), although the
routed path can have encoded dot segments removed before that check. For a custom-domain request
such as /oauth2/%2E%2E/cognito-idp/oauth2/token, that gate can exit before setting the pool
property, allowing the normalized path to reach the generic token handler without the custom-domain
pool restriction; valid token credentials are still required.
Agent Prompt
## Issue description
The preliminary check can miss an original `/oauth2/` path when routing normalizes encoded dot segments out of it, skipping the custom-domain pool binding.
## Fix Focus Areas
- src/main/java/io/floci/aws/services/cognito/CognitoCustomDomainFilter.java[75-82]
- src/test/java/io/floci/aws/services/cognito/CognitoCustomDomainFilterTest.java[204-208]
## Recommended Fix
Ensure the preliminary decision preserves the original request path's `/oauth2/` prefix rather than relying solely on the decoded or normalized routed path. If a safe raw-path fast path is unavailable, retain URI parsing for ambiguous requests. Add a test that supplies distinct normalized and raw paths for an encoded-dot-segment URL and verifies the custom-domain response and pool isolation.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

Dismiss ↗ | View ↗

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.

This opens no new way to reach the token handler. The filter rewrites /oauth2/token to /cognito-idp/oauth2/token, and a client can send /cognito-idp/oauth2/token to the custom domain host directly. The filter has always left that path alone, since it does not start with /oauth2/. Before this change, /oauth2/%2E%2E/cognito-idp/oauth2/token got the 404 for an unknown /oauth2/ path. It now routes like /oauth2/../cognito-idp/oauth2/token, which the URI normalization already sent past the filter. The PR description notes this.

}
URI originalUri = requestContext.getUriInfo().getRequestUri();
String path = originalUri.getRawPath();
if (path == null || !(path.startsWith(OAUTH_PREFIX) || MANAGED_LOGIN_PATHS.contains(path))) {
if (path == null || !mayServe(path)) {
return;
}
String host = RequestHost.of(requestContext);
Expand Down Expand Up @@ -116,6 +120,10 @@ public void filter(ContainerRequestContext requestContext) {
RequestUriRewrite.apply(requestContext, newUri);
}

private static boolean mayServe(String path) {
return path.startsWith(OAUTH_PREFIX) || MANAGED_LOGIN_PATHS.contains(path);
}

private static String stripPort(String host) {
int colonIndex = host.lastIndexOf(':');
if (colonIndex > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,16 @@ static Set<String> buildServiceHostSuffixes(Optional<String> hostname, Optional<

@Override
public void filter(ContainerRequestContext requestContext) {
URI uri = requestContext.getUriInfo().getRequestUri();

// HTTP/2 (RFC 9113) has no "Host" header — the authority travels in the
// ":authority" pseudo-header, surfaced here as the request URI authority.
// Falling back to it keeps virtual-hosted-style routing working when a
// browser negotiates HTTP/2 over HTTPS (where the Host header is absent).
// Reverse proxies (e.g. Traefik, Nginx) preserve the client's original host
// in X-Forwarded-Host when Host is rewritten to the internal upstream.
String host = resolveHost(requestContext.getHeaderString("Host"),
requestContext.getHeaderString("X-Forwarded-Host"), uri);
String hostHeader = requestContext.getHeaderString("Host");
// Every request of every service passes here, so the URI is parsed only when the host or a bucket needs it.
URI hostUri = hostHeader == null || hostHeader.isBlank() ? requestContext.getUriInfo().getRequestUri() : null;
String host = resolveHost(hostHeader, requestContext.getHeaderString("X-Forwarded-Host"), hostUri);
if (host == null) return;

// Do not hijack requests meant for other AWS services
Expand Down Expand Up @@ -156,6 +156,7 @@ public void filter(ContainerRequestContext requestContext) {
}
if (bucket == null) return;

URI uri = requestContext.getUriInfo().getRequestUri();
String path = uri.getRawPath();

// Do not rewrite S3 Control API paths — the account ID appears as a host label
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void leavesOtherPathsAlone() {

verify(request, never()).setRequestUri(any());
verify(request, never()).setProperty(any(), any());
verify(request.getUriInfo(), never()).getRequestUri();
}
verifyNoInteractions(cognitoService);
}
Expand Down Expand Up @@ -203,6 +204,7 @@ private static ContainerRequestContext request(String method, String uri, String
when(request.getMethod()).thenReturn(method);
UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getRequestUri()).thenReturn(URI.create(uri));
when(uriInfo.getPath()).thenReturn(URI.create(uri).getPath());
when(request.getUriInfo()).thenReturn(uriInfo);
when(request.getHeaderString("Host")).thenReturn(hostHeader);
return request;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ void filterStillIgnoresUnsignedFormRequests() {
new S3VirtualHostFilter().filter(ctx);

verify(ctx, never()).setRequestUri(any(URI.class));
verify(uriInfo, never()).getRequestUri();
}

// --- Other services' virtual-host schemes must not be swallowed as dotted buckets ---
Expand Down
Loading