Skip to content
Myr

fix(cookie): use `tryDecode` when parsing cookie (#4240)

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

* fix(cookie): use `tryDecode` when parsing cookie * add test * typo

authored by Yusuke Wada · committed by GitHub · parent 3e51a1f · Browse files · 2 files+9 added−2 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+9 added−2 removed

  • tests:+6 added−0 removed
  • other:+3 added−2 removed

2 files+9 added−2 removed

modified:src/utils/cookie.test.ts+6 added
16 unmodified lines
17 expect(cookie['tasty_cookie']).toBe(' strawberry ')18 expect(cookie['best_cookie']).toBe(' sugar ')19 })2021 it('Should not throw a URIError when parsing an invalid string', () => {22 const cookieString = 'yummy_cookie="choco%2";'23 const cookie: Cookie = parse(cookieString)24 expect(cookie['yummy_cookie']).toBe('choco%2')25 })2627 it('Should parse empty cookies', () => {28 const cookie: Cookie = parse('')29 expect(Object.keys(cookie).length).toBe(0)30 })
242 unmodified lines
modified:src/utils/cookie.ts+3 added-2 removed
1 unmodified line
2 * @module3 * Cookie utility.4 */56import { decodeURIComponent_ } from './url'6import { decodeURIComponent_, tryDecode } from './url'78export type Cookie = Record<string, string>9export type SignedCookie = Record<string, string | false>10
89 unmodified lines
100 if (cookieValue.startsWith('"') && cookieValue.endsWith('"')) {101 cookieValue = cookieValue.slice(1, -1)102 }103 if (validCookieValueRegEx.test(cookieValue)) {104 parsedCookie[cookieName] = decodeURIComponent_(cookieValue)104 parsedCookie[cookieName] =105 cookieValue.indexOf('%') !== -1 ? tryDecode(cookieValue, decodeURIComponent_) : cookieValue106 if (name) {107 // Fast-path: return only the demanded-key immediately. Other keys are not needed.108 break109 }
135 unmodified lines