fix: preserve multiple set-cookie headers#60
Conversation
|
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 (1)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughmergeHeaders now builds merged headers by copying all response headers except ChangesHeader Merge Logic Refinement
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
src/utils.tsESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox. 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 |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/utils.ts (1)
117-122:⚠️ Potential issue | 🟠 MajorAra ara~ Did you forget something, baka? ♡
There's an inconsistency between how the
Headersbranch and the plain object branch handleset-cookie:
Missing precedence guard: The
Headersbranch (lines 109-110) skipssetHeaders's cookies ifresponseHeadersalready has set-cookie. But this plain object branch just blindly appends without checking~! That's not very smart, is it? (´・ω・`)Array handling: According to
src/ws.ts,set.headers['set-cookie']can be an array. Appending an array directly will stringify it incorrectly instead of preserving individual cookies~ How embarrassing ♡🍪 Here's a fix, since you obviously need help~
else for (const key in setHeaders) - if (key === 'set-cookie') - headers.append(key, setHeaders[key] as any) - else if (!responseHeaders.has(key)) + if (key === 'set-cookie') { + if (headers.has('set-cookie')) continue + + const cookies = setHeaders[key] + if (Array.isArray(cookies)) + for (const cookie of cookies) + headers.append('set-cookie', cookie) + else + headers.append('set-cookie', cookies as string) + } else if (!responseHeaders.has(key)) headers.set(key, setHeaders[key] as any)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/utils.ts` around lines 117 - 122, The plain-object branch that iterates setHeaders currently appends 'set-cookie' entries without the precedence check and without handling arrays; update the loop in src/utils.ts so that when key === 'set-cookie' you first check responseHeaders.has('set-cookie') and skip appending if true (matching the Headers-branch behavior), and when appending handle both string and string[] values by appending each cookie individually (use Array.isArray on setHeaders['set-cookie'] to decide whether to iterate and call headers.append for each item or append the single value); leave the non-'set-cookie' path unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Outside diff comments:
In `@src/utils.ts`:
- Around line 117-122: The plain-object branch that iterates setHeaders
currently appends 'set-cookie' entries without the precedence check and without
handling arrays; update the loop in src/utils.ts so that when key ===
'set-cookie' you first check responseHeaders.has('set-cookie') and skip
appending if true (matching the Headers-branch behavior), and when appending
handle both string and string[] values by appending each cookie individually
(use Array.isArray on setHeaders['set-cookie'] to decide whether to iterate and
call headers.append for each item or append the single value); leave the
non-'set-cookie' path unchanged.
Problem
The Node adapter rebuilds
responseHeaderswith:This collapses duplicate header names, which is especially problematic for
Set-Cookie.In OAuth callback flows, downstream handlers can return multiple
Set-Cookieheaders.On Bun this worked, but on the Node adapter one of the cookies could be dropped during header merging.
In my reproduction this broke Better Auth on the OAuth callback route:
codeandstateFix
Preserve multiple
Set-Cookieheaders when merging response headers.Related
This looks related to elysiajs/elysia#1547, which reports another
Set-Cookieloss path in Elysia.Summary by CodeRabbit