Skip to content

fix(cloudflare): Forward method, headers and body when a Request is passed as fetch init - #26368

Merged
andreiborza merged 3 commits into
getsentry:developfrom
Vivek1898:fix/cloudflare-instrument-fetcher-request-init
Nov 16, 2026
Merged

andreiborza merged 3 commits into
getsentry:developfrom
Vivek1898:fix/cloudflare-instrument-fetcher-request-init

Conversation

@Vivek1898

@Vivek1898 Vivek1898 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #26367

The problem

instrumentFetcher built the outgoing init with an object spread:

const mergedInit = { ...init, ...(headers ? { headers } : {}) };
return fetchFn(input, mergedInit);

Forwarding a request as stub.fetch(url, request) is a common Workers idiom, so init is often itself a Request. A Request's method, headers, body and the rest are prototype getters, not own enumerable properties, so the spread copied none of them. Behind an instrumented Durable Object namespace or service binding the call reached the object as a bare GET with no headers and no body, which broke WebSocket upgrades (Upgrade: websocket) and every forwarded POST. The same call works without Sentry.

The fix

When init is a Request, normalize it up front into a Request input (new Request(originalInput, originalInit)), which reads method, headers and body by property access, and clear init. The call then flows through the existing input instanceof Request && init === undefined branch, which layers the tracing headers on top (or forwards the request as-is when tracing is disabled). Plain-object init still goes through the spread path, so no other case changes.

Tests

instrumentFetcher.test.ts adds cases for a Request-valued init: method, headers and body are forwarded with tracing on, and forwarded with tracing disabled (no sentry-trace/baggage added).

…assed as fetch init

`instrumentFetcher` built the outgoing init with an object spread (`{ ...init }`).
When `init` is itself a Request (the common `stub.fetch(url, request)` Workers
idiom), its method, headers and body are prototype getters rather than own
properties, so the spread copied none of them. Behind an instrumented Durable
Object or service binding the call arrived as a bare GET with no headers and no
body, breaking WebSocket upgrades and forwarded POSTs that work without Sentry.

Handle a Request-valued `init` the same way the existing single-Request branch
does: rebuild through the Request constructor (which reads those members by
property access) and layer the tracing headers, which already include the
request's own headers, on top.

Fixes getsentry#26367
@Vivek1898
Vivek1898 requested review from a team and JPeer264 November 13, 2026 18:04
@github-actions github-actions Bot added the external PR from an external contributor label Nov 13, 2026
@andreiborza

Copy link
Copy Markdown
Member

Hi @Vivek1898, thank you for the contribution but next time please read our contribution guideline a bit closer. We typically don't accept contributions without prior discussion.

@JPeer264 JPeer264 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. The description is out of date though

expect(headers.get('baggage')).toBe('sentry-environment=production');
});

it('forwards method, headers and body when init is a Request', async () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: We should re-add the test to forward headers when tracing is disabled

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-added it in 79b82ef, and updated the description. Thanks for the review!

Covers the no-trace-data path for a Request-valued init: the method,
headers and body are forwarded and no sentry-trace header is added.
@andreiborza
andreiborza merged commit f22a8e9 into getsentry:develop Nov 16, 2026
783 of 784 checks passed
msonnb pushed a commit that referenced this pull request Nov 16, 2026
This PR adds the external contributor to the CHANGELOG.md file, so that
they are credited for their contribution. See #26368

Co-authored-by: andreiborza <168741329+andreiborza@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external PR from an external contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@sentry/cloudflare 11: instrumented Durable Object stubs drop method, headers and body when init is a Request

3 participants