-
Notifications
You must be signed in to change notification settings - Fork 75
feat: first-party mode #577
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
base: main
Are you sure you want to change the base?
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
e33eaf5 to
64ac9b2
Compare
- Add `scripts.firstParty` config option to route scripts through your domain - Download scripts at build time and rewrite collection URLs to local paths - Inject Nitro route rules to proxy requests to original endpoints - Privacy benefits: hides user IPs, eliminates third-party cookies - Add `proxy` field to RegistryScript type to mark supported scripts - Deprecate `bundle` option in favor of unified `firstParty` config - Add comprehensive unit tests and documentation Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
64ac9b2 to
7ef19de
Compare
commit: |
src/plugins/transform.ts
Outdated
| const firstPartyOption = scriptOptions?.value.properties?.find((prop) => { | ||
| return prop.type === 'Property' && prop.key?.name === 'firstParty' && prop.value.type === 'Literal' | ||
| }) | ||
| const firstPartyOptOut = firstPartyOption?.value.value === false |
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.
The code doesn't detect firstParty: false when passed as a direct option in useScript calls, only when nested in scriptOptions. Users attempting to opt-out of first-party routing would have their opt-out silently ignored for direct option usage.
View Details
π Patch Details
diff --git a/src/plugins/transform.ts b/src/plugins/transform.ts
index 98e3aeb..95d3176 100644
--- a/src/plugins/transform.ts
+++ b/src/plugins/transform.ts
@@ -380,17 +380,39 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti
forceDownload = bundleValue === 'force'
}
// Check for per-script first-party opt-out (firstParty: false)
+ // Check in three locations:
+ // 1. In scriptOptions (nested property) - useScriptGoogleAnalytics({ scriptOptions: { firstParty: false } })
+ // 2. In the second argument for direct options - useScript('...', { firstParty: false })
+ // 3. In the first argument's direct properties - useScript({ src: '...', firstParty: false })
+
+ // Check in scriptOptions (nested)
// @ts-expect-error untyped
const firstPartyOption = scriptOptions?.value.properties?.find((prop) => {
return prop.type === 'Property' && prop.key?.name === 'firstParty' && prop.value.type === 'Literal'
})
- const firstPartyOptOut = firstPartyOption?.value.value === false
+
+ // Check in second argument (direct options)
+ let firstPartyOptOut = firstPartyOption?.value.value === false
+ if (!firstPartyOptOut && node.arguments[1]?.type === 'ObjectExpression') {
+ const secondArgFirstPartyProp = (node.arguments[1] as ObjectExpression).properties.find(
+ (p: any) => p.type === 'Property' && p.key?.name === 'firstParty' && p.value.type === 'Literal'
+ )
+ firstPartyOptOut = (secondArgFirstPartyProp as any)?.value.value === false
+ }
+
+ // Check in first argument's direct properties for useScript with object form
+ if (!firstPartyOptOut && node.arguments[0]?.type === 'ObjectExpression') {
+ const firstArgFirstPartyProp = (node.arguments[0] as ObjectExpression).properties.find(
+ (p: any) => p.type === 'Property' && p.key?.name === 'firstParty' && p.value.type === 'Literal'
+ )
+ firstPartyOptOut = (firstArgFirstPartyProp as any)?.value.value === false
+ }
if (canBundle) {
const { url: _url, filename } = normalizeScriptData(src, options.assetsBaseURL)
let url = _url
// Get proxy rewrites if first-party is enabled, not opted out, and script supports it
// Use script's proxy field if defined, otherwise fall back to registry key
- const script = options.scripts.find(s => s.import.name === fnName)
+ const script = options.scripts?.find(s => s.import.name === fnName)
const proxyConfigKey = script?.proxy !== false ? (script?.proxy || registryKey) : undefined
const proxyRewrites = options.firstPartyEnabled && !firstPartyOptOut && proxyConfigKey && options.firstPartyCollectPrefix
? getProxyConfig(proxyConfigKey, options.firstPartyCollectPrefix)?.rewrite
diff --git a/test/unit/transform.test.ts b/test/unit/transform.test.ts
index 8d317e0..cc1e578 100644
--- a/test/unit/transform.test.ts
+++ b/test/unit/transform.test.ts
@@ -1280,4 +1280,84 @@ const _sfc_main = /* @__PURE__ */ _defineComponent({
expect(code).toContain('bundle.js')
})
})
+
+ describe('firstParty option detection', () => {
+ it('detects firstParty: false in scriptOptions (nested)', async () => {
+ vi.mocked(hash).mockImplementationOnce(() => 'analytics')
+ const code = await transform(
+ `const instance = useScriptGoogleAnalytics({
+ id: 'GA_MEASUREMENT_ID',
+ scriptOptions: { firstParty: false, bundle: true }
+ })`,
+ {
+ defaultBundle: false,
+ firstPartyEnabled: true,
+ firstPartyCollectPrefix: '/_scripts/c',
+ scripts: [
+ {
+ scriptBundling() {
+ return 'https://www.googletagmanager.com/gtag/js'
+ },
+ import: {
+ name: 'useScriptGoogleAnalytics',
+ from: '',
+ },
+ },
+ ],
+ },
+ )
+ // If firstParty: false is detected, proxyRewrites should be undefined (opt-out honored)
+ // This is verified by the script being bundled without proxy rewrites
+ expect(code).toBeDefined()
+ })
+
+ it('detects firstParty: false in second argument', async () => {
+ vi.mocked(hash).mockImplementationOnce(() => 'beacon.min')
+ const code = await transform(
+ `const instance = useScript('https://static.cloudflareinsights.com/beacon.min.js', {
+ bundle: true,
+ firstParty: false
+ })`,
+ {
+ defaultBundle: false,
+ firstPartyEnabled: true,
+ firstPartyCollectPrefix: '/_scripts/c',
+ scripts: [],
+ },
+ )
+ // If firstParty: false is detected, proxyRewrites should be undefined (opt-out honored)
+ expect(code).toBeDefined()
+ })
+
+ it('detects firstParty: false in first argument direct properties (integration script)', async () => {
+ vi.mocked(hash).mockImplementationOnce(() => 'analytics')
+ const code = await transform(
+ `const instance = useScriptGoogleAnalytics({
+ id: 'GA_MEASUREMENT_ID',
+ scriptOptions: { bundle: true }
+ }, {
+ firstParty: false
+ })`,
+ {
+ defaultBundle: false,
+ firstPartyEnabled: true,
+ firstPartyCollectPrefix: '/_scripts/c',
+ scripts: [
+ {
+ scriptBundling() {
+ return 'https://www.googletagmanager.com/gtag/js'
+ },
+ import: {
+ name: 'useScriptGoogleAnalytics',
+ from: '',
+ },
+ },
+ ],
+ },
+ )
+ // When firstParty: false is detected, bundling should work but without proxy rewrites
+ // Verify the script was bundled and the firstParty option is properly handled
+ expect(code).toBeDefined()
+ })
+ })
})
Analysis
firstParty: false option not detected in direct useScript calls
What fails: The firstParty: false opt-out option is only detected when passed nested in scriptOptions, but is silently ignored when passed as a direct option to useScript() or useScriptGoogleAnalytics() calls, causing proxy rewrites to be applied even when the user explicitly requested to opt-out.
How to reproduce:
In a Nuxt component, use:
// Case 1: Direct in second argument (NOT detected before fix)
useScript('https://example.com/script.js', { firstParty: false })
// Case 2: Direct in first argument's properties (NOT detected before fix)
useScript({
src: 'https://example.com/script.js',
firstParty: false
})
// Case 3: Works correctly (nested in scriptOptions)
useScriptGoogleAnalytics({
id: 'G-XXXXXX',
scriptOptions: { firstParty: false }
})When scripts.firstParty: true is enabled in nuxt.config, Cases 1 and 2 would have their script URLs rewritten to proxy paths even though firstParty: false was explicitly set, violating the user's opt-out request.
Result before fix: The firstPartyOptOut variable remained false for Cases 1 and 2, so the condition at line 395 would apply proxy rewrites: options.firstPartyEnabled && !firstPartyOptOut && proxyConfigKey && options.firstPartyCollectPrefix evaluated to true.
Expected: The firstParty: false option should be honored in all three usage patterns, preventing proxy rewrites when the user explicitly opts out.
Implementation: Extended the firstParty detection logic in src/plugins/transform.ts (lines 382-407) to check for firstParty: false in three locations:
- In
scriptOptions?.value.properties(nested property - original behavior) - In
node.arguments[1]?.properties(second argument direct options) - In
node.arguments[0]?.properties(first argument direct properties for useScript with object form)
Also fixed a pre-existing issue where options.scripts.find could fail when options.scripts is undefined by adding optional chaining.
- Default firstParty to true (graceful degradation for static) - Add /_scripts/status.json and /_scripts/health.json dev endpoints - Add DevTools First-Party tab with status, routes, and badges - Add CLI commands: status, clear, health - Add dev startup logging for proxy routes - Improve static preset error messages with actionable guidance - Expand documentation: - Platform rewrites (Vercel, Netlify, Cloudflare) - Architecture diagram - Troubleshooting section - FAQ section - Hybrid rendering (ISR, edge, route-level SSR) - Consent integration examples - Health check verification - Add first-party unit tests Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
| // Test each route by making a HEAD request to the target | ||
| for (const [route, target] of Object.entries(scriptsConfig.routes)) { | ||
| // Extract script name from route (e.g., /_scripts/c/ga/** -> ga) | ||
| const scriptMatch = route.match(/\/_scripts\/c\/([^/]+)/) |
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.
| // Test each route by making a HEAD request to the target | |
| for (const [route, target] of Object.entries(scriptsConfig.routes)) { | |
| // Extract script name from route (e.g., /_scripts/c/ga/** -> ga) | |
| const scriptMatch = route.match(/\/_scripts\/c\/([^/]+)/) | |
| // Build regex dynamically from collectPrefix to extract script name | |
| const escapedPrefix = scriptsConfig.collectPrefix.replace(/\//g, '\\/') | |
| const scriptNameRegex = new RegExp(`${escapedPrefix}\\/([^/]+)`) | |
| // Test each route by making a HEAD request to the target | |
| for (const [route, target] of Object.entries(scriptsConfig.routes)) { | |
| // Extract script name from route (e.g., /_scripts/c/ga/** -> ga) | |
| const scriptMatch = route.match(scriptNameRegex) |
The script name extraction in the health check uses a hardcoded regex pattern for /_scripts/c/, which won't work if users configure a custom collectPrefix.
View Details
Analysis
Hardcoded regex in health check fails with custom collectPrefix
What fails: The scripts-health.ts health check endpoint uses a hardcoded regex pattern /\/_scripts\/c\/([^/]+)/ to extract script names from routes, which only matches the default collectPrefix of /_scripts/c. When users configure a custom collectPrefix (e.g., /_analytics), the regex fails to match routes like /_analytics/ga/**, causing all scripts to be labeled as 'unknown' in the health check output.
How to reproduce:
- Configure custom
collectPrefixin Nuxt config:
export default defineNuxtConfig({
scripts: {
firstParty: {
collectPrefix: '/_analytics'
}
}
})- Access the health check endpoint at
/_scripts/health.json - Observe that all scripts have
script: 'unknown'instead of actual script names (ga, gtm, meta, etc.)
Expected behavior: The script name should be correctly extracted from routes regardless of the collectPrefix value. With collectPrefix: '/_analytics', a route like /_analytics/ga/** should extract 'ga' as the script name, not 'unknown'.
Root cause: The regex pattern is hardcoded for the default path and doesn't account for custom configurations available in scriptsConfig.collectPrefix.
src/plugins/transform.ts
Outdated
| // Use storage to cache the font data between builds | ||
| const cacheKey = `bundle:${filename}` | ||
| // Include proxy in cache key to differentiate proxied vs non-proxied versions | ||
| const cacheKey = proxyRewrites?.length ? `bundle-proxy:${filename}` : `bundle:${filename}` |
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.
The cache key for proxied scripts doesn't include the collectPrefix, so changing this setting between builds will reuse cached scripts with outdated rewrite URLs.
View Details
π Patch Details
diff --git a/src/plugins/transform.ts b/src/plugins/transform.ts
index 98e3aeb..8a497be 100644
--- a/src/plugins/transform.ts
+++ b/src/plugins/transform.ts
@@ -113,7 +113,9 @@ async function downloadScript(opts: {
if (!res) {
// Use storage to cache the font data between builds
// Include proxy in cache key to differentiate proxied vs non-proxied versions
- const cacheKey = proxyRewrites?.length ? `bundle-proxy:${filename}` : `bundle:${filename}`
+ // Also include a hash of proxyRewrites content to handle different collectPrefix values
+ const proxyRewritesHash = proxyRewrites?.length ? `-${ohash(proxyRewrites)}` : ''
+ const cacheKey = proxyRewrites?.length ? `bundle-proxy:${filename}${proxyRewritesHash}` : `bundle:${filename}`
const shouldUseCache = !forceDownload && await storage.hasItem(cacheKey) && !(await isCacheExpired(storage, filename, cacheMaxAge))
if (shouldUseCache) {
@@ -390,7 +392,7 @@ export function NuxtScriptBundleTransformer(options: AssetBundlerTransformerOpti
let url = _url
// Get proxy rewrites if first-party is enabled, not opted out, and script supports it
// Use script's proxy field if defined, otherwise fall back to registry key
- const script = options.scripts.find(s => s.import.name === fnName)
+ const script = options.scripts?.find(s => s.import.name === fnName)
const proxyConfigKey = script?.proxy !== false ? (script?.proxy || registryKey) : undefined
const proxyRewrites = options.firstPartyEnabled && !firstPartyOptOut && proxyConfigKey && options.firstPartyCollectPrefix
? getProxyConfig(proxyConfigKey, options.firstPartyCollectPrefix)?.rewrite
Analysis
Cache key mismatch when collectPrefix changes between builds
What fails: The cache key for proxied scripts in downloadScript() doesn't include the actual collectPrefix value, causing scripts cached with one configuration to be reused with different URL rewrites when the config changes within the cache TTL.
How to reproduce:
- Build with
firstParty: { collectPrefix: '/_scripts/c' }- script URLs rewritten to/_scripts/c/ga/g/collect - Within 7 days, change config to
firstParty: { collectPrefix: '/_analytics' }and rebuild - The cached script from step 1 is loaded from cache key
bundle-proxy:filename - Runtime expects requests at
/_analytics/ga/...but cached script sends to/_scripts/c/ga/... - Proxy requests fail because routes don't match the rewritten URLs
Result: Script gets wrong rewrite paths from cache, causing analytics/tracking requests to fail.
Expected: Each combination of script filename + collectPrefix should have its own cache entry, ensuring the correct rewritten URLs are used regardless of cache age.
Root cause: Line 116 in src/plugins/transform.ts creates cache key as bundle-proxy: when proxyRewrites?.length is truthy, but doesn't include a hash of the actual proxyRewrites content. Different collectPrefix values produce different rewrite mappings, but the same cache key.
Fix: Include hash of proxyRewrites in cache key: bundle-proxy:
| if (captures.length > 0) { | ||
| const hasValidCapture = captures.some(c => | ||
| c.path?.startsWith('/_proxy/gtm') | ||
| && c.targetUrl?.includes('googletagmanager.com') |
Check failure
Code scanning / CodeQL
Incomplete URL substring sanitization High test
googletagmanager.com
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 7 days ago
In general, the fix is to stop treating the URL as an opaque string and instead parse it, then check the hostname against a precise condition (e.g., exact match or safe subdomain match), rather than includes. For this case, the test wants to verify that at least one capture is a proxied request to Google Tag Manager. The most direct, nonβbehaviorβchanging fix is:
- Parse
c.targetUrlwith the standardURLconstructor. - Extract its
hostname. - Check that
hostnameis exactlygoogletagmanager.comor a subdomain of it (e.g.,www.googletagmanager.com,www2.googletagmanager.com), depending on what the test intends. The existingincludescheck would match both, so we should allow the base domain and subdomains.
Concretely, inside test/e2e/first-party.test.ts, in the googleTagManager test, change the captures.some(...) predicate:
- Replace
c.targetUrl?.includes('googletagmanager.com')with a helper that:- Safely handles
undefined/invalid URLs withtry/catch. - Uses
new URL(c.targetUrl)to gethostname. - Returns
truewhenhostname === 'googletagmanager.com'orhostname.endsWith('.googletagmanager.com').
- Safely handles
Because this is TypeScript targeting Node, URL is globally available and we donβt need new imports. We also stay within the shown file and only touch the provided snippet.
-
Copy modified lines R374-R384
| @@ -371,11 +371,17 @@ | ||
|
|
||
| // GTM may not fire requests if no tags are configured | ||
| if (captures.length > 0) { | ||
| const hasValidCapture = captures.some(c => | ||
| c.path?.startsWith('/_proxy/gtm') | ||
| && c.targetUrl?.includes('googletagmanager.com') | ||
| && c.privacy === 'anonymize', | ||
| ) | ||
| const hasValidCapture = captures.some((c) => { | ||
| if (!c.path?.startsWith('/_proxy/gtm') || c.privacy !== 'anonymize' || !c.targetUrl) { | ||
| return false | ||
| } | ||
| try { | ||
| const hostname = new URL(c.targetUrl).hostname | ||
| return hostname === 'googletagmanager.com' || hostname.endsWith('.googletagmanager.com') | ||
| } catch { | ||
| return false | ||
| } | ||
| }) | ||
| expect(hasValidCapture).toBe(true) | ||
|
|
||
| // Verify ALL fingerprinting params are stripped |
| expect(captures.length).toBeGreaterThan(0) | ||
| const hasValidCapture = captures.some(c => | ||
| c.path?.startsWith('/_proxy/x') | ||
| && (c.targetUrl?.includes('twitter.com') || c.targetUrl?.includes('t.co')) |
Check failure
Code scanning / CodeQL
Incomplete URL substring sanitization High test
twitter.com
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 7 days ago
In general, the fix is to avoid substring checks on the full URL string when you intend to match a host. Instead, parse the URL and compare its hostname (or host) against an explicit whitelist of allowed hosts. This prevents URLs where the allowed domain appears in the path, query, or as part of another domain from being mistakenly accepted.
Concretely for this file, we should change the xPixel test so that, instead of:
c.targetUrl?.includes('twitter.com') || c.targetUrl?.includes('t.co')we:
- Safely parse
c.targetUrlwith the standardURLconstructor (available in Node and browsers). - Extract
hostnamefrom the parsed URL. - Check that
hostnameis exactly one of the allowed hosts, e.g.'twitter.com'or't.co'.
To avoid duplicating parsing logic and to keep the change minimal, we can parse inline where the check is done, guarding against absent or malformed URLs by returning false in those cases. No new imports are needed because URL is a builtβin global in modern Node.js and the test environment.
The only region that needs to change is the hasValidCapture definition inside the "xPixel" test, around lines 466β470, within test/e2e/first-party.test.ts. We replace the .includes checks with a small inline parse-and-compare block.
-
Copy modified lines R466-R478
| @@ -463,11 +463,19 @@ | ||
| const { captures } = await testProvider('xPixel', '/x') | ||
|
|
||
| expect(captures.length).toBeGreaterThan(0) | ||
| const hasValidCapture = captures.some(c => | ||
| c.path?.startsWith('/_proxy/x') | ||
| && (c.targetUrl?.includes('twitter.com') || c.targetUrl?.includes('t.co')) | ||
| && c.privacy === 'anonymize', | ||
| ) | ||
| const hasValidCapture = captures.some((c) => { | ||
| if (!c.path?.startsWith('/_proxy/x') || c.privacy !== 'anonymize' || !c.targetUrl) { | ||
| return false | ||
| } | ||
| try { | ||
| const hostname = new URL(c.targetUrl).hostname | ||
| const allowedHosts = ['twitter.com', 't.co'] | ||
| return allowedHosts.includes(hostname) | ||
| } | ||
| catch { | ||
| return false | ||
| } | ||
| }) | ||
| expect(hasValidCapture).toBe(true) | ||
|
|
||
| // Verify ALL fingerprinting params are stripped |
| expect(captures.length).toBeGreaterThan(0) | ||
| const hasValidCapture = captures.some(c => | ||
| c.path?.startsWith('/_proxy/snap') | ||
| && c.targetUrl?.includes('snapchat.com') |
Check failure
Code scanning / CodeQL
Incomplete URL substring sanitization High test
snapchat.com
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 7 days ago
In general, the fix is to stop treating the URL as an opaque string for domain checks and instead parse it, then compare the hostname against an explicit allowed value or pattern. This avoids matches in the path or query string and ensures arbitrary prefixes/suffixes on the hostname (e.g., evil-snapchat.com, snapchat.com.evil.com) do not pass.
For this specific test, we should replace c.targetUrl?.includes('snapchat.com') with a check that:
- safely parses
c.targetUrlusing the standardURLclass, and - asserts that
hostnameis exactlysnapchat.comor an allowed Snapchat subdomain, such aswww.snapchat.com.
Because this is test code, we can keep it simple: parse the URL inside thesomecallback and then checkhostname === 'snapchat.com' || hostname.endsWith('.snapchat.com'). We should also guard against invalid URLs by catchingURLconstruction errors; invalid URLs can simply be treated as nonβmatches.
Concretely, in test/e2e/first-party.test.ts around the snapchatPixel test, update the hasValidCapture definition to wrap the URL parsing in a try/catch and replace the .includes('snapchat.com') predicate with a hostnameβbased check. No new imports are required because URL is globally available in Node and in most test environments.
-
Copy modified lines R499-R510
| @@ -496,11 +496,18 @@ | ||
| await page.close() | ||
|
|
||
| expect(captures.length).toBeGreaterThan(0) | ||
| const hasValidCapture = captures.some(c => | ||
| c.path?.startsWith('/_proxy/snap') | ||
| && c.targetUrl?.includes('snapchat.com') | ||
| && c.privacy === 'anonymize', | ||
| ) | ||
| const hasValidCapture = captures.some(c => { | ||
| if (!c.path?.startsWith('/_proxy/snap') || c.privacy !== 'anonymize' || !c.targetUrl) { | ||
| return false | ||
| } | ||
| try { | ||
| const target = new URL(c.targetUrl) | ||
| const hostname = target.hostname | ||
| return hostname === 'snapchat.com' || hostname.endsWith('.snapchat.com') | ||
| } catch { | ||
| return false | ||
| } | ||
| }) | ||
| expect(hasValidCapture).toBe(true) | ||
|
|
||
| // Verify ALL fingerprinting params are stripped |
| if (captures.length > 0) { | ||
| const hasValidCapture = captures.some(c => | ||
| c.path?.startsWith('/_proxy/hotjar') | ||
| && c.targetUrl?.includes('hotjar.com') |
Check failure
Code scanning / CodeQL
Incomplete URL substring sanitization High test
hotjar.com
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 7 days ago
In general, the substring check on the full URL should be replaced by a check on the parsed host (and, if desired, protocol), using a strict comparison or a clear rule for allowed domains. Instead of targetUrl.includes('hotjar.com'), parse targetUrl with the standard URL class and test url.host against 'hotjar.com' or an explicit whitelist of allowed Hotjar hosts (e.g. ['hotjar.com', 'www.hotjar.com'] or a suffix check like .endsWith('.hotjar.com') including the bare domain).
In this file, the single best fix with minimal behavioral change is to compute the host from c.targetUrl using the builtβin URL class and then ensure that either the host is exactly hotjar.com or ends with .hotjar.com. This mirrors the likely intent (βtraffic to Hotjarβ) while preventing arbitrary hosts that merely contain hotjar.com elsewhere in the URL from satisfying the test. The change is localized to the captures.some(...) predicate in the hotjar test around line 585. No new imports are needed because URL is a global in the Node.js runtime used by Vitest.
Concretely:
- Replace the
captures.some(c => ...)predicate so that:- It first checks that
c.targetUrlis truthy. - It then parses
c.targetUrlwithnew URL(c.targetUrl), catching errors by shortβcircuiting if parsing fails. - It derives
hostand checkshost === 'hotjar.com' || host.endsWith('.hotjar.com'). - It keeps the existing checks on
c.path?.startsWith('/_proxy/hotjar')andc.privacy === 'anonymize'.
- It first checks that
No other parts of the file need to change.
-
Copy modified lines R585-R599
| @@ -582,11 +582,21 @@ | ||
|
|
||
| // Hotjar uses WebSocket for real-time data which can't be HTTP proxied | ||
| if (captures.length > 0) { | ||
| const hasValidCapture = captures.some(c => | ||
| c.path?.startsWith('/_proxy/hotjar') | ||
| && c.targetUrl?.includes('hotjar.com') | ||
| && c.privacy === 'anonymize', | ||
| ) | ||
| const hasValidCapture = captures.some(c => { | ||
| if (!c.targetUrl) { | ||
| return false | ||
| } | ||
| let host: string | ||
| try { | ||
| host = new URL(c.targetUrl).host | ||
| } catch { | ||
| return false | ||
| } | ||
| const isHotjarHost = host === 'hotjar.com' || host.endsWith('.hotjar.com') | ||
| return c.path?.startsWith('/_proxy/hotjar') | ||
| && isHotjarHost | ||
| && c.privacy === 'anonymize' | ||
| }) | ||
| expect(hasValidCapture).toBe(true) | ||
|
|
||
| // Verify ALL fingerprinting params are stripped |
| if (captures.length > 0) { | ||
| const hasValidCapture = captures.some(c => | ||
| c.path?.startsWith('/_proxy/tiktok') | ||
| && c.targetUrl?.includes('tiktok.com') |
Check failure
Code scanning / CodeQL
Incomplete URL substring sanitization High test
tiktok.com
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 7 days ago
In general, the fix is to stop checking for '<domain>.com' as a substring of the full URL and instead parse the URL, extract its hostname, and compare that hostname against an explicit allowlist of expected domains (e.g., tiktok.com, www.tiktok.com) or otherwise validate it precisely.
For this specific code, we should change:
c.targetUrl?.includes('tiktok.com')to a hostname check that:- Parses
c.targetUrlwith the standardURLconstructor. - Extracts
hostname. - Verifies the hostname is exactly an allowed value or an allowed subdomain of the domain we care about (depending on what the test intends). The original check allowed things like
evil-tiktok.com, so tightening to the actual TikTok host(s) is safe and more accurate.
- Parses
- Similarly, we should change
c.targetUrl?.includes('reddit.com')in the Reddit test.
Since we can only modify the shown file, weβll:
- Add a small helper in
test/e2e/first-party.test.tsthat checks if a URL belongs to a given domain by parsing it and validatinghostname. For tests, we can keep it simple and robust, e.g., allow exact match and subdomains:hostname === domainorhostname.endsWith('.' + domain). - Replace the two
.includes('tiktok.com')and.includes('reddit.com')calls with calls to this helper. - Use the standard builtβin
URLclass; no new imports are needed.
This maintains existing functionality (tests still pass for real TikTok/Reddit URLs) while avoiding the incomplete substring sanitization pattern.
-
Copy modified lines R11-R25 -
Copy modified line R638 -
Copy modified line R674
| @@ -8,6 +8,21 @@ | ||
| const fixtureDir = resolve('../fixtures/first-party') | ||
| const captureDir = join(fixtureDir, '.captures') | ||
|
|
||
| function isAllowedDomain(targetUrl: string | undefined | null, domain: string): boolean { | ||
| if (!targetUrl) { | ||
| return false | ||
| } | ||
| try { | ||
| const parsed = new URL(targetUrl) | ||
| const hostname = parsed.hostname.toLowerCase() | ||
| const expected = domain.toLowerCase() | ||
| return hostname === expected || hostname.endsWith('.' + expected) | ||
| } | ||
| catch { | ||
| return false | ||
| } | ||
| } | ||
|
|
||
| // Set env var for capture plugin | ||
| process.env.NUXT_SCRIPTS_CAPTURE_DIR = captureDir | ||
|
|
||
| @@ -620,7 +635,7 @@ | ||
| if (captures.length > 0) { | ||
| const hasValidCapture = captures.some(c => | ||
| c.path?.startsWith('/_proxy/tiktok') | ||
| && c.targetUrl?.includes('tiktok.com') | ||
| && isAllowedDomain(c.targetUrl, 'tiktok.com') | ||
| && c.privacy === 'anonymize', | ||
| ) | ||
| expect(hasValidCapture).toBe(true) | ||
| @@ -656,7 +671,7 @@ | ||
| if (captures.length > 0) { | ||
| const hasValidCapture = captures.some(c => | ||
| c.path?.startsWith('/_proxy/reddit') | ||
| && c.targetUrl?.includes('reddit.com') | ||
| && isAllowedDomain(c.targetUrl, 'reddit.com') | ||
| && c.privacy === 'anonymize', | ||
| ) | ||
| expect(hasValidCapture).toBe(true) |
| if (captures.length > 0) { | ||
| const hasValidCapture = captures.some(c => | ||
| c.path?.startsWith('/_proxy/reddit') | ||
| && c.targetUrl?.includes('reddit.com') |
Check failure
Code scanning / CodeQL
Incomplete URL substring sanitization High test
reddit.com
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 7 days ago
In general, the fix is to avoid checking for reddit.com with String.prototype.includes on the entire URL string and instead parse the URL, then validate the host against a whitelist of allowed Reddit domains. This removes the possibility that a crafted URL containing reddit.com somewhere other than the host (or as part of a larger, malicious host) will satisfy the check.
The best fix here, without changing existing functionality, is:
- Parse
c.targetUrlusing the standardURLconstructor. - Extract the
hostname(nothost, to avoid port complications). - Compare that hostname to a small whitelist of allowed Reddit hosts for this test, e.g.
reddit.com,www.reddit.com, and potentially pixel endpoints likewww.reddit.comor other known tracking subdomains. To keep behavior close to the existing intent (βa Reddit URLβ), we can treat any hostname that is exactlyreddit.comor ends with.reddit.comas valid; thatβs a common and safe pattern for βthis domain or subdomainsβ.
To implement this in the shown snippet:
- Update the predicate inside
captures.some(...)to parsec.targetUrlinto a URL object inside atry/catchand then:- If parsing fails, treat it as not valid (return
falsefor that element). - If parsing succeeds, compute
const hostname = parsed.hostnameand checkhostname === 'reddit.com' || hostname.endsWith('.reddit.com').
- If parsing fails, treat it as not valid (return
- This is all done inline; no new imports are required, since
URLis a built-in global in Node and browser environments.
We only need to edit the lines around 657β661 in test/e2e/first-party.test.ts.
-
Copy modified lines R657-R668
| @@ -654,11 +654,18 @@ | ||
|
|
||
| // Reddit may not fire events in headless without valid advertiser ID | ||
| if (captures.length > 0) { | ||
| const hasValidCapture = captures.some(c => | ||
| c.path?.startsWith('/_proxy/reddit') | ||
| && c.targetUrl?.includes('reddit.com') | ||
| && c.privacy === 'anonymize', | ||
| ) | ||
| const hasValidCapture = captures.some((c) => { | ||
| if (!c.path?.startsWith('/_proxy/reddit') || c.privacy !== 'anonymize' || !c.targetUrl) { | ||
| return false | ||
| } | ||
| try { | ||
| const parsed = new URL(c.targetUrl) | ||
| const hostname = parsed.hostname | ||
| return hostname === 'reddit.com' || hostname.endsWith('.reddit.com') | ||
| } catch { | ||
| return false | ||
| } | ||
| }) | ||
| expect(hasValidCapture).toBe(true) | ||
|
|
||
| // Verify ALL fingerprinting params are stripped |
| function rewriteScriptUrls(content: string, rewrites: ProxyRewrite[]): string { | ||
| let result = content | ||
| for (const { from, to } of rewrites) { | ||
| // Rewrite various URL formats | ||
| result = result | ||
| // Full URLs | ||
| .replaceAll(`"https://${from}`, `"${to}`) | ||
| .replaceAll(`'https://${from}`, `'${to}`) | ||
| .replaceAll(`\`https://${from}`, `\`${to}`) | ||
| .replaceAll(`"http://${from}`, `"${to}`) | ||
| .replaceAll(`'http://${from}`, `'${to}`) | ||
| .replaceAll(`\`http://${from}`, `\`${to}`) | ||
| .replaceAll(`"//${from}`, `"${to}`) | ||
| .replaceAll(`'//${from}`, `'${to}`) | ||
| .replaceAll(`\`//${from}`, `\`${to}`) | ||
| } | ||
| return result |
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.
The rewriteScriptUrls function in proxy-handler.ts is an incomplete copy of the one in proxy-configs.ts, missing critical URL rewriting patterns needed for proper script proxying.
View Details
π Patch Details
diff --git a/src/runtime/server/proxy-handler.ts b/src/runtime/server/proxy-handler.ts
index c5b30c3..1474f40 100644
--- a/src/runtime/server/proxy-handler.ts
+++ b/src/runtime/server/proxy-handler.ts
@@ -1,11 +1,7 @@
import { defineEventHandler, getHeaders, getRequestIP, readBody, getQuery, setResponseHeader, createError } from 'h3'
import { useRuntimeConfig } from '#imports'
import { useNitroApp } from 'nitropack/runtime'
-
-interface ProxyRewrite {
- from: string
- to: string
-}
+import { rewriteScriptUrls, type ProxyRewrite } from '../../proxy-configs'
interface ProxyConfig {
routes: Record<string, string>
@@ -17,29 +13,6 @@ interface ProxyConfig {
debug?: boolean
}
-/**
- * Rewrite URLs in script content based on proxy config.
- * Inlined from proxy-configs.ts for runtime use.
- */
-function rewriteScriptUrls(content: string, rewrites: ProxyRewrite[]): string {
- let result = content
- for (const { from, to } of rewrites) {
- // Rewrite various URL formats
- result = result
- // Full URLs
- .replaceAll(`"https://${from}`, `"${to}`)
- .replaceAll(`'https://${from}`, `'${to}`)
- .replaceAll(`\`https://${from}`, `\`${to}`)
- .replaceAll(`"http://${from}`, `"${to}`)
- .replaceAll(`'http://${from}`, `'${to}`)
- .replaceAll(`\`http://${from}`, `\`${to}`)
- .replaceAll(`"//${from}`, `"${to}`)
- .replaceAll(`'//${from}`, `'${to}`)
- .replaceAll(`\`//${from}`, `\`${to}`)
- }
- return result
-}
-
/**
* Headers that reveal user IP address - always stripped in strict mode,
* anonymized in anonymize mode.
Analysis
Missing URL rewriting patterns in proxy-handler.ts causes collection requests to bypass the proxy
What fails: The rewriteScriptUrls function in src/runtime/server/proxy-handler.ts (lines 24-40) is an incomplete copy that's missing critical URL rewriting patterns compared to the exported version in src/proxy-configs.ts. This causes JavaScript responses fetched through the proxy to retain unrewritten URLs for:
- Bare domain patterns (e.g.,
"api.segment.io"without protocol) - Segment SDK - Google Analytics dynamic URL construction (e.g.,
"https://"+(...)+".google-analytics.com/g/collect") - Minified GA4 code
How to reproduce: Test with synthetic script content containing these patterns:
// Bare domain - NOT rewritten by old version
var apiHost = "api.segment.io/v1/batch";
// GA dynamic construction - NOT rewritten by old version
var collect = "https://"+("www")+".google-analytics.com/g/collect";Old inline version result: URLs remain unchanged, allowing collection requests to bypass proxy Fixed version result: URLs are properly rewritten to proxy paths
What happens vs expected:
- Before fix: Collection endpoint requests embedded in JavaScript responses bypass the proxy and send data directly to third parties, exposing user IPs and defeating privacy protection
- After fix: All collection requests are routed through the proxy and privacy-filtered based on configured mode
Root cause: src/runtime/server/proxy-handler.ts defines a local rewriteScriptUrls function (lines 24-40) instead of importing the complete exported version from src/proxy-configs.ts. The runtime version was missing the bare domain pattern handling (lines 267-269 in proxy-configs.ts) and Google Analytics dynamic construction regex patterns (lines 275-287 in proxy-configs.ts).
Fix implemented: Removed the incomplete inline function and imported the complete rewriteScriptUrls function from src/proxy-configs.ts.
Verification: All 180 unit tests pass, including the comprehensive third-party-proxy-replacements.test.ts which tests URL rewriting patterns for Google Analytics, Meta Pixel, TikTok, Segment, and other SDKs.
| const checks: HealthCheckResult[] = [] | ||
|
|
||
| // Build regex dynamically from collectPrefix to extract script name | ||
| const escapedPrefix = scriptsConfig.collectPrefix.replace(/\//g, '\\/') |
Check failure
Code scanning / CodeQL
Incomplete string escaping or encoding High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix
AI 23 minutes ago
In general, when building a RegExp from dynamic input, first escape all characters that have special meaning in regex syntax, including backslashes, using a dedicated βescape for regexβ helper function or a well-tested library. Then interpolate the escaped value into the RegExp constructor.
For this file, the best fix is to replace the ad-hoc replace(/\//g, '\\/') call with a proper escape helper that handles all regex metacharacters. We can define a small local function (e.g., escapeRegExp) that escapes [\-\[\]{}()*+?.,\\^$|#\s] or similar, and then call it on scriptsConfig.collectPrefix. Concretely:
- Above the existing
escapedPrefixline, add a helperfunction escapeRegExp(str: string): string { ... }. - Replace line 39 so
escapedPrefixis set viaescapeRegExp(scriptsConfig.collectPrefix)instead of only replacing/. - Keep the
new RegExp(${escapedPrefix}\/([^/]+))logic intact so functionality is unchanged except thatcollectPrefixis now fully regex-escaped.
No new external imports are strictly necessary; a small local helper is sufficient and typical for this pattern.
-
Copy modified lines R39-R43
| @@ -36,7 +36,11 @@ | ||
| const checks: HealthCheckResult[] = [] | ||
|
|
||
| // Build regex dynamically from collectPrefix to extract script name | ||
| const escapedPrefix = scriptsConfig.collectPrefix.replace(/\//g, '\\/') | ||
| function escapeRegExp(str: string): string { | ||
| // Escape characters that have special meaning in regular expressions | ||
| return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') | ||
| } | ||
| const escapedPrefix = escapeRegExp(scriptsConfig.collectPrefix) | ||
| const scriptNameRegex = new RegExp(`${escapedPrefix}\\/([^/]+)`) | ||
|
|
||
| // Test each route by making a HEAD request to the target |
π WalkthroughWalkthroughThis pull request introduces a comprehensive first-party privacy mode feature for the Nuxt Scripts module. The implementation enables server-side proxying of third-party analytics and tracking scripts, allowing requests to originate from the application's own domain rather than directly from third-party services. Changes include: a new CLI tool for cache and health management, a proxy configuration system supporting multiple services (Google Analytics, Meta Pixel, TikTok, Segment, etc.), server-side proxy handlers with privacy controls (strict and anonymize modes), service worker integration for intercepting cross-origin requests, privacy-aware request transformation to strip fingerprinting parameters, build-time configuration, runtime type definitions, extensive documentation updates, and comprehensive test coverage (unit, integration, and end-to-end tests with snapshots). Estimated code review effortπ― 4 (Complex) | β±οΈ ~75 minutes π₯ Pre-merge checks | β 4 | β 1β Failed checks (1 warning)
β Passed checks (4 passed)
βοΈ Tip: You can configure your own custom pre-merge checks in the settings. β¨ Finishing touches
π§ͺ Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Actionable comments posted: 15
Note
Due to the large number of review comments, Critical, Major severity comments were prioritized as inline comments.
Caution
Some comments are outside the diff and canβt be posted inline due to platform limitations.
β οΈ Outside diff range comments (3)
docs/content/scripts/tracking/x-pixel.md (1)
17-18: Copy-paste error: "Meta Pixel" should be "X Pixel".Line 17 incorrectly references "Meta Pixel" instead of "X Pixel".
π Proposed fix
-The simplest way to load Meta Pixel globally in your Nuxt App is to use Nuxt config. Alternatively you can directly +The simplest way to load X Pixel globally in your Nuxt App is to use Nuxt config. Alternatively you can directly use the [useScriptXPixel](`#useScriptXPixel`) composable.src/assets.ts (1)
85-85: No-oppush()call has no effect.This line calls
push()with no arguments, which does nothing. This appears to be a leftover from incomplete code or a copy-paste error.Proposed fix
nuxt.options.nitro.publicAssets ||= [] const cacheDir = join(nuxt.options.buildDir, 'cache', 'scripts') - nuxt.options.nitro.publicAssets.push() nuxt.options.nitro = defu(nuxt.options.nitro, {src/plugins/transform.ts (1)
103-167: Compute SRI after proxy rewrites to prevent integrity failures.
WhenproxyRewritesmutates content, the integrity hash (and cached metadata) reflects the preβrewrite bytes, which can cause browsers to reject the script when integrity is enabled. Compute the hash after rewrites (and consider scoping metadata per cacheKey if you support switching proxy/nonβproxy modes).π Suggested fix (order of operations)
- // Calculate integrity hash if enabled - const integrityHash = integrity && res - ? calculateIntegrity(res, integrity === true ? 'sha384' : integrity) - : undefined - - await storage.setItemRaw(`bundle:${filename}`, res) - // Apply URL rewrites for proxy mode - if (proxyRewrites?.length && res) { - const content = res.toString('utf-8') - const rewritten = rewriteScriptUrls(content, proxyRewrites) - res = Buffer.from(rewritten, 'utf-8') - logger.debug(`Rewrote ${proxyRewrites.length} URL patterns in ${filename}`) - } - - await storage.setItemRaw(cacheKey, res) + await storage.setItemRaw(`bundle:${filename}`, res) + // Apply URL rewrites for proxy mode + if (proxyRewrites?.length && res) { + const content = res.toString('utf-8') + const rewritten = rewriteScriptUrls(content, proxyRewrites) + res = Buffer.from(rewritten, 'utf-8') + logger.debug(`Rewrote ${proxyRewrites.length} URL patterns in ${filename}`) + } + + // Calculate integrity hash after any rewrites so it matches served content + const integrityHash = integrity && res + ? calculateIntegrity(res, integrity === true ? 'sha384' : integrity) + : undefined + + await storage.setItemRaw(cacheKey, res)
π€ Fix all issues with AI agents
In `@src/module.ts`:
- Around line 389-393: Replace the direct console.log calls with the module's
logger (use this.logger.info) to comply with no-console: locate the
console.log('[nuxt-scripts] First-party config:', ...) in src/module.ts (and the
similar console.log at the later occurrence around the 496-499 area) and change
them to this.logger.info with the same message and object payload so the output
goes through Nuxt's logging system.
- Line 30: The import statement includes an unused type symbol ProxyConfig which
triggers a lint error; edit the import that currently lists getAllProxyConfigs,
getSWInterceptRules, ProxyConfig to remove ProxyConfig (or change it to an
explicit type-only import only where the type is actually used). Update any
references to ProxyConfig elsewhere if you intended to use it (either add a real
usage or import it with "import type { ProxyConfig } from './proxy-configs'") so
the module no longer imports an unused symbol.
In `@src/proxy-configs.ts`:
- Around line 198-231: The SWInterceptRule interface is missing the pathPrefix
property used by getSWInterceptRules and other runtime code; update the
SWInterceptRule declaration to include pathPrefix: string so its shape matches
how getSWInterceptRules (and sw-handler logic) builds rules from
buildProxyConfig, and ensure any exports/types referencing SWInterceptRule
reflect the added property.
In `@src/runtime/server/proxy-handler.ts`:
- Around line 395-425: The loop over originalHeaders currently forwards
non-fingerprinting headers (via headers[key] = value) which can leak
credentials; update proxy-handler.ts to drop a sensitive header denylist (e.g.,
'cookie', 'authorization', and any other session headers) unconditionally before
forwarding. In the header-processing block that checks lowerKey, add a check
against SENSITIVE_HEADERS (or inline list) and continue (skip) when matched,
ensuring functions like normalizeUserAgent and normalizeLanguage still run for
allowed headers but cookie/authorization are never copied into headers.
- Around line 324-327: The current debug logger uses console.log which ESLint
forbids; replace the log initializer in proxy-handler.ts that sets log = debug ?
console.log.bind(console) : () => {} to use the approved logger (e.g.,
logger.debug) or console.warn instead. Locate the proxyConfig destructure and
the log constant (symbols: proxyConfig, log) and change the truthy branch to
bind the project logger (e.g., logger.debug.bind(logger) or
logger.warn.bind(logger)); ensure the logger is imported or available in the
module and that fallback remains a no-op when debug is false.
- Around line 451-470: Split combined statements into separate lines in the JSON
and form-parsing blocks to satisfy `@stylistic/max-statements-per-line`: in the
rawBody JSON parsing section, move the JSON.parse assignment into its own line
inside the try block and put the empty catch block on its own line; in the
URL-encoded handling, expand the params.forEach callback so the assignment
obj[key] = value is on its own line inside the callback body; also separate the
creation of stripped and the conversion to a string so the const stripped =
stripPayloadFingerprinting(obj, privacyMode) and the body = new
URLSearchParams(stripped as Record<string, string>).toString() become two
distinct statements on separate lines. Reference symbols: rawBody, parsed,
stripPayloadFingerprinting, params.forEach, obj, stripped, body.
- Around line 485-486: Remove the unnecessary and unsafe TypeScript cast on the
hook call: replace the casted invocation "(nitro.hooks.callHook as
Function)('nuxt-scripts:proxy', {...})" with a direct call to
nitro.hooks.callHook('nuxt-scripts:proxy', {...}); update the invocation site in
proxy-handler.ts (the nitro.hooks.callHook usage) to call the method without "as
Function" so it uses the proper typed signature and satisfies the
no-unsafe-function-type rule.
In `@src/runtime/server/sw-handler.ts`:
- Around line 63-71: The service worker currently uses event.request.body
directly (inside event.respondWith when fetching proxyUrl.href), which will fail
if the body stream was consumed; update the handler to clone the incoming
request (use event.request.clone()) and read/forward the body from the clone
when constructing the fetch to proxyUrl.href so the original request stream
remains intact; adjust the logic around event.respondWith / fetch(proxyUrl.href,
{ method: event.request.method, headers: event.request.headers, body: ... }) to
use the cloned request's body and headers.
In `@src/runtime/sw/proxy-sw.ts`:
- Around line 4-7: The SWInterceptRule interface is missing the pathPrefix
property used by the code that constructs and consumes rules; update the
SWInterceptRule interface to include pathPrefix: string so it matches the
objects created in sw-handler (which builds { pattern, pathPrefix, target }) and
the generated service worker logic that reads rule.pathPrefix for
matching/stripping paths.
In `@test/e2e/__snapshots__/proxy/googleTagManager.json`:
- Around line 1-194: This snapshot file contains invalid trailing commas in JSON
objects (e.g., after "z": "0", inside the "original" and "stripped" objects and
other query objects); remove all trailing commas so each object and array uses
strict JSON syntax (for example edit the entries under "original" and "stripped"
query objects and the top-level array items to eliminate commas before closing
braces/brackets).
In `@test/e2e/first-party.test.ts`:
- Around line 190-196: The test currently uses console.log('[test] Proxy
error:', response) which violates the no-console lint rule; replace that
console.log call with console.warn or console.error so the lint rule is
satisfied (locate the call near writeFileSync(join(fixtureDir,
'proxy-test.json'), JSON.stringify(response, null, 2)) and the response object
check in first-party.test.ts and update the console invocation accordingly).
In `@test/fixtures/first-party/pages/segment.vue`:
- Around line 8-10: The call to useScriptSegment currently embeds a hardcoded
Segment writeKey; update the invocation of useScriptSegment to read the key from
an environment/config variable (e.g. SEGMENT_WRITE_KEY) with a non-secret
placeholder fallback so tests/fixtures do not contain credentials β locate the
useScriptSegment(...) call and replace the literal
'KBXOGxgqMFjm2mxtJDJg0iDn5AnGYb9C' with a lookup of the env/config value and a
clear placeholder default.
In `@test/unit/proxy-privacy.test.ts`:
- Around line 290-373: Move the stripFingerprintingFromPayload function out of
the test file into a shared test util module (e.g., create
test/utils/proxy-privacy.ts), export it from there, and update any tests
importing it to import from the new module; when moving, also import and
re-export or reference its dependencies (NORMALIZE_PARAMS, STRIP_PARAMS,
normalizeLanguage, normalizeUserAgent, anonymizeIP, generalizeScreen) inside the
new util so the function compiles, and remove the function export from the
original test file.
- Around line 176-285: Several tests in the "proxy privacy - payload analysis"
suite use console.log which violates the no-console rule; replace each
console.log(...) call in the GA, Meta, X/Twitter and generic fingerprint tests
(the ones logging 'GA fingerprinting params found', 'GA params to normalize',
'GA safe params', 'Meta fingerprinting params found', 'X/Twitter fingerprinting
params found', and 'All fingerprinting vectors:') with console.warn(...) or
remove the logging lines altogether so ESLint accepts them; update the
occurrences near the gaPayload/fingerprintParams, normalizeParams, safeParams,
metaPayload/fingerprintParams, xPayload/fingerprintParams, and fp/vectors
blocks.
In `@test/unit/third-party-proxy-replacements.test.ts`:
- Around line 1-5: Replace the cross-test import of
stripFingerprintingFromPayload from './proxy-privacy.test' with the shared
helper module where you extracted it; update the import statement to point to
the new shared util (the module that now exports stripFingerprintingFromPayload)
so tests import stripFingerprintingFromPayload from the shared utility instead
of another test file.
π‘ Minor comments (6)
test/e2e/__snapshots__/proxy/metaPixel.json-1-16 (1)
1-16: Remove trailing commas from JSON object literals.The file contains invalid JSON syntax. Line 7 has a trailing comma after
"query": {}, and the same issue appears on lines 13 and 15. Standard JSON parsers reject this format and will throw parse errors. Remove the trailing commas to make the JSON valid.docs/content/docs/1.guides/2.first-party.md-523-552 (1)
523-552: Consent banner example has disconnected consent triggers.The example creates consent triggers inline in
useScriptGoogleAnalyticsanduseScriptMetaPixel(lines 532, 537), butacceptAll()callsacceptGA()andacceptMeta()which are from differentuseScriptTriggerConsent()instances (lines 526-527). This won't trigger the scripts.π Suggested fix
<script setup> const hasConsent = ref(false) -const { accept: acceptGA } = useScriptTriggerConsent() -const { accept: acceptMeta } = useScriptTriggerConsent() +const { status: gaStatus, accept: acceptGA } = useScriptTriggerConsent() +const { status: metaStatus, accept: acceptMeta } = useScriptTriggerConsent() // Configure scripts with consent triggers useScriptGoogleAnalytics({ id: 'G-XXXXXX', - scriptOptions: { trigger: useScriptTriggerConsent().status } + scriptOptions: { trigger: gaStatus } }) useScriptMetaPixel({ id: '123456', - scriptOptions: { trigger: useScriptTriggerConsent().status } + scriptOptions: { trigger: metaStatus } }) function acceptAll() { hasConsent.value = true acceptGA() acceptMeta() // Scripts now load through first-party proxy }src/runtime/server/api/scripts-health.ts-81-82 (1)
81-82: Guard against division by zero when no routes are configured.If
scriptsConfig.routesis empty,checks.lengthwill be 0, causingavgLatencyto beNaN.Proposed fix
const allOk = checks.every(c => c.status === 'ok') - const avgLatency = checks.reduce((sum, c) => sum + (c.latency || 0), 0) / checks.length + const avgLatency = checks.length > 0 + ? checks.reduce((sum, c) => sum + (c.latency || 0), 0) / checks.length + : 0src/runtime/server/api/scripts-health.ts-38-40 (1)
38-40: Incomplete regex escaping β backslashes not escaped.The escaping only handles forward slashes. For robustness, also escape backslashes and other regex metacharacters. While
collectPrefixis unlikely to contain such characters, this ensures correctness.Proposed fix
- const escapedPrefix = scriptsConfig.collectPrefix.replace(/\//g, '\\/') + const escapedPrefix = scriptsConfig.collectPrefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')src/cli.ts-122-134 (1)
122-134: Add timeout to health check fetch to prevent indefinite hangs.The fetch call has no timeout. If the server is unresponsive, the CLI will hang indefinitely. Also, line 123 has a redundant default since
args.portalready defaults to'3000'in the args definition.Proposed fix
async run({ args }) { - const port = args.port || '3000' + const port = args.port const url = `http://localhost:${port}/_scripts/health.json` consola.info(`Checking health at ${url}...`) - const res = await fetch(url) + const res = await fetch(url, { signal: AbortSignal.timeout(10000) }) .then(r => r.json()) .catch((err) => { - consola.error(`Failed to connect: ${err.message}`) + const message = err.name === 'TimeoutError' ? 'Request timed out' : err.message + consola.error(`Failed to connect: ${message}`) consola.info('Make sure the dev server is running with `nuxi dev`') return null })src/module.ts-55-60 (1)
55-60: Doc default for collectPrefix doesnβt match implementation.JSDoc says
/ _scripts/c, but the code defaults to/ _proxy. Align the docs (or default) to avoid confusion.π οΈ Proposed fix
- * `@default` '/_scripts/c' + * `@default` '/_proxy'
π§Ή Nitpick comments (5)
test/fixtures/first-party/server/plugins/capture-proxy.ts (1)
7-13: Consider async file operations and improved typing.For a test fixture this is acceptable, but note:
- Synchronous
writeFileSync/mkdirSynccan block the event loop during concurrent proxy eventsdata: anyloses type information β consider a typed interface- No guard if
data.timestampis undefined (would producecapture-undefined.json)β»οΈ Optional improvement with async operations
-import { writeFileSync, mkdirSync } from 'node:fs' +import { writeFile, mkdir } from 'node:fs/promises' import { join } from 'node:path' // Use NUXT_SCRIPTS_CAPTURE_DIR env var or default to rootDir/.captures const captureDir = process.env.NUXT_SCRIPTS_CAPTURE_DIR || join(process.cwd(), '.captures') +interface ProxyEventData { + timestamp: number + [key: string]: unknown +} + export default defineNitroPlugin((nitro) => { - nitro.hooks.hook('nuxt-scripts:proxy', (data: any) => { + nitro.hooks.hook('nuxt-scripts:proxy', async (data: ProxyEventData) => { // Ensure dir exists before each write (handles race conditions) - mkdirSync(captureDir, { recursive: true }) - const filename = join(captureDir, `capture-${data.timestamp}.json`) - writeFileSync(filename, JSON.stringify(data, null, 2)) + await mkdir(captureDir, { recursive: true }) + const filename = join(captureDir, `capture-${data.timestamp ?? Date.now()}.json`) + await writeFile(filename, JSON.stringify(data, null, 2)) }) })src/runtime/composables/useScriptTriggerServiceWorker.ts (1)
47-51: Race condition: scope disposal may resolve an already-settled promise.If the promise already resolved via
done()(from controllerchange or timeout), thetryOnScopeDisposecallback will still callresolve(false). While this won't throw in JavaScript (calling resolve multiple times is a no-op on an already-settled promise), the cleanup should use the same guard pattern for consistency and clarity.β»οΈ Suggested fix
tryOnScopeDispose(() => { navigator.serviceWorker.removeEventListener('controllerchange', onControllerChange) clearTimeout(timer) - resolve(false) + if (!resolved) { + resolved = true + resolve(false) + } })src/runtime/server/api/scripts-health.ts (1)
19-19: Prefix unused parameter with underscore.The
eventparameter is defined but never used. Prefix it with an underscore to satisfy the linter.Proposed fix
-export default defineEventHandler(async (event) => { +export default defineEventHandler(async (_event) => {test/unit/first-party.test.ts (1)
2-2: Remove unused import.
getProxyConfigis imported but never used in this test file.Proposed fix
-import { getAllProxyConfigs, getProxyConfig } from '../../src/proxy-configs' +import { getAllProxyConfigs } from '../../src/proxy-configs'src/cli.ts (1)
92-100: Consider handling prompt cancellation (Ctrl+C).If the user cancels the prompt with Ctrl+C,
consola.promptmay throw an error or returnundefined. The current checkif (!confirmed)handlesfalseand falsy values, but you may want to wrap this in a try-catch for cleaner handling.
| } from './runtime/types' | ||
| import { NuxtScriptsCheckScripts } from './plugins/check-scripts' | ||
| import { registerTypeTemplates, templatePlugin, templateTriggerResolver } from './templates' | ||
| import { getAllProxyConfigs, getSWInterceptRules, type ProxyConfig } from './proxy-configs' |
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.
Remove unused ProxyConfig import (lint).
π οΈ Proposed fix
-import { getAllProxyConfigs, getSWInterceptRules, type ProxyConfig } from './proxy-configs'
+import { getAllProxyConfigs, getSWInterceptRules } from './proxy-configs'π Committable suggestion
βΌοΈ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| import { getAllProxyConfigs, getSWInterceptRules, type ProxyConfig } from './proxy-configs' | |
| import { getAllProxyConfigs, getSWInterceptRules } from './proxy-configs' |
π§° Tools
πͺ ESLint
[error] 30-30: 'ProxyConfig' is defined but never used. Allowed unused vars must match /^_/u.
(@typescript-eslint/no-unused-vars)
π€ Prompt for AI Agents
In `@src/module.ts` at line 30, The import statement includes an unused type
symbol ProxyConfig which triggers a lint error; edit the import that currently
lists getAllProxyConfigs, getSWInterceptRules, ProxyConfig to remove ProxyConfig
(or change it to an explicit type-only import only where the type is actually
used). Update any references to ProxyConfig elsewhere if you intended to use it
(either add a real usage or import it with "import type { ProxyConfig } from
'./proxy-configs'") so the module no longer imports an unused symbol.
| // Pre-resolve paths needed for hooks | ||
| const swHandlerPath = await resolvePath('./runtime/server/sw-handler') | ||
|
|
||
| console.log('[nuxt-scripts] First-party config:', { firstPartyEnabled, firstPartyPrivacy, firstPartyCollectPrefix }) | ||
|
|
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.
Replace console.log with module logger to satisfy no-console.
π οΈ Proposed fix
- console.log('[nuxt-scripts] First-party config:', { firstPartyEnabled, firstPartyPrivacy, firstPartyCollectPrefix })
+ logger.debug('[nuxt-scripts] First-party config:', { firstPartyEnabled, firstPartyPrivacy, firstPartyCollectPrefix })
...
- console.log('[nuxt-scripts] Registering proxy handler:', `${firstPartyCollectPrefix}/**`, '->', proxyHandlerPath)
+ logger.debug('[nuxt-scripts] Registering proxy handler:', `${firstPartyCollectPrefix}/**`, '->', proxyHandlerPath)Also applies to: 496-499
π§° Tools
πͺ ESLint
[error] 392-392: Unexpected console statement. Only these console methods are allowed: warn, error.
(no-console)
π€ Prompt for AI Agents
In `@src/module.ts` around lines 389 - 393, Replace the direct console.log calls
with the module's logger (use this.logger.info) to comply with no-console:
locate the console.log('[nuxt-scripts] First-party config:', ...) in
src/module.ts (and the similar console.log at the later occurrence around the
496-499 area) and change them to this.logger.info with the same message and
object payload so the output goes through Nuxt's logging system.
| export interface SWInterceptRule { | ||
| /** Domain pattern to match (supports wildcards like *.google-analytics.com) */ | ||
| pattern: string | ||
| /** Path prefix to match and strip from the original URL (e.g., /tr for www.facebook.com/tr) */ | ||
| pathPrefix: string | ||
| /** Local path prefix to rewrite to */ | ||
| target: string | ||
| } | ||
|
|
||
| /** | ||
| * Get service worker intercept rules from all proxy configs. | ||
| * These rules are used by the SW to intercept and rewrite outbound requests. | ||
| */ | ||
| export function getSWInterceptRules(collectPrefix: string): SWInterceptRule[] { | ||
| const configs = buildProxyConfig(collectPrefix) | ||
| const rules: SWInterceptRule[] = [] | ||
|
|
||
| // Extract unique domain -> target mappings from route rules | ||
| for (const config of Object.values(configs)) { | ||
| if (!config.routes) | ||
| continue | ||
| for (const [localPath, { proxy }] of Object.entries(config.routes)) { | ||
| // Extract domain and path prefix from proxy URL | ||
| // e.g., "https://www.facebook.com/tr/**" -> domain="www.facebook.com", pathPrefix="/tr" | ||
| // e.g., "https://connect.facebook.net/**" -> domain="connect.facebook.net", pathPrefix="" | ||
| const match = proxy.match(/^https?:\/\/([^/]+)(\/.*)?\/\*\*$/) | ||
| if (match?.[1]) { | ||
| const domain = match[1] | ||
| // Path prefix is everything between domain and /** (e.g., /tr), empty if none | ||
| const pathPrefix = match[2] || '' | ||
| // Extract target prefix: "/_proxy/meta-tr/**" -> "/_proxy/meta-tr" | ||
| const target = localPath.replace(/\/\*\*$/, '') | ||
| rules.push({ pattern: domain, pathPrefix, target }) | ||
| } |
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.
π§© Analysis chain
π Script executed:
#!/bin/bash
# Search for SWInterceptRule definition and pathPrefix usage
echo "=== Searching for SWInterceptRule definition ==="
rg -n "interface SWInterceptRule|type SWInterceptRule" src/
echo -e "\n=== Searching for pathPrefix usage in runtime ==="
rg -n "pathPrefix" src/runtime/
echo -e "\n=== Checking getSWInterceptRules usage ==="
rg -n "getSWInterceptRules" src/Repository: nuxt/scripts
Length of output: 1163
π Script executed:
#!/bin/bash
# Check SWInterceptRule definition in src/runtime/sw/proxy-sw.ts
echo "=== src/runtime/sw/proxy-sw.ts (around line 4) ==="
sed -n '1,20p' src/runtime/sw/proxy-sw.ts
echo -e "\n=== src/runtime/server/sw-handler.ts (full file to see pathPrefix usage) ==="
cat -n src/runtime/server/sw-handler.tsRepository: nuxt/scripts
Length of output: 4088
SWInterceptRule interface in src/runtime/sw/proxy-sw.ts is missing the pathPrefix field.
The interface definition (line 4β6) only includes pattern and target, but src/runtime/server/sw-handler.ts generates rules with pathPrefix (line 18) and the generated code actively uses it to match and strip path prefixes from URLs (lines 54, 58β59). Update the interface to include pathPrefix: string to align with the actual implementation.
π€ Prompt for AI Agents
In `@src/proxy-configs.ts` around lines 198 - 231, The SWInterceptRule interface
is missing the pathPrefix property used by getSWInterceptRules and other runtime
code; update the SWInterceptRule declaration to include pathPrefix: string so
its shape matches how getSWInterceptRules (and sw-handler logic) builds rules
from buildProxyConfig, and ensure any exports/types referencing SWInterceptRule
reflect the added property.
| const { routes, privacy, cacheTtl = 3600, debug = import.meta.dev } = proxyConfig | ||
| const path = event.path | ||
| const log = debug ? console.log.bind(console) : () => {} | ||
|
|
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.
Swap console.log debug for allowed logger output.
ESLint disallows console.log; use logger.debug or console.warn.
π οΈ Proposed fix
- const log = debug ? console.log.bind(console) : () => {}
+ const log = debug ? console.warn.bind(console) : () => {}π Committable suggestion
βΌοΈ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const { routes, privacy, cacheTtl = 3600, debug = import.meta.dev } = proxyConfig | |
| const path = event.path | |
| const log = debug ? console.log.bind(console) : () => {} | |
| const { routes, privacy, cacheTtl = 3600, debug = import.meta.dev } = proxyConfig | |
| const path = event.path | |
| const log = debug ? console.warn.bind(console) : () => {} |
π§° Tools
πͺ ESLint
[error] 326-326: Unexpected console statement. Only these console methods are allowed: warn, error.
(no-console)
π€ Prompt for AI Agents
In `@src/runtime/server/proxy-handler.ts` around lines 324 - 327, The current
debug logger uses console.log which ESLint forbids; replace the log initializer
in proxy-handler.ts that sets log = debug ? console.log.bind(console) : () => {}
to use the approved logger (e.g., logger.debug) or console.warn instead. Locate
the proxyConfig destructure and the log constant (symbols: proxyConfig, log) and
change the truthy branch to bind the project logger (e.g.,
logger.debug.bind(logger) or logger.warn.bind(logger)); ensure the logger is
imported or available in the module and that fallback remains a no-op when debug
is false.
| // Anonymize mode: preserve useful analytics, prevent fingerprinting | ||
| for (const [key, value] of Object.entries(originalHeaders)) { | ||
| if (!value) | ||
| continue | ||
|
|
||
| const lowerKey = key.toLowerCase() | ||
|
|
||
| // Skip IP-revealing headers entirely | ||
| if (IP_HEADERS.includes(lowerKey)) | ||
| continue | ||
|
|
||
| // Skip content-length - we modify the body so fetch needs to recalculate | ||
| if (lowerKey === 'content-length') | ||
| continue | ||
|
|
||
| // Normalize fingerprinting headers | ||
| if (lowerKey === 'user-agent') { | ||
| headers[key] = normalizeUserAgent(value) | ||
| } | ||
| else if (lowerKey === 'accept-language') { | ||
| headers[key] = normalizeLanguage(value) | ||
| } | ||
| else if (FINGERPRINT_HEADERS.includes(lowerKey)) { | ||
| // Skip other fingerprinting headers (sec-ch-ua-*) | ||
| continue | ||
| } | ||
| else { | ||
| // Forward other headers (content-type, accept, referer, etc.) | ||
| headers[key] = value | ||
| } | ||
| } |
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.
Donβt forward origin cookies/authorization to third parties.
In anonymize mode, nonβfingerprinting headers (including Cookie/Authorization) are forwarded upstream, which can leak session credentials by default. Add a sensitiveβheader denylist and drop these headers in all modes.
π οΈ Proposed fix
const FINGERPRINT_HEADERS = [
'user-agent',
'accept-language',
'accept-encoding',
'sec-ch-ua',
'sec-ch-ua-platform',
'sec-ch-ua-mobile',
'sec-ch-ua-full-version-list',
]
+
+const SENSITIVE_HEADERS = [
+ 'cookie',
+ 'authorization',
+ 'proxy-authorization',
+ 'x-csrf-token',
+]
...
for (const [key, value] of Object.entries(originalHeaders)) {
if (!value)
continue
const lowerKey = key.toLowerCase()
+ // Never forward origin credentials to third parties
+ if (SENSITIVE_HEADERS.includes(lowerKey))
+ continueπ€ Prompt for AI Agents
In `@src/runtime/server/proxy-handler.ts` around lines 395 - 425, The loop over
originalHeaders currently forwards non-fingerprinting headers (via headers[key]
= value) which can leak credentials; update proxy-handler.ts to drop a sensitive
header denylist (e.g., 'cookie', 'authorization', and any other session headers)
unconditionally before forwarding. In the header-processing block that checks
lowerKey, add a check against SENSITIVE_HEADERS (or inline list) and continue
(skip) when matched, ensuring functions like normalizeUserAgent and
normalizeLanguage still run for allowed headers but cookie/authorization are
never copied into headers.
| // Write debug info | ||
| writeFileSync(join(fixtureDir, 'proxy-test.json'), JSON.stringify(response, null, 2)) | ||
|
|
||
| // Should return JS content (or at least not 404) | ||
| if (typeof response === 'object' && response.error) { | ||
| console.log('[test] Proxy error:', response) | ||
| } |
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.
Replace console.log to satisfy no-console lint.
ESLint only allows warn/error; this console.log will fail linting.
π οΈ Proposed fix
- if (typeof response === 'object' && response.error) {
- console.log('[test] Proxy error:', response)
- }
+ if (typeof response === 'object' && response.error) {
+ console.warn('[test] Proxy error:', response)
+ }π Committable suggestion
βΌοΈ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // Write debug info | |
| writeFileSync(join(fixtureDir, 'proxy-test.json'), JSON.stringify(response, null, 2)) | |
| // Should return JS content (or at least not 404) | |
| if (typeof response === 'object' && response.error) { | |
| console.log('[test] Proxy error:', response) | |
| } | |
| // Write debug info | |
| writeFileSync(join(fixtureDir, 'proxy-test.json'), JSON.stringify(response, null, 2)) | |
| // Should return JS content (or at least not 404) | |
| if (typeof response === 'object' && response.error) { | |
| console.warn('[test] Proxy error:', response) | |
| } |
π§° Tools
πͺ ESLint
[error] 195-195: Unexpected console statement. Only these console methods are allowed: warn, error.
(no-console)
π€ Prompt for AI Agents
In `@test/e2e/first-party.test.ts` around lines 190 - 196, The test currently uses
console.log('[test] Proxy error:', response) which violates the no-console lint
rule; replace that console.log call with console.warn or console.error so the
lint rule is satisfied (locate the call near writeFileSync(join(fixtureDir,
'proxy-test.json'), JSON.stringify(response, null, 2)) and the response object
check in first-party.test.ts and update the console invocation accordingly).
| const { proxy, status } = useScriptSegment({ | ||
| writeKey: 'KBXOGxgqMFjm2mxtJDJg0iDn5AnGYb9C', | ||
| }) |
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.
π§© Analysis chain
π Script executed:
# First, let's examine the file in question
cat -n test/fixtures/first-party/pages/segment.vueRepository: nuxt/scripts
Length of output: 1142
π Script executed:
# Search for this specific writeKey across the entire codebase
rg 'KBXOGxgqMFjm2mxtJDJg0iDn5AnGYb9C' --type-listRepository: nuxt/scripts
Length of output: 445
π Script executed:
# Look for Segment write keys or similar patterns in the codebase
rg 'writeKey' -t js -t vue -t ts -A 2 -B 2Repository: nuxt/scripts
Length of output: 83
π Script executed:
# Check if there's any documentation about test fixtures or keys
find . -name "README*" -o -name "TESTING*" -o -name ".env*" | head -20Repository: nuxt/scripts
Length of output: 1883
π Script executed:
# Look for gitleaks or other security scanning configs
find . -name ".gitleaks*" -o -name "*.gitleaks*" -o -name "secrets*" 2>/dev/nullRepository: nuxt/scripts
Length of output: 38
π Script executed:
# Search for the exact writeKey across entire codebase
rg 'KBXOGxgqMFjm2mxtJDJg0iDn5AnGYb9C'Repository: nuxt/scripts
Length of output: 901
π Script executed:
# Search for writeKey patterns
rg 'writeKey' -A 1 -B 1Repository: nuxt/scripts
Length of output: 2628
π Script executed:
# Check .env.example for fixture documentation
cat -n docs/.env.exampleRepository: nuxt/scripts
Length of output: 276
π Script executed:
# Look for test configuration and fixture setup docs
find . -path ./node_modules -prune -o -type f \( -name "*.md" -o -name "*.txt" \) -print | xargs grep -l "Segment\|fixture" 2>/dev/null | head -10Repository: nuxt/scripts
Length of output: 191
π Script executed:
# Check if this key pattern matches Segment's documentation or public test keys
rg 'test.*key\|fixture.*key\|public.*key' -i --type-list | head -5Repository: nuxt/scripts
Length of output: 445
π Script executed:
# Check how environment variables are used in tests and fixtures
rg 'process.env\|NUXT_' test/fixtures -A 1 -B 1 | head -30Repository: nuxt/scripts
Length of output: 38
π Script executed:
# Look at the git history to see if this key was always used or if there's context
git log --oneline -20 -- test/fixtures/first-party/pages/segment.vue 2>/dev/null | head -5Repository: nuxt/scripts
Length of output: 134
π Script executed:
# Check Segment documentation to see if this is a known test/public key
web_search
Segment 'KBXOGxgqMFjm2mxtJDJg0iDn5AnGYb9C' test writeKeyRepository: nuxt/scripts
Length of output: 152
π Script executed:
# Look at whether this is documented as a test fixture
grep -r 'KBXOGxgqMFjm2mxtJDJg0iDn5AnGYb9C' docs/ | head -5Repository: nuxt/scripts
Length of output: 38
π Web query:
Segment writeKey test fixture dummy public key
π‘ Result:
There isnβt a βpublic dummy Segment writeKeyβ you can safely use as a universal test keyβSegment writeKeys are per-source credentials, so for anything that actually sends events you should create a separate dev/test Source in your own workspace and use its writeKey. (twilio.com)
If you just need a test fixture string (non-working, for unit tests/mocks), Segmentβs own docs/snippets commonly use placeholders like "writekey" / 'SEGMENT_API_KEY'. (segment-docs.netlify.app)
If you want an example-looking key for fixtures, Segmentβs HTTP API doc includes a sample writeKey value (as an example payload): DmBXIN4JnwqBnTqXccTF0wBnLXNQmFtk (treat as documentation sample only; donβt expect it to authenticate). (segment-docs.netlify.app)
Citations:
- 1: https://www.twilio.com/docs/segment/connections/auto-instrumentation/swift-setup?utm_source=openai
- 2: https://segment-docs.netlify.app/docs/connections/sources/catalog/libraries/website/javascript/?utm_source=openai
- 3: https://segment-docs.netlify.app/docs/connections/sources/catalog/libraries/server/http-api/?utm_source=openai
π Script executed:
# Check the Segment documentation in the repo itself
cat -n docs/content/scripts/tracking/segment.md | head -100Repository: nuxt/scripts
Length of output: 2980
π Script executed:
# Look at other fixture files to understand the pattern
ls -la test/fixtures/first-party/Repository: nuxt/scripts
Length of output: 745
π Script executed:
# Check if there's any documentation about test fixtures or credentials
find . -path ./node_modules -prune -o -type f -name "*.md" -print | xargs grep -l "test key\|fixture\|credential" 2>/dev/nullRepository: nuxt/scripts
Length of output: 194
Replace hardcoded Segment writeKey with environment variable or placeholder.
Line 9 contains a hardcoded writeKey. Segment writeKeys are authentication credentials tied to specific sources. Even if this is a test key, hardcoding credentials in fixtures exposes them in version control. Use an environment variable with a placeholder fallback instead.
Example fix
-const { proxy, status } = useScriptSegment({
- writeKey: 'KBXOGxgqMFjm2mxtJDJg0iDn5AnGYb9C',
-})
+const { proxy, status } = useScriptSegment({
+ writeKey: process.env.NUXT_PUBLIC_SCRIPTS_SEGMENT_WRITE_KEY ?? 'YOUR_WRITE_KEY',
+})π Committable suggestion
βΌοΈ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const { proxy, status } = useScriptSegment({ | |
| writeKey: 'KBXOGxgqMFjm2mxtJDJg0iDn5AnGYb9C', | |
| }) | |
| const { proxy, status } = useScriptSegment({ | |
| writeKey: process.env.NUXT_PUBLIC_SCRIPTS_SEGMENT_WRITE_KEY ?? 'YOUR_WRITE_KEY', | |
| }) |
π§° Tools
πͺ Gitleaks (8.30.0)
[high] 9-9: Detected a Generic API Key, potentially exposing access to various services and sensitive operations.
(generic-api-key)
π€ Prompt for AI Agents
In `@test/fixtures/first-party/pages/segment.vue` around lines 8 - 10, The call to
useScriptSegment currently embeds a hardcoded Segment writeKey; update the
invocation of useScriptSegment to read the key from an environment/config
variable (e.g. SEGMENT_WRITE_KEY) with a non-secret placeholder fallback so
tests/fixtures do not contain credentials β locate the useScriptSegment(...)
call and replace the literal 'KBXOGxgqMFjm2mxtJDJg0iDn5AnGYb9C' with a lookup of
the env/config value and a clear placeholder default.
| describe('proxy privacy - payload analysis', () => { | ||
| describe('GA payload', () => { | ||
| it('identifies fingerprinting params in GA payload', () => { | ||
| const gaPayload = FINGERPRINT_PAYLOAD.ga | ||
| const fingerprintParams = Object.keys(gaPayload).filter((key) => { | ||
| return STRIP_PARAMS.ip.includes(key) | ||
| || STRIP_PARAMS.userId.includes(key) | ||
| || STRIP_PARAMS.screen.includes(key) | ||
| || STRIP_PARAMS.browser.includes(key) | ||
| }) | ||
|
|
||
| console.log('GA fingerprinting params found:', fingerprintParams) | ||
| expect(fingerprintParams).toContain('uip') // IP | ||
| expect(fingerprintParams).toContain('cid') // Client ID | ||
| expect(fingerprintParams).toContain('uid') // User ID | ||
| expect(fingerprintParams).toContain('sr') // Screen resolution | ||
| expect(fingerprintParams).toContain('vp') // Viewport | ||
| expect(fingerprintParams).toContain('ua') // User agent | ||
| }) | ||
|
|
||
| it('identifies params to normalize in GA payload', () => { | ||
| const gaPayload = FINGERPRINT_PAYLOAD.ga | ||
| const normalizeParams = Object.keys(gaPayload).filter((key) => { | ||
| return NORMALIZE_PARAMS.language.includes(key) | ||
| }) | ||
|
|
||
| console.log('GA params to normalize:', normalizeParams) | ||
| expect(normalizeParams).toContain('ul') // Language | ||
| }) | ||
|
|
||
| it('identifies safe params in GA payload', () => { | ||
| const gaPayload = FINGERPRINT_PAYLOAD.ga | ||
| const safeParams = Object.keys(gaPayload).filter((key) => { | ||
| return ALLOWED_PARAMS.page.includes(key) | ||
| || ALLOWED_PARAMS.event.includes(key) | ||
| || ALLOWED_PARAMS.time.includes(key) | ||
| }) | ||
|
|
||
| console.log('GA safe params:', safeParams) | ||
| expect(safeParams).toContain('dt') // Title | ||
| expect(safeParams).toContain('dl') // Location | ||
| expect(safeParams).toContain('dr') // Referrer | ||
| expect(safeParams).toContain('z') // Cache buster | ||
| }) | ||
| }) | ||
|
|
||
| describe('Meta pixel payload', () => { | ||
| it('identifies fingerprinting params in Meta payload', () => { | ||
| const metaPayload = FINGERPRINT_PAYLOAD.meta | ||
| const fingerprintParams: string[] = [] | ||
|
|
||
| for (const key of Object.keys(metaPayload)) { | ||
| if (STRIP_PARAMS.ip.some(p => key.toLowerCase().includes(p.toLowerCase()))) fingerprintParams.push(key) | ||
| if (STRIP_PARAMS.userId.some(p => key.toLowerCase() === p.toLowerCase())) fingerprintParams.push(key) | ||
| if (STRIP_PARAMS.userData.some(p => key.toLowerCase() === p.toLowerCase())) fingerprintParams.push(key) | ||
| if (STRIP_PARAMS.browser.some(p => key.toLowerCase().includes(p.toLowerCase()))) fingerprintParams.push(key) | ||
| } | ||
|
|
||
| console.log('Meta fingerprinting params found:', fingerprintParams) | ||
| expect(fingerprintParams).toContain('client_ip_address') | ||
| expect(fingerprintParams).toContain('external_id') | ||
| expect(fingerprintParams).toContain('ud') // User data | ||
| expect(fingerprintParams).toContain('fbp') // Browser ID | ||
| expect(fingerprintParams).toContain('fbc') // Click ID | ||
| }) | ||
| }) | ||
|
|
||
| describe('X/Twitter pixel payload', () => { | ||
| it('identifies fingerprinting params in X pixel payload', () => { | ||
| const xPayload = FINGERPRINT_PAYLOAD.xPixel | ||
| const fingerprintParams: string[] = [] | ||
|
|
||
| for (const key of Object.keys(xPayload)) { | ||
| const lowerKey = key.toLowerCase() | ||
| if (STRIP_PARAMS.deviceInfo.some(p => lowerKey === p.toLowerCase())) fingerprintParams.push(key) | ||
| if (STRIP_PARAMS.userId.some(p => lowerKey === p.toLowerCase())) fingerprintParams.push(key) | ||
| } | ||
|
|
||
| console.log('X/Twitter fingerprinting params found:', fingerprintParams) | ||
| expect(fingerprintParams).toContain('dv') // Device info - contains timezone, screen, platform etc. | ||
| expect(fingerprintParams).toContain('bci') // Browser context indicator | ||
| expect(fingerprintParams).toContain('eci') // Environment context indicator | ||
| expect(fingerprintParams).toContain('pl_id') // Pixel/placement ID | ||
| expect(fingerprintParams).toContain('p_user_id') // User ID | ||
| }) | ||
| }) | ||
|
|
||
| describe('generic fingerprint payload', () => { | ||
| it('identifies all fingerprinting vectors', () => { | ||
| const fp = FINGERPRINT_PAYLOAD.fingerprint | ||
| const vectors: string[] = [] | ||
|
|
||
| // Check each category | ||
| if (fp.screen) vectors.push('screen') | ||
| if (fp.viewport) vectors.push('viewport') | ||
| if (fp.hardwareConcurrency) vectors.push('hardwareConcurrency') | ||
| if (fp.deviceMemory) vectors.push('deviceMemory') | ||
| if (fp.platform) vectors.push('platform') | ||
| if (fp.userAgent) vectors.push('userAgent') | ||
| if (fp.languages) vectors.push('languages') | ||
| if (fp.timezone) vectors.push('timezone') | ||
| if (fp.plugins) vectors.push('plugins') | ||
| if (fp.canvas) vectors.push('canvas') | ||
| if (fp.webgl) vectors.push('webgl') | ||
| if (fp.audioFingerprint) vectors.push('audioFingerprint') | ||
| if (fp.fonts) vectors.push('fonts') | ||
| if (fp.connection) vectors.push('connection') | ||
|
|
||
| console.log('All fingerprinting vectors:', vectors) | ||
| expect(vectors.length).toBeGreaterThan(10) |
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.
Replace console.log in tests (no-console).
ESLint only allows warn/error; swap these logs or remove them to keep lint green.
π οΈ Example fix (apply similarly to the remaining logs)
- console.log('GA fingerprinting params found:', fingerprintParams)
+ console.warn('GA fingerprinting params found:', fingerprintParams)π Committable suggestion
βΌοΈ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| describe('proxy privacy - payload analysis', () => { | |
| describe('GA payload', () => { | |
| it('identifies fingerprinting params in GA payload', () => { | |
| const gaPayload = FINGERPRINT_PAYLOAD.ga | |
| const fingerprintParams = Object.keys(gaPayload).filter((key) => { | |
| return STRIP_PARAMS.ip.includes(key) | |
| || STRIP_PARAMS.userId.includes(key) | |
| || STRIP_PARAMS.screen.includes(key) | |
| || STRIP_PARAMS.browser.includes(key) | |
| }) | |
| console.log('GA fingerprinting params found:', fingerprintParams) | |
| expect(fingerprintParams).toContain('uip') // IP | |
| expect(fingerprintParams).toContain('cid') // Client ID | |
| expect(fingerprintParams).toContain('uid') // User ID | |
| expect(fingerprintParams).toContain('sr') // Screen resolution | |
| expect(fingerprintParams).toContain('vp') // Viewport | |
| expect(fingerprintParams).toContain('ua') // User agent | |
| }) | |
| it('identifies params to normalize in GA payload', () => { | |
| const gaPayload = FINGERPRINT_PAYLOAD.ga | |
| const normalizeParams = Object.keys(gaPayload).filter((key) => { | |
| return NORMALIZE_PARAMS.language.includes(key) | |
| }) | |
| console.log('GA params to normalize:', normalizeParams) | |
| expect(normalizeParams).toContain('ul') // Language | |
| }) | |
| it('identifies safe params in GA payload', () => { | |
| const gaPayload = FINGERPRINT_PAYLOAD.ga | |
| const safeParams = Object.keys(gaPayload).filter((key) => { | |
| return ALLOWED_PARAMS.page.includes(key) | |
| || ALLOWED_PARAMS.event.includes(key) | |
| || ALLOWED_PARAMS.time.includes(key) | |
| }) | |
| console.log('GA safe params:', safeParams) | |
| expect(safeParams).toContain('dt') // Title | |
| expect(safeParams).toContain('dl') // Location | |
| expect(safeParams).toContain('dr') // Referrer | |
| expect(safeParams).toContain('z') // Cache buster | |
| }) | |
| }) | |
| describe('Meta pixel payload', () => { | |
| it('identifies fingerprinting params in Meta payload', () => { | |
| const metaPayload = FINGERPRINT_PAYLOAD.meta | |
| const fingerprintParams: string[] = [] | |
| for (const key of Object.keys(metaPayload)) { | |
| if (STRIP_PARAMS.ip.some(p => key.toLowerCase().includes(p.toLowerCase()))) fingerprintParams.push(key) | |
| if (STRIP_PARAMS.userId.some(p => key.toLowerCase() === p.toLowerCase())) fingerprintParams.push(key) | |
| if (STRIP_PARAMS.userData.some(p => key.toLowerCase() === p.toLowerCase())) fingerprintParams.push(key) | |
| if (STRIP_PARAMS.browser.some(p => key.toLowerCase().includes(p.toLowerCase()))) fingerprintParams.push(key) | |
| } | |
| console.log('Meta fingerprinting params found:', fingerprintParams) | |
| expect(fingerprintParams).toContain('client_ip_address') | |
| expect(fingerprintParams).toContain('external_id') | |
| expect(fingerprintParams).toContain('ud') // User data | |
| expect(fingerprintParams).toContain('fbp') // Browser ID | |
| expect(fingerprintParams).toContain('fbc') // Click ID | |
| }) | |
| }) | |
| describe('X/Twitter pixel payload', () => { | |
| it('identifies fingerprinting params in X pixel payload', () => { | |
| const xPayload = FINGERPRINT_PAYLOAD.xPixel | |
| const fingerprintParams: string[] = [] | |
| for (const key of Object.keys(xPayload)) { | |
| const lowerKey = key.toLowerCase() | |
| if (STRIP_PARAMS.deviceInfo.some(p => lowerKey === p.toLowerCase())) fingerprintParams.push(key) | |
| if (STRIP_PARAMS.userId.some(p => lowerKey === p.toLowerCase())) fingerprintParams.push(key) | |
| } | |
| console.log('X/Twitter fingerprinting params found:', fingerprintParams) | |
| expect(fingerprintParams).toContain('dv') // Device info - contains timezone, screen, platform etc. | |
| expect(fingerprintParams).toContain('bci') // Browser context indicator | |
| expect(fingerprintParams).toContain('eci') // Environment context indicator | |
| expect(fingerprintParams).toContain('pl_id') // Pixel/placement ID | |
| expect(fingerprintParams).toContain('p_user_id') // User ID | |
| }) | |
| }) | |
| describe('generic fingerprint payload', () => { | |
| it('identifies all fingerprinting vectors', () => { | |
| const fp = FINGERPRINT_PAYLOAD.fingerprint | |
| const vectors: string[] = [] | |
| // Check each category | |
| if (fp.screen) vectors.push('screen') | |
| if (fp.viewport) vectors.push('viewport') | |
| if (fp.hardwareConcurrency) vectors.push('hardwareConcurrency') | |
| if (fp.deviceMemory) vectors.push('deviceMemory') | |
| if (fp.platform) vectors.push('platform') | |
| if (fp.userAgent) vectors.push('userAgent') | |
| if (fp.languages) vectors.push('languages') | |
| if (fp.timezone) vectors.push('timezone') | |
| if (fp.plugins) vectors.push('plugins') | |
| if (fp.canvas) vectors.push('canvas') | |
| if (fp.webgl) vectors.push('webgl') | |
| if (fp.audioFingerprint) vectors.push('audioFingerprint') | |
| if (fp.fonts) vectors.push('fonts') | |
| if (fp.connection) vectors.push('connection') | |
| console.log('All fingerprinting vectors:', vectors) | |
| expect(vectors.length).toBeGreaterThan(10) | |
| describe('proxy privacy - payload analysis', () => { | |
| describe('GA payload', () => { | |
| it('identifies fingerprinting params in GA payload', () => { | |
| const gaPayload = FINGERPRINT_PAYLOAD.ga | |
| const fingerprintParams = Object.keys(gaPayload).filter((key) => { | |
| return STRIP_PARAMS.ip.includes(key) | |
| || STRIP_PARAMS.userId.includes(key) | |
| || STRIP_PARAMS.screen.includes(key) | |
| || STRIP_PARAMS.browser.includes(key) | |
| }) | |
| console.warn('GA fingerprinting params found:', fingerprintParams) | |
| expect(fingerprintParams).toContain('uip') // IP | |
| expect(fingerprintParams).toContain('cid') // Client ID | |
| expect(fingerprintParams).toContain('uid') // User ID | |
| expect(fingerprintParams).toContain('sr') // Screen resolution | |
| expect(fingerprintParams).toContain('vp') // Viewport | |
| expect(fingerprintParams).toContain('ua') // User agent | |
| }) | |
| it('identifies params to normalize in GA payload', () => { | |
| const gaPayload = FINGERPRINT_PAYLOAD.ga | |
| const normalizeParams = Object.keys(gaPayload).filter((key) => { | |
| return NORMALIZE_PARAMS.language.includes(key) | |
| }) | |
| console.warn('GA params to normalize:', normalizeParams) | |
| expect(normalizeParams).toContain('ul') // Language | |
| }) | |
| it('identifies safe params in GA payload', () => { | |
| const gaPayload = FINGERPRINT_PAYLOAD.ga | |
| const safeParams = Object.keys(gaPayload).filter((key) => { | |
| return ALLOWED_PARAMS.page.includes(key) | |
| || ALLOWED_PARAMS.event.includes(key) | |
| || ALLOWED_PARAMS.time.includes(key) | |
| }) | |
| console.warn('GA safe params:', safeParams) | |
| expect(safeParams).toContain('dt') // Title | |
| expect(safeParams).toContain('dl') // Location | |
| expect(safeParams).toContain('dr') // Referrer | |
| expect(safeParams).toContain('z') // Cache buster | |
| }) | |
| }) | |
| describe('Meta pixel payload', () => { | |
| it('identifies fingerprinting params in Meta payload', () => { | |
| const metaPayload = FINGERPRINT_PAYLOAD.meta | |
| const fingerprintParams: string[] = [] | |
| for (const key of Object.keys(metaPayload)) { | |
| if (STRIP_PARAMS.ip.some(p => key.toLowerCase().includes(p.toLowerCase()))) fingerprintParams.push(key) | |
| if (STRIP_PARAMS.userId.some(p => key.toLowerCase() === p.toLowerCase())) fingerprintParams.push(key) | |
| if (STRIP_PARAMS.userData.some(p => key.toLowerCase() === p.toLowerCase())) fingerprintParams.push(key) | |
| if (STRIP_PARAMS.browser.some(p => key.toLowerCase().includes(p.toLowerCase()))) fingerprintParams.push(key) | |
| } | |
| console.warn('Meta fingerprinting params found:', fingerprintParams) | |
| expect(fingerprintParams).toContain('client_ip_address') | |
| expect(fingerprintParams).toContain('external_id') | |
| expect(fingerprintParams).toContain('ud') // User data | |
| expect(fingerprintParams).toContain('fbp') // Browser ID | |
| expect(fingerprintParams).toContain('fbc') // Click ID | |
| }) | |
| }) | |
| describe('X/Twitter pixel payload', () => { | |
| it('identifies fingerprinting params in X pixel payload', () => { | |
| const xPayload = FINGERPRINT_PAYLOAD.xPixel | |
| const fingerprintParams: string[] = [] | |
| for (const key of Object.keys(xPayload)) { | |
| const lowerKey = key.toLowerCase() | |
| if (STRIP_PARAMS.deviceInfo.some(p => lowerKey === p.toLowerCase())) fingerprintParams.push(key) | |
| if (STRIP_PARAMS.userId.some(p => lowerKey === p.toLowerCase())) fingerprintParams.push(key) | |
| } | |
| console.warn('X/Twitter fingerprinting params found:', fingerprintParams) | |
| expect(fingerprintParams).toContain('dv') // Device info - contains timezone, screen, platform etc. | |
| expect(fingerprintParams).toContain('bci') // Browser context indicator | |
| expect(fingerprintParams).toContain('eci') // Environment context indicator | |
| expect(fingerprintParams).toContain('pl_id') // Pixel/placement ID | |
| expect(fingerprintParams).toContain('p_user_id') // User ID | |
| }) | |
| }) | |
| describe('generic fingerprint payload', () => { | |
| it('identifies all fingerprinting vectors', () => { | |
| const fp = FINGERPRINT_PAYLOAD.fingerprint | |
| const vectors: string[] = [] | |
| // Check each category | |
| if (fp.screen) vectors.push('screen') | |
| if (fp.viewport) vectors.push('viewport') | |
| if (fp.hardwareConcurrency) vectors.push('hardwareConcurrency') | |
| if (fp.deviceMemory) vectors.push('deviceMemory') | |
| if (fp.platform) vectors.push('platform') | |
| if (fp.userAgent) vectors.push('userAgent') | |
| if (fp.languages) vectors.push('languages') | |
| if (fp.timezone) vectors.push('timezone') | |
| if (fp.plugins) vectors.push('plugins') | |
| if (fp.canvas) vectors.push('canvas') | |
| if (fp.webgl) vectors.push('webgl') | |
| if (fp.audioFingerprint) vectors.push('audioFingerprint') | |
| if (fp.fonts) vectors.push('fonts') | |
| if (fp.connection) vectors.push('connection') | |
| console.warn('All fingerprinting vectors:', vectors) | |
| expect(vectors.length).toBeGreaterThan(10) |
π§° Tools
πͺ ESLint
[error] 187-187: Unexpected console statement. Only these console methods are allowed: warn, error.
(no-console)
[error] 202-202: Unexpected console statement. Only these console methods are allowed: warn, error.
(no-console)
[error] 214-214: Unexpected console statement. Only these console methods are allowed: warn, error.
(no-console)
[error] 234-234: Unexpected console statement. Only these console methods are allowed: warn, error.
(no-console)
[error] 254-254: Unexpected console statement. Only these console methods are allowed: warn, error.
(no-console)
[error] 284-284: Unexpected console statement. Only these console methods are allowed: warn, error.
(no-console)
π€ Prompt for AI Agents
In `@test/unit/proxy-privacy.test.ts` around lines 176 - 285, Several tests in the
"proxy privacy - payload analysis" suite use console.log which violates the
no-console rule; replace each console.log(...) call in the GA, Meta, X/Twitter
and generic fingerprint tests (the ones logging 'GA fingerprinting params
found', 'GA params to normalize', 'GA safe params', 'Meta fingerprinting params
found', 'X/Twitter fingerprinting params found', and 'All fingerprinting
vectors:') with console.warn(...) or remove the logging lines altogether so
ESLint accepts them; update the occurrences near the
gaPayload/fingerprintParams, normalizeParams, safeParams,
metaPayload/fingerprintParams, xPayload/fingerprintParams, and fp/vectors
blocks.
| // Helper functions for the proxy handler | ||
| export function stripFingerprintingFromPayload( | ||
| payload: Record<string, unknown>, | ||
| mode: 'strict' | 'anonymize', | ||
| ): Record<string, unknown> { | ||
| const result: Record<string, unknown> = {} | ||
|
|
||
| for (const [key, value] of Object.entries(payload)) { | ||
| const lowerKey = key.toLowerCase() | ||
|
|
||
| // Check if this should be normalized FIRST (takes priority over stripping) | ||
| const isLanguageParam = NORMALIZE_PARAMS.language.some(p => lowerKey === p.toLowerCase()) | ||
| const isUserAgentParam = NORMALIZE_PARAMS.userAgent.some(p => lowerKey === p.toLowerCase()) | ||
| const shouldNormalize = isLanguageParam || isUserAgentParam | ||
|
|
||
| if (shouldNormalize && typeof value === 'string') { | ||
| if (isLanguageParam) { | ||
| result[key] = normalizeLanguage(value) | ||
| } | ||
| else if (isUserAgentParam) { | ||
| result[key] = normalizeUserAgent(value) | ||
| } | ||
| continue | ||
| } | ||
|
|
||
| // Check if this is a fingerprinting param to strip (excluding those we normalize) | ||
| // Handle bracket notation (e.g., ud[em] matches ud) and dot notation | ||
| const matchesParam = (k: string, params: string[]) => { | ||
| const lk = k.toLowerCase() | ||
| return params.some((p) => { | ||
| const lp = p.toLowerCase() | ||
| if (lk === lp) return true | ||
| if (lk.startsWith(lp + '[')) return true | ||
| return false | ||
| }) | ||
| } | ||
|
|
||
| const isIpParam = matchesParam(key, STRIP_PARAMS.ip) | ||
| const isUserIdParam = matchesParam(key, STRIP_PARAMS.userId) | ||
| const isUserDataParam = matchesParam(key, STRIP_PARAMS.userData) | ||
| const isScreenParam = STRIP_PARAMS.screen.some(p => lowerKey === p.toLowerCase() || lowerKey.includes(p.toLowerCase())) | ||
| const isPlatformParam = matchesParam(key, STRIP_PARAMS.platform) | ||
| const isCanvasParam = matchesParam(key, STRIP_PARAMS.canvas) | ||
| // Browser params that aren't UA (plugins, fonts) - UA is handled above | ||
| const isBrowserParam = ['plugins', 'fonts'].some(p => lowerKey === p.toLowerCase()) | ||
| const isLocationParam = matchesParam(key, STRIP_PARAMS.location) | ||
| const isDeviceInfoParam = matchesParam(key, STRIP_PARAMS.deviceInfo) | ||
|
|
||
| const shouldStrip = isIpParam || isUserIdParam || isUserDataParam || isScreenParam | ||
| || isPlatformParam || isCanvasParam || isBrowserParam || isLocationParam || isDeviceInfoParam | ||
|
|
||
| if (mode === 'strict') { | ||
| if (shouldStrip) { | ||
| // Skip entirely in strict mode | ||
| continue | ||
| } | ||
| } | ||
| else if (mode === 'anonymize') { | ||
| // In anonymize mode, some params get transformed instead of stripped | ||
| if (isIpParam && typeof value === 'string') { | ||
| result[key] = anonymizeIP(value) | ||
| continue | ||
| } | ||
| if (isScreenParam) { | ||
| result[key] = generalizeScreen(value) | ||
| continue | ||
| } | ||
| // Always strip these even in anonymize mode | ||
| if (isUserIdParam || isUserDataParam || isCanvasParam || isPlatformParam || isBrowserParam || isLocationParam || isDeviceInfoParam) { | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| // Recursively process nested objects | ||
| if (typeof value === 'object' && value !== null && !Array.isArray(value)) { | ||
| result[key] = stripFingerprintingFromPayload(value as Record<string, unknown>, mode) | ||
| } | ||
| else { | ||
| result[key] = value | ||
| } | ||
| } | ||
|
|
||
| return result | ||
| } |
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.
Move stripFingerprintingFromPayload into a shared util (lint + coupling).
Biome flags exports in test files, and other tests importing from here will break. Extract this helper to a shared module (e.g., test/utils/proxy-privacy.ts) and import it where needed.
π§° Tools
πͺ Biome (2.1.2)
[error] 290-373: Do not export from a test file.
(lint/suspicious/noExportsInTest)
π€ Prompt for AI Agents
In `@test/unit/proxy-privacy.test.ts` around lines 290 - 373, Move the
stripFingerprintingFromPayload function out of the test file into a shared test
util module (e.g., create test/utils/proxy-privacy.ts), export it from there,
and update any tests importing it to import from the new module; when moving,
also import and re-export or reference its dependencies (NORMALIZE_PARAMS,
STRIP_PARAMS, normalizeLanguage, normalizeUserAgent, anonymizeIP,
generalizeScreen) inside the new util so the function compiles, and remove the
function export from the original test file.
| import { describe, expect, it } from 'vitest' | ||
| import { $fetch } from 'ofetch' | ||
| import { getAllProxyConfigs, rewriteScriptUrls } from '../../src/proxy-configs' | ||
| import { stripFingerprintingFromPayload } from './proxy-privacy.test' | ||
|
|
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.
Import helper from a shared util, not another test file.
This currently depends on proxy-privacy.test.ts; after extracting the helper, update the import to avoid cross-test coupling and lint errors.
π οΈ Proposed fix
-import { stripFingerprintingFromPayload } from './proxy-privacy.test'
+import { stripFingerprintingFromPayload } from '../utils/proxy-privacy'π€ Prompt for AI Agents
In `@test/unit/third-party-proxy-replacements.test.ts` around lines 1 - 5, Replace
the cross-test import of stripFingerprintingFromPayload from
'./proxy-privacy.test' with the shared helper module where you extracted it;
update the import statement to point to the new shared util (the module that now
exports stripFingerprintingFromPayload) so tests import
stripFingerprintingFromPayload from the shared utility instead of another test
file.
π Linked issue
Resolves #87
β Type of change
π Description
Third-party scripts expose user data directly to external servers - every request shares the user's IP address, and scripts can set third-party cookies for cross-site tracking. Ad blockers rightfully block these for privacy reasons.
This PR adds a
firstPartyoption that routes all script traffic through your own domain:Scripts are downloaded at build time, collection URLs rewritten to local paths (
/_scripts/c/ga), and Nitro route rules proxy requests to original endpoints.Supported: Google Analytics, GTM, Meta Pixel, TikTok, Segment, Clarity, Hotjar, X/Twitter, Snapchat, Reddit.
Includes new
/docs/guides/first-partydocumentation and deprecation notice on bundling guide.