61 lines
4.3 KiB
JavaScript
61 lines
4.3 KiB
JavaScript
import { createHash } from "node:crypto";
|
|
|
|
export const assetPathPattern = /^\/brands\/apps\/[a-z0-9-]+\.(svg|png)$/;
|
|
export const sha256 = (bytes) => createHash("sha256").update(bytes).digest("hex");
|
|
export const normalizeBrandKey = (value) => value.trim().toLowerCase().replace(/&/g, "and").replace(/[^a-z0-9]+/g, "-").replace(/^-+|-+$/g, "");
|
|
|
|
// Conservative structural rejection, not a general-purpose SVG sanitizer.
|
|
// Original accepted artwork is copied byte-for-byte, never rewritten here.
|
|
export function validateArtwork(bytes, filename) {
|
|
if (filename.endsWith(".png")) {
|
|
if (bytes.length < 45 || !bytes.subarray(0, 8).equals(Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]))
|
|
|| bytes.toString("ascii", 12, 16) !== "IHDR" || bytes.readUInt32BE(16) === 0 || bytes.readUInt32BE(20) === 0
|
|
|| bytes.toString("ascii", bytes.length - 8, bytes.length - 4) !== "IEND") {
|
|
throw new Error(`${filename}: invalid PNG signature`);
|
|
}
|
|
return;
|
|
}
|
|
const svg = bytes.toString("utf8");
|
|
if (!filename.endsWith(".svg") || (!/<svg\b[^>]*\bviewBox\s*=\s*["'][^"']+["']/i.test(svg) && !(/<svg\b[^>]*\bwidth\s*=\s*["'][0-9.]+(?:px)?["']/i.test(svg) && /<svg\b[^>]*\bheight\s*=\s*["'][0-9.]+(?:px)?["']/i.test(svg)))) {
|
|
throw new Error(`${filename}: SVG requires a viewBox or intrinsic width and height`);
|
|
}
|
|
if (/<!DOCTYPE|<!ENTITY|<\?xml-stylesheet|&#|<\s*(?:[\w-]+:)?(?:script|foreignObject|image|feImage|iframe|object|embed|animate\w*|set|a)\b|\bon[\w-]+\s*=/i.test(svg)
|
|
|| /\b(?:[\w-]+:)?href\s*=\s*["'](?!#)/i.test(svg)
|
|
|| /@import|@media|url\(\s*["']?(?!#)[^)]*\)|(?:javascript|data|https?):|\\/i.test(svg.replace(/xmlns(?::[\w-]+)?\s*=\s*["'][^"']*["']/gi, ""))) {
|
|
throw new Error(`${filename}: unsafe SVG feature, external resource, theme media query, or embedded raster wrapper`);
|
|
}
|
|
}
|
|
|
|
export function validateManifest(manifest, readAsset) {
|
|
if (manifest.schemaVersion !== 1 || !Array.isArray(manifest.providers)) throw new Error("Invalid brand manifest");
|
|
const keys = new Map();
|
|
const slugs = new Set();
|
|
for (const row of manifest.providers) {
|
|
if (!/^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(row.slug) || slugs.has(row.slug)) throw new Error(`Duplicate or invalid slug: ${row.slug}`);
|
|
slugs.add(row.slug);
|
|
if (typeof row.provider !== "string" || !row.provider.trim()) throw new Error(`${row.slug}: missing provider name`);
|
|
if (!Array.isArray(row.aliases) || row.aliases.some((alias) => typeof alias !== "string" || !alias.trim())) throw new Error(`${row.slug}: invalid aliases`);
|
|
for (const name of [row.slug, row.provider, ...row.aliases]) {
|
|
const key = normalizeBrandKey(name);
|
|
if (keys.has(key) && keys.get(key) !== row.slug) throw new Error(`${row.slug}: ambiguous brand alias ${name}`);
|
|
keys.set(key, row.slug);
|
|
}
|
|
if (!["same", "pair"].includes(row.themeMode) || !row.localAsset || !row.darkAsset
|
|
|| (row.themeMode === "same") !== (row.localAsset === row.darkAsset)
|
|
|| row.darkVariantRequired !== (row.themeMode === "pair")) throw new Error(`${row.slug}: incomplete or inconsistent theme pair`);
|
|
if (row.opticalFit !== "standard" || !row.selection?.note || !row.selection?.opticalReview) throw new Error(`${row.slug}: missing optical fit or selection evidence`);
|
|
for (const [theme, asset] of [["light", row.localAsset], ["dark", row.darkAsset]]) {
|
|
if (!assetPathPattern.test(asset) || !asset.endsWith(`.${row.assetType}`)) throw new Error(`${row.slug}: invalid asset path`);
|
|
const provenance = row.provenance?.[theme];
|
|
if (!provenance || !["board-selected", "user-supplied", "vendor-theme-export", "simple-icons-derivative", "retained-upstream", "vendor-sourced"].includes(provenance.sourceClass)
|
|
|| !provenance.sourceFile || !provenance.sourceUrl || !provenance.acquiredAt || !/^[a-f0-9]{64}$/.test(provenance.sha256)) throw new Error(`${row.slug}/${theme}: missing provenance`);
|
|
if (new URL(provenance.sourceUrl).protocol !== "https:" || provenance.sourceFile.startsWith("/")
|
|
|| provenance.sourceFile.split(/[\\/]/).includes("..")) throw new Error(`${row.slug}/${theme}: invalid provenance source`);
|
|
const bytes = readAsset(asset, provenance);
|
|
if (sha256(bytes) !== provenance.sha256) throw new Error(`${row.slug}/${theme}: source hash mismatch`);
|
|
validateArtwork(bytes, asset);
|
|
}
|
|
}
|
|
return manifest.providers.length;
|
|
}
|