Skip to content

fix(ratelimiter): split each rate limit at the comma before reading its fields - #2245

Merged
jpnurmi merged 2 commits into
getsentry:masterfrom
Denimworld12:fix/rate-limit-header-without-scope
Oct 26, 2026
Merged

jpnurmi merged 2 commits into
getsentry:masterfrom
Denimworld12:fix/rate-limit-header-without-scope

Conversation

@Denimworld12

Copy link
Copy Markdown
Contributor

Summary

The scope field of an X-Sentry-Rate-Limits entry is optional, but the parser looked for the end of the categories with the next : anywhere in the remainder of the header. A limit that left the scope out therefore ran past its own comma and swallowed the limit after it.

X-Sentry-Rate-Limits: 60:error, 120:session, 30:transaction

On master only the transaction limit applies. For the first entry the parser takes everything up to the : before session as the category list, so it reads error, 120, which matches no category and is silently discarded. The same happens to session. The SDK carries on sending errors and sessions that the server has just asked it to stop sending.

Headers where every limit carries a scope — 120:error:project:reason, 60:session:foo — are unaffected, which is why the existing tests pass: all of them supply a scope.

Fix

Split each limit at the comma first, then read its fields, which is the order the other SDKs use. sentry-python:

for limit in header.split(","):
    parameters = limit.strip().split(":")
    retry_after_val, categories = parameters[:2]

The category and ; handling is unchanged.

Tests

Two cases in tests/unit/test_ratelimiter.c, both failing on master and passing here:

  • rate_limit_header_without_scope — 60:error, 120:session, 30:transaction, asserting all three apply.
  • rate_limit_header_mixed_scopes — 60:replay, 120:feedback:organization, 30:error;session, so a scoped and an unscoped limit sit next to each other, and asserting that nothing becomes a blanket limit.

make test-unit is 616 passed (614 before, plus these two). On master the pair fails exactly as described. clang-format 20.1.5 clean on both files.

Note

I kept the existing behaviour of returning false on a malformed entry rather than skipping it and carrying on, since that is a separate decision. sentry-python skips the bad entry and keeps parsing the rest — happy to match that instead if you prefer.

…ts fields

The scope field of an X-Sentry-Rate-Limits entry is optional, but the parser
looked for the end of the categories with the next ':' anywhere in the rest of
the header. A limit that left the scope out therefore ran past its own comma
and swallowed the limit after it, so its categories matched nothing and were
never rate limited.

For "60:error, 120:session, 30:transaction" only the transaction limit applied;
error and session were dropped and the SDK kept sending both. Splitting at the
comma first, the way the other SDKs do, keeps each limit to its own fields.
@codecov

codecov Bot commented Oct 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.52%. Comparing base (8d90c56) to head (4c45ac6).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2245      +/-   ##
==========================================
- Coverage   75.63%   75.52%   -0.12%     
==========================================
  Files         103      103              
  Lines       28263    28263              
  Branches     5180     5180              
==========================================
- Hits        21378    21345      -33     
- Misses       5540     5577      +37     
+ Partials     1345     1341       -4     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jpnurmi jpnurmi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you, LGTM! 👍

@jpnurmi
jpnurmi merged commit 2794b86 into getsentry:master Oct 26, 2026
104 of 105 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants