Skip to content

Commit bb12dff

Browse files
authored
fix(sveltekit): Avoid request body double read errors (#15368)
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.
1 parent 1c7edab commit bb12dff

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

packages/sveltekit/src/server/handle.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ export function sentryHandle(handlerOptions?: SentryHandleOptions): Handle {
113113
// We only call continueTrace in the initial top level request to avoid
114114
// creating a new root span for the sub request.
115115
isolationScope.setSDKProcessingMetadata({
116-
normalizedRequest: winterCGRequestToRequestData(input.event.request.clone()),
116+
// We specifically avoid cloning the request here to avoid double read errors.
117+
// We only read request headers so we're not consuming the body anyway.
118+
// Note to future readers: This sounds counter-intuitive but please read
119+
// https://github.com/getsentry/sentry-javascript/issues/14583
120+
normalizedRequest: winterCGRequestToRequestData(input.event.request),
117121
});
118122
return continueTrace(getTracePropagationData(input.event), () => instrumentHandle(input, options));
119123
});
@@ -163,7 +167,11 @@ async function instrumentHandle(
163167
},
164168
async (span?: Span) => {
165169
getCurrentScope().setSDKProcessingMetadata({
166-
normalizedRequest: winterCGRequestToRequestData(event.request.clone()),
170+
// We specifically avoid cloning the request here to avoid double read errors.
171+
// We only read request headers so we're not consuming the body anyway.
172+
// Note to future readers: This sounds counter-intuitive but please read
173+
// https://github.com/getsentry/sentry-javascript/issues/14583
174+
normalizedRequest: winterCGRequestToRequestData(event.request),
167175
});
168176
const res = await resolve(event, {
169177
transformPageChunk: addSentryCodeToPage({ injectFetchProxyScript: options.injectFetchProxyScript ?? true }),

packages/sveltekit/test/server/handle.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ function mockEvent(override: Record<string, unknown> = {}): Parameters<Handle>[0
4646
...override,
4747
};
4848

49-
event.request.clone = () => event.request;
50-
5149
return event;
5250
}
5351

0 commit comments

Comments
 (0)