import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs/promises'; import path from 'node:path'; import crypto from 'node:crypto'; import { spawnSync } from 'node:child_process'; const boundary = await fs.realpath('storage/framework/testing'); const verifier = path.resolve('scripts/verify-compras-release.php'); const hash = bytes => crypto.createHash('sha256').update(bytes).digest('hex'); async function fixture(run) { const root = await fs.mkdtemp(path.join(boundary, 'release-verifier-')); try { await fs.mkdir(path.join(root, 'public/build/assets'), { recursive: true }); const source = ' entry.setting+'="'+path.join(root, 'component-'+index).replaceAll('\\', '/')+'"').join('\n')); const save = async () => { delete manifest.release_id; manifest.release_id = 'compras-'+hash(JSON.stringify(manifest)).slice(0, 16); await fs.writeFile(path.join(root, 'compras-release.json'), JSON.stringify(manifest)); }; await save(); const verify = (...args) => spawnSync('php', [verifier, root, ...args], { encoding: 'utf8', windowsHide: true }); await run({ root, manifest, save, verify }); } finally { const resolved = await fs.realpath(root); assert.equal(path.dirname(resolved), boundary); assert.ok(path.basename(resolved).startsWith('release-verifier-')); await fs.rm(resolved, { recursive: true }); } } test('verifies one source, compiled assets and both component manifest formats without executing product code', () => fixture(async ({ verify }) => { const result = verify('--installed'); assert.equal(result.status, 0, result.stderr); assert.equal(JSON.parse(result.stdout).installed_components, 2); })); test('rejects changed source', () => fixture(async ({ root, verify }) => { await fs.writeFile(path.join(root, 'main.php'), 'changed'); assert.notEqual(verify().status, 0); })); test('accepts Git LF and Windows CRLF as the same source without accepting changed text', () => fixture(async ({ root, manifest, save, verify }) => { manifest.source_eol = 'lf'; manifest.source_files['main.php'] = hash('first\nsecond\n'); await save(); await fs.writeFile(path.join(root, 'main.php'), 'first\r\nsecond\r\n'); assert.equal(verify().status, 0); await fs.writeFile(path.join(root, 'main.php'), 'first\r\nchanged\r\n'); assert.notEqual(verify().status, 0); })); test('rejects a changed compiled asset', () => fixture(async ({ root, verify }) => { await fs.writeFile(path.join(root, 'public/build/assets/a.js'), 'changed'); assert.notEqual(verify('--compiled').status, 0); })); test('rejects a changed component', () => fixture(async ({ root, verify }) => { await fs.writeFile(path.join(root, 'component-1/writer.php'), 'changed'); assert.notEqual(verify('--installed').status, 0); })); test('rejects forged identity', () => fixture(async ({ root, manifest, verify }) => { manifest.release_id = 'compras-forged'; await fs.writeFile(path.join(root, 'compras-release.json'), JSON.stringify(manifest)); assert.notEqual(verify().status, 0); })); test('rejects path traversal even with a recomputed descriptor', () => fixture(async ({ manifest, save, verify }) => { manifest.source_files['../outside.php'] = hash('test'); await save(); assert.notEqual(verify().status, 0); })); test('rejects disabled installation', () => fixture(async ({ root, verify }) => { await fs.writeFile(path.join(root, '.env'), 'COMPRAS_RUNTIME_ENABLED=false'); assert.notEqual(verify('--installed').status, 0); })); test('rejects a missing descriptor', () => fixture(async ({ root, verify }) => { await fs.unlink(path.join(root, 'compras-release.json')); assert.notEqual(verify().status, 0); }));