import { Component, SecurityCheck, SecurityWarning, Value } from '@celljs/core'; /** The key in the cell.yml of @celljs/web, for development only. */ export const DEFAULT_COOKIE_KEYS: readonly string[] = ['abcdef']; /** Shorter keys can be guessed; 32 characters of `openssl rand -base64 32` are plenty. */ export const MIN_COOKIE_KEY_LENGTH = 16; /** * `web.cookies.keys` sign the cookies, the session cookie among them: whoever knows a key can write a * session (with the security context in it) that the server accepts. */ @Component(SecurityCheck) export class CookiesSecurityCheck implements SecurityCheck { @Value('web.cookies.keys') protected readonly keys?: string[]; check(): SecurityWarning[] { const keys = (this.keys ?? []).map(String); if (keys.length === 0) { return [{ code: 'CELL_SECURITY_COOKIE_KEYS_MISSING', message: 'web.cookies.keys is empty, so cookies are not signed: clients can edit the session cookie and everything in it.', hint: 'Set web.cookies.keys to a random secret from the environment, e.g. keys: ["${env.COOKIE_KEY}"] with COOKIE_KEY from openssl rand -base64 32.' }]; } if (keys.some(key => DEFAULT_COOKIE_KEYS.includes(key))) { return [{ code: 'CELL_SECURITY_DEFAULT_COOKIE_KEYS', message: 'web.cookies.keys holds the development key of @celljs/web, which is public: anyone can sign a session cookie the server accepts.', hint: 'Set web.cookies.keys to a random secret from the environment, e.g. keys: ["${env.COOKIE_KEY}"] with COOKIE_KEY from openssl rand -base64 32.' }]; } if (keys.some(key => key.length < MIN_COOKIE_KEY_LENGTH)) { return [{ code: 'CELL_SECURITY_WEAK_COOKIE_KEYS', message: `A key of web.cookies.keys is shorter than ${MIN_COOKIE_KEY_LENGTH} characters and can be guessed.`, hint: 'Use random keys of at least 32 characters, e.g. from openssl rand -base64 32; keep the old key after the new one while sessions signed with it expire.' }]; } return []; } }