fix(cors): preserve CORS headers on error responses#1352
Conversation
set CORS headers on both `event.res.headers` and `event.res.errHeaders` so they are merged into error responses when an HTTPError is thrown. closes #1301
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (7)
📝 WalkthroughWalkthroughAdds per-event error-response header storage and merges those headers into error responses; updates CORS helpers to write into both normal and error headers; documents the new recommended patterns; and adds a unit test plus a minor bundle-size test threshold change. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
commit: |
mention that prepared headers are discarded on errors and that `event.res.errHeaders` should be used for headers that need to persist on error responses (e.g., CORS headers).
clarify that explicit error headers via `HTTPError` constructor is the preferred approach, and `event.res.errHeaders` is a last resort for implicit headers like CORS.
Summary
When an
HTTPErroris thrown after CORS headers have been set viahandleCors(), the CORS headers were dropped from the error response. This caused browsers to hide error details from the client, since the response lacked the requiredaccess-control-allow-originheader.Root cause:
event.res.headers(prepared headers) are only merged into successful responses. When an error is thrown, the error response is built separately and these headers are discarded.Fix: Use
event.res.errHeaders— a dedicated header store that persists into error responses:appendCorsHeaders,appendCorsPreflightHeaders) now set headers on bothevent.res.headersandevent.res.errHeaderserrorResponse()inresponse.tsmergeserrHeadersinto the error responseevent.res.headers(discarded on error) andevent.res.errHeaders(preserved on error)Closes #1301
Test plan
preserves CORS headers on HTTPError responses🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Documentation
Tests