fix(cors): expose Retry-After so browser JS can honor 429 back-off (#322)#323
Merged
Conversation
express-rate-limit (standardHeaders: true) sets `Retry-After` on
the 429 response — the integer-seconds the client should wait
before retrying (RFC 7231 §7.1.3). Browser JS reading the
response across CORS can't see headers outside the CORS-safelisted
set unless the server explicitly exposes them via
`Access-Control-Expose-Headers`.
`Retry-After` is NOT on the CORS safelist (only Cache-Control,
Content-Language, Content-Length, Content-Type, Expires,
Last-Modified, Pragma are), so a browser-side `fetch()` getting
a 429 reads `headers.get('Retry-After')` as null and falls back
to a fixed-delay retry instead of honoring the server's
back-off. Add it to the existing exposedHeaders list alongside
the other RateLimit-* headers.
Update tests:
- cors-expose-headers.test.js mirrors the new entry.
- rate-limit.test.js asserts the 429 response actually carries
`Retry-After` as a positive-integer string (defensive — the
server-side path could change without the CORS list catching
it).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #322.
Summary
Adds
'Retry-After'to server.jsexposedHeadersand mirrors the entry in the CORS expose-headers test. Adds an assertion in the rate-limit test that the 429 response actually carries the header (positive-integer-seconds per RFC 7231 §7.1.3).Without this, browser JS reading a cross-origin 429 sees
Retry-Afteras null and can't honor the server's back-off.Test plan
npm run lint && npm test— 782 passing (1 new assertion added to existing 429 test rather than a new test block).Proudly Made in Nebraska. Go Big Red! 🌽 https://xkcd.com/2347/