Repository navigation
fix(proxy): backport GA4 client IP forwarding to 1.x #941
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
Merged
Merged
Changes from all commits
Commits
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 |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| import type { Server } from 'node:http' | ||
| import { createServer } from 'node:http' | ||
| import { createApp, toNodeListener } from 'h3' | ||
| import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| /** | ||
| * Issue #939: GA4 geolocates collection hits by the connecting IP and ignores | ||
| * X-Forwarded-For, so the proxy forwards the first X-Forwarded-For entry as `_uip`. | ||
| */ | ||
|
|
||
| vi.mock('#nuxt-scripts/nitro', () => ({ | ||
| useRuntimeConfig: () => ({ | ||
| 'nuxt-scripts-proxy': { | ||
| proxyPrefix: '/_scripts/p', | ||
| domainPrivacy: { | ||
| 'www.google-analytics.com': true, | ||
| 'region1.google-analytics.com': false, | ||
| 'analytics.google.com': true, | ||
| 'www.google.com': true, | ||
| 'stats.g.doubleclick.net': true, | ||
| 'www.facebook.com': true, | ||
| }, | ||
| debug: false, | ||
| }, | ||
| }), | ||
| useNitroApp: () => ({ | ||
| hooks: { callHook: async () => {} }, | ||
| }), | ||
| })) | ||
|
|
||
| vi.mock('../../packages/script/src/runtime/server/utils/network-host', async (importOriginal) => { | ||
| const actual = await importOriginal<typeof import('../../packages/script/src/runtime/server/utils/network-host')>() | ||
| return { | ||
| ...actual, | ||
| createPublicNetworkDispatcher: async () => ({ | ||
| fetch: (...args: Parameters<typeof fetch>) => globalThis.fetch(...args), | ||
| close: async () => {}, | ||
| }), | ||
| } | ||
| }) | ||
|
|
||
| describe('proxy handler - GA4 client IP (#939)', () => { | ||
| let proxyServer: Server | ||
| let proxyPort: number | ||
| let upstreamServer: Server | ||
| let upstreamPort: number | ||
| let realFetch: typeof globalThis.fetch | ||
| let lastTargetUrl = '' | ||
|
|
||
| beforeAll(async () => { | ||
| upstreamServer = createServer((_req, res) => { | ||
| res.writeHead(204) | ||
| res.end() | ||
| }) | ||
| await new Promise<void>(resolve => upstreamServer.listen(0, resolve)) | ||
| upstreamPort = (upstreamServer.address() as any).port | ||
|
|
||
| realFetch = globalThis.fetch | ||
| globalThis.fetch = async (input: any, init?: any) => { | ||
| const reqUrl = typeof input === 'string' ? input : input.url | ||
| lastTargetUrl = reqUrl | ||
| const url = new URL(reqUrl) | ||
| return realFetch(`http://127.0.0.1:${upstreamPort}${url.pathname}${url.search}`, { ...init, headers: {} }) | ||
| } | ||
|
|
||
| const mod = await import('../../packages/script/src/runtime/server/proxy-handler') | ||
| const app = createApp() | ||
| app.use(mod.default) | ||
| proxyServer = createServer(toNodeListener(app)) | ||
| await new Promise<void>(resolve => proxyServer.listen(0, resolve)) | ||
| proxyPort = (proxyServer.address() as any).port | ||
| }) | ||
|
|
||
| beforeEach(() => { | ||
| lastTargetUrl = '' | ||
| }) | ||
|
|
||
| afterAll(() => { | ||
| if (realFetch) | ||
| globalThis.fetch = realFetch | ||
| upstreamServer?.close() | ||
| proxyServer?.close() | ||
| }) | ||
|
|
||
| async function post(path: string, xForwardedFor = '203.0.113.7') { | ||
| const res = await realFetch(`http://127.0.0.1:${proxyPort}/_scripts/p/${path}`, { | ||
| method: 'POST', | ||
| headers: { 'x-forwarded-for': xForwardedFor }, | ||
| }) | ||
| expect(res.status).toBe(204) | ||
| return lastTargetUrl | ||
| } | ||
|
|
||
| it('adds the anonymized client IP to www.google-analytics.com /g/collect', async () => { | ||
| expect(await post('www.google-analytics.com/g/collect?v=2&tid=G-TEST')) | ||
| .toBe('https://www.google-analytics.com/g/collect?v=2&tid=G-TEST&_uip=203.0.113.0') | ||
| expect(await post('www.google-analytics.com/g/collect?v=2&tid=G-TEST', '2001:db8:abcd:12::1')) | ||
| .toBe('https://www.google-analytics.com/g/collect?v=2&tid=G-TEST&_uip=2001%3Adb8%3Aabcd%3A%3A') | ||
| }) | ||
|
|
||
| it('uses the first X-Forwarded-For entry for region1.analytics.google.com', async () => { | ||
| expect(await post('region1.analytics.google.com/g/collect?v=2', '198.51.100.23, 10.0.0.1')) | ||
| .toBe('https://region1.analytics.google.com/g/collect?v=2&_uip=198.51.100.0') | ||
| }) | ||
|
|
||
| it('adds the client IP to the www.google.com copy of /g/collect', async () => { | ||
| expect(await post('www.google.com/g/collect?v=2&gaf=1')) | ||
| .toBe('https://www.google.com/g/collect?v=2&gaf=1&_uip=203.0.113.0') | ||
| }) | ||
|
|
||
| it('adds the client IP to /g/s/collect', async () => { | ||
| expect(await post('www.google-analytics.com/g/s/collect?v=2')) | ||
| .toBe('https://www.google-analytics.com/g/s/collect?v=2&_uip=203.0.113.0') | ||
| }) | ||
|
|
||
| it('uses the raw client IP when IP privacy is off', async () => { | ||
| expect(await post('region1.google-analytics.com/g/collect?v=2')) | ||
| .toBe('https://region1.google-analytics.com/g/collect?v=2&_uip=203.0.113.7') | ||
| }) | ||
|
|
||
| it('replaces a client-supplied _uip with the anonymized client IP when IP privacy is on', async () => { | ||
| expect(await post('www.google-analytics.com/g/collect?v=2&_uip=192.0.2.55&tid=G-TEST')) | ||
| .toBe('https://www.google-analytics.com/g/collect?v=2&tid=G-TEST&_uip=203.0.113.0') | ||
| }) | ||
|
|
||
| it('keeps a client-supplied _uip when IP privacy is off', async () => { | ||
| expect(await post('region1.google-analytics.com/g/collect?v=2&_uip=192.0.2.55')) | ||
| .toBe('https://region1.google-analytics.com/g/collect?v=2&_uip=192.0.2.55') | ||
| }) | ||
|
|
||
| it('leaves non-GA4 endpoints unchanged', async () => { | ||
| expect(await post('www.google.com/pagead/1p-conversion/123/?v=2')) | ||
| .toBe('https://www.google.com/pagead/1p-conversion/123/?v=2') | ||
| expect(await post('stats.g.doubleclick.net/g/collect?v=2')) | ||
| .toBe('https://stats.g.doubleclick.net/g/collect?v=2') | ||
| expect(await post('www.facebook.com/tr?id=1&ev=PageView')) | ||
| .toBe('https://www.facebook.com/tr?id=1&ev=PageView') | ||
| }) | ||
| }) |
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.
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: nuxt/scripts
Length of output: 8697
🏁 Script executed:
Repository: nuxt/scripts
Length of output: 30627
🏁 Script executed:
Repository: nuxt/scripts
Length of output: 36928
🏁 Script executed:
Repository: nuxt/scripts
Length of output: 19046
🌐 Web query:
H3 1.15.11 getRequestIP xForwardedFor true implementation first X-Forwarded-For entry💡 Result:
🏁 Script executed:
Repository: nuxt/scripts
Length of output: 5666
Derive GA4
_uipfrom a trusted client address.When a GA4 proxy request reaches Nitro without a trusted
event.context.clientAddress, H3 can take the first incomingX-Forwarded-Forentry. This handler then uses that address for_uip—anonymized when IP privacy is enabled—so a caller can make events use a chosen IP or subnet for geolocation. Use only an ingress-resolved trusted address, and omit_uipwhen none is available.🤖 Prompt for AI Agents