Skip to content
Myr

cookie.test.ts

This file

Path

src/utils/cookie.test.ts

313 lines · 13.0 KiB · TypeScript

Last changed by

Merge commit from forkTaku Amanobefore adoption44ae0c8

History of this file

Take it

The walk stopped after 200 commits: lines older than 82bdc4d point at it.

4.9.0Yusuke Wadabefore adoption82bdc4d

lines 1–272why, one step back
1import type { Cookie, SignedCookie } from './cookie'
2import { parse, parseSigned, serialize, serializeSigned } from './cookie'34describe('Parse cookie', () => {5 it('Should parse cookies', () => {6 const cookieString = 'yummy_cookie=choco; tasty_cookie = strawberry '7 const cookie: Cookie = parse(cookieString)8 expect(cookie['yummy_cookie']).toBe('choco')9 expect(cookie['tasty_cookie']).toBe('strawberry')10 })1112 it('Should parse quoted cookie values', () => {13 const cookieString =14 'yummy_cookie="choco"; tasty_cookie = " strawberry " ; best_cookie="%20sugar%20";'15 const cookie: Cookie = parse(cookieString)16 expect(cookie['yummy_cookie']).toBe('choco')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 })3132 it('Should parse one cookie specified by name', () => {33 const cookieString = 'yummy_cookie=choco; tasty_cookie = strawberry '34 const cookie: Cookie = parse(cookieString, 'yummy_cookie')35 expect(cookie['yummy_cookie']).toBe('choco')36 expect(cookie['tasty_cookie']).toBeUndefined()37 })3839 it('Should parse one cookie specified by name even if it is not found', () => {40 const cookieString = 'yummy_cookie=choco; tasty_cookie = strawberry '41 const cookie: Cookie = parse(cookieString, 'no_such_cookie')42 expect(cookie['yummy_cookie']).toBeUndefined()43 expect(cookie['tasty_cookie']).toBeUndefined()44 expect(cookie['no_such_cookie']).toBeUndefined()45 })4647 it('Should parse cookies with no value', () => {48 const cookieString = 'yummy_cookie=; tasty_cookie = ; best_cookie= ; last_cookie=""'49 const cookie: Cookie = parse(cookieString)50 expect(cookie['yummy_cookie']).toBe('')51 expect(cookie['tasty_cookie']).toBe('')52 expect(cookie['best_cookie']).toBe('')53 expect(cookie['last_cookie']).toBe('')54 })5556 it('Should parse cookies but not process signed cookies', () => {57 // also contains another cookie with a '.' in its value to test it is not misinterpreted as signed cookie58 const cookieString =59 'yummy_cookie=choco; tasty_cookie = strawberry.I9qAeGQOvWjCEJgRPmrw90JjYpnnX2C9zoOiGSxh1Ig%3D; great_cookie=rating3.5; best_cookie=sugar.valueShapedLikeASignatureButIsNotASignature%3D'60 const cookie: Cookie = parse(cookieString)61 expect(cookie['yummy_cookie']).toBe('choco')62 expect(cookie['tasty_cookie']).toBe('strawberry.I9qAeGQOvWjCEJgRPmrw90JjYpnnX2C9zoOiGSxh1Ig=')63 expect(cookie['great_cookie']).toBe('rating3.5')64 expect(cookie['best_cookie']).toBe('sugar.valueShapedLikeASignatureButIsNotASignature=')65 })6667 it('Should ignore invalid cookie names', () => {68 const cookieString = 'yummy_cookie=choco; tasty cookie=strawberry; best_cookie\\=sugar; =ng'69 const cookie: Cookie = parse(cookieString)70 expect(cookie['yummy_cookie']).toBe('choco')71 expect(cookie['tasty cookie']).toBeUndefined()72 expect(cookie['best_cookie\\']).toBeUndefined()73 expect(cookie['']).toBeUndefined()74 })7576 it('Should ignore invalid cookie values', () => {77 const cookieString = 'yummy_cookie=choco\\nchip; tasty_cookie=strawberry; best_cookie="sugar'78 const cookie: Cookie = parse(cookieString)79 expect(cookie['yummy_cookie']).toBeUndefined()80 expect(cookie['tasty_cookie']).toBe('strawberry')81 expect(cookie['best_cookie\\']).toBeUndefined()82 })8384 it('Should parse signed cookies', async () => {85 const secret = 'secret ingredient'86 const cookieString =87 'yummy_cookie=choco.UdFR2rBpS1GsHfGlUiYyMIdqxqwuEgplyQIgTJgpGWY%3D; tasty_cookie = strawberry.I9qAeGQOvWjCEJgRPmrw90JjYpnnX2C9zoOiGSxh1Ig%3D'88 const cookie: SignedCookie = await parseSigned(cookieString, secret)89 expect(cookie['yummy_cookie']).toBe('choco')90 expect(cookie['tasty_cookie']).toBe('strawberry')91 })9293 it('Should parse signed cookies with binary secret', async () => {94 const secret = new Uint8Array([95 172, 142, 204, 63, 210, 136, 58, 143, 25, 18, 159, 16, 161, 34, 94,96 ])97 const cookieString =98 'yummy_cookie=choco.8Km4IwZETZdwiOfrT7KgYjKXwiO98XIkms0tOtRa2TA%3D; tasty_cookie = strawberry.TbV33P%2Bi1K0JTxMzNYq7FV9fB4s2VlQcBCBFDxTrUSg%3D'99 const cookie: SignedCookie = await parseSigned(cookieString, secret)100 expect(cookie['yummy_cookie']).toBe('choco')101 expect(cookie['tasty_cookie']).toBe('strawberry')102 })103104 it('Should parse signed cookies containing the signature separator', async () => {105 const secret = 'secret ingredient'106 const cookieString = 'yummy_cookie=choco.chip.2%2FJA0c68Y3zm0DvSvHyR6IRysDWmHW0LfoaC0AkyOpw%3D'107 const cookie: SignedCookie = await parseSigned(cookieString, secret)108 expect(cookie['yummy_cookie']).toBe('choco.chip')109 })110111 it('Should parse signed cookies and return "false" for wrong signature', async () => {112 const secret = 'secret ingredient'113 // tasty_cookie has invalid signature114 const cookieString =115 'yummy_cookie=choco.UdFR2rBpS1GsHfGlUiYyMIdqxqwuEgplyQIgTJgpGWY%3D; tasty_cookie = strawberry.LAa7RX43t2vCrLNcKmNG65H41OkyV02sraRPuY5RuVg%3D'116 const cookie: SignedCookie = await parseSigned(cookieString, secret)117 expect(cookie['yummy_cookie']).toBe('choco')118 expect(cookie['tasty_cookie']).toBe(false)119 })120121 it('Should parse signed cookies and return "false" for corrupt signature', async () => {122 const secret = 'secret ingredient'123 // yummy_cookie has corrupt signature (i.e. invalid base64 encoding)124 // best_cookie has a shape that matches the signature format but isn't actually a signature125 const cookieString =126 'yummy_cookie=choco.?dFR2rBpS1GsHfGlUiYyMIdqxqwuEgplyQIgTJgpGWY%3D; tasty_cookie = strawberry.I9qAeGQOvWjCEJgRPmrw90JjYpnnX2C9zoOiGSxh1Ig%3D; best_cookie=sugar.valueShapedLikeASignatureButIsNotASignature%3D'127 const cookie: SignedCookie = await parseSigned(cookieString, secret)128 expect(cookie['yummy_cookie']).toBe(false)129 expect(cookie['tasty_cookie']).toBe('strawberry')130 expect(cookie['best_cookie']).toBe(false)131 })132133 it('Should parse one signed cookie specified by name', async () => {134 const secret = 'secret ingredient'135 const cookieString =136 'yummy_cookie=choco.UdFR2rBpS1GsHfGlUiYyMIdqxqwuEgplyQIgTJgpGWY%3D; tasty_cookie = strawberry.I9qAeGQOvWjCEJgRPmrw90JjYpnnX2C9zoOiGSxh1Ig%3D'137 const cookie: SignedCookie = await parseSigned(cookieString, secret, 'tasty_cookie')138 expect(cookie['yummy_cookie']).toBeUndefined()139 expect(cookie['tasty_cookie']).toBe('strawberry')140 })141142 it('Should parse one signed cookie specified by name and return "false" for wrong signature', async () => {143 const secret = 'secret ingredient'144 // tasty_cookie has invalid signature145 const cookieString =146 'yummy_cookie=choco.UdFR2rBpS1GsHfGlUiYyMIdqxqwuEgplyQIgTJgpGWY%3D; tasty_cookie = strawberry.LAa7RX43t2vCrLNcKmNG65H41OkyV02sraRPuY5RuVg%3D'147 const cookie: SignedCookie = await parseSigned(cookieString, secret, 'tasty_cookie')148 expect(cookie['yummy_cookie']).toBeUndefined()149 expect(cookie['tasty_cookie']).toBe(false)150 })151152 it('Should parse signed cookies and ignore regular cookies', async () => {153 const secret = 'secret ingredient'154 // also contains another cookie with a '.' in its value to test it is not misinterpreted as signed cookie155 const cookieString =156 'yummy_cookie=choco; tasty_cookie = strawberry.I9qAeGQOvWjCEJgRPmrw90JjYpnnX2C9zoOiGSxh1Ig%3D; great_cookie=rating3.5'157 const cookie: SignedCookie = await parseSigned(cookieString, secret)158 expect(cookie['yummy_cookie']).toBeUndefined()159 expect(cookie['tasty_cookie']).toBe('strawberry')160 expect(cookie['great_cookie']).toBeUndefined()161 })162})163164describe('Set cookie', () => {165 it('Should serialize cookie', () => {166 const serialized = serialize('delicious_cookie', 'macha')167 expect(serialized).toBe('delicious_cookie=macha')168 })169170 it('Should serialize cookie with all options', () => {171 const serialized = serialize('__Secure-great_cookie', 'banana', {172 path: '/',173 secure: true,174 domain: 'example.com',175 httpOnly: true,176 maxAge: 1000,177 expires: new Date(Date.UTC(2000, 11, 24, 10, 30, 59, 900)),178 sameSite: 'Strict',179 priority: 'High',180 partitioned: true,181 })182 expect(serialized).toBe(183 '__Secure-great_cookie=banana; Max-Age=1000; Domain=example.com; Path=/; Expires=Sun, 24 Dec 2000 10:30:59 GMT; HttpOnly; Secure; SameSite=Strict; Priority=High; Partitioned'184 )185 })186187 it('Should serialize __Host- cookie with all valid options', () => {188 const serialized = serialize('__Host-great_cookie', 'banana', {189 path: '/',190 secure: true,191 httpOnly: true,192 maxAge: 1000,193 expires: new Date(Date.UTC(2000, 11, 24, 10, 30, 59, 900)),194 sameSite: 'Strict',195 priority: 'High',196 partitioned: true,197 })198 expect(serialized).toBe(199 '__Host-great_cookie=banana; Max-Age=1000; Path=/; Expires=Sun, 24 Dec 2000 10:30:59 GMT; HttpOnly; Secure; SameSite=Strict; Priority=High; Partitioned'200 )201 })202203 it('Should serialize a signed cookie', async () => {204 const secret = 'secret chocolate chips'205 const serialized = await serializeSigned('delicious_cookie', 'macha', secret)206 expect(serialized).toBe(207 'delicious_cookie=macha.diubJPY8O7hI1pLa42QSfkPiyDWQ0I4DnlACH%2FN2HaA%3D'208 )209 })210211 it('Should serialize signed cookie with all options', async () => {212 const secret = 'secret chocolate chips'213 const serialized = await serializeSigned('great_cookie', 'banana', secret, {214 path: '/',215 secure: true,216 domain: 'example.com',217 httpOnly: true,218 maxAge: 1000,219 expires: new Date(Date.UTC(2000, 11, 24, 10, 30, 59, 900)),220 sameSite: 'Strict',221 priority: 'High',222 partitioned: true,223 })224 expect(serialized).toBe(225 'great_cookie=banana.hSo6gB7YT2db0WBiEAakEmh7dtwEL0DSp76G23WvHuQ%3D; Max-Age=1000; Domain=example.com; Path=/; Expires=Sun, 24 Dec 2000 10:30:59 GMT; HttpOnly; Secure; SameSite=Strict; Priority=High; Partitioned'226 )227 })228229 it('Should serialize cookie with maxAge is 0', () => {230 const serialized = serialize('great_cookie', 'banana', {231 maxAge: 0,232 })233 expect(serialized).toBe('great_cookie=banana; Max-Age=0')234 })235236 it('Should serialize cookie with maxAge is -1', () => {237 const serialized = serialize('great_cookie', 'banana', {238 maxAge: -1,239 })240 expect(serialized).toBe('great_cookie=banana')241 })242243 it('Should throw Error cookie with maxAge grater than 400days', () => {244 expect(() => {245 serialize('great_cookie', 'banana', {246 maxAge: 3600 * 24 * 401,247 })248 }).toThrowError(249 'Cookies Max-Age SHOULD NOT be greater than 400 days (34560000 seconds) in duration.'250 )251 })252253 it('Should throw Error cookie with expires grater than 400days', () => {254 const now = Date.now()255 const day401 = new Date(now + 1000 * 3600 * 24 * 401)256 expect(() => {257 serialize('great_cookie', 'banana', {258 expires: day401,259 })260 }).toThrowError(261 'Cookies Expires SHOULD NOT be greater than 400 days (34560000 seconds) in the future.'262 )263 })264265 it('Should throw Error Partitioned cookie without Secure attributes', () => {266 expect(() => {267 serialize('great_cookie', 'banana', {268 partitioned: true,269 })270 }).toThrowError('Partitioned Cookie must have Secure attributes')271 })272

Merge commit from forkTaku Amanobefore adoption44ae0c8

lines 273–296why, one step back
273 it('Should throw Error cookie with domain or path containing ";", "\\r", or "\\n"', () => {
274 // domain275 expect(() => {276 serialize('great_cookie', 'banana', { domain: 'example.com;evil' })277 }).toThrowError('domain must not contain ";", "\\r", or "\\n"')278 expect(() => {279 serialize('great_cookie', 'banana', { domain: 'example.com\revil' })280 }).toThrowError('domain must not contain ";", "\\r", or "\\n"')281 expect(() => {282 serialize('great_cookie', 'banana', { domain: 'example.com\nevil' })283 }).toThrowError('domain must not contain ";", "\\r", or "\\n"')284285 // path286 expect(() => {287 serialize('great_cookie', 'banana', { path: '/;evil' })288 }).toThrowError('path must not contain ";", "\\r", or "\\n"')289 expect(() => {290 serialize('great_cookie', 'banana', { path: '/\revil' })291 }).toThrowError('path must not contain ";", "\\r", or "\\n"')292 expect(() => {293 serialize('great_cookie', 'banana', { path: '/\nevil' })294 }).toThrowError('path must not contain ";", "\\r", or "\\n"')295 })296

4.9.0Yusuke Wadabefore adoption82bdc4d

lines 297–313why, one step back
297 it('Should serialize cookie with lowercase priority values', () => {
298 const lowSerialized = serialize('test_cookie', 'value', {299 priority: 'low',300 })301 expect(lowSerialized).toBe('test_cookie=value; Priority=Low')302303 const mediumSerialized = serialize('test_cookie', 'value', {304 priority: 'medium',305 })306 expect(mediumSerialized).toBe('test_cookie=value; Priority=Medium')307308 const highSerialized = serialize('test_cookie', 'value', {309 priority: 'high',310 })311 expect(highSerialized).toBe('test_cookie=value; Priority=High')312 })313})