feat(cookie): Add Cookie bis condition check (#2314)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* throw Erro for cookie max-age longer than 400days * throw Error for cookie with expires longer than 400days * throw Error for --Secure- cookie without Secure attributes * throw Error for --Host- cookie without valid settings * throw Error for Partitioned cookie without Secure attributes * npm run denoify * npm run format:fix * bun run denoify
authored by Jxck · committed by GitHub · parent 568f75f · Browse files · 3 files·+163 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
3 files+163 added−2 removed
- tests:+81 added−2 removed
- other:+82 added−0 removed
Provenance
- Commit
6eaa696- Tree
f509fc5- Author
Jxck <jxck@jxck.io>- Committer
GitHub <noreply@github.com>- Parent
568f75f
3 files changed+163 added−2 removed
modified:deno_dist/utils/cookie.ts+41 added
112 unmodified linesExpand all1131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491504 unmodified lines1551561571581591601611621631641651661671681698 unmodified lines17817918018118218318418518618718818919018 unmodified lines112 unmodified linesExpand all
const _serialize = (name: string, value: string, opt: CookieOptions = {}): string => { let cookie = `${name}=${value}`
if (name.startsWith('__Secure-') && !opt.secure) { // FIXME: replace link to RFC // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-13#section-4.1.3.1 throw new Error('__Secure- Cookie must have Secure attributes') }
if (name.startsWith('__Host-')) { // FIXME: replace link to RFC // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-13#section-4.1.3.2 if (!opt.secure) { throw new Error('__Host- Cookie must have Secure attributes') }
if (opt.path !== '/') { throw new Error('__Host- Cookie must have Path attributes with "/"') }
if (opt.domain) { throw new Error('__Host- Cookie must not have Domain attributes') } }
if (opt && typeof opt.maxAge === 'number' && opt.maxAge >= 0) { if (opt.maxAge > 34560000) { // FIXME: replace link to RFC // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-13#section-4.1.2.2 throw new Error( 'Cookies Max-Age SHOULD NOT be greater than 400 days (34560000 seconds) in duration.' ) } cookie += `; Max-Age=${Math.floor(opt.maxAge)}` }
if (opt.domain && opt.prefix !== 'host') {4 unmodified lines cookie += `; Path=${opt.path}` }
if (opt.expires) { if (opt.expires.getTime() - Date.now() > 34560000_000) { // FIXME: replace link to RFC // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-13#section-4.1.2.1 throw new Error( 'Cookies Expires SHOULD NOT be greater than 400 days (34560000 seconds) in the future.' ) } cookie += `; Expires=${opt.expires.toUTCString()}` }
if (opt.httpOnly) {8 unmodified lines cookie += `; SameSite=${opt.sameSite}` }
if (opt.partitioned) { // FIXME: replace link to RFC // https://www.ietf.org/archive/id/draft-cutler-httpbis-partitioned-cookies-01.html#section-2.3 if (!opt.secure) { throw new Error('Partitioned Cookie must have Secure attributes') } cookie += '; Partitioned' }
return cookie18 unmodified linesmodified:src/utils/cookie.test.ts+81 added-2 removed
152 unmodified lines153 expect(serialized).toBe('delicious_cookie=macha')154 })155 156 it('Should serialize cookie with all options', () => {157- const serialized = serialize('great_cookie', 'banana', {157+ const serialized = serialize('__Secure-great_cookie', 'banana', {158 path: '/',159 secure: true,160 domain: 'example.com',161 httpOnly: true,2 unmodified lines164 sameSite: 'Strict',165 partitioned: true,166 })167 expect(serialized).toBe(168- 'great_cookie=banana; Max-Age=1000; Domain=example.com; Path=/; Expires=Sun, 24 Dec 2000 10:30:59 GMT; HttpOnly; Secure; SameSite=Strict; Partitioned'168+ '__Secure-great_cookie=banana; Max-Age=1000; Domain=example.com; Path=/; Expires=Sun, 24 Dec 2000 10:30:59 GMT; HttpOnly; Secure; SameSite=Strict; Partitioned'169 )170 })171 172+ it('Should serialize __Host- cookie with all valid options', () => {173+ const serialized = serialize('__Host-great_cookie', 'banana', {174+ path: '/',175+ secure: true,176+ httpOnly: true,177+ maxAge: 1000,178+ expires: new Date(Date.UTC(2000, 11, 24, 10, 30, 59, 900)),179+ sameSite: 'Strict',180+ partitioned: true,181+ })182+ expect(serialized).toBe(183+ '__Host-great_cookie=banana; Max-Age=1000; Path=/; Expires=Sun, 24 Dec 2000 10:30:59 GMT; HttpOnly; Secure; SameSite=Strict; Partitioned'184+ )185+ })186+187 it('Should serialize a signed cookie', async () => {188 const secret = 'secret chocolate chips'189 const serialized = await serializeSigned('delicious_cookie', 'macha', secret)190 expect(serialized).toBe(30 unmodified lines221 maxAge: -1,222 })223 expect(serialized).toBe('great_cookie=banana')224 })225+226+ it('Should throw Error cookie with maxAge grater than 400days', () => {227+ expect(() => {228+ serialize('great_cookie', 'banana', {229+ maxAge: 3600 * 24 * 401,230+ })231+ }).toThrowError(232+ 'Cookies Max-Age SHOULD NOT be greater than 400 days (34560000 seconds) in duration.'233+ )234+ })235+236+ it('Should throw Error cookie with expires grater than 400days', () => {237+ const now = Date.now()238+ const day401 = new Date(now + 1000 * 3600 * 24 * 401)239+ expect(() => {240+ serialize('great_cookie', 'banana', {241+ expires: day401,242+ })243+ }).toThrowError(244+ 'Cookies Expires SHOULD NOT be greater than 400 days (34560000 seconds) in the future.'245+ )246+ })247+248+ it('Should throw Error __Secure- cookie without Secure attributes', () => {249+ expect(() => {250+ serialize('__Secure-great_cookie', 'banana', {})251+ }).toThrowError('__Secure- Cookie must have Secure attributes')252+ })253+254+ it('Should throw Error __Host- cookie without Secure attributes', () => {255+ expect(() => {256+ serialize('__Host-great_cookie', 'banana', {257+ secure: false,258+ path: '/',259+ })260+ }).toThrowError('__Host- Cookie must have Secure attributes')261+ })262+263+ it('Should throw Error __Host- cookie without Path attributes with "/"', () => {264+ expect(() => {265+ serialize('__Host-great_cookie', 'banana', {266+ secure: true,267+ path: '/admin',268+ })269+ }).toThrowError('__Host- Cookie must have Path attributes with "/"')270+ })271+272+ it('Should throw Error __Host- cookie with Domain attributes', () => {273+ expect(() => {274+ serialize('__Host-great_cookie', 'banana', {275+ secure: true,276+ path: '/',277+ domain: 'site.example',278+ })279+ }).toThrowError('__Host- Cookie must not have Domain attributes')280+ })281+282+ it('Should throw Error Partitioned cookie without Secure attributes', () => {283+ expect(() => {284+ serialize('great_cookie', 'banana', {285+ partitioned: true,286+ })287+ }).toThrowError('Partitioned Cookie must have Secure attributes')288+ })289 })modified:src/utils/cookie.ts+41 added
112 unmodified lines113 114 const _serialize = (name: string, value: string, opt: CookieOptions = {}): string => {115 let cookie = `${name}=${value}`116 117+ if (name.startsWith('__Secure-') && !opt.secure) {118+ // FIXME: replace link to RFC119+ // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-13#section-4.1.3.1120+ throw new Error('__Secure- Cookie must have Secure attributes')121+ }122+123+ if (name.startsWith('__Host-')) {124+ // FIXME: replace link to RFC125+ // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-13#section-4.1.3.2126+ if (!opt.secure) {127+ throw new Error('__Host- Cookie must have Secure attributes')128+ }129+130+ if (opt.path !== '/') {131+ throw new Error('__Host- Cookie must have Path attributes with "/"')132+ }133+134+ if (opt.domain) {135+ throw new Error('__Host- Cookie must not have Domain attributes')136+ }137+ }138+139 if (opt && typeof opt.maxAge === 'number' && opt.maxAge >= 0) {140+ if (opt.maxAge > 34560000) {141+ // FIXME: replace link to RFC142+ // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-13#section-4.1.2.2143+ throw new Error(144+ 'Cookies Max-Age SHOULD NOT be greater than 400 days (34560000 seconds) in duration.'145+ )146+ }147 cookie += `; Max-Age=${Math.floor(opt.maxAge)}`148 }149 150 if (opt.domain && opt.prefix !== 'host') {4 unmodified lines155 cookie += `; Path=${opt.path}`156 }157 158 if (opt.expires) {159+ if (opt.expires.getTime() - Date.now() > 34560000_000) {160+ // FIXME: replace link to RFC161+ // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-13#section-4.1.2.1162+ throw new Error(163+ 'Cookies Expires SHOULD NOT be greater than 400 days (34560000 seconds) in the future.'164+ )165+ }166 cookie += `; Expires=${opt.expires.toUTCString()}`167 }168 169 if (opt.httpOnly) {8 unmodified lines178 cookie += `; SameSite=${opt.sameSite}`179 }180 181 if (opt.partitioned) {182+ // FIXME: replace link to RFC183+ // https://www.ietf.org/archive/id/draft-cutler-httpbis-partitioned-cookies-01.html#section-2.3184+ if (!opt.secure) {185+ throw new Error('Partitioned Cookie must have Secure attributes')186+ }187 cookie += '; Partitioned'188 }189 190 return cookie18 unmodified lines