Skip to content

Commit ee04661

Browse files
JPeer264claude
andauthored
fix(browser): Apply urlQueryParams to event URLs, static span names and LCP URLs (#25262)
closes #25247 closes [JS-3907](https://linear.app/getsentry/issue/JS-3907/sensitive-url-query-params-leak-to-sentry-in-transaction-events) ## Description This applies `dataCollection.urlQueryParams` to the URLs that the browser SDK still sent unfiltered: the page URL in `event.request.url`, the `browser.*` and `resource.*` span names in the static trace lifecycle, and the LCP URL attributes (`lcp.url`, `browser.web_vital.lcp.url`). `event.request.url` leaked on error events with every trace lifecycle, the span names only with `traceLifecycle: 'static'` (the v10 default). Most of the added lines are tests. ## Sentry traces Before (11.6.0), `jwt=sensitive-jwt-value` in `request.url` and span names: https://sentry-sdks.sentry.io/explore/traces/trace/056ddfee720849c78e4e0b68f9da8c5b/?project=4510555608449024&timestamp=1791796663 After (this PR), `jwt=[Filtered]`: https://sentry-sdks.sentry.io/explore/traces/trace/ea9fd2cc90de4343bf5bfb6eb0d923ad/?project=4510555608449024&timestamp=1791796677 --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 1e86178 commit ee04661

16 files changed

Lines changed: 155 additions & 21 deletions

File tree

‎dev-packages/browser-integration-tests/suites/integrations/httpContext/test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ sentryTest('httpContextIntegration captures user-agent and referrer', async ({ g
2525
});
2626
});
2727

28+
sentryTest('httpContextIntegration filters sensitive query params in the URL', async ({ getLocalTestUrl, page }) => {
29+
const url = await getLocalTestUrl({ testDir: __dirname });
30+
31+
const errorEventPromise = waitForErrorRequest(page);
32+
33+
await page.goto(`${url}?jwt=sensitive-jwt-value&page=1`);
34+
35+
const errorEvent = envelopeRequestParser(await errorEventPromise);
36+
37+
expect(errorEvent.request?.url).toBe(`${url}?jwt=[Filtered]&page=1`);
38+
});
39+
2840
sentryTest('httpContextIntegration captures url, user-agent, and referer', async ({ getLocalTestUrl, page }) => {
2941
sentryTest.skip(shouldSkipTracingTest());
3042
const url = await getLocalTestUrl({ testDir: __dirname });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
traceLifecycle: 'static',
7+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
8+
integrations: [Sentry.browserTracingIntegration()],
9+
tracesSampleRate: 1,
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<body>
7+
<img src="https://sentry-test-site.example/path/to/image.svg?token=sensitive-token-value&size=2" />
8+
</body>
9+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { BROWSER_REQUEST, PAGELOAD, RESOURCE_IMG } from '@sentry/conventions/op';
2+
import { expect } from '@playwright/test';
3+
import type { Event } from '@sentry/core';
4+
import { sentryTest } from '../../../../utils/fixtures';
5+
import { envelopeRequestParser, shouldSkipTracingTest, waitForTransactionRequest } from '../../../../utils/helpers';
6+
7+
sentryTest(
8+
'filters sensitive query params in the pageload transaction URL and span descriptions',
9+
async ({ getLocalTestUrl, page }) => {
10+
sentryTest.skip(shouldSkipTracingTest());
11+
12+
await page.route('https://sentry-test-site.example/path/to/image.svg?*', route =>
13+
route.fulfill({ body: '<svg xmlns="http://www.w3.org/2000/svg"/>', contentType: 'image/svg+xml' }),
14+
);
15+
16+
const url = await getLocalTestUrl({ testDir: __dirname });
17+
18+
const pageloadRequestPromise = waitForTransactionRequest(page, event => event.contexts?.trace?.op === PAGELOAD);
19+
await page.goto(`${url}?jwt=sensitive-jwt-value&page=1`);
20+
const eventData = envelopeRequestParser<Event>(await pageloadRequestPromise);
21+
22+
expect(eventData.request?.url).toBe(`${url}?jwt=[Filtered]&page=1`);
23+
expect(eventData.spans?.find(({ op }) => op === BROWSER_REQUEST)?.description).toBe(`${url}?jwt=[Filtered]&page=1`);
24+
expect(eventData.spans?.find(({ op }) => op === RESOURCE_IMG)?.description).toBe(
25+
'https://sentry-test-site.example/path/to/image.svg?token=[Filtered]&size=2',
26+
);
27+
28+
const serializedEvent = JSON.stringify(eventData);
29+
expect(serializedEvent).not.toContain('sensitive-jwt-value');
30+
expect(serializedEvent).not.toContain('sensitive-token-value');
31+
},
32+
);

‎dev-packages/browser-integration-tests/suites/tracing/metrics/web-vitals-lcp-static/template.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
</head>
66
<body>
77
<div id="content"></div>
8-
<img src="https://sentry-test-site.example/my/image.png" />
8+
<img src="https://sentry-test-site.example/my/image.png?token=sensitive-token-value&v=1" />
99
</body>
1010
</html>

‎dev-packages/browser-integration-tests/suites/tracing/metrics/web-vitals-lcp-static/test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ sentryTest('captures LCP vitals with element details', async ({ browserName, get
1616
}
1717

1818
page.route('**', route => route.continue());
19-
page.route('**/my/image.png', async (route: Route) => {
19+
page.route('**/my/image.png?*', async (route: Route) => {
2020
return route.fulfill({
2121
path: `${__dirname}/assets/sentry-logo-600x179.png`,
2222
});
@@ -36,6 +36,9 @@ sentryTest('captures LCP vitals with element details', async ({ browserName, get
3636
expect(eventData.contexts?.trace?.data?.['lcp.size']).toBeGreaterThan(0);
3737
expect(eventData.contexts?.trace?.data?.['lcp.loadTime']).toBeGreaterThan(0);
3838
expect(eventData.contexts?.trace?.data?.['lcp.renderTime']).toBeGreaterThan(0);
39+
expect(eventData.contexts?.trace?.data?.['lcp.url']).toBe(
40+
'https://sentry-test-site.example/my/image.png?token=[Filtered]&v=1',
41+
);
3942

4043
// The LCP value should be the renderTime because the renderTime is set
4144
expect(eventData.measurements?.lcp?.value).toBeCloseTo(eventData.contexts?.trace?.data?.['lcp.renderTime']);

‎dev-packages/browser-integration-tests/utils/fixtures.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ const sentryTest = base.extend<TestFixtures>({
9090
}
9191

9292
await page.route(`${TEST_HOST}/*.*`, route => {
93-
const file = route.request().url().split('/').pop();
93+
const file = new URL(route.request().url()).pathname.split('/').pop();
9494
const filePath = path.resolve(tmpDir, `./${file}`);
9595

9696
return fs.existsSync(filePath)

‎packages/browser-utils/src/performance/entries.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -360,14 +360,15 @@ function _addPerformanceNavigationTiming(
360360
return;
361361
}
362362
const op = NAVIGATION_TIMING_SPAN_OPS[event];
363+
const filteredUrl = filterCollectedUrl(entry.name);
363364
startAndEndSpan(span, _navigationTimeToSeconds(entry, start), _navigationTimeToSeconds(entry, end), {
364365
// With span streaming, span names have to be low cardinality, so we can't fall back to the
365366
// document URL. `url.full` keeps it, and is what Relay derives the description from.
366-
name: spanStreamingEnabled ? BROWSER_NAVIGATION_TIMING_SPAN_NAMES[op] : entry.name,
367+
name: spanStreamingEnabled ? BROWSER_NAVIGATION_TIMING_SPAN_NAMES[op] : filteredUrl,
367368
attributes: {
368369
[SENTRY_OP]: op,
369370
[SENTRY_ORIGIN]: 'auto.ui.browser.metrics',
370-
[URL_FULL]: filterCollectedUrl(entry.name),
371+
[URL_FULL]: filteredUrl,
371372
...(event === 'redirect' && entry.redirectCount != null ? { 'http.redirect_count': entry.redirectCount } : {}),
372373
},
373374
});
@@ -396,23 +397,23 @@ function _addRequest(span: Span, entry: PerformanceNavigationTiming, spanStreami
396397

397398
// With span streaming, span names have to be low cardinality, so we can't fall back to the
398399
// document URL. `url.full` keeps it, and is what Relay derives the description from.
399-
const url = filterCollectedUrl(entry.name);
400+
const filteredUrl = filterCollectedUrl(entry.name);
400401

401402
startAndEndSpan(span, requestStartTimestamp, responseEndTimestamp, {
402-
name: spanStreamingEnabled ? BROWSER_NAVIGATION_TIMING_SPAN_NAMES[BROWSER_REQUEST] : entry.name,
403+
name: spanStreamingEnabled ? BROWSER_NAVIGATION_TIMING_SPAN_NAMES[BROWSER_REQUEST] : filteredUrl,
403404
attributes: {
404405
[SENTRY_OP]: BROWSER_REQUEST,
405406
[SENTRY_ORIGIN]: 'auto.ui.browser.metrics',
406-
[URL_FULL]: url,
407+
[URL_FULL]: filteredUrl,
407408
},
408409
});
409410

410411
startAndEndSpan(span, responseStartTimestamp, responseEndTimestamp, {
411-
name: spanStreamingEnabled ? BROWSER_NAVIGATION_TIMING_SPAN_NAMES[BROWSER_RESPONSE] : entry.name,
412+
name: spanStreamingEnabled ? BROWSER_NAVIGATION_TIMING_SPAN_NAMES[BROWSER_RESPONSE] : filteredUrl,
412413
attributes: {
413414
[SENTRY_OP]: BROWSER_RESPONSE,
414415
[SENTRY_ORIGIN]: 'auto.ui.browser.metrics',
415-
[URL_FULL]: url,
416+
[URL_FULL]: filteredUrl,
416417
},
417418
});
418419
}
@@ -468,7 +469,8 @@ export function _addResourceSpans(
468469

469470
attributes[HTTP_REQUEST_SAME_ORIGIN] = resourceUrl.includes(WINDOW.location.origin);
470471

471-
attributes[URL_FULL] = filterCollectedUrl(resourceUrl);
472+
const filteredUrl = filterCollectedUrl(resourceUrl);
473+
attributes[URL_FULL] = filteredUrl;
472474

473475
_setResourceRequestAttributes(entry, attributes, [
474476
// https://developer.mozilla.org/en-US/docs/Web/API/PerformanceResourceTiming/responseStatus
@@ -493,7 +495,7 @@ export function _addResourceSpans(
493495
// With span streaming, span names have to be low cardinality, so we can't fall back to the URL.
494496
name: spanStreamingEnabled
495497
? domain || RESOURCE_SPAN_NAME_FALLBACK
496-
: resourceUrl.replace(WINDOW.location.origin, ''),
498+
: filteredUrl.replace(WINDOW.location.origin, ''),
497499
op,
498500
attributes: attributesWithResourceTiming,
499501
});

‎packages/browser-utils/src/web-vitals/spans.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { Client, Span, SpanAttributes } from '@sentry/core';
33
import {
44
browserPerformanceTimeOrigin,
55
debug,
6+
filterCollectedUrl,
67
getActiveSpan,
78
getClient,
89
getRootSpan,
@@ -219,7 +220,7 @@ export function _sendLcpSpan(
219220
attributes[UI_COMPONENT_NAME] = componentName;
220221
}
221222
entry?.id && (attributes['browser.web_vital.lcp.id'] = entry.id);
222-
entry?.url && (attributes['browser.web_vital.lcp.url'] = entry.url);
223+
entry?.url && (attributes['browser.web_vital.lcp.url'] = filterCollectedUrl(entry.url, client));
223224
entry?.loadTime != null && (attributes['browser.web_vital.lcp.load_time'] = entry.loadTime);
224225
entry?.renderTime != null && (attributes['browser.web_vital.lcp.render_time'] = entry.renderTime);
225226
entry?.size != null && (attributes['browser.web_vital.lcp.size'] = entry.size);

‎packages/browser-utils/src/web-vitals/tracking.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Client, Measurements, Span } from '@sentry/core';
2-
import { browserPerformanceTimeOrigin, debug, setMeasurement, spanToJSON } from '@sentry/core';
2+
import { browserPerformanceTimeOrigin, debug, filterCollectedUrl, setMeasurement, spanToJSON } from '@sentry/core';
33
import { BROWSER_NAVIGATION_TYPE, SENTRY_OP } from '@sentry/conventions/attributes';
44
import { DEBUG_BUILD } from '../debug-build';
55
import { htmlTreeAsString } from '../htmlTreeAsString';
@@ -253,7 +253,7 @@ function _setWebVitalAttributes(span: Span, options: AddWebVitalsToSpanOptions):
253253

254254
if (_lcpEntry.url) {
255255
// Trim URL to the first 200 characters.
256-
span.setAttribute('lcp.url', _lcpEntry.url.trim().slice(0, 200));
256+
span.setAttribute('lcp.url', filterCollectedUrl(_lcpEntry.url.trim()).slice(0, 200));
257257
}
258258

259259
if (_lcpEntry.loadTime != null) {

0 commit comments

Comments
 (0)