Skip to content
Myr

fix(cookie): allow parsing signed cookies with empty string values (#5246)

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

authored by Nanasi · committed by GitHub · parent c409d85 · Browse files · 3 files+20 added−4 removed
This commit

State

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

Diff stats

3 files+20 added−4 removed

  • tests:+19 added−3 removed
  • other:+1 added−1 removed

3 files+20 added−4 removed

modified:src/helper/cookie/index.test.ts+12 added-3 removed
65 unmodified lines
66 )67 const res = new Response('Signed fortune cookie')68 if (typeof fortuneCookie !== 'undefined' && typeof fruitCookie !== 'undefined') {69 // just examples for tests sake70 res.headers.set('Fortune-Cookie', fortuneCookie || 'INVALID')71 res.headers.set('Fruit-Cookie', fruitCookie || 'INVALID')70 res.headers.set('Fortune-Cookie', fortuneCookie === false ? 'INVALID' : fortuneCookie)71 res.headers.set('Fruit-Cookie', fruitCookie === false ? 'INVALID' : fruitCookie)72 }73 return res74 })75
2 unmodified lines
78 const fortuneCookie = await getSignedCookie(c, secret, 'fortune_cookie')79 const res = new Response('Signed fortune cookie')80 if (typeof fortuneCookie !== 'undefined') {81 // just an example for tests sake82 res.headers.set('Fortune-Cookie', fortuneCookie || 'INVALID')82 res.headers.set('Fortune-Cookie', fortuneCookie === false ? 'INVALID' : fortuneCookie)83 }84 return res85 })86
36 unmodified lines
123 const res = await app.request(req)124 expect(res.headers.get('Fortune-Cookie')).toBe('INVALID')125 })126127 it('Get signed cookie with empty string value', async () => {128 const secret = 'secret lucky charm'129 const emptySignedCookie = await generateSignedCookie('fortune_cookie', '', secret)130 const req = new Request('http://localhost/cookie-signed-get-one')131 req.headers.set('Cookie', emptySignedCookie)132 const res = await app.request(req)133 expect(res.headers.get('Fortune-Cookie')).toBe('')134 })135136 describe('get null if the value is undefined', () => {137 const app = new Hono()138139 app.get('/cookie', (c) => {
371 unmodified lines
modified:src/utils/cookie.test.ts+7 added
199 unmodified lines
200 expect(cookie['yummy_cookie']).toBe('choco')201 expect(cookie['tasty_cookie']).toBe(false)202 })203204 it('Should parse signed cookie with empty string value', async () => {205 const secret = 'secret ingredient'206 const cookieString = await serializeSigned('empty_cookie', '', secret)207 const cookie: SignedCookie = await parseSigned(cookieString, secret)208 expect(cookie['empty_cookie']).toBe('')209 })210211 it('Should parse signed cookies and return "false" for corrupt signature', async () => {212 const secret = 'secret ingredient'213 // yummy_cookie has corrupt signature (i.e. invalid base64 encoding)214 // best_cookie has a shape that matches the signature format but isn't actually a signature
246 unmodified lines
modified:src/utils/cookie.ts+1 added-1 removed
151 unmodified lines
152 const secretKey = await getCryptoKey(secret)153154 for (const [key, value] of Object.entries(parse(cookie, name))) {155 const signatureStartPos = value.lastIndexOf('.')156 if (signatureStartPos < 1) {156 if (signatureStartPos < 0) {157 continue158 }159160 const signedValue = value.substring(0, signatureStartPos)
120 unmodified lines