Skip to content
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

Fix splitting of concatenated Set-Cookie headers in edge-runtime package #896

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/old-houses-film.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@edge-runtime/cookies': minor
'edge-runtime': path
---

feat(cookies): expose splitCookiesString function
fix: splitting cookies when concatenated
7 changes: 6 additions & 1 deletion packages/cookies/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export type { CookieListItem, RequestCookie, ResponseCookie } from './types'
export { RequestCookies } from './request-cookies'
export { ResponseCookies } from './response-cookies'
export { stringifyCookie, parseCookie, parseSetCookie } from './serialize'
export {
stringifyCookie,
parseCookie,
parseSetCookie,
splitCookiesString,
} from './serialize'
1 change: 1 addition & 0 deletions packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"web"
],
"dependencies": {
"@edge-runtime/cookies": "workspace:*",
"@edge-runtime/format": "workspace:*",
"@edge-runtime/ponyfill": "workspace:*",
"@edge-runtime/vm": "workspace:*",
Expand Down
7 changes: 5 additions & 2 deletions packages/runtime/src/server/create-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { EdgeRuntime } from '../edge-runtime'
import type { IncomingMessage, ServerResponse } from 'http'
import type { Logger, NodeHeaders } from '../types'
import type { EdgeContext } from '@edge-runtime/vm'
import { splitCookiesString } from '@edge-runtime/cookies'
import { getClonableBodyStream, pipeBodyStreamToResponse } from './body-streams'
import prettyMs from 'pretty-ms'
import timeSpan from 'time-span'
Expand Down Expand Up @@ -112,13 +113,15 @@ function toRequestInitHeaders(req: IncomingMessage): RequestInit['headers'] {

/**
* Transforms WHATWG Headers into a Node Headers shape. Copies all items but
* does a special case for Set-Cookie using the [`getSetCookie`](https://developer.mozilla.org/en-US/docs/Web/API/Headers/getSetCookie) method.
* does a special case for 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.
*/
function toNodeHeaders(headers?: Headers): NodeHeaders {
const result: NodeHeaders = {}
if (headers) {
for (const [key, value] of headers.entries()) {
result[key] = key === 'set-cookie' ? headers.getSetCookie() : value
result[key] =
key?.toLowerCase() === 'set-cookie' ? splitCookiesString(value) : value
}
}
return result
Expand Down
11 changes: 7 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.