-
Notifications
You must be signed in to change notification settings - Fork 26.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Split Set-Cookie header correctly #30560
Merged
+235
−12
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
faf8427
Split Set-Cookie header correctly
karaggeorge b6f5d2d
Add unit tests for splitCookiesString
karaggeorge 741aea0
Merge branch 'canary' into fix-set-cookie-header
karaggeorge 056ca42
Fix failing test and re-use toNodeHeaders
karaggeorge dae9a97
Merge branch 'fix-set-cookie-header' of https://github.com/vercel/nex…
karaggeorge 40f729e
Merge branch 'canary' into fix-set-cookie-header
kodiakhq[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,3 +45,79 @@ export function toNodeHeaders(headers?: Headers): NodeHeaders { | |
} | ||
return result | ||
} | ||
|
||
/* | ||
Set-Cookie header field-values are sometimes comma joined in one string. This splits them without choking on commas | ||
that are within a single set-cookie field-value, such as in the Expires portion. | ||
This is uncommon, but explicitly allowed - see https://tools.ietf.org/html/rfc2616#section-4.2 | ||
Node.js does this for every header *except* set-cookie - see https://github.com/nodejs/node/blob/d5e363b77ebaf1caf67cd7528224b651c86815c1/lib/_http_incoming.js#L128 | ||
React Native's fetch does this for *every* header, including set-cookie. | ||
|
||
Based on: https://github.com/google/j2objc/commit/16820fdbc8f76ca0c33472810ce0cb03d20efe25 | ||
Credits to: https://github.com/tomball for original and https://github.com/chrusart for JavaScript implementation | ||
*/ | ||
export function splitCookiesString(cookiesString: string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a bunch of unit tests for this function so we can refactor in the future? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added 👍 |
||
var cookiesStrings = [] | ||
var pos = 0 | ||
var start | ||
var ch | ||
var lastComma | ||
var nextStart | ||
var cookiesSeparatorFound | ||
|
||
function skipWhitespace() { | ||
while (pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))) { | ||
pos += 1 | ||
} | ||
return pos < cookiesString.length | ||
} | ||
|
||
function notSpecialChar() { | ||
ch = cookiesString.charAt(pos) | ||
|
||
return ch !== '=' && ch !== ';' && ch !== ',' | ||
} | ||
|
||
while (pos < cookiesString.length) { | ||
start = pos | ||
cookiesSeparatorFound = false | ||
|
||
while (skipWhitespace()) { | ||
ch = cookiesString.charAt(pos) | ||
if (ch === ',') { | ||
// ',' is a cookie separator if we have later first '=', not ';' or ',' | ||
lastComma = pos | ||
pos += 1 | ||
|
||
skipWhitespace() | ||
nextStart = pos | ||
|
||
while (pos < cookiesString.length && notSpecialChar()) { | ||
pos += 1 | ||
} | ||
|
||
// currently special character | ||
if (pos < cookiesString.length && cookiesString.charAt(pos) === '=') { | ||
// we found cookies separator | ||
cookiesSeparatorFound = true | ||
// pos is inside the next cookie, so back up and return it. | ||
pos = nextStart | ||
cookiesStrings.push(cookiesString.substring(start, lastComma)) | ||
start = pos | ||
} else { | ||
// in param ',' or param separator ';', | ||
// we continue from that comma | ||
pos = lastComma + 1 | ||
} | ||
} else { | ||
pos += 1 | ||
} | ||
} | ||
|
||
if (!cookiesSeparatorFound || pos >= cookiesString.length) { | ||
cookiesStrings.push(cookiesString.substring(start, cookiesString.length)) | ||
} | ||
} | ||
|
||
return cookiesStrings | ||
} |
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need this added to
toNodeHeaders()
too:next.js/packages/next/server/web/utils.ts
Lines 41 to 43 in 3828ebe