Skip to content

Commit a586cd7

Browse files
authored
Merge commit from fork
1 parent 48fa223 commit a586cd7

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

src/utils/cookie.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,18 @@ describe('Set cookie', () => {
294294
}).toThrowError('path must not contain ";", "\\r", or "\\n"')
295295
})
296296

297+
it('Should throw Error for invalid cookie name', () => {
298+
expect(() => {
299+
serialize('legit\r\nX-Injected: evil', 'value')
300+
}).toThrowError('Invalid cookie name')
301+
expect(() => {
302+
serialize('bad;name', 'value')
303+
}).toThrowError('Invalid cookie name')
304+
expect(() => {
305+
serialize('bad=name', 'value')
306+
}).toThrowError('Invalid cookie name')
307+
})
308+
297309
it('Should serialize cookie with lowercase priority values', () => {
298310
const lowSerialized = serialize('test_cookie', 'value', {
299311
priority: 'low',

src/utils/cookie.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ export const parseSigned = async (
139139
}
140140

141141
const _serialize = (name: string, value: string, opt: CookieOptions = {}): string => {
142+
if (!validCookieNameRegEx.test(name)) {
143+
throw new Error('Invalid cookie name')
144+
}
145+
142146
let cookie = `${name}=${value}`
143147

144148
if (name.startsWith('__Secure-') && !opt.secure) {

0 commit comments

Comments
 (0)