fix(Router): add x-forwarded-host to cache key for domain-based routing#6384
Open
ax-at wants to merge 1 commit intoanomalyco:devfrom
Open
fix(Router): add x-forwarded-host to cache key for domain-based routing#6384ax-at wants to merge 1 commit intoanomalyco:devfrom
ax-at wants to merge 1 commit intoanomalyco:devfrom
Conversation
…n content mismatch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Summary
This PR fixes a critical caching bug in
sst.aws.Routerwhere CloudFront serves cached content from one subdomain to requests for another subdomain. The fix addsx-forwarded-hostto the CloudFront cache key to ensure each subdomain has separate cache entries.Fixes: #6383
Technical Explanation
The Problem
The Router's cache policy (lazy routes mode) only includes
x-open-next-cache-keyin the cache key:File: platform/src/components/aws/router.ts (lines 1452-1457)
This causes CloudFront to cache responses without considering the domain:
When a user visits
light.development.example.comafter visitingdark.development.example.com, CloudFront finds a cache hit and returns the cached dark app content without executing the CloudFront Function.The Solution
File:
platform/src/components/aws/router.tsChange:
Add
x-forwarded-hostto the cache key:This creates domain-specific cache entries:
Now each subdomain has separate cache entries, and CloudFront serves the correct content.
Why
x-forwarded-hostInstead ofhost?Critical Design Decision: We use
x-forwarded-hostinstead of the standardhostheader to avoid breaking Lambda URL OAC (Origin Access Control) authentication.How CloudFront Cache Policy Works
When a header is added to the cache policy whitelist, CloudFront does TWO things:
The Lambda URL OAC Problem
Lambda URLs with OAC use AWS SigV4 authentication, which requires:
If we whitelist the
hostheader in the cache policy:Host: dark.development.example.comhostin cache key ✅Host: dark.development.example.comto origin ❌Host: <lambda-url>.lambda-url.ap-south-1.on.awsWhy
x-forwarded-hostWorksThe CloudFront Function already sets
x-forwarded-host(seeplatform/src/components/aws/router.ts, lines 2085-2086):By using
x-forwarded-host:x-forwarded-host: dark.development.example.comx-forwarded-host✅Host: <lambda-url>...(unchanged) ✅x-forwarded-host: dark.development.example.com(preserved)x-forwarded-host✅Request Flow Diagram
sequenceDiagram participant Browser participant CloudFront participant CFFunction as CloudFront_Function participant LambdaURL as Lambda_URL_with_OAC Browser->>CloudFront: GET dark.development.example.com/ Note over CloudFront: Cache key includes x-forwarded-host CloudFront->>CFFunction: viewer-request event<br/>Host: dark.development.example.com Note over CFFunction: Sets x-forwarded-host = dark.development.example.com CFFunction->>CFFunction: Match route in KV Store CFFunction->>CloudFront: Update origin to Lambda URL CloudFront->>LambdaURL: Forward request<br/>Host: xxx.lambda-url.ap-south-1.on.aws<br/>x-forwarded-host: dark.development.example.com Note over LambdaURL: OAC validates with Lambda URL host ✅ Note over LambdaURL: App knows original domain from x-forwarded-host LambdaURL-->>CloudFront: Response Note over CloudFront: Cache with x-forwarded-host in key CloudFront-->>Browser: ResponseTesting Performed
Test Setup
Test Scenarios
Without Fix
dark.development.example.com→ Dark app loads ✅light.development.example.com→ Dark app loads ❌ (cached)light.development.example.com→ Light app loads ✅dark.development.example.com→ Light app loads ❌ (cached)With Fix
dark.development.example.com→ Dark app loads ✅light.development.example.com→ Light app loads ✅dark.development.example.com→ Dark app loads ✅app-1-pr-123.development.example.com✅curl Validation
Breaking Changes
None. This change is additive and only affects how CloudFront caches responses for domain-based routing. Existing deployments continue to work (albeit with the caching bug).