@@ -0,0 +1,91 @@
|
||||
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 = '<?php exit(99); // verification must never execute product code';
|
||||
await fs.writeFile(path.join(root, 'main.php'), source);
|
||||
await fs.writeFile(path.join(root, 'public/build/manifest.json'), '{}');
|
||||
await fs.writeFile(path.join(root, 'public/build/assets/a.js'), 'test');
|
||||
const manifest = { contract: 'compras.release.v1', product: 'compras', source_files: { 'main.php': hash(source) },
|
||||
frontend: { manifest_sha256: hash('{}'), files: { 'assets/a.js': hash('test') } }, components: [] };
|
||||
for (const [index, list] of [true, false].entries()) {
|
||||
const component = path.join(root, 'component-'+index);
|
||||
await fs.mkdir(component);
|
||||
await fs.writeFile(path.join(component, 'writer.php'), 'not executable');
|
||||
const files = list ? [{ file: 'writer.php', sha256: hash('not executable') }] : { 'writer.php': hash('not executable') };
|
||||
const bytes = JSON.stringify({ files });
|
||||
await fs.writeFile(path.join(component, 'manifest.json'), bytes);
|
||||
manifest.components.push({ setting: 'COMPONENT_'+index, manifest_sha256: hash(bytes) });
|
||||
}
|
||||
await fs.writeFile(path.join(root, '.env'), 'COMPRAS_RUNTIME_ENABLED="true"\n'+manifest.components.map((entry, index) => 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);
|
||||
}));
|
||||
Reference in New Issue
Block a user