Skip to content
Myr

fix(cookie): return the first cookie when there are multiple cookies with the same name (#4922)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(cookie): return the first cookie when there are multiple cookies with the same name * fix(cookie): avoid dropping cookies whose names collide with Object.prototype

authored by Taku Amano · committed by GitHub · parent 26f8c33 · Browse files · 2 files+59 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+59 added−3 removed

  • tests:+52 added−0 removed
  • other:+7 added−3 removed

2 files+59 added−3 removed

modified:src/utils/cookie.test.ts+52 added
99 unmodified lines
100 expect(cookie['dummy-cookie']).toBe('victim')101 expect(cookie['\u00a0dummy-cookie']).toBeUndefined()102 })103104 it('Should return the first value for duplicate cookie names', () => {105 const cookieString = 'a=first; a=last'106 const cookie: Cookie = parse(cookieString)107 expect(cookie['a']).toBe('first')108 })109110 it('Should return the first value for duplicate cookie names when parsed by name', () => {111 const cookieString = 'a=first; a=last'112 const cookie: Cookie = parse(cookieString, 'a')113 expect(cookie['a']).toBe('first')114 })115116 it('Should return consistent values between parse() and parse(name) for duplicates', () => {117 const cookieString = 'session=legit; other=x; session=evil'118 expect(parse(cookieString)['session']).toBe(parse(cookieString, 'session')['session'])119 })120121 it('Should parse cookies whose names collide with Object.prototype properties', () => {122 const cookieString = 'toString=foo; hasOwnProperty=bar; constructor=baz'123 const cookie: Cookie = parse(cookieString)124 expect(cookie['toString']).toBe('foo')125 expect(cookie['hasOwnProperty']).toBe('bar')126 expect(cookie['constructor']).toBe('baz')127 })128129 it('Should parse a cookie whose name collides with Object.prototype when parsed by name', () => {130 const cookieString = 'toString=foo'131 const cookie: Cookie = parse(cookieString, 'toString')132 expect(cookie['toString']).toBe('foo')133 })134135 it('Should keep first-wins behavior for cookies whose names collide with Object.prototype', () => {136 const cookieString = 'toString=first; toString=last'137 const cookie: Cookie = parse(cookieString)138 expect(cookie['toString']).toBe('first')139 })140141 it('Should parse signed cookies', async () => {142 const secret = 'secret ingredient'143 const cookieString =144 'yummy_cookie=choco.UdFR2rBpS1GsHfGlUiYyMIdqxqwuEgplyQIgTJgpGWY%3D; tasty_cookie = strawberry.I9qAeGQOvWjCEJgRPmrw90JjYpnnX2C9zoOiGSxh1Ig%3D'
78 unmodified lines
223 '\u00a0dummy-cookie=evil.UdFR2rBpS1GsHfGlUiYyMIdqxqwuEgplyQIgTJgpGWY%3D; dummy-cookie=choco.UdFR2rBpS1GsHfGlUiYyMIdqxqwuEgplyQIgTJgpGWY%3D'224 const cookie: SignedCookie = await parseSigned(cookieString, secret, 'dummy-cookie')225 expect(cookie['dummy-cookie']).toBe('choco')226 })227228 it('Should return the first signed cookie when there are duplicate names', async () => {229 const secret = 'secret ingredient'230 const cookieString =231 'yummy_cookie=choco.UdFR2rBpS1GsHfGlUiYyMIdqxqwuEgplyQIgTJgpGWY%3D; yummy_cookie=strawberry.I9qAeGQOvWjCEJgRPmrw90JjYpnnX2C9zoOiGSxh1Ig%3D'232 const cookie: SignedCookie = await parseSigned(cookieString, secret)233 expect(cookie['yummy_cookie']).toBe('choco')234 })235236 it('Should parse signed cookies whose names collide with Object.prototype properties', async () => {237 const secret = 'secret ingredient'238 const cookieString = 'toString=choco.UdFR2rBpS1GsHfGlUiYyMIdqxqwuEgplyQIgTJgpGWY%3D'239 const cookie: SignedCookie = await parseSigned(cookieString, secret)240 expect(cookie['toString']).toBe('choco')241 })242})243244describe('Set cookie', () => {245 it('Should serialize cookie', () => {
160 unmodified lines
modified:src/utils/cookie.ts+7 added-3 removed
103 unmodified lines
104 // Fast-path: return immediately if the demanded-key is not in the cookie string105 return {}106 }107 const pairs = cookie.split(';')108 const parsedCookie: Cookie = {}108 const parsedCookie: Cookie = Object.create(null)109 for (const pairStr of pairs) {110 const valueStartPos = pairStr.indexOf('=')111 if (valueStartPos === -1) {112 continue113 }114115 const cookieName = trimCookieWhitespace(pairStr.substring(0, valueStartPos))116 if ((name && name !== cookieName) || !validCookieNameRegEx.test(cookieName)) {116 if (117 (name && name !== cookieName) ||118 !validCookieNameRegEx.test(cookieName) ||119 cookieName in parsedCookie120 ) {121 continue122 }123124 let cookieValue = trimCookieWhitespace(pairStr.substring(valueStartPos + 1))
16 unmodified lines
141 cookie: string,142 secret: string | BufferSource,143 name?: string144): Promise<SignedCookie> => {141 const parsedCookie: SignedCookie = {}145 const parsedCookie: SignedCookie = Object.create(null)146 const secretKey = await getCryptoKey(secret)147148 for (const [key, value] of Object.entries(parse(cookie, name))) {149 const signatureStartPos = value.lastIndexOf('.')
125 unmodified lines