Skip to content

Incorrect Headers handling in @sentry/apm breaks fetch #2685

@Janpot

Description

@Janpot

Package + Version

  • @sentry/browser
  • @sentry/node
  • raven-js
  • raven-node (raven for node)
  • other: @sentry/apm

Version:

5.17.0

Description

The ApmIntegrations.Tracing doesn't handle headers properly in fetch when it is supplied as an array or as a Headers object:

import * as Sentry from '@sentry/browser';
import { Integrations as ApmIntegrations } from '@sentry/apm';

Sentry.init({
  // ...
  integrations: [new ApmIntegrations.Tracing()]
});

In the following case, the headers are cleared by the tracing integration:

await fetch('/', { headers: new Headers({ 'content-type': 'application/json' }) })

In the following case, it downright errors:

await fetch('/', { headers: [['content-type', 'application/json']] })
// Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': The object must have a callable @@iterator property.

This just flat out started to break our application in production for some of our users, with no indication whatsoever as to what causes this.

From digging through the code, I think the problem comes from here. Didn't test this, but I think something like the following should work:

          if (options.headers && typeof options.headers[Symbol.iterator] === 'function') {
            options.headers = [...options.headers, ['sentry-trace', span.toTraceparent()]];
          } else {
            options.headers = {
              ...options.headers,
              'sentry-trace': span.toTraceparent(),
            };
          }

Looking at the code it seems like it would also incorrectly handle

await fetch(new Request('/', { headers: new Headers({ 'content-type': 'application/json' }) }))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions