/** * The names (and the one safe way to expire) the storefront's credential cookies. * * Why the `__Host-` prefix: several storefronts can share one registrable domain (a multi-tenant host that serves every store on * `.example.app`), and a cookie set by one subdomain with `Domain=example.app` is sent to every other. Without * the prefix a hostile store could plant a cookie with the SAME NAME as the victim store's session cookie and have it * sent to the victim (cookie tossing: login CSRF, session fixation). A browser refuses to accept a `__Host-` cookie that has * a `Domain` attribute, is not `Secure`, or whose path is not `/`, so only the exact host can ever set it. It only works * over HTTPS, so the plain names stay for local development (`next dev`). * * The rule follows `isSecureCookie()`, the same one the cookies' `secure` flag always used: production means HTTPS. * * Reading: in secure mode ONLY the `__Host-` name is read. Falling back to the plain name would let a tossed cookie in * again, which defeats the point. A shopper who was logged in before this change signs in once more. */ export const LOGGED_IN_COOKIE = 'brainerce_logged_in'; const TOKEN = 'brainerce_customer_token'; const RESET = 'brainerce_reset_token'; const OAUTH_NONCE = 'brainerce_oauth_nonce'; /** * `true` in production, which is served over HTTPS, and ALWAYS on Cloudflare Workers. * * The Workers half matters: on a Cloudflare Workers deployment `process.env.NODE_ENV` is NOT "production" at run time, so * without this line the plain cookie names would be used, and a hostile store on another subdomain of the same registrable * domain could plant a valid session token for a victim's browser (login CSRF, session fixation), which the `__Host-` * prefix exists to prevent. Every Workers request is HTTPS, so there is nothing to lose by always being secure there. Same * detector as the read cache. */ export function isSecureCookie(): boolean { if (process.env.NODE_ENV === 'production') return true; return typeof navigator !== 'undefined' && navigator.userAgent === 'Cloudflare-Workers'; } const prefixed = (name: string): string => (isSecureCookie() ? `__Host-${name}` : name); /** The shopper's session token (httpOnly). */ export function tokenCookieName(): string { return prefixed(TOKEN); } /** The one-time password reset token (httpOnly). */ export function resetCookieName(): string { return prefixed(RESET); } /** * The one-time value that binds a social-login round trip to the browser that started it (httpOnly, * 10 minutes). See `core/lib/oauth-nonce.ts`. */ export function oauthNonceCookieName(): string { return prefixed(OAUTH_NONCE); } /** The names an older storefront used, to be cleared when a shopper signs in or out. */ export const LEGACY_TOKEN_COOKIE = TOKEN; export const LEGACY_RESET_COOKIE = RESET; interface CookieJar { set(name: string, value: string, options: Record): unknown; } /** * Expires a cookie. A `__Host-` cookie must be `Secure` even when it is being removed (the browser rejects the * `Set-Cookie` otherwise), so a plain `cookies.delete()` would silently leave it in place. */ export function expireCookie(jar: CookieJar, name: string): void { jar.set(name, '', { httpOnly: true, secure: name.startsWith('__Host-') || isSecureCookie(), sameSite: 'lax', path: '/', maxAge: 0, }); }