cookie.ts
This file
Path
src/utils/cookie.ts
280 lines 路 8.9 KiB 路 TypeScript
Last changed by
fix(cookie): allow parsing signed cookies with empty string values (#5246)Nanasibefore adoption241ae4c
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281/** * @module * Cookie utility. */
import { tryDecodeURIComponent } from './url'
export type Cookie = Record<string, string>export type SignedCookie = Record<string, string | false>
type PartitionedCookieConstraint = | { partitioned: true; secure: true } | { partitioned?: boolean; secure?: boolean } // reset to defaulttype SecureCookieConstraint = { secure: true }type HostCookieConstraint = { secure: true; path: '/'; domain?: undefined }
export type CookieOptions = { domain?: string expires?: Date httpOnly?: boolean maxAge?: number path?: string secure?: boolean sameSite?: 'Strict' | 'Lax' | 'None' | 'strict' | 'lax' | 'none' partitioned?: boolean priority?: 'Low' | 'Medium' | 'High' | 'low' | 'medium' | 'high' prefix?: CookiePrefixOptions} & PartitionedCookieConstraintexport type CookiePrefixOptions = 'host' | 'secure'
export type CookieConstraint<Name> = Name extends `__Secure-${string}` ? CookieOptions & SecureCookieConstraint : Name extends `__Host-${string}` ? CookieOptions & HostCookieConstraint : CookieOptions
const algorithm = { name: 'HMAC', hash: 'SHA-256' }
const getCryptoKey = async (secret: string | BufferSource): Promise<CryptoKey> => { const secretBuf = typeof secret === 'string' ? new TextEncoder().encode(secret) : secret return await crypto.subtle.importKey('raw', secretBuf, algorithm, false, ['sign', 'verify'])}
const makeSignature = async (value: string, secret: string | BufferSource): Promise<string> => { const key = await getCryptoKey(secret) const signature = await crypto.subtle.sign(algorithm.name, key, new TextEncoder().encode(value)) // the returned base64 encoded signature will always be 44 characters long and end with one or two equal signs return btoa(String.fromCharCode(...new Uint8Array(signature)))}
const verifySignature = async ( base64Signature: string, value: string, secret: CryptoKey): Promise<boolean> => { try { const signatureBinStr = atob(base64Signature) const signature = new Uint8Array(signatureBinStr.length) for (let i = 0, len = signatureBinStr.length; i < len; i++) { signature[i] = signatureBinStr.charCodeAt(i) } return await crypto.subtle.verify(algorithm, secret, signature, new TextEncoder().encode(value)) } catch { return false }}
// all alphanumeric chars and all of _!#$%&'*.^`|~+-// (see: https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1)// used only when producing cookies via serialize(); parsing uses the relaxed regex belowconst validCookieNameRegEx = /^[\w!#$%&'*.^`|~+-]+$/
// all ASCII chars 33-126 except 34, 59, 61, and 92 (i.e. "!" to "~" but not double quote, semicolon, equals, or backslash)// the strict RFC 6265 token rule above is a requirement for cookie producers; when consuming// the Cookie header we accept the names other producers emit in the wild, e.g. "paraglide:lang"// (see: https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis#section-5.6 and https://github.com/honojs/hono/issues/3189)const relaxedCookieNameRegEx = /^[!#-:<>-[\]-~]+$/
// all ASCII chars 32-126 except 34, 59, and 92 (i.e. space to tilde but not double quote, semicolon, or backslash)// (see: https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1)//// note: the spec also prohibits comma and space, but we allow both since they are very common in the real world// (see: https://github.com/golang/go/issues/7243)const validCookieValueRegEx = /^[ !#-:<-[\]-~]*$/
const trimCookieWhitespace = (value: string): string => { let start = 0 let end = value.length
while (start < end) { const charCode = value.charCodeAt(start) if (charCode !== 0x20 && charCode !== 0x09) { break } start++ }
while (end > start) { const charCode = value.charCodeAt(end - 1) if (charCode !== 0x20 && charCode !== 0x09) { break } end-- }
return start === 0 && end === value.length ? value : value.slice(start, end)}
export const parse = (cookie: string, name?: string): Cookie => { if (name && cookie.indexOf(name) === -1) { // Fast-path: return immediately if the demanded-key is not in the cookie string return {} } const pairs = cookie.split(';') const parsedCookie: Cookie = Object.create(null) for (const pairStr of pairs) { const valueStartPos = pairStr.indexOf('=') if (valueStartPos === -1) { continue }
const cookieName = trimCookieWhitespace(pairStr.substring(0, valueStartPos)) if ( (name && name !== cookieName) || !relaxedCookieNameRegEx.test(cookieName) || cookieName in parsedCookie ) { continue }
let cookieValue = trimCookieWhitespace(pairStr.substring(valueStartPos + 1)) if (cookieValue.startsWith('"') && cookieValue.endsWith('"')) { cookieValue = cookieValue.slice(1, -1) } if (validCookieValueRegEx.test(cookieValue)) { parsedCookie[cookieName] = tryDecodeURIComponent(cookieValue) if (name) { // Fast-path: return only the demanded-key immediately. Other keys are not needed. break } } } return parsedCookie}
export const parseSigned = async ( cookie: string, secret: string | BufferSource, name?: string): Promise<SignedCookie> => { const parsedCookie: SignedCookie = Object.create(null) const secretKey = await getCryptoKey(secret)
for (const [key, value] of Object.entries(parse(cookie, name))) { const signatureStartPos = value.lastIndexOf('.') if (signatureStartPos < 0) { continue }
const signedValue = value.substring(0, signatureStartPos) const signature = value.substring(signatureStartPos + 1) if (signature.length !== 44 || !signature.endsWith('=')) { continue }
const isVerified = await verifySignature(signature, signedValue, secretKey) parsedCookie[key] = isVerified ? signedValue : false }
return parsedCookie}
const _serialize = (name: string, value: string, opt: CookieOptions = {}): string => { if (!validCookieNameRegEx.test(name)) { throw new Error('Invalid cookie name') }
let cookie = `${name}=${value}`
if (name.startsWith('__Secure-') && !opt.secure) { // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-22#section-4.1.3.1 throw new Error('__Secure- Cookie must have Secure attributes') }
if (name.startsWith('__Host-')) { // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-22#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') } }
for (const key of ['domain', 'path', 'sameSite', 'priority'] as (keyof CookieOptions)[]) { if (opt[key] && /[;\r\n]/.test(opt[key] as string)) { throw new Error(`${key} must not contain ";", "\\r", or "\\n"`) } }
if (opt && typeof opt.maxAge === 'number' && opt.maxAge >= 0) { if (opt.maxAge > 34560000) { // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-22#section-5.6.2 throw new Error( 'Cookies Max-Age SHOULD NOT be greater than 400 days (34560000 seconds) in duration.' ) } cookie += `; Max-Age=${opt.maxAge | 0}` }
if (opt.domain && opt.prefix !== 'host') { cookie += `; Domain=${opt.domain}` }
if (opt.path) { cookie += `; Path=${opt.path}` }
if (opt.expires) { if (opt.expires.getTime() - Date.now() > 34560000_000) { // https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-22#section-5.5 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) { cookie += '; HttpOnly' }
if (opt.secure) { cookie += '; Secure' }
if (opt.sameSite) { cookie += `; SameSite=${opt.sameSite.charAt(0).toUpperCase() + opt.sameSite.slice(1)}` }
if (opt.priority) { cookie += `; Priority=${opt.priority.charAt(0).toUpperCase() + opt.priority.slice(1)}` }
if (opt.partitioned) { // https://www.ietf.org/archive/id/draft-cutler-httpbis-partitioned-cookies-01.html#section-2.1 if (!opt.secure) { throw new Error('Partitioned Cookie must have Secure attributes') } cookie += '; Partitioned' }
return cookie}
export const serialize = <Name extends string>( name: Name, value: string, opt?: CookieConstraint<Name>): string => { value = encodeURIComponent(value) return _serialize(name, value, opt)}
export const serializeSigned = async ( name: string, value: string, secret: string | BufferSource, opt: CookieOptions = {}): Promise<string> => { const signature = await makeSignature(value, secret) value = `${value}.${signature}` value = encodeURIComponent(value) return _serialize(name, value, opt)}