Files
pbakaus_impeccable/tests/launcher-download.test.mjs
T
Paul BakausandGitHub 6e61113e3c Fix: report removed launcher downloads accurately (#741)
* Fix: distinguish removed launcher downloads from checksum failures

Related to #740; keep the antivirus investigation open. Add executable launcher regressions and a native Windows CI lane. AI assistance: prepared with Codex under Paul Bakaus direction.

* Fix: diagnose downloads removed during cache placement

Cover removal and truncation around rename and preserve fail-closed behavior through cache placement. AI-assisted under maintainer direction.

* Test Windows launcher hash and placement failures

Inject failures at command boundaries in a staged test copy while retaining real launcher control flow. Cover both platforms with the same assertions. AI-assisted under maintainer direction.
2026-09-05 11:30:19 -07:00

141 lines
7.9 KiB
JavaScript

import assert from 'node:assert/strict';
import { spawn } from 'node:child_process';
import { createHash } from 'node:crypto';
import fs from 'node:fs';
import http from 'node:http';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { test } from 'node:test';
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
const WINDOWS = process.platform === 'win32';
const COMSPEC = process.env.ComSpec || process.env.COMSPEC || 'C:\\Windows\\System32\\cmd.exe';
// Use the host's command interpreter as a harmless Windows executable. No
// downloaded release binary is executed, and all network traffic is loopback.
const PAYLOAD = WINDOWS ? fs.readFileSync(COMSPEC) : Buffer.from('#!/bin/sh\necho verified-engine\n');
const HASH = createHash('sha256').update(PAYLOAD).digest('hex');
async function exercise(t, scenario) {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'impeccable-launcher-'));
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
const scripts = path.join(root, 'skill scripts');
const home = path.join(root, 'home');
const cache = path.join(root, 'cache');
fs.mkdirSync(scripts);
fs.mkdirSync(home);
fs.writeFileSync(path.join(scripts, 'VERSION'), '0.0.0-test\n');
const name = WINDOWS ? 'impeccable.cmd' : 'impeccable';
const launcher = path.join(scripts, name);
fs.copyFileSync(path.join(ROOT, 'skill/scripts', name), launcher);
const cacheDir = path.join(cache, 'bin', '0.0.0-test');
const tools = path.join(root, 'tools');
fs.mkdirSync(tools);
if (!WINDOWS && ['hash-failure', 'removed-during-hash'].includes(scenario)) {
fs.writeFileSync(path.join(tools, 'shasum'),
`#!/bin/sh\n${scenario === 'removed-during-hash' ? 'rm -f "$3"\n' : ''}printf '%s %s\\n' '${HASH}' "$3"\nexit ${scenario === 'hash-failure' ? 1 : 0}\n`,
{ mode: 0o755 });
}
const placementScenarios = ['removed-before-move', 'removed-after-move', 'emptied-after-move', 'move-failure'];
if (!WINDOWS && placementScenarios.includes(scenario)) {
const before = scenario === 'removed-before-move' ? 'rm -f "$2"\n' : '';
const after = scenario === 'removed-after-move' ? 'rm -f "$3"\n' : scenario === 'emptied-after-move' ? ': > "$3"\n' : '';
fs.writeFileSync(path.join(tools, 'mv'), scenario === 'move-failure' ? '#!/bin/sh\nexit 1\n' : `#!/bin/sh\n${before}/bin/mv "$@" || exit $?\n${after}`, { mode: 0o755 });
}
if (WINDOWS && (placementScenarios.includes(scenario) || ['hash-failure', 'removed-during-hash'].includes(scenario))) {
// move is a cmd builtin and certutil is an .exe: PATH shims cannot
// intercept them. Instrument ONLY the external-operation boundary in the
// staged test copy, preserving the launcher's real labels/checks/status
// handling. The separate valid case always runs the unmodified launcher.
const fault = path.join(tools, 'fault.cmd');
const hashFault = ['hash-failure', 'removed-during-hash'].includes(scenario);
const operation = hashFault
? 'certutil -hashfile "%cached%.part" SHA256 >"%cached%.sha256" 2>nul'
: 'move /y "%cached%.part" "%cached%" >nul 2>nul';
let script;
if (hashFault) {
script = `@echo off\n${scenario === 'removed-during-hash' ? 'del "%cached%.part" >nul 2>nul\n' : ''}echo hash header\necho ${HASH}\nexit /b 1\n`;
} else if (scenario === 'move-failure') {
script = '@echo off\nexit /b 1\n';
} else {
const before = scenario === 'removed-before-move' ? 'del "%cached%.part" >nul 2>nul\n' : '';
const after = scenario === 'removed-after-move' ? 'del "%cached%" >nul 2>nul\n' : scenario === 'emptied-after-move' ? 'type nul >"%cached%"\n' : '';
script = `@echo off\n${before}${operation}\nif errorlevel 1 exit /b 1\n${after}exit /b 0\n`;
}
fs.writeFileSync(fault, script.replaceAll('\n', '\r\n'));
const source = fs.readFileSync(launcher, 'utf8');
assert.equal(source.split(operation).length, 2, 'instrument exactly one operation');
const replacement = `call "${fault}"${hashFault ? ' >"%cached%.sha256" 2>nul' : ''}`;
fs.writeFileSync(launcher, source.replace(operation, replacement));
}
const requests = [];
const server = http.createServer((req, res) => {
requests.push(req.url);
if (req.url.endsWith('.sha256')) {
const part = fs.readdirSync(cacheDir).find(file => file.includes('.part'));
if (scenario === 'removed') fs.unlinkSync(path.join(cacheDir, part));
if (scenario === 'emptied') fs.truncateSync(path.join(cacheDir, part));
res.writeHead(scenario === 'no-sidecar' ? 404 : 200);
res.end(scenario === 'empty-sidecar' ? '' : `${scenario === 'mismatch' ? '0'.repeat(64) : HASH} engine\n`);
} else {
res.end(scenario === 'empty-download' ? '' : PAYLOAD);
}
});
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
t.after(() => new Promise(resolve => server.close(resolve)));
// Keep system tools, but exclude user/npm PATH candidates and all launcher
// overrides so the test cannot accidentally execute an installed engine.
const env = {
PATH: WINDOWS ? `${process.env.SystemRoot}\\System32;${process.env.SystemRoot}` : `${tools}:/usr/bin:/bin`,
HOME: home, USERPROFILE: home, TEMP: root, TMP: root,
IMPECCABLE_HOME: cache,
IMPECCABLE_DOWNLOAD_BASE: `http://127.0.0.1:${server.address().port}`,
...(WINDOWS ? { SystemRoot: process.env.SystemRoot, ComSpec: COMSPEC, PROCESSOR_ARCHITECTURE: 'AMD64' } : {}),
};
const result = await new Promise((resolve, reject) => {
const child = WINDOWS
? spawn(COMSPEC, ['/d', '/s', '/c', `""${launcher}" /d /c echo verified-engine"`], { env, cwd: root, windowsVerbatimArguments: true, timeout: 20000 })
: spawn('/bin/sh', [launcher], { env, cwd: root, timeout: 20000 });
let stdout = '';
let stderr = '';
child.stdout.on('data', data => { stdout += data; });
child.stderr.on('data', data => { stderr += data; });
child.on('error', reject);
child.on('close', (status, signal) => resolve({ status, signal, stdout, stderr }));
});
assert.equal(result.signal, null, JSON.stringify(result));
assert.equal(requests.filter(url => !url.endsWith('.sha256')).length, 1, 'one binary download, no verification retry loop');
return { ...result, files: fs.readdirSync(cacheDir), requests };
}
test('launcher downloads and runs a verified executable', async t => {
const result = await exercise(t, 'valid');
assert.equal(result.status, 0, result.stderr);
assert.match(result.stdout, /verified-engine/);
assert.deepEqual(result.files, [WINDOWS ? 'impeccable.exe' : 'impeccable']);
assert.equal(result.requests.length, 2);
});
for (const scenario of ['removed', 'emptied', 'empty-download', 'no-sidecar', 'empty-sidecar', 'mismatch', 'hash-failure', 'removed-during-hash', 'removed-before-move', 'removed-after-move', 'emptied-after-move', 'move-failure']) {
test(`launcher refuses ${scenario} with an accurate diagnostic`, async t => {
const result = await exercise(t, scenario);
assert.equal(result.status, 127, JSON.stringify(result));
assert.doesNotMatch(result.stdout, /verified-engine/);
assert.deepEqual(result.files, [], 'no unverified file or sidecar left behind');
if (scenario.startsWith('removed')) {
assert.match(result.stderr, /download completed but the file was removed before (verification|execution)/);
assert.match(result.stderr, /antivirus.*logs/i);
assert.doesNotMatch(result.stderr, /checksum mismatch/);
} else if (scenario.startsWith('emptied') || scenario === 'empty-download') {
assert.match(result.stderr, /downloaded file is empty/);
assert.doesNotMatch(result.stderr, /checksum mismatch/);
} else if (scenario === 'mismatch') {
assert.match(result.stderr, /checksum mismatch/);
} else if (scenario === 'move-failure') {
assert.match(result.stderr, /could not cache the verified download/);
} else {
assert.match(result.stderr, /refusing the unverified download/);
}
});
}