Repository navigation
perf(core): parse the request URI only when the Cognito or S3 filter needs it - #7932
Conversation
…needs it The Cognito custom domain filter and the S3 virtual host filter run on every request of every service. Each read the full request URI first, which parses it and allocates about 340 bytes. RESTEasy keeps the first parse for the request, so both filters had to change. The Cognito filter now checks the routed path before it reads the URI. The S3 filter reads the URI only when the Host header is missing or a bucket matched.
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
PR Summary by QodoAvoid eager request URI parsing in Cognito and S3 filters
AI Description
Diagram
High-Level Assessment
Files changed (4)
|
Code Review by Qodo
1. Slashless paths miss custom-domain login
|
| if (!mayServe(requestContext.getUriInfo().getPath())) { | ||
| return; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
| if (!mayServe(requestContext.getUriInfo().getPath())) { | ||
| return; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
|
🎉 This PR is included in version 2.5.0 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
Summary
The Cognito custom domain filter and the S3 virtual host filter run on every request of every service. Each began by reading the full request URI, which parsed it and allocated about 340 bytes. The parse is kept for the rest of the request, so both filters had to stop.
The Cognito filter now checks the routed path first. The S3 filter reads the URI only when the Host header is missing or a bucket matched. Requests to other services no longer parse the URI, and the share of request thread CPU in the two filters fell from 1.6% to 0.6%. An S3 request still parses its URI, now in the S3 handler that reads the key. Allocation per request fell by 0.6% to 1.5%. Latency fell by up to 1.0%, and no request got slower beyond the noise.
Measured on the JVM in memory mode. Both builds ran at the same time and one client alternated between them in batches of 1,000 requests, for 6 passes on fresh servers. Each latency change is the median of the 6 paired ratios. Comparing main with itself over 6 passes stayed within 0.3% for p50, 0.8% for p95, and 0.9% for the allocation per request. S3 GetObject varied about twice as much between passes as the other requests, so its row comes from a separate run of S3 GetObject alone for 12 passes. Main against itself in that run stayed within 0.6% for p50, 0.5% for p95, and 0.2% for the allocation. Allocation comes from an async-profiler profile.
Type of change
fix:)feat:)feat!:orfix!:)AWS Compatibility
No change for the paths the Cognito filter serves or for any request the S3 filter routes. Both still read the same URI for those. On a Cognito custom domain, a path that leaves /oauth2/ through encoded dots, such as /oauth2/%2E%2E/x, now gets the routing that /oauth2/../x already got. New test lines check that the URI is not read for other paths and for another service's request.
Checklist
./mvnw testpasses locally