120 lines
4.6 KiB
JavaScript
120 lines
4.6 KiB
JavaScript
#!/usr/bin/env node
|
|
'use strict';
|
|
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const crypto = require('node:crypto');
|
|
|
|
const REPOSITORY_ROOT = path.resolve(__dirname, '..');
|
|
const MANIFEST_PATH = path.join(REPOSITORY_ROOT, 'tests/fixtures/protected-lib-manifest.json');
|
|
|
|
function readManifest() {
|
|
return JSON.parse(fs.readFileSync(MANIFEST_PATH, 'utf8'));
|
|
}
|
|
|
|
/** Read the entire tree without following symbolic links or writing bytecode. */
|
|
function inspectTree(directory) {
|
|
const entries = new Map();
|
|
function visit(absolutePath, relativePath) {
|
|
const stat = fs.lstatSync(absolutePath);
|
|
if (stat.isSymbolicLink()) {
|
|
entries.set(relativePath, { type: 'symlink' });
|
|
return;
|
|
}
|
|
if (stat.isDirectory()) {
|
|
entries.set(relativePath, { type: 'directory' });
|
|
for (const name of fs.readdirSync(absolutePath).sort()) {
|
|
visit(path.join(absolutePath, name), relativePath === '.' ? name : `${relativePath}/${name}`);
|
|
}
|
|
return;
|
|
}
|
|
if (stat.isFile()) {
|
|
entries.set(relativePath, {
|
|
type: 'file',
|
|
// Git's regular-file mode records the owner executable bit.
|
|
executable: Boolean(stat.mode & 0o100),
|
|
sha256: crypto.createHash('sha256').update(fs.readFileSync(absolutePath)).digest('hex'),
|
|
});
|
|
return;
|
|
}
|
|
entries.set(relativePath, { type: 'other' });
|
|
}
|
|
visit(directory, '.');
|
|
return entries;
|
|
}
|
|
|
|
/**
|
|
* Verify the fixed scripts/lib boundary against the reviewed baseline.
|
|
* No git dependency, imports of Python code, or filesystem writes are used.
|
|
*/
|
|
function verifyProtectedTree(repositoryRoot = REPOSITORY_ROOT, manifest = readManifest()) {
|
|
const errors = [];
|
|
if (manifest.version !== 1 || manifest.protectedRoot !== 'scripts/lib' || !Array.isArray(manifest.entries)) {
|
|
return { ok: false, errors: ['Invalid protected-library manifest.'], fileCount: 0 };
|
|
}
|
|
|
|
const expected = new Map();
|
|
for (const entry of manifest.entries) {
|
|
if (!entry || typeof entry.path !== 'string' || expected.has(entry.path) ||
|
|
(entry.path !== '.' && (path.posix.isAbsolute(entry.path) || entry.path.split('/').some(part => !part || part === '.' || part === '..'))) ||
|
|
!['file', 'directory'].includes(entry.type) ||
|
|
(entry.type === 'file' && (!/^[a-f0-9]{64}$/.test(entry.sha256) || typeof entry.executable !== 'boolean'))) {
|
|
return { ok: false, errors: ['Invalid protected-library manifest entry.'], fileCount: 0 };
|
|
}
|
|
expected.set(entry.path, entry);
|
|
}
|
|
if (expected.get('.')?.type !== 'directory') {
|
|
return { ok: false, errors: ['Protected-library manifest is missing its root directory.'], fileCount: 0 };
|
|
}
|
|
|
|
let actual;
|
|
try {
|
|
const scriptsDirectory = path.join(path.resolve(repositoryRoot), 'scripts');
|
|
const scriptsStat = fs.lstatSync(scriptsDirectory);
|
|
if (!scriptsStat.isDirectory() || scriptsStat.isSymbolicLink()) {
|
|
return { ok: false, errors: ['scripts must be a real directory, not a symbolic link.'], fileCount: 0 };
|
|
}
|
|
actual = inspectTree(path.join(scriptsDirectory, 'lib'));
|
|
} catch (error) {
|
|
return { ok: false, errors: [`Cannot inspect scripts/lib (${error.code || 'filesystem error'}).`], fileCount: 0 };
|
|
}
|
|
|
|
for (const [name, wanted] of expected) {
|
|
const found = actual.get(name);
|
|
const label = name === '.' ? 'scripts/lib' : `scripts/lib/${name}`;
|
|
if (!found) {
|
|
errors.push(`Missing protected entry: ${label}`);
|
|
} else if (found.type !== wanted.type) {
|
|
errors.push(`Protected entry type changed: ${label}`);
|
|
} else if (wanted.type === 'file') {
|
|
if (found.sha256 !== wanted.sha256) errors.push(`Protected file content changed: ${label}`);
|
|
if (found.executable !== wanted.executable) errors.push(`Protected file executable bit changed: ${label}`);
|
|
}
|
|
}
|
|
for (const name of actual.keys()) {
|
|
if (!expected.has(name)) errors.push(`Unexpected protected entry: scripts/lib/${name}`);
|
|
}
|
|
return {
|
|
ok: errors.length === 0,
|
|
errors,
|
|
fileCount: [...actual.values()].filter(entry => entry.type === 'file').length,
|
|
};
|
|
}
|
|
|
|
if (require.main === module) {
|
|
try {
|
|
const result = verifyProtectedTree();
|
|
if (result.ok) {
|
|
console.log(`Protected library verified: ${result.fileCount} files match the reviewed baseline; no added or missing entries.`);
|
|
} else {
|
|
for (const error of result.errors) console.error(error);
|
|
process.exitCode = 1;
|
|
}
|
|
} catch (error) {
|
|
console.error(`Protected-library verification failed (${error.code || 'invalid manifest'}).`);
|
|
process.exitCode = 1;
|
|
}
|
|
}
|
|
|
|
module.exports = { verifyProtectedTree, readManifest };
|