Skip to content
Myr

Merge commit from fork

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

authored by Taku Amano · committed by GitHub · parent a586cd7 · Browse files · 2 files+55 added−5 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+55 added−5 removed

  • tests:+28 added−0 removed
  • other:+27 added−5 removed

2 files+55 added−5 removed

modified:src/utils/cookie.test.ts+28 added
7 unmodified lines
8 expect(cookie['yummy_cookie']).toBe('choco')9 expect(cookie['tasty_cookie']).toBe('strawberry')10 })1112 it('Should trim only SP and HTAB around cookie pairs', () => {13 const cookieString = '\tyummy_cookie=choco;\t tasty_cookie = strawberry \t'14 const cookie: Cookie = parse(cookieString)15 expect(cookie['yummy_cookie']).toBe('choco')16 expect(cookie['tasty_cookie']).toBe('strawberry')17 })1819 it('Should parse quoted cookie values', () => {20 const cookieString =21 'yummy_cookie="choco"; tasty_cookie = " strawberry " ; best_cookie="%20sugar%20";'22 const cookie: Cookie = parse(cookieString)
64 unmodified lines
87 expect(cookie['tasty_cookie']).toBe('strawberry')88 expect(cookie['best_cookie\\']).toBeUndefined()89 })9091 it('Should ignore NBSP-prefixed cookie names when parsing one cookie by name', () => {92 const cookieString = '\u00a0dummy-cookie=evil; dummy-cookie=victim'93 const cookie: Cookie = parse(cookieString, 'dummy-cookie')94 expect(cookie['dummy-cookie']).toBe('victim')95 })9697 it('Should not collapse NBSP-prefixed cookie names when parsing all cookies', () => {98 const cookieString = 'dummy-cookie=victim; \u00a0dummy-cookie=evil'99 const cookie: Cookie = parse(cookieString)100 expect(cookie['dummy-cookie']).toBe('victim')101 expect(cookie['\u00a0dummy-cookie']).toBeUndefined()102 })103104 it('Should parse signed cookies', async () => {105 const secret = 'secret ingredient'106 const cookieString =107 'yummy_cookie=choco.UdFR2rBpS1GsHfGlUiYyMIdqxqwuEgplyQIgTJgpGWY%3D; tasty_cookie = strawberry.I9qAeGQOvWjCEJgRPmrw90JjYpnnX2C9zoOiGSxh1Ig%3D'
70 unmodified lines
178 expect(cookie['yummy_cookie']).toBeUndefined()179 expect(cookie['tasty_cookie']).toBe('strawberry')180 expect(cookie['great_cookie']).toBeUndefined()181 })182183 it('Should ignore NBSP-prefixed signed cookie names when parsing one cookie by name', async () => {184 const secret = 'secret ingredient'185 const cookieString =186 '\u00a0dummy-cookie=evil.UdFR2rBpS1GsHfGlUiYyMIdqxqwuEgplyQIgTJgpGWY%3D; dummy-cookie=choco.UdFR2rBpS1GsHfGlUiYyMIdqxqwuEgplyQIgTJgpGWY%3D'187 const cookie: SignedCookie = await parseSigned(cookieString, secret, 'dummy-cookie')188 expect(cookie['dummy-cookie']).toBe('choco')189 })190})191192describe('Set cookie', () => {193 it('Should serialize cookie', () => {
160 unmodified lines
modified:src/utils/cookie.ts+27 added-5 removed
74 unmodified lines
75// note: the spec also prohibits comma and space, but we allow both since they are very common in the real world76// (see: https://github.com/golang/go/issues/7243)77const validCookieValueRegEx = /^[ !#-:<-[\]-~]*$/7879const trimCookieWhitespace = (value: string): string => {80 let start = 081 let end = value.length8283 while (start < end) {84 const charCode = value.charCodeAt(start)85 if (charCode !== 0x20 && charCode !== 0x09) {86 break87 }88 start++89 }9091 while (end > start) {92 const charCode = value.charCodeAt(end - 1)93 if (charCode !== 0x20 && charCode !== 0x09) {94 break95 }96 end--97 }9899 return start === 0 && end === value.length ? value : value.slice(start, end)100}101102export const parse = (cookie: string, name?: string): Cookie => {103 if (name && cookie.indexOf(name) === -1) {104 // Fast-path: return immediately if the demanded-key is not in the cookie string105 return {}106 }84 const pairs = cookie.trim().split(';')107 const pairs = cookie.split(';')108 const parsedCookie: Cookie = {}86 for (let pairStr of pairs) {87 pairStr = pairStr.trim()109 for (const pairStr of pairs) {110 const valueStartPos = pairStr.indexOf('=')111 if (valueStartPos === -1) {112 continue113 }11493 const cookieName = pairStr.substring(0, valueStartPos).trim()115 const cookieName = trimCookieWhitespace(pairStr.substring(0, valueStartPos))116 if ((name && name !== cookieName) || !validCookieNameRegEx.test(cookieName)) {117 continue118 }11998 let cookieValue = pairStr.substring(valueStartPos + 1).trim()120 let cookieValue = trimCookieWhitespace(pairStr.substring(valueStartPos + 1))121 if (cookieValue.startsWith('"') && cookieValue.endsWith('"')) {122 cookieValue = cookieValue.slice(1, -1)123 }124 if (validCookieValueRegEx.test(cookieValue)) {
146 unmodified lines