UNPKG

astro

Version:

Astro is a modern site builder with web best practices, performance, and DX front-of-mind.

54 lines (53 loc) 1.18 kB
import { z } from "zod"; const ALGORITHMS = { "SHA-256": "sha256-", "SHA-384": "sha384-", "SHA-512": "sha512-" }; const ALGORITHM_VALUES = Object.values(ALGORITHMS); const cspAlgorithmSchema = z.enum(Object.keys(ALGORITHMS)).optional().default("SHA-256"); const cspHashSchema = z.custom((value) => { if (typeof value !== "string") { return false; } return ALGORITHM_VALUES.some((allowedValue) => { return value.startsWith(allowedValue); }); }); const ALLOWED_DIRECTIVES = [ "base-uri", "child-src", "connect-src", "default-src", "fenced-frame-src", "font-src", "form-action", "frame-ancestors", "frame-src", "img-src", "manifest-src", "media-src", "object-src", "referrer", "report-to", "report-uri", "require-trusted-types-for", "sandbox", "trusted-types", "upgrade-insecure-requests", "worker-src" ]; const allowedDirectivesSchema = z.custom((value) => { if (typeof value !== "string") { return false; } return ALLOWED_DIRECTIVES.some((allowedValue) => { return value.startsWith(allowedValue); }); }); export { ALGORITHMS, allowedDirectivesSchema, cspAlgorithmSchema, cspHashSchema };