Skip to content
Myr

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

3 files+163 added−2 removed

modified:deno_dist/utils/cookie.ts+41 added
modified:src/utils/cookie.test.ts+81 added-2 removed
152 unmodified lines
153 expect(serialized).toBe('delicious_cookie=macha')154 })155156 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 lines
164 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 })171172 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 })186187 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 lines
221 maxAge: -1,222 })223 expect(serialized).toBe('great_cookie=banana')224 })225226 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 })235236 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 })247248 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 })253254 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 })262263 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 })271272 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 })281282 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 lines
113114const _serialize = (name: string, value: string, opt: CookieOptions = {}): string => {115 let cookie = `${name}=${value}`116117 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 }122123 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 }129130 if (opt.path !== '/') {131 throw new Error('__Host- Cookie must have Path attributes with "/"')132 }133134 if (opt.domain) {135 throw new Error('__Host- Cookie must not have Domain attributes')136 }137 }138139 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 }149150 if (opt.domain && opt.prefix !== 'host') {
4 unmodified lines
155 cookie += `; Path=${opt.path}`156 }157158 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 }168169 if (opt.httpOnly) {
8 unmodified lines
178 cookie += `; SameSite=${opt.sameSite}`179 }180181 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 }189190 return cookie
18 unmodified lines