Skip to content

Commit

Permalink
fix(sveltekit): Avoid request body double read errors (#15368)
Browse files Browse the repository at this point in the history
Remove `request.clone()` calls from our request handler. This is safe because our SDK does not
consume the request body. Cloning here was a precautionary measure that
apparently backfired.
  • Loading branch information
Lms24 authored Feb 11, 2025
1 parent 1c7edab commit bb12dff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 10 additions & 2 deletions packages/sveltekit/src/server/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ export function sentryHandle(handlerOptions?: SentryHandleOptions): Handle {
// We only call continueTrace in the initial top level request to avoid
// creating a new root span for the sub request.
isolationScope.setSDKProcessingMetadata({
normalizedRequest: winterCGRequestToRequestData(input.event.request.clone()),
// We specifically avoid cloning the request here to avoid double read errors.
// We only read request headers so we're not consuming the body anyway.
// Note to future readers: This sounds counter-intuitive but please read
// https://github.com/getsentry/sentry-javascript/issues/14583
normalizedRequest: winterCGRequestToRequestData(input.event.request),
});
return continueTrace(getTracePropagationData(input.event), () => instrumentHandle(input, options));
});
Expand Down Expand Up @@ -163,7 +167,11 @@ async function instrumentHandle(
},
async (span?: Span) => {
getCurrentScope().setSDKProcessingMetadata({
normalizedRequest: winterCGRequestToRequestData(event.request.clone()),
// We specifically avoid cloning the request here to avoid double read errors.
// We only read request headers so we're not consuming the body anyway.
// Note to future readers: This sounds counter-intuitive but please read
// https://github.com/getsentry/sentry-javascript/issues/14583
normalizedRequest: winterCGRequestToRequestData(event.request),
});
const res = await resolve(event, {
transformPageChunk: addSentryCodeToPage({ injectFetchProxyScript: options.injectFetchProxyScript ?? true }),
Expand Down
2 changes: 0 additions & 2 deletions packages/sveltekit/test/server/handle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ function mockEvent(override: Record<string, unknown> = {}): Parameters<Handle>[0
...override,
};

event.request.clone = () => event.request;

return event;
}

Expand Down

0 comments on commit bb12dff

Please sign in to comment.