From 9cd6013ba05e00e7de21009dc13b29b49928275e Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Mon, 16 Nov 2026 13:08:47 +0000 Subject: [PATCH 1/3] fix(core): Keep the longest active rate limit for each category `updateRateLimits` stored the last limit it saw for a category, so a shorter limit could replace a longer one that was still active. The spec requires keeping the longer one. The parsing of the response now lives in a module-private function that only reports each category and deadline through a callback. It has no access to the stored limits. The callback in `updateRateLimits` is the only code that writes a limit, and it keeps the later of the stored and the new deadline, so no update path can shorten an active limit. Fixes #26409 Co-Authored-By: Daniel Szoke <7881302+szokeasaurusrex@users.noreply.github.com> --- packages/core/src/utils/ratelimit.ts | 32 ++++++++++++++----- .../core/test/lib/utils/ratelimit.test.ts | 20 ++++++++++-- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/packages/core/src/utils/ratelimit.ts b/packages/core/src/utils/ratelimit.ts index d97506d9c89b..d568906a40be 100644 --- a/packages/core/src/utils/ratelimit.ts +++ b/packages/core/src/utils/ratelimit.ts @@ -52,13 +52,31 @@ export function isRateLimited(limits: RateLimits, dataCategory: DataCategory, no */ export function updateRateLimits( limits: RateLimits, - { statusCode, headers }: TransportMakeRequestResponse, + response: TransportMakeRequestResponse, now: number = safeDateNow(), ): RateLimits { const updatedRateLimits: RateLimits = { ...limits, }; + // The spec requires keeping the longer of two limits for the same category. This callback is the only code that + // writes a limit, so no update path can shorten an active one. + // https://develop.sentry.dev/sdk/foundations/transport/rate-limiting/#stage-2-determine-rate-limits + forEachRateLimit(response, now, (category, until) => { + updatedRateLimits[category] = Math.max(updatedRateLimits[category] || 0, until); + }); + + return updatedRateLimits; +} + +/** + * Calls `extendRateLimit` with the category and deadline of each rate limit in the response. + */ +function forEachRateLimit( + { statusCode, headers }: TransportMakeRequestResponse, + now: number, + extendRateLimit: (category: string, until: number) => void, +): void { // "The name is case-insensitive." // https://developer.mozilla.org/en-US/docs/Web/API/Headers/get const rateLimitHeader = headers?.['x-sentry-rate-limits']; @@ -84,25 +102,23 @@ export function updateRateLimits( const headerDelay = parseInt(retryAfter, 10); const delay = (!isNaN(headerDelay) ? headerDelay : 60) * 1000; // 60sec default if (!categories) { - updatedRateLimits.all = now + delay; + extendRateLimit('all', now + delay); } else { for (const category of categories.split(';')) { if (category === 'metric_bucket') { // namespaces will be present when category === 'metric_bucket' if (!namespaces || namespaces.split(';').includes('custom')) { - updatedRateLimits[category] = now + delay; + extendRateLimit(category, now + delay); } } else { - updatedRateLimits[category] = now + delay; + extendRateLimit(category, now + delay); } } } } } else if (retryAfterHeader) { - updatedRateLimits.all = now + parseRetryAfterHeader(retryAfterHeader, now); + extendRateLimit('all', now + parseRetryAfterHeader(retryAfterHeader, now)); } else if (statusCode === 429) { - updatedRateLimits.all = now + 60 * 1000; + extendRateLimit('all', now + 60 * 1000); } - - return updatedRateLimits; } diff --git a/packages/core/test/lib/utils/ratelimit.test.ts b/packages/core/test/lib/utils/ratelimit.test.ts index 772bd5e5c9d4..a104f8957238 100644 --- a/packages/core/test/lib/utils/ratelimit.test.ts +++ b/packages/core/test/lib/utils/ratelimit.test.ts @@ -110,15 +110,29 @@ describe('updateRateLimits()', () => { expect(updatedRateLimits.transaction).toEqual(15 * 1000); }); - test('should use last entry from multi `x-sentry-rate-limits` header for a given category', () => { + test('should use longest entry from multi `x-sentry-rate-limits` header for a given category', () => { const rateLimits: RateLimits = {}; const headers = { 'retry-after': null, - 'x-sentry-rate-limits': '13:error,15:transaction;error', + 'x-sentry-rate-limits': '15:error,13:transaction;error', }; const updatedRateLimits = updateRateLimits(rateLimits, { headers }, 0); expect(updatedRateLimits.error).toEqual(15 * 1000); - expect(updatedRateLimits.transaction).toEqual(15 * 1000); + expect(updatedRateLimits.transaction).toEqual(13 * 1000); + }); + + test('should keep a stored limit when a new one is shorter, and replace it when a new one is longer', () => { + const rateLimits: RateLimits = { error: 15 * 1000 }; + const shorterHeaders = { + 'retry-after': null, + 'x-sentry-rate-limits': '13:error', + }; + const longerHeaders = { + 'retry-after': null, + 'x-sentry-rate-limits': '20:error', + }; + expect(updateRateLimits(rateLimits, { headers: shorterHeaders }, 0).error).toEqual(15 * 1000); + expect(updateRateLimits(rateLimits, { headers: longerHeaders }, 0).error).toEqual(20 * 1000); }); test('should fallback to `all` if `x-sentry-rate-limits` header is missing a category', () => { From c5466269431d3112d2c704c1b92b0c81a3862469 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Mon, 16 Nov 2026 13:13:38 +0000 Subject: [PATCH 2/3] ref(core): Store transport rate limits as readonly The transport keeps the rate limits across responses. Typing them as readonly makes a direct write to a deadline fail to compile, so the transport can only replace them with the result of `updateRateLimits`, which never shortens a limit. This also removes the comment on the writer callback. --- packages/core/src/transports/base.ts | 2 +- packages/core/src/utils/ratelimit.ts | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/core/src/transports/base.ts b/packages/core/src/transports/base.ts index b88568de269a..ba2580b124aa 100644 --- a/packages/core/src/transports/base.ts +++ b/packages/core/src/transports/base.ts @@ -34,7 +34,7 @@ export function createTransport( options.bufferSize || DEFAULT_TRANSPORT_BUFFER_SIZE, ), ): Transport { - let rateLimits: RateLimits = {}; + let rateLimits: Readonly = {}; const flush = (timeout?: number): PromiseLike => buffer.drain(timeout); function recordItemLoss(reason: EventDropReason, [headers, payload]: EnvelopeItem, type: EnvelopeItemType): void { diff --git a/packages/core/src/utils/ratelimit.ts b/packages/core/src/utils/ratelimit.ts index d568906a40be..152a0f37fbc7 100644 --- a/packages/core/src/utils/ratelimit.ts +++ b/packages/core/src/utils/ratelimit.ts @@ -59,9 +59,6 @@ export function updateRateLimits( ...limits, }; - // The spec requires keeping the longer of two limits for the same category. This callback is the only code that - // writes a limit, so no update path can shorten an active one. - // https://develop.sentry.dev/sdk/foundations/transport/rate-limiting/#stage-2-determine-rate-limits forEachRateLimit(response, now, (category, until) => { updatedRateLimits[category] = Math.max(updatedRateLimits[category] || 0, until); }); From d00558e44622de679f24e202cfa2765c58b940c1 Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Tue, 17 Nov 2026 12:32:12 +0000 Subject: [PATCH 3/3] test(core): Put the longest rate limit between two shorter ones With the longest entry in the middle of the header, the test fails if the code keeps the first or the last entry for a category instead of the longest one. --- packages/core/test/lib/utils/ratelimit.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/test/lib/utils/ratelimit.test.ts b/packages/core/test/lib/utils/ratelimit.test.ts index a104f8957238..6897b3983d01 100644 --- a/packages/core/test/lib/utils/ratelimit.test.ts +++ b/packages/core/test/lib/utils/ratelimit.test.ts @@ -114,10 +114,10 @@ describe('updateRateLimits()', () => { const rateLimits: RateLimits = {}; const headers = { 'retry-after': null, - 'x-sentry-rate-limits': '15:error,13:transaction;error', + 'x-sentry-rate-limits': '15:error,22:error,13:transaction;error', }; const updatedRateLimits = updateRateLimits(rateLimits, { headers }, 0); - expect(updatedRateLimits.error).toEqual(15 * 1000); + expect(updatedRateLimits.error).toEqual(22 * 1000); expect(updatedRateLimits.transaction).toEqual(13 * 1000); });