Title: Duplicated values in Sentry Baggage header cause eventual 431 "Request header fields too large" error on HTTP fetch · Issue #12350 · getsentry/sentry-javascript · GitHub
Open Graph Title: Duplicated values in Sentry Baggage header cause eventual 431 "Request header fields too large" error on HTTP fetch · Issue #12350 · getsentry/sentry-javascript
X Title: Duplicated values in Sentry Baggage header cause eventual 431 "Request header fields too large" error on HTTP fetch · Issue #12350 · getsentry/sentry-javascript
Description: Is there an existing issue for this? I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues I have reviewed the documentation https://docs.sentry.io/ I am using the latest SDK release https://github.com/...
Open Graph Description: Is there an existing issue for this? I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues I have reviewed the documentation https://docs.sentry.io/ I am using th...
X Description: Is there an existing issue for this? I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues I have reviewed the documentation https://docs.sentry.io/ I am using th...
Opengraph URL: https://github.com/getsentry/sentry-javascript/issues/12350
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Duplicated values in Sentry Baggage header cause eventual 431 \"Request header fields too large\" error on HTTP fetch","articleBody":"### Is there an existing issue for this?\n\n- [X] I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues\n- [X] I have reviewed the documentation https://docs.sentry.io/\n- [X] I am using the latest SDK release https://github.com/getsentry/sentry-javascript/releases\n\n### How do you use Sentry?\n\nSentry Saas (sentry.io)\n\n### Which SDK are you using?\n\n@sentry/nextjs\n\n### SDK Version\n\n7.112.2\n\n### Framework Version\n\n13.5.6\n\n### Link to Sentry event\n\nn/a\n\n### SDK Setup\n\n\r\n```\r\n// sentry.client.config.ts\r\n// This file configures the initialization of Sentry on the client.\r\n// The config you add here will be used whenever a users loads a page in their browser.\r\n// https://docs.sentry.io/platforms/javascript/guides/nextjs/\r\n\r\nimport * as Sentry from '@sentry/nextjs';\r\n\r\nSentry.init({\r\n dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,\r\n\r\n // Adjust this value in production, or use tracesSampler for greater control\r\n tracesSampleRate: process.env.NEXT_PUBLIC_SENTRY_SAMPLE_RATE\r\n ? parseFloat(process.env.NEXT_PUBLIC_SENTRY_SAMPLE_RATE)\r\n : 0.2,\r\n\r\n // Setting this option to true will print useful information to the console while you're setting up Sentry.\r\n debug: false,\r\n\r\n replaysOnErrorSampleRate: 1.0,\r\n\r\n // This sets the sample rate to be 10%. You may want this to be 100% while\r\n // in development and sample at a lower rate in production\r\n replaysSessionSampleRate: 0.1,\r\n\r\n // You can remove this option if you're not planning to use the Sentry Session Replay feature:\r\n integrations: [\r\n Sentry.replayIntegration({\r\n // Additional Replay configuration goes in here, for example:\r\n maskAllText: true,\r\n blockAllMedia: true,\r\n }),\r\n ],\r\n\r\n // Only enable sentry in production with feature flag on.\r\n enabled: process.env.NODE_ENV === 'production' && process.env.ENABLE_SENTRY === 'true',\r\n});\r\n```\r\n\r\n```\r\n// sentry.edge.config.ts\r\n// This file configures the initialization of Sentry for edge features (middleware, edge routes, and so on).\r\n// The config you add here will be used whenever one of the edge features is loaded.\r\n// Note that this config is unrelated to the Vercel Edge Runtime and is also required when running locally.\r\n// https://docs.sentry.io/platforms/javascript/guides/nextjs/\r\n\r\nimport * as Sentry from '@sentry/nextjs';\r\n\r\nSentry.init({\r\n dsn: process.env.SENTRY_DSN,\r\n\r\n // Adjust this value in production, or use tracesSampler for greater control\r\n tracesSampleRate: process.env.SENTRY_SAMPLE_RATE\r\n ? parseFloat(process.env.SENTRY_SAMPLE_RATE)\r\n : 0.2,\r\n\r\n // Setting this option to true will print useful information to the console while you're setting up Sentry.\r\n debug: false,\r\n\r\n // Only enable sentry in production with feature flag on.\r\n enabled: process.env.NODE_ENV === 'production' && process.env.ENABLE_SENTRY === 'true',\r\n});\r\n```\r\n\r\n```\r\n// sentry.server.config.ts\r\n// This file configures the initialization of Sentry on the server.\r\n// The config you add here will be used whenever the server handles a request.\r\n// https://docs.sentry.io/platforms/javascript/guides/nextjs/\r\n\r\nimport * as Sentry from '@sentry/nextjs';\r\n\r\nSentry.init({\r\n dsn: process.env.SENTRY_DSN,\r\n\r\n // Adjust this value in production, or use tracesSampler for greater control\r\n tracesSampleRate: process.env.SENTRY_SAMPLE_RATE\r\n ? parseFloat(process.env.SENTRY_SAMPLE_RATE)\r\n : 0.2,\r\n\r\n // Setting this option to true will print useful information to the console while you're setting up Sentry.\r\n debug: false,\r\n\r\n // uncomment the line below to enable Spotlight (https://spotlightjs.com)\r\n // spotlight: process.env.NODE_ENV === 'development',\r\n\r\n // Only enable sentry in production with feature flag on.\r\n enabled: process.env.NODE_ENV === 'production' && process.env.ENABLE_SENTRY === 'true',\r\n});\r\n\r\n```\n\n### Steps to Reproduce\n\n- create a simple next app which uses [rewrites](https://nextjs.org/docs/pages/api-reference/next-config-js/rewrites) to proxy requests to some external API. Call this /my/api/route\r\n- use the sentry configs described above\r\n- create a button that performs client side fetch calls to this /my/api/route every time click.\r\n- observe the \"Baggage\" headers attached to the client fetch calls, and how duplicate values are continually added on subsequent requests.\n\n### Expected Result\n\nNon-duplicated Sentry `Baggage` header.\n\n### Actual Result\n\nApp is experiencing the exact same issue as described in https://github.com/getsentry/sentry-javascript/issues/11500. Each subsequent request made from our NextJS client includes a duplicated entry under the `Baggage` header. For example, we have an `/account/settings` page that fires off 3 requests during load:\r\n\r\nrequest 1\r\n\u003cimg width=\"700\" alt=\"Screenshot 2024-06-04 at 9 17 21 AM\" src=\"https://github.com/getsentry/sentry-javascript/assets/4588531/99ee2835-d9c3-4444-90b3-1d7d8dc1ec16\">\r\n\r\nrequest 2\r\n\u003cimg width=\"686\" alt=\"Screenshot 2024-06-04 at 9 17 31 AM\" src=\"https://github.com/getsentry/sentry-javascript/assets/4588531/335930fd-037b-4e58-8299-9fb550213595\">\r\n\r\nrequest 3\r\n\u003cimg width=\"772\" alt=\"Screenshot 2024-06-04 at 9 17 43 AM\" src=\"https://github.com/getsentry/sentry-javascript/assets/4588531/488f863e-9642-462f-8697-3462e3c25d07\">\r\n\r\nthis pattern continues until the one of the fetch calls recieves back a 431 \"Request header fields too large\" response, which usually results in app hanging until hard refresh.\r\n\r\nUsing `\"@sentry/nextjs\": \"^7.112.2\"` for our Next app.\r\n\r\nTaking a look at the logic that @lforst mentioned in https://github.com/getsentry/sentry-javascript/issues/11500#issuecomment-2047136837, I suspect this is a bug with the construction of the sentry baggage headers, specifically either [here](https://github.com/getsentry/sentry-javascript/blob/15d6cb17909da3284a05d8da9356ef2ce0c3bc60/packages/core/src/fetch.ts#L166-L170) or [here](https://github.com/getsentry/sentry-javascript/blob/15d6cb17909da3284a05d8da9356ef2ce0c3bc60/packages/core/src/fetch.ts#L177-L191).\r\n\r\nIn both cases, doing a deduplication of existing key-value pairs might resolve the issue:\r\n\r\n```js\r\nfunction deduplicateHeaderValues(headerString) {\r\n // Split the string into an array of key-value pairs\r\n const pairsArray = headerString.split(',');\r\n\r\n // Create a Map to store unique key-value pairs\r\n const uniquePairs = new Map();\r\n\r\n // Iterate over the pairs array\r\n pairsArray.forEach(pair => {\r\n const [key, value] = pair.split('=');\r\n // Store the pair in the Map, overwriting any duplicate keys\r\n uniquePairs.set(key, value);\r\n });\r\n\r\n // Convert the Map back to a string of key-value pairs\r\n const deduplicatedString = Array.from(uniquePairs).map(pair => pair.join('=')).join(',');\r\n\r\n return deduplicatedString;\r\n}\r\n\r\n// Example usage:\r\nconst headerString = 'sentry-environment=vercel-production,sentry-release=44628bc0cc9159d525751a0da2c82fa54d01124a,sentry-public_key=ca2f84b9f6e4d93d9c70dd70f4ad9bbe,sentry-trace_id=559215e003254fe5abecdb9dd001cd2e,sentry-environment=vercel-production,sentry-release=44628bc0cc9159d525751a0da2c82fa54d01124a,sentry-public_key=ca2f84b9f6e4d93d9c70dd70f4ad9bbe,sentry-trace_id=559215e003254fe5abecdb9dd001cd2e,sentry-environment=vercel-production,sentry-release=44628bc0cc9159d525751a0da2c82fa54d01124a,sentry-public_key=ca2f84b9f6e4d93d9c70dd70f4ad9bbe,sentry-trace_id=559215e003254fe5abecdb9dd001cd2e';\r\n\r\nconst deduplicatedHeaderString = deduplicateHeaderValues(headerString);\r\nconsole.log(deduplicatedHeaderString);\r\n```\r\n\r\n","author":{"url":"https://github.com/etnichols","@type":"Person","name":"etnichols"},"datePublished":"2024-06-04T13:34:22Z","url":"https://github.com/getsentry/sentry-javascript/issues/12350"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:bd0a3589-2f1c-e93f-7655-af240209412a |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 978C:F3BB5:151D58:1D6A24:6B074584 |
| html-safe-nonce | 1efbd8b670b24bd74e69694a012aa36a31b57077a12e2ae01cb077c42312fada |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NzhDOkYzQkI1OjE1MUQ1ODoxRDZBMjQ6NkIwNzQ1ODQiLCJ2aXNpdG9yX2lkIjoiNDk5MzkyOTc5ODgwNTMwODgwNCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3824d3f3fcac9101ed92dc3f86f34ffc9bc206f2e00bf0a216563278ae901a90 |
| hovercard-subject-tag | issue:2333557678 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-javascript/12350/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8af49d2e98eff4079d46bf58a241e69dbbac62bfb975883c46069b295a2f2c28/getsentry/sentry-javascript/issues/12350 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8af49d2e98eff4079d46bf58a241e69dbbac62bfb975883c46069b295a2f2c28/getsentry/sentry-javascript/issues/12350 |
| og:image:alt | Is there an existing issue for this? I have checked for existing issues https://github.com/getsentry/sentry-javascript/issues I have reviewed the documentation https://docs.sentry.io/ I am using th... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | etnichols |
| hostname | github.com |
| expected-hostname | github.com |
| None | e906c59266d83416af9bcc8b61b9a96e70a3a84a066bd24f6671c193dd134a04 |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-javascript git https://github.com/getsentry/sentry-javascript.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3328572 |
| octolytics-dimension-repository_nwo | getsentry/sentry-javascript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3328572 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-javascript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | a8418b8cd1f6ecc99bee02e3f7dde540702dd08c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width