Skip to content

Commit d85909e

Browse files
authored
refactor: use parseCookies method from Next.js (#12599)
Following up on #12515, we could instead use the same `parseCookies` method that Next.js uses. This handles a few edge-cases differently: - correctly strips whitespace - parses attributes without explicit values I think it's a good idea to match the behavior of Next.js as close as possible here. [This](vercel/edge-runtime#374) is a good example of how the Next.js behavior behaves differently. ## Example Input: `'my_value=true; Secure; HttpOnly'` Previous Output: ``` Map(3) { 'my_value' => 'true', 'Secure' => '', 'HttpOnly' => '', } ``` New Output: ``` Map(3) { 'my_value' => 'true', 'Secure' => 'true', 'HttpOnly' => 'true' } ```
1 parent 699af8d commit d85909e

File tree

2 files changed

+84
-16
lines changed

2 files changed

+84
-16
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { parseCookies } from './cookies.js'
2+
3+
describe('parseCookies', () => {
4+
it('parses cookie attributes without values', () => {
5+
const fakeHeaders = new Map()
6+
fakeHeaders.set('Cookie', 'my_value=true; Secure; HttpOnly')
7+
8+
const parsed = parseCookies(fakeHeaders as unknown as Request['headers'])
9+
10+
expect(parsed.get('my_value')).toBe('true')
11+
expect(parsed.get('Secure')).toBe('true')
12+
expect(parsed.get('HttpOnly')).toBe('true')
13+
expect(parsed.size).toBe(3)
14+
})
15+
it('strips whitespace', () => {
16+
const fakeHeaders = new Map()
17+
fakeHeaders.set('Cookie', 'my_value=true; ')
18+
19+
const parsed = parseCookies(fakeHeaders as unknown as Request['headers'])
20+
21+
expect(parsed.get('my_value')).toBe('true')
22+
expect(parsed.size).toBe(1)
23+
})
24+
25+
it('ensure invalid cookies are ignored', () => {
26+
const fakeHeaders = new Map()
27+
fakeHeaders.set('Cookie', 'my_value=true; invalid_cookie=%E0%A4%A')
28+
29+
const parsed = parseCookies(fakeHeaders as unknown as Request['headers'])
30+
31+
expect(parsed.get('my_value')).toBe('true')
32+
expect(parsed.size).toBe(1)
33+
})
34+
35+
it('ensure empty map is returned if there are no cookies', () => {
36+
const fakeHeaders = new Map()
37+
38+
const parsed = parseCookies(fakeHeaders as unknown as Request['headers'])
39+
40+
expect(parsed.size).toBe(0)
41+
})
42+
})

packages/payload/src/auth/cookies.ts

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,24 +185,50 @@ export const generateExpiredPayloadCookie = <T extends Omit<GeneratePayloadCooki
185185
})
186186
}
187187

188-
export const parseCookies = (headers: Request['headers']): Map<string, string> => {
189-
const cookieMap = new Map<string, string>()
188+
export function parseCookies(headers: Request['headers']) {
189+
// Taken from https://github.com/vercel/edge-runtime/blob/main/packages/cookies/src/serialize.ts
190+
191+
/*
192+
The MIT License (MIT)
193+
194+
Copyright (c) 2024 Vercel, Inc.
195+
196+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
197+
198+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
199+
200+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
201+
*/
202+
const map = new Map<string, string>()
203+
190204
const cookie = headers.get('Cookie')
191205

192-
if (cookie) {
193-
cookie.split(';').forEach((cookie) => {
194-
const parts = cookie.split('=')
195-
const key = parts.shift()?.trim()
196-
const encodedValue = parts.join('=')
197-
198-
try {
199-
const decodedValue = decodeURI(encodedValue)
200-
cookieMap.set(key, decodedValue)
201-
} catch (ignore) {
202-
return null
203-
}
204-
})
206+
if (!cookie) {
207+
return map
208+
}
209+
210+
for (const pair of cookie.split(/; */)) {
211+
if (!pair) {
212+
continue
213+
}
214+
215+
const splitAt = pair.indexOf('=')
216+
217+
// If the attribute doesn't have a value, set it to 'true'.
218+
if (splitAt === -1) {
219+
map.set(pair, 'true')
220+
continue
221+
}
222+
223+
// Otherwise split it into key and value and trim the whitespace on the
224+
// value.
225+
const [key, value] = [pair.slice(0, splitAt), pair.slice(splitAt + 1)]
226+
try {
227+
map.set(key, decodeURIComponent(value ?? 'true'))
228+
} catch {
229+
// ignore invalid encoded values
230+
}
205231
}
206232

207-
return cookieMap
233+
return map
208234
}

0 commit comments

Comments
 (0)