SPB Git

Repository Public

headers.ts 3.3 KB 40 lines typescript

1// Validation des en-têtes, adresses et sujets fournis par un appelant : aucune injection CR/LF, liste blanche d'en-têtes.2import { z } from "zod"3import { bad } from "../util.js"45export const EMAIL_RE = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$/6const CTRL = /[\u0000-\u001f\u007f\u0085\u2028\u2029]/7export const hasCtl = (s: string) => CTRL.test(s)89export const zEmail = z.string().max(254).transform((s, ctx) => {10  if (hasCtl(s)) { ctx.addIssue({ code: "custom", message: "control characters (CR/LF) are not allowed in addresses" }); return z.NEVER }11  const v = s.trim().toLowerCase()12  if (!EMAIL_RE.test(v)) { ctx.addIssue({ code: "custom", message: "invalid e-mail address" }); return z.NEVER }13  return v14})15const zDisplayName = z.string().max(120).refine((s) => !hasCtl(s), "control characters (CR/LF) are not allowed in names")16export const zRecipient = z.union([zEmail.transform((email) => ({ email, name: undefined as string | undefined })), z.object({ email: zEmail, name: zDisplayName.optional() })])17export type Recipient = { email: string; name?: string }18export const zSubject = z.string().max(998).refine((s) => !hasCtl(s), "control characters (CR/LF) are not allowed in the subject")19export const zMessageIdHeader = z.string().max(500).refine((s) => !hasCtl(s) && /^<?[^\s<>]+@[^\s<>]+>?$/.test(s), "invalid Message-ID")2021/** En-têtes libres permis : `X-…`, `Reply-To`, `List-Unsubscribe`, `List-Unsubscribe-Post`. Tout le reste est refusé. */22const FORBIDDEN_X = new Set(["x-spam-status", "x-spam-score", "x-spam-flag", "x-virus-status", "x-virus-scanned", "x-originating-ip", "x-forwarded-for", "x-forwarded-to", "x-original-to", "x-envelope-from", "x-envelope-to", "x-sender"])23export function validateCustomHeaders(h: Record<string, string> | undefined): { name: string; value: string }[] {24  const out: { name: string; value: string }[] = []25  for (const [rawName, value] of Object.entries(h ?? {})) {26    const lower = rawName.toLowerCase()27    if (!/^[A-Za-z0-9-]{1,64}$/.test(rawName)) throw bad("invalid_header", 400, `Invalid header name`, { header: rawName.slice(0, 64) })28    if (typeof value !== "string" || hasCtl(value) || value.length > 900) throw bad("invalid_header", 400, `Header ${rawName} has an invalid value (control characters, CR/LF, or too long)`, { header: rawName })29    const allowed = (lower.startsWith("x-") && !FORBIDDEN_X.has(lower)) || lower === "reply-to" || lower === "list-unsubscribe" || lower === "list-unsubscribe-post"30    if (!allowed) throw bad("header_not_allowed", 400, `Header ${rawName} is not allowed (only X-*, Reply-To, List-Unsubscribe, List-Unsubscribe-Post)`, { header: rawName })31    if (lower === "reply-to" && !value.split(",").every((a) => EMAIL_RE.test(a.trim().toLowerCase()))) throw bad("invalid_header", 400, "Reply-To must be a comma-separated list of addresses", { header: rawName })32    out.push({ name: rawName, value })33  }34  if (out.length > 20) throw bad("invalid_header", 400, "Too many custom headers (max 20)")35  return out36}3738export const LABEL_RE = /^[a-z0-9][a-z0-9_.-]{0,31}$/39export const zLabel = z.string().transform((s) => s.trim().toLowerCase()).refine((s) => LABEL_RE.test(s), "labels are 1–32 characters of a-z 0-9 _ . - and start with a letter or digit")40
Files main
Menu

Theme