scripts/publish-check
#!/usr/bin/env node
import { publicFiles, isPublicPath } from './public-files.mjs';
import { root } from './configure.mjs';
import { lstat, readFile, writeFile, mkdir, copyFile } from 'node:fs/promises';
import { createHash } from 'node:crypto';
import { resolve, dirname, relative, sep } from 'node:path';
const args = new Set(process.argv.slice(2));
const hashes = {};
// Check every parent before opening an allowlisted file; reject symlinks and
// junctions so a public filename cannot point into a private directory.
async function checkedPath(base, name, { allowMissing = false } = {}) {
if (!isPublicPath(name)) throw Error('Unsafe public allowlist entry; contents suppressed.');
let path = base;
for (const part of name.split('/')) {
path = resolve(path, part);
try { if ((await lstat(path)).isSymbolicLink()) throw Error('Symlink/junction in public path; refused.'); }
catch (error) { if (!(allowMissing && error.code === 'ENOENT')) throw error; }
}
const rel = relative(base, path);
if (rel.startsWith('..' + sep) || rel === '..') throw Error('Public path escaped project.');
return path;
}
try {
for (const name of publicFiles) {
const path = await checkedPath(root, name);
if (!(await lstat(path)).isFile()) throw Error(`Missing public file: ${name}`);
if (name !== 'docs/MANIFEST.json') hashes[name] = createHash('sha256').update(await readFile(path)).digest('hex');
}
if (args.has('--refresh-manifest')) await writeFile(resolve(root, 'docs/MANIFEST.json'), JSON.stringify(hashes, null, 2) + '\n', 'utf8');
const saved = JSON.parse(await readFile(resolve(root, 'docs/MANIFEST.json'), 'utf8'));
if (JSON.stringify(saved) !== JSON.stringify(hashes)) throw Error('Public file hashes changed. Run npm run publish-check -- --refresh-manifest, then review the list again.');
if (args.has('--json')) console.log(JSON.stringify({ files: publicFiles, count: publicFiles.length, policy: 'Only this allowlist; no .env files, caches, generated output or browser data.' }, null, 2));
else { console.log(`PUBLIC REPOSITORY: exactly ${publicFiles.length} files`); publicFiles.forEach(name => console.log(name)); console.log('EXCLUDED: all .env* (including examples), caches, output, dist, node_modules, vault, browser profiles and account data. No directory scanning or git staging performed.'); }
if (args.has('--export')) {
const destination = resolve(root, 'output/public-repo');
// Resolve and check output itself before any writes. Never follow a junction.
for (const dir of ['output']) {
const path = resolve(root, dir);
try { if ((await lstat(path)).isSymbolicLink()) throw Error('Export directory is a junction/symlink; refused.'); }
catch (error) { if (error.code !== 'ENOENT') throw error; }
await mkdir(path, { recursive: true });
}
// Refuse ANY existing export; it could contain files from an older run.
await mkdir(destination);
for (const name of publicFiles) {
const target = await checkedPath(destination, name, { allowMissing: true });
await mkdir(dirname(target), { recursive: true });
await copyFile(await checkedPath(root, name), target);
}
console.log('Exported exactly the allowlist to output/public-repo. Existing exports are never merged or overwritten.');
}
} catch (error) { console.error(error.code ? 'Publish check failed: missing or unreadable allowlisted file, or export already exists.' : error.message); process.exitCode = 1; }