| 1 | #!/usr/bin/env node |
| 2 | // Checks a built vendor folder against web/js/ext2/vendor-manifest.json: |
| 3 | // exactly the files the manifest names and nothing else, the SRI of every |
| 4 | // .gz, the sha256 of every inflated file and of LICENSES.txt, and the path |
| 5 | // sweep. |
| 6 | // |
| 7 | // node check-output.mjs [<web dir>] |
| 8 | // the local build folder (deploy-vendor.sh runs it before uploading) |
| 9 | // node check-output.mjs --sums [<web dir>] |
| 10 | // the sha256 of each file as `sha256sum` prints it, from the local |
| 11 | // files once they are checked |
| 12 | // node check-output.mjs --files [<web dir>] |
| 13 | // the names of the files the manifest names, one per line (what |
| 14 | // deploy-vendor.sh uploads) |
| 15 | // node check-output.mjs --manifest-sums [<web dir>] |
| 16 | // the sums `sha256sum` must print on the server, from the manifest |
| 17 | // alone (no local build needed: deploy.sh uses it) |
| 18 | // node check-output.mjs --live [<web dir>] |
| 19 | // exit 0 when browsers will ask for this build: the manifest is enabled |
| 20 | // and web/js/ext2/config.js names a sandbox for a production Code origin |
| 21 | import fs from 'node:fs'; |
| 22 | import path from 'node:path'; |
| 23 | import zlib from 'node:zlib'; |
| 24 | import crypto from 'node:crypto'; |
| 25 | import { fileURLToPath, pathToFileURL } from 'node:url'; |
| 26 | import { sweepBuffers } from './sweep.mjs'; |
| 27 | |
| 28 | const HERE = path.dirname(fileURLToPath(import.meta.url)); |
| 29 | |
| 30 | const readManifest = webDir => JSON.parse(fs.readFileSync(path.join(webDir, 'js/ext2/vendor-manifest.json'), 'utf8')); |
| 31 | export const fileNames = manifest => [...manifest.files.map(f => f.name + '.gz'), manifest.licenses.name].sort(); |
| 32 | |
| 33 | // lo que `sha256sum` tiene que dar en el servidor, sacado solo del manifiesto |
| 34 | export function manifestSums(manifest) { |
| 35 | const sums = manifest.files.map(f => `${Buffer.from(f.sriGz.replace(/^sha256-/, ''), 'base64').toString('hex')} ${f.name}.gz`); |
| 36 | sums.push(`${manifest.licenses.sha256} ${manifest.licenses.name}`); |
| 37 | return sums.sort((a, b) => (a.slice(66) < b.slice(66) ? -1 : 1)); |
| 38 | } |
| 39 | |
| 40 | export function checkOutput(webDir) { |
| 41 | const manifest = readManifest(webDir); |
| 42 | const dir = path.join(webDir, 'vendor/x2', manifest.buildId); |
| 43 | const problems = []; |
| 44 | const sums = []; |
| 45 | const swept = []; |
| 46 | // nada más que lo que nombra el manifiesto: otro fichero junto al bundle |
| 47 | // (un .DS_Store, unas notas, una copia del editor) no se ha barrido |
| 48 | let present = []; |
| 49 | try { present = fs.readdirSync(dir); } catch { problems.push(`${dir} does not exist`); } |
| 50 | const named = new Set(fileNames(manifest)); |
| 51 | for (const f of present) if (!named.has(f)) problems.push(`${f} is not in the manifest (remove it: only the manifest's files are deployed)`); |
| 52 | for (const f of manifest.files) { |
| 53 | let gz; |
| 54 | try { gz = fs.readFileSync(path.join(dir, f.name + '.gz')); } catch { problems.push(`${f.name}.gz is missing`); continue; } |
| 55 | const digest = crypto.createHash('sha256').update(gz).digest(); |
| 56 | if ('sha256-' + digest.toString('base64') !== f.sriGz) problems.push(`${f.name}.gz does not match its SRI`); |
| 57 | if (gz.length !== f.gz) problems.push(`${f.name}.gz is ${gz.length} bytes, the manifest says ${f.gz}`); |
| 58 | let raw; |
| 59 | try { raw = zlib.gunzipSync(gz); } catch (e) { problems.push(`${f.name}.gz does not inflate (${String(e.message || e).slice(0, 80)})`); continue; } |
| 60 | if (crypto.createHash('sha256').update(raw).digest('hex') !== f.sha256Raw) problems.push(`${f.name} does not match its sha256`); |
| 61 | sums.push(`${digest.toString('hex')} ${f.name}.gz`); |
| 62 | swept.push({ name: f.name, bytes: raw }); |
| 63 | } |
| 64 | let lic = null; |
| 65 | try { lic = fs.readFileSync(path.join(dir, manifest.licenses.name)); } catch { problems.push(manifest.licenses.name + ' is missing'); } |
| 66 | if (lic) { |
| 67 | const h = crypto.createHash('sha256').update(lic).digest('hex'); |
| 68 | if (h !== manifest.licenses.sha256) problems.push(manifest.licenses.name + ' does not match its sha256'); |
| 69 | sums.push(`${h} ${manifest.licenses.name}`); |
| 70 | swept.push({ name: manifest.licenses.name, bytes: lic }); |
| 71 | } |
| 72 | const allow = JSON.parse(fs.readFileSync(path.join(HERE, 'sweep-allowlist.json'), 'utf8')); |
| 73 | const report = sweepBuffers(swept, allow, { buildRoot: HERE }); |
| 74 | problems.push(...report.problems); |
| 75 | return { manifest, dir, problems, sums: sums.sort((a, b) => (a.slice(66) < b.slice(66) ? -1 : 1)) }; |
| 76 | } |
| 77 | |
| 78 | // ¿Pedirá algún navegador este bundle? Solo si el manifiesto está encendido y |
| 79 | // config.js da un recinto a algún origen de producción de Code. |
| 80 | export async function live(webDir) { |
| 81 | const manifest = readManifest(webDir); |
| 82 | if (!manifest.enabled) return false; |
| 83 | const { sandboxOrigin } = await import(pathToFileURL(path.join(webDir, 'js/ext2/config.js')).href); |
| 84 | const spec = JSON.parse(fs.readFileSync(path.join(HERE, '../../sandbox/x2/headers.json'), 'utf8')); |
| 85 | return spec.environments.production.code.some(o => { const u = new URL(o); return !!sandboxOrigin({ hostname: u.hostname, origin: u.origin, port: u.port }); }); |
| 86 | } |
| 87 | |
| 88 | // (por su ruta real: /var y /tmp son enlaces en macOS) |
| 89 | if (process.argv[1] && fs.realpathSync(path.resolve(process.argv[1])) === fs.realpathSync(fileURLToPath(import.meta.url))) { |
| 90 | const args = process.argv.slice(2); |
| 91 | const webDir = path.resolve(args.find(a => !a.startsWith('--')) || path.join(HERE, '../../web')); |
| 92 | if (args.includes('--files')) { for (const f of fileNames(readManifest(webDir))) console.log(f); process.exit(0); } |
| 93 | if (args.includes('--manifest-sums')) { for (const s of manifestSums(readManifest(webDir))) console.log(s); process.exit(0); } |
| 94 | if (args.includes('--live')) process.exit((await live(webDir)) ? 0 : 3); |
| 95 | const r = checkOutput(webDir); |
| 96 | if (args.includes('--sums')) { if (!r.problems.length) for (const s of r.sums) console.log(s); } |
| 97 | else console.log(`${r.manifest.buildId}: ${r.problems.length ? r.problems.length + ' problems' : 'matches the manifest, sweep passed'}`); |
| 98 | for (const p of r.problems) console.error(' ✗ ' + p); |
| 99 | process.exit(r.problems.length ? 1 : 0); |
| 100 | } |