Skip to content

Commit 320309e

Browse files
committed
fix(nextjs): Filter the query of the document URL on the pageload request data
The pageload wrote the raw document URL into `normalizedRequest`, so the `dataCollection.urlQueryParams` denylist no longer applied to `request.url` of any App Router pageload. The URL is now filtered once, when it is captured.
1 parent f46ce6b commit 320309e

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎packages/nextjs/src/client/routing/appRouterRoutingInstrumentation.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ function startPageloadSpan(
136136

137137
/** Instruments the Next.js app router for pageloads. */
138138
export function appRouterInstrumentPageLoad(client: Client): void {
139-
const documentUrl = WINDOW.location.href;
139+
const documentUrl = filterCollectedUrl(WINDOW.location.href, client);
140140
const pathname = stripTrailingSlash(WINDOW.location.pathname);
141141
const parameterizedPathname = resolveCurrentRoute(client);
142142
const spanOptions: StartSpanOptions = {
@@ -148,7 +148,7 @@ export function appRouterInstrumentPageLoad(client: Client): void {
148148
[SENTRY_ORIGIN]: 'auto.pageload.nextjs.app_router_instrumentation',
149149
[SENTRY_SEGMENT_NAME_SOURCE]: parameterizedPathname ? 'route' : 'url',
150150
[URL_PATH]: WINDOW.location.pathname,
151-
[URL_FULL]: filterCollectedUrl(documentUrl, client),
151+
[URL_FULL]: documentUrl,
152152
...(parameterizedPathname && { [URL_TEMPLATE]: parameterizedPathname }),
153153
},
154154
};

‎packages/nextjs/test/client/appRouterRoutingInstrumentation.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,24 @@ describe('appRouterInstrumentPageLoad', () => {
315315
'http://localhost:3000/navigation?from=document',
316316
);
317317
});
318+
319+
// `settlePendingPageloadWait` is a no-op when the pageload did not wait, so both cases run the same steps.
320+
it.each([
321+
{ start: 'at once', cacheComponents: 'false' },
322+
{ start: 'after the wait', cacheComponents: 'true' },
323+
])(
324+
'filters sensitive query parameters from the document URL of a pageload that starts $start',
325+
async ({ cacheComponents }) => {
326+
vi.stubEnv('_sentryCacheComponents', cacheComponents);
327+
window.history.replaceState({}, '', '/navigation?token=2c4f9e1b&from=document');
328+
const { core, instrumentation } = await setup('static');
329+
330+
instrumentation.appRouterInstrumentPageLoad(core.getClient()!);
331+
instrumentation.settlePendingPageloadWait();
332+
333+
const filteredUrl = 'http://localhost:3000/navigation?token=[Filtered]&from=document';
334+
expect(core.spanToJSON(core.getActiveSpan()!).attributes?.[URL_FULL]).toBe(filteredUrl);
335+
expect(core.getCurrentScope().getScopeData().sdkProcessingMetadata.normalizedRequest?.url).toBe(filteredUrl);
336+
},
337+
);
318338
});

0 commit comments

Comments
 (0)