Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L5npnUYgSPkAaHkdaeUqBr
35 lines
2.5 KiB
JavaScript
35 lines
2.5 KiB
JavaScript
// v1.53.0 — la CSP del nodo puede ser «script-src 'self'»: lo construido no
|
|
// tiene scripts en línea, el código va en app-….js con su «integrity», y no
|
|
// usa eval ni new Function (que exigirían 'unsafe-eval').
|
|
//
|
|
// Necesita vendor/babel.min.js (como construir.js). Uso: node tests/test44.js
|
|
const { execFileSync } = require("child_process");
|
|
const crypto = require("crypto");
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
const raiz = path.join(__dirname, "..");
|
|
if (!fs.existsSync(path.join(raiz, "vendor", "babel.min.js"))) { console.log("Sin vendor/babel.min.js: no se puede construir; prueba omitida."); process.exit(0); }
|
|
execFileSync(process.execPath, [path.join(raiz, "construir.js")], { stdio: "ignore" });
|
|
|
|
let pass = 0, fail = 0;
|
|
const ok = (n, c, x) => { console.log(` ${c ? "✓" : "✗"} ${n}`); if (!c && x !== undefined) console.log(" " + x); c ? pass++ : fail++; };
|
|
const dist = path.join(raiz, "dist");
|
|
const html = fs.readFileSync(path.join(dist, "dashboard.html"), "utf8");
|
|
const apps = fs.readdirSync(dist).filter(f => /^app-[0-9a-f]{12}\.js$/.test(f));
|
|
ok("hay un único app-….js en dist/", apps.length === 1, apps.join(", "));
|
|
const etiquetas = html.match(/<script\b[^>]*>/g) || [];
|
|
ok("ningún <script> en línea en dashboard.html", etiquetas.every(e => /\bsrc=/.test(e)) && !/<script\b[^>]*>\s*[^<\s]/.test(html.replace(/<script\b[^>]*src=[^>]*><\/script>/g, "")), etiquetas.join(" "));
|
|
const m = html.match(/<script src="(app-[0-9a-f]{12}\.js)" integrity="(sha256-[A-Za-z0-9+/=]+)"><\/script>/);
|
|
ok("dashboard.html carga app-….js con integrity", !!m && m[1] === apps[0]);
|
|
const codigo = fs.readFileSync(path.join(dist, apps[0]));
|
|
const sri = "sha256-" + crypto.createHash("sha256").update(codigo).digest("base64");
|
|
ok("el integrity coincide con el fichero", m && m[2] === sri);
|
|
ok("el nombre lleva su huella", apps[0].slice(4, 16) === crypto.createHash("sha256").update(codigo).digest("hex").slice(0, 12));
|
|
const txt = codigo.toString("utf8");
|
|
ok("sin new Function ni eval( en la app", !/new Function\s*\(/.test(txt) && !/(^|[^.\w$])eval\s*\(/.test(txt));
|
|
ok("sin atributos on…= en el HTML (manejadores en línea)", !/<[^>]+\son[a-z]+\s*=/i.test(html));
|
|
const csp = (fs.readFileSync(path.join(raiz, "nginx-txoko-seguridad.conf"), "utf8").match(/^add_header Content-Security-Policy .*$/m) || [""])[0];
|
|
ok("la CSP del repo es script-src 'self' sin unsafe", /script-src 'self';/.test(csp) && !/unsafe-eval/.test(csp) && !/script-src[^;]*unsafe-inline/.test(csp));
|
|
console.log(`\n${pass} correctas, ${fail} fallidas`);
|
|
process.exit(fail ? 1 : 0);
|