fix: only discard prepared headers for error responses#1486
Merged
Conversation
`prepareResponse` used `!val.ok` to decide whether to merge prepared
headers into a returned `Response`, which dropped them for every non-2xx
status — including intentional 3xx redirects and deliberate 4xx/5xx
responses. `setCookie()` before `return new Response(null, {status: 302})`
silently lost the cookie, while `return redirect(event, "/x")` kept it.
Scope the guard to error statuses and honor `event.res.errHeaders` there,
matching what `errorResponse()` already does for thrown errors:
- `< 400`: merge all prepared headers (fixes redirects and cookies)
- `>= 400`: merge only `errHeaders` (fixes CORS headers being dropped on a
returned 401, which previously survived only when the error was thrown)
Success-only headers (caching, content negotiation, ...) still never leak
into error responses, preserving the intent of #1228.
Closes #1481
Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
📝 WalkthroughWalkthroughChangesResponse header behavior
Bundle size baselines
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related issues
Possibly related PRs
Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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 |
pi0
approved these changes
Jul 18, 2026
This was referenced Jul 19, 2026
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 #1481
Problem
prepareResponseguarded header merging with!val.ok(src/response.ts:118), butResponse.okis false for all non-2xx statuses — not just errors. So prepared headers were dropped for intentional redirects and deliberate 4xx too:return redirect(event, "/login")kept the cookie (it returns anHTTPResponse, which takes the merging path), so the two forms disagreed.While confirming the report I found a second gap:
event.res.errHeaders— the documented escape hatch for headers that must survive an error render, whichhandleCorswrites to — was also dropped by this fast path. CORS headers survived a thrown 401 but vanished on a returned one:Fix
Scope the guard to error statuses and honor
errHeadersthere, matching whaterrorResponse()already does:< 400— merge all prepared headers (fixes redirects/cookies; makesredirect()and a native302behave alike)>= 400— merge onlyerrHeaders(fixes CORS on a returned 401)Success-only headers (caching, content negotiation, ...) still never leak into error responses, so #1228's intent is preserved. A custom error render returned from
onErroris still left untouched.Behavior change
Returning a non-2xx
Responseno longer drops everything staged onevent.res. Code that relied onevent.res.headersbeing discarded for a 3xx will now see those headers on the response; error statuses are unchanged except thaterrHeadersare now applied.Tests
Three regression tests in
test/app.test.ts, all failing onmain:ResponsekeepssetCookieResponsedropscache-controlbut keepserrHeadersNote on bundle size
The fix costs +19 bytes minified (+8 gzip), and both budgets were at their ceiling —
defineHandler6396→6415 against a 6400 limit, andH3Core7281→7300 against a 7300 limit (i.e. passing with zero slack, which would have broken the next PR regardless). Raised to 6450/7350.I tried hoisting the
preparedResread above the error branch to dedupe thekEventReslookup and buy the bytes back; it came out a byte worse, so I reverted it. Happy to pay for the bytes elsewhere if you'd prefer the budgets held.🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
Documentation
Tests