-
Notifications
You must be signed in to change notification settings - Fork 30
fix: preserve client IP and VTEX route priority when proxying #1655
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nicacioliveira
wants to merge
4
commits into
main
Choose a base branch
from
fix/proxy-preserve-client-ip
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+40
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ee88a55
fix(website): preserve client IP when proxying
nicacioliveira 73966ef
fix(website): do not duplicate client IP in x-forwarded-for
nicacioliveira 57e4f0c
fix(vtex): give VTEX system paths route priority
nicacioliveira 6f69aef
fix(website): normalize IPs before the x-forwarded-for dedup check
nicacioliveira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,6 +17,20 @@ const HOP_BY_HOP = [ | |||||||||||||||||||||||||||||||||||||||||||
| const noTrailingSlashes = (str: string) => | ||||||||||||||||||||||||||||||||||||||||||||
| str.at(-1) === "/" ? str.slice(0, -1) : str; | ||||||||||||||||||||||||||||||||||||||||||||
| const sanitize = (str: string) => str.startsWith("/") ? str : `/${str}`; | ||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||
| * Canonical form of an IP for comparison only — never for forwarding. | ||||||||||||||||||||||||||||||||||||||||||||
| * x-forwarded-for entries may be bracketed and carry a port ([::1]:443, | ||||||||||||||||||||||||||||||||||||||||||||
| * 1.2.3.4:56789) and IPv6 hex casing varies between hops; cf-connecting-ip | ||||||||||||||||||||||||||||||||||||||||||||
| * is always a bare address. | ||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||
| const normalizeIp = (value: string): string => { | ||||||||||||||||||||||||||||||||||||||||||||
| const ip = value.trim().toLowerCase(); | ||||||||||||||||||||||||||||||||||||||||||||
| const bracketed = ip.match(/^\[(.+)\](?::\d+)?$/); | ||||||||||||||||||||||||||||||||||||||||||||
| if (bracketed) return bracketed[1]; | ||||||||||||||||||||||||||||||||||||||||||||
| const ipv4WithPort = ip.match(/^([\d.]+):\d+$/); | ||||||||||||||||||||||||||||||||||||||||||||
| if (ipv4WithPort) return ipv4WithPort[1]; | ||||||||||||||||||||||||||||||||||||||||||||
| return ip; | ||||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+26
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Equivalent IPv6 spellings can still be prepended as duplicate client entries because Prompt for AI agents
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||
| export const removeCFHeaders = (headers: Headers) => { | ||||||||||||||||||||||||||||||||||||||||||||
| headers.forEach((_value, key) => { | ||||||||||||||||||||||||||||||||||||||||||||
| if (key.startsWith("cf-")) { | ||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -145,7 +159,28 @@ export default function Proxy({ | |||||||||||||||||||||||||||||||||||||||||||
| if (isFreshCtx<DecoSiteState>(_ctx)) { | ||||||||||||||||||||||||||||||||||||||||||||
| _ctx?.state?.monitoring?.logger?.log?.("proxy received headers", headers); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| // cf-connecting-ip carries the real client IP and removeCFHeaders is about | ||||||||||||||||||||||||||||||||||||||||||||
| // to drop it, leaving the proxied origin without x-real-ip. x-forwarded-for | ||||||||||||||||||||||||||||||||||||||||||||
| // usually already arrives with the client IP first, so only fill the gaps. | ||||||||||||||||||||||||||||||||||||||||||||
| // | ||||||||||||||||||||||||||||||||||||||||||||
| // Trust boundary: these headers are only as trustworthy as the ingress in | ||||||||||||||||||||||||||||||||||||||||||||
| // front of this handler. x-forwarded-for is already forwarded untouched, so | ||||||||||||||||||||||||||||||||||||||||||||
| // an origin reachable outside the CDN could always be fed a forged first | ||||||||||||||||||||||||||||||||||||||||||||
| // entry — deriving x-real-ip from cf-connecting-ip does not widen that. | ||||||||||||||||||||||||||||||||||||||||||||
| // Authenticating the edge belongs at the ingress, not here. | ||||||||||||||||||||||||||||||||||||||||||||
| const clientIp = headers.get("cf-connecting-ip"); | ||||||||||||||||||||||||||||||||||||||||||||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||||||||||||||||||||||||||||||||||||||||||||
| removeCFHeaders(headers); // cf-headers are not ASCII-compliant | ||||||||||||||||||||||||||||||||||||||||||||
| if (clientIp) { | ||||||||||||||||||||||||||||||||||||||||||||
| const forwardedFor = headers.get("x-forwarded-for"); | ||||||||||||||||||||||||||||||||||||||||||||
| if (!forwardedFor) { | ||||||||||||||||||||||||||||||||||||||||||||
| headers.set("x-forwarded-for", clientIp); | ||||||||||||||||||||||||||||||||||||||||||||
| } else if ( | ||||||||||||||||||||||||||||||||||||||||||||
| normalizeIp(forwardedFor.split(",")[0]) !== normalizeIp(clientIp) | ||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||
| headers.set("x-forwarded-for", `${clientIp}, ${forwardedFor}`); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| headers.set("x-real-ip", clientIp); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
| if (removeDirtyCookies) { | ||||||||||||||||||||||||||||||||||||||||||||
| removeDirtyCookiesFn(headers); | ||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: deco-cx/apps
Length of output: 24295
🏁 Script executed:
Repository: deco-cx/apps
Length of output: 23663
Keep
extraPathsout of the high-priority block.routeFromPathis closed overhighPriority: trueand invoked for bothPATHS_TO_PROXYand every configuredextraPaths. Use normal priority forextraPaths, or explicitly document these routes as VTEX system paths that must bypass A/B test catch-all routes.🤖 Prompt for AI Agents