* fix(application): retain completed evidence through cancellation Validate completed provider reports before classifying cancellation and preserve Evidence and captured diagnostics. Share MCP recording and delivery across custom observation handlers. Add source-owned completion and public SDK recording controls. Refs #3923, #3927, #4261. * fix(wasm): retain final observations through cancellation Keep completed Evidence after finalization and through CLI/MCP delivery. Preserve output and cleanup failure precedence. Refs #4254. * fix(browser): retain final observations and forward cancellation Classify final CDP detach and completed scenario cleanup at the provider boundary. Translate completed partials into canonical Evidence for MCP recording, and forward CLI cancellation to passive Electron tools. Refs #4255, #4256, #4257. * fix(ghidra): isolate cancellation of shared startup waiters Keep startup group-owned while each caller has an independent abortable wait. Await final-owner cleanup and preserve cleanup facts before retrying. Reuse the shared abortable promise helper. Refs #3983. * fix(mcp): keep shutdown registrations until cleanup succeeds Retain repeated event requests while unresolved owners need cleanup, and unregister only after success. Verify coalescing and failed-owner retry. Refs #4253. * fix(adb): own transfer snapshots and stage screen publication Bind transfer digests to the exact private input bytes and preserve caller APK names. Retain failed descriptors and private-root owners with typed cleanup evidence; publish captures exclusively after the cancellation cutoff. Refs #4251, #4252. * fix(verification): report ADB cleanup failures Always close after attempted verification and keep unexpected primary failures and cleanup details visible. Exercise the production script with controlled imports, including null and undefined throws. Refs #4260. * fix(runner): stop owned Windows commands on cancellation Use the retained ChildProcess handle for IPC cancellation and parent disconnection. Keep the SQLite lock until observed child close, and include native Windows regressions in the curated CI lane. Native Windows results remain pending. Refs #4258. * fix(website): canonicalize link containment boundaries Compare resolved targets with resolved repository and site roots. Accept directory aliases while preserving escape checks and keep fixtures within their temporary owner. Refs #4264. * docs(testing): cover completion races and cleanup retry Clarify validation-before-cancellation, independent startup waiter controls, producer and adapter coverage, and repeated shutdown events. * fix(filesystem): retain file owners and completed reads after close failure * fix(session): report retained snapshot staging in cleanup status Fixes #4277. Include both snapshot and profile file-resource scopes in the owning status fact, preserving publication receipts and cleanup retry. * docs(mcp): clarify recovery of raw partial observations Document the observed SDK cancellation boundary and the open design decision in #4276 without promoting raw errors to Evidence. * fix(kuna): own the complete installation inspection lifetime * test(kuna): cover session cleanup of shared cache resources * fix(hopper): isolate publisher operation resource admission * fix(hopper): preserve launcher cleanup failure classification * fix(adb): retain ordered cleanup and screen publication outcomes * fix(runner): retain Windows job ownership through cleanup retries * docs: clarify inspection admission and ordered cleanup regressions * fix(ghidra): preserve startup cleanup and request deadlines * fix(runner): supervise stdin errors and bootstrap native controls * docs: require queued lifecycle controls to survive admission * fix(cli): leave cleanup resolution with resource owners * test(adb): verify owned install snapshots through CLI * fix(errors): exclude resolved primary cleanup resources * fix(adb): resolve publication cleanup after its handle closes * fix(android): resolve JADX hash cleanup after owned close * refactor(android): share hash failure cause options * fix(kuna): gate commands on retained descriptor cleanup * docs: cover cleanup admission and owner-confirmed recovery * refactor(artifacts): bind cleanup errors to confirmed release * fix(adb): resolve recovered cleanup at retained owners * fix(kuna): resolve installation cleanup after owned close * fix(kuna): gate cache reads on retained hash cleanup * fix(setup): preserve publisher cleanup admission failures * fix(android): resolve the reported retirement cleanup error * test(runner): scope post-spawn fault control to POSIX * test(adb): cover persistent snapshot owner retirement * fix(kuna): preserve file outcomes with explicit cleanup ownership * fix(artifacts): resolve errors through retained cleanup owners * docs(lifecycle): carry cleanup ownership in typed outcomes * fix(adb): bind staged screen errors to their cleanup lease * fix(kuna): bind profile cleanup through a close capability * fix(artifacts): retain descriptor resources in cleanup errors * fix(pcap): bind existing errors to snapshot retirement * fix(windows): load source owner through its file URL * test(build): copy the command runner helper into fixtures * fix(mcp): own shared observation providers in the transport * fix(jadx): resolve every retained cleanup retry error * fix(kuna): bind retry errors to every retained cleanup owner * fix(adb): bind admission and shutdown errors to exact cleanup leases * fix(windows): resolve command paths before native ownership * test(publisher): assert exact descriptor recovery without release metadata * docs(lifecycle): require every retry error to follow its owners * fix(kuna): keep hash cleanup state stable across concurrent retries * test(windows): exercise surviving descendants with scoped shutdown deadlines * fix(uninstall): preserve primary read errors through cleanup failure * test(publisher): assert cleanup Result and exact owner recovery * test(windows): observe supervisor resources through IPC disconnect * fix(browser): retain typed cancellation after interrupted actions * test(publisher): assert ownership phases without retry-count coupling * fix(lifecycle): preserve abort reasons through shared provider execution * fix(jadx): preserve execution and late hash cancellation causes * fix(adb): compose late cancellation with exact cleanup failures * test(mcp): use the shared budget for session workflows
992 lines
32 KiB
TypeScript
992 lines
32 KiB
TypeScript
import { execFile } from "node:child_process";
|
|
import { createHash } from "node:crypto";
|
|
import { once } from "node:events";
|
|
import { promises as filesystem } from "node:fs";
|
|
import { syncBuiltinESMExports } from "node:module";
|
|
import {
|
|
access,
|
|
copyFile,
|
|
cp,
|
|
mkdir,
|
|
readFile,
|
|
readdir,
|
|
rm,
|
|
stat,
|
|
symlink,
|
|
writeFile,
|
|
} from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
import { promisify } from "node:util";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
import { createMessage, generateKey, readPrivateKey, sign } from "openpgp";
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
LinuxHopperPublisher,
|
|
linuxHopperReleaseCacheDirectory,
|
|
retryLinuxHopperPublisherCleanup,
|
|
} from "../../../src/application/LinuxHopperPublisher.js";
|
|
import {
|
|
HOPPER_LINUX_PRIMARY_FINGERPRINT,
|
|
type LinuxHopperRelease,
|
|
} from "../../../src/application/HopperReleaseFeed.js";
|
|
import * as signatures from "../../../src/process/DetachedPgpSignature.js";
|
|
import { downloadHopperPackage } from "../../../src/application/HopperDownload.js";
|
|
import { PrivateRuntimeRoot } from "../../../src/process/PrivateRuntimeRoot.js";
|
|
import { OwnedFileHandle } from "../../../src/filesystem/OwnedFileHandle.js";
|
|
import * as leases from "../../../src/process/HopperLease.js";
|
|
import * as nativeCommands from "../../../src/process/ExecFileOutput.js";
|
|
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
|
import { createTestProcessRegistry } from "../../support/process/processFixture.js";
|
|
|
|
const run = promisify(execFile);
|
|
const verifySignature = signatures.verifyDetachedPgpSignature;
|
|
const sha256 = (bytes: Uint8Array) =>
|
|
createHash("sha256").update(bytes).digest("hex");
|
|
const linux = it.skipIf(process.platform !== "linux");
|
|
const portableTar = it.skipIf(process.platform === "win32");
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks();
|
|
vi.unstubAllGlobals();
|
|
});
|
|
|
|
/** Real native archives and cryptography; substitute only the test publisher identity. */
|
|
const fixture = async (family: "deb" | "arch" = "deb") => {
|
|
const directory = await createTestTempDirectory("rea-linux-publisher-");
|
|
const packageRoot = join(directory, "package-root");
|
|
const launcher = join(packageRoot, "opt/hopper/bin/Hopper");
|
|
await mkdir(join(packageRoot, "opt/hopper/bin"), { recursive: true });
|
|
await copyFile("/usr/bin/true", launcher);
|
|
const archive = join(directory, "archive");
|
|
if (family === "deb") {
|
|
await mkdir(join(packageRoot, "DEBIAN"));
|
|
await writeFile(
|
|
join(packageRoot, "DEBIAN/control"),
|
|
"Package: hopper-fixture\nVersion: 1.0\nArchitecture: amd64\nMaintainer: Fixture\nDescription: Signature boundary fixture\n",
|
|
);
|
|
await run("dpkg-deb", ["--build", packageRoot, archive]);
|
|
} else await run("tar", ["-cJf", archive, "-C", packageRoot, "opt"]);
|
|
const bytes = await readFile(archive);
|
|
const generated = await generateKey({
|
|
type: "ecc",
|
|
curve: "curve25519Legacy",
|
|
userIDs: [{ name: "Fixture publisher" }],
|
|
format: "armored",
|
|
});
|
|
const key = await readPrivateKey({ armoredKey: generated.privateKey });
|
|
const signature = await sign({
|
|
message: await createMessage({ binary: bytes }),
|
|
signingKeys: key,
|
|
detached: true,
|
|
format: "binary",
|
|
});
|
|
const release: LinuxHopperRelease = {
|
|
version: "1.0",
|
|
filename: `https://www.hopperapp.com:443/downloader/public/Hopper-1.0-Linux-demo.${family === "deb" ? "deb" : "pkg.tar.xz"}`,
|
|
file_length: String(bytes.length),
|
|
file_hash: createHash("sha1").update(bytes).digest("hex"),
|
|
main_exe_hash: sha256(await readFile(launcher)),
|
|
signature: Buffer.from(signature).toString("base64"),
|
|
public: "1",
|
|
beta: "0",
|
|
};
|
|
const publisher = new LinuxHopperPublisher(
|
|
{ ...process.env, XDG_CACHE_HOME: join(directory, "cache") },
|
|
async () => family,
|
|
);
|
|
const cache = join(publisher.cacheRoot, release.main_exe_hash);
|
|
return {
|
|
publisher,
|
|
archive,
|
|
launcher,
|
|
release,
|
|
cache,
|
|
publicKey: generated.publicKey,
|
|
fingerprint: key.getFingerprint().toUpperCase(),
|
|
};
|
|
};
|
|
|
|
const admitFixturePublisher = (
|
|
fingerprint: string,
|
|
module: typeof signatures = signatures,
|
|
) =>
|
|
vi
|
|
.spyOn(module, "verifyDetachedPgpSignature")
|
|
.mockImplementation((bytes, signature, key, primary, signal) => {
|
|
expect(primary).toBe(HOPPER_LINUX_PRIMARY_FINGERPRINT);
|
|
return verifySignature(bytes, signature, key, fingerprint, signal);
|
|
});
|
|
|
|
it.each([undefined, "", "/selected/cache", "relative/cache"])(
|
|
"resolves the selected XDG cache setting %j once for every consumer",
|
|
(setting) => {
|
|
const selected = linuxHopperReleaseCacheDirectory({
|
|
HOME: "/caller/home",
|
|
...(setting === undefined ? {} : { XDG_CACHE_HOME: setting }),
|
|
});
|
|
if (setting === "relative/cache")
|
|
expect(selected).toMatchObject({ ok: false });
|
|
else
|
|
expect(selected).toEqual({
|
|
ok: true,
|
|
value: join(setting || "/caller/home/.cache", "rea/hopper-releases"),
|
|
});
|
|
},
|
|
);
|
|
|
|
describe("Linux signed package retention boundary", () => {
|
|
linux(
|
|
"rejects a cryptographically valid package signed by another publisher",
|
|
async () => {
|
|
const f = await fixture();
|
|
try {
|
|
expect(
|
|
await f.publisher.verifyArchive(
|
|
"deb",
|
|
f.release,
|
|
f.archive,
|
|
undefined,
|
|
f.publicKey,
|
|
),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: { reason: "publisher_verification_failed" },
|
|
});
|
|
await expect(access(f.publisher.cacheRoot)).rejects.toMatchObject({
|
|
code: "ENOENT",
|
|
});
|
|
} finally {
|
|
await f.publisher.close();
|
|
}
|
|
},
|
|
);
|
|
|
|
linux.each(["deb", "arch"] as const)(
|
|
"authenticates a real %s payload and verifies a retained install without network",
|
|
async (family) => {
|
|
const f = await fixture(family);
|
|
admitFixturePublisher(f.fingerprint);
|
|
const network = vi.fn(() => {
|
|
throw new Error("Offline");
|
|
});
|
|
vi.stubGlobal("fetch", network);
|
|
try {
|
|
const verified = await f.publisher.verifyArchive(
|
|
family,
|
|
f.release,
|
|
f.archive,
|
|
undefined,
|
|
f.publicKey,
|
|
);
|
|
if (!verified.ok) throw verified.error;
|
|
expect(
|
|
await f.publisher.retainVerifiedArchive(verified.value, f.archive),
|
|
).toEqual({ ok: true, value: undefined });
|
|
await f.publisher.close();
|
|
// A new instance alone still shares process-local cryptographic proofs.
|
|
// Reload the owner and assert actual PGP and native payload inspection.
|
|
vi.resetModules();
|
|
const coldSignatures =
|
|
await import("../../../src/process/DetachedPgpSignature.js");
|
|
const signature = admitFixturePublisher(f.fingerprint, coldSignatures);
|
|
const coldCommands =
|
|
await import("../../../src/process/ExecFileOutput.js");
|
|
const extraction = vi.spyOn(coldCommands, "execFileOutput");
|
|
const { LinuxHopperPublisher: ColdPublisher } =
|
|
await import("../../../src/application/LinuxHopperPublisher.js");
|
|
const fresh = new ColdPublisher(
|
|
f.publisher.environment,
|
|
async () => family,
|
|
);
|
|
try {
|
|
expect(await fresh.inspectLauncher(f.launcher)).toMatchObject({
|
|
ok: true,
|
|
value: {
|
|
sha256: f.release.main_exe_hash,
|
|
primaryFingerprint: f.fingerprint,
|
|
source: "signed_package_cache",
|
|
},
|
|
});
|
|
expect(network).not.toHaveBeenCalled();
|
|
expect(signature).toHaveBeenCalledOnce();
|
|
expect(extraction).toHaveBeenCalledWith(
|
|
family === "deb" ? "dpkg-deb" : "tar",
|
|
expect.any(Array),
|
|
expect.any(Object),
|
|
);
|
|
} finally {
|
|
await fresh.close();
|
|
}
|
|
} finally {
|
|
await f.publisher.close();
|
|
}
|
|
},
|
|
);
|
|
});
|
|
|
|
linux.each([
|
|
"archive",
|
|
"metadata",
|
|
"missing metadata",
|
|
"missing key",
|
|
"malformed key",
|
|
"missing archive",
|
|
"malformed JSON",
|
|
"empty directory",
|
|
"symlink archive",
|
|
])("fails closed on retained %s tampering", async (kind) => {
|
|
const f = await fixture();
|
|
admitFixturePublisher(f.fingerprint);
|
|
try {
|
|
const verified = await f.publisher.verifyArchive(
|
|
"deb",
|
|
f.release,
|
|
f.archive,
|
|
undefined,
|
|
f.publicKey,
|
|
);
|
|
if (!verified.ok) throw verified.error;
|
|
const retained = await f.publisher.retainVerifiedArchive(
|
|
verified.value,
|
|
f.archive,
|
|
);
|
|
if (!retained.ok) throw retained.error;
|
|
if (kind === "archive") {
|
|
const tampered = Buffer.from("tampered");
|
|
await writeFile(join(f.cache, "archive"), tampered);
|
|
const metadata = JSON.parse(
|
|
await readFile(join(f.cache, "release.json"), "utf8"),
|
|
);
|
|
metadata.release.file_length = String(tampered.length);
|
|
metadata.release.file_hash = createHash("sha1")
|
|
.update(tampered)
|
|
.digest("hex");
|
|
await writeFile(join(f.cache, "release.json"), JSON.stringify(metadata));
|
|
}
|
|
if (kind === "metadata") {
|
|
const metadata = JSON.parse(
|
|
await readFile(join(f.cache, "release.json"), "utf8"),
|
|
);
|
|
metadata.release.main_exe_hash = "a".repeat(64);
|
|
await writeFile(join(f.cache, "release.json"), JSON.stringify(metadata));
|
|
}
|
|
if (kind === "missing metadata") await rm(join(f.cache, "release.json"));
|
|
if (kind === "missing key") await rm(join(f.cache, "public-key.asc"));
|
|
if (kind === "malformed key")
|
|
await writeFile(join(f.cache, "public-key.asc"), Buffer.from([0xff]));
|
|
if (kind === "missing archive") await rm(join(f.cache, "archive"));
|
|
if (kind === "malformed JSON")
|
|
await writeFile(join(f.cache, "release.json"), "{broken JSON");
|
|
if (kind === "empty directory")
|
|
await Promise.all(
|
|
["release.json", "public-key.asc", "archive"].map((name) =>
|
|
rm(join(f.cache, name)),
|
|
),
|
|
);
|
|
if (kind === "symlink archive") {
|
|
await rm(join(f.cache, "archive"));
|
|
await symlink(f.archive, join(f.cache, "archive"));
|
|
}
|
|
const network = vi.fn(() => {
|
|
throw new Error("Must preserve the cache failure");
|
|
});
|
|
vi.stubGlobal("fetch", network);
|
|
expect(await f.publisher.inspectLauncher(f.launcher)).toMatchObject({
|
|
ok: false,
|
|
});
|
|
expect(network).not.toHaveBeenCalled();
|
|
// An explicitly approved fresh package can repair this entry while
|
|
// preserving the original evidence and keeping ordinary reads strict.
|
|
expect(
|
|
await f.publisher.retainVerifiedArchive(verified.value, f.archive),
|
|
).toEqual({ ok: true, value: undefined });
|
|
const backups = (await readdir(f.publisher.cacheRoot)).filter((name) =>
|
|
name.startsWith(`${f.release.main_exe_hash}.backup-`),
|
|
);
|
|
expect(backups).toHaveLength(1);
|
|
if (kind === "missing metadata" || kind === "empty directory")
|
|
await expect(
|
|
access(join(f.publisher.cacheRoot, backups[0]!, "release.json")),
|
|
).rejects.toMatchObject({ code: "ENOENT" });
|
|
else
|
|
expect(
|
|
await readFile(
|
|
join(f.publisher.cacheRoot, backups[0]!, "release.json"),
|
|
"utf8",
|
|
),
|
|
).toBeTruthy();
|
|
expect(await f.publisher.inspectLauncher(f.launcher)).toMatchObject({
|
|
ok: true,
|
|
});
|
|
expect(
|
|
await f.publisher.retainVerifiedArchive(verified.value, f.archive),
|
|
).toEqual({ ok: true, value: undefined });
|
|
expect(
|
|
(await readdir(f.publisher.cacheRoot)).filter((name) =>
|
|
name.includes(".backup-"),
|
|
),
|
|
).toEqual(backups);
|
|
expect(network).not.toHaveBeenCalled();
|
|
} finally {
|
|
await f.publisher.close();
|
|
}
|
|
});
|
|
|
|
linux.each([false, true])(
|
|
"serializes real cross-process repair and cancellation: %s",
|
|
async (cancel) => {
|
|
const f = await fixture();
|
|
admitFixturePublisher(f.fingerprint);
|
|
const processes = createTestProcessRegistry();
|
|
const controller = new AbortController();
|
|
let retention:
|
|
| ReturnType<LinuxHopperPublisher["retainVerifiedArchive"]>
|
|
| undefined;
|
|
try {
|
|
const verified = await f.publisher.verifyArchive(
|
|
"deb",
|
|
f.release,
|
|
f.archive,
|
|
undefined,
|
|
f.publicKey,
|
|
);
|
|
if (!verified.ok) throw verified.error;
|
|
expect(
|
|
await f.publisher.retainVerifiedArchive(verified.value, f.archive),
|
|
).toEqual({ ok: true, value: undefined });
|
|
const staged = join(f.publisher.cacheRoot, "authenticated-fixture-stage");
|
|
await cp(f.cache, staged, { recursive: true });
|
|
const broken = "{interrupted cache record";
|
|
await writeFile(join(f.cache, "release.json"), broken);
|
|
const child = processes.spawn(process.execPath, [
|
|
fileURLToPath(
|
|
new URL("../../fixtures/hopperCachePublisher.mjs", import.meta.url),
|
|
),
|
|
f.publisher.cacheRoot,
|
|
f.release.main_exe_hash,
|
|
staged,
|
|
]);
|
|
let stderr = "";
|
|
child.stderr.on("data", (chunk: Buffer) => {
|
|
stderr += chunk.toString();
|
|
});
|
|
const exited = once(child, "close");
|
|
const ready = await Promise.race([
|
|
once(child.stdout, "data").then(([bytes]) => {
|
|
if (!Buffer.isBuffer(bytes))
|
|
throw new Error("Expected fixture readiness bytes");
|
|
return JSON.parse(bytes.toString());
|
|
}),
|
|
exited.then(([code]) => {
|
|
throw new Error(
|
|
`Fixture exited before acquiring its lease: ${String(code)} ${stderr}`,
|
|
);
|
|
}),
|
|
]);
|
|
expect(ready).toEqual({ held: true });
|
|
let notifyBusy: (() => void) | undefined;
|
|
const busy = new Promise<void>((resolve) => {
|
|
notifyBusy = resolve;
|
|
});
|
|
const acquire = leases.acquireHopperReleaseCacheLease;
|
|
vi.spyOn(leases, "acquireHopperReleaseCacheLease").mockImplementation(
|
|
async (input) => {
|
|
const observed = await acquire(input);
|
|
if (!observed.acquired) notifyBusy?.();
|
|
return observed;
|
|
},
|
|
);
|
|
retention = f.publisher.retainVerifiedArchive(
|
|
verified.value,
|
|
f.archive,
|
|
controller.signal,
|
|
);
|
|
await Promise.race([
|
|
busy,
|
|
retention.then(() => {
|
|
throw new Error("Retention bypassed the other process's cache lease");
|
|
}),
|
|
]);
|
|
expect(await readFile(join(f.cache, "release.json"), "utf8")).toBe(
|
|
broken,
|
|
);
|
|
expect(
|
|
(await readdir(f.publisher.cacheRoot)).some((name) =>
|
|
name.includes(".backup-"),
|
|
),
|
|
).toBe(false);
|
|
if (cancel) {
|
|
controller.abort();
|
|
expect(await retention).toMatchObject({
|
|
ok: false,
|
|
error: { reason: "cancelled" },
|
|
});
|
|
child.stdin.end("release\n");
|
|
} else {
|
|
child.stdin.end("publish\n");
|
|
expect(await retention).toEqual({ ok: true, value: undefined });
|
|
expect(await f.publisher.inspectLauncher(f.launcher)).toMatchObject({
|
|
ok: true,
|
|
});
|
|
expect(
|
|
(await readdir(f.publisher.cacheRoot)).filter((name) =>
|
|
name.includes(".backup-"),
|
|
),
|
|
).toHaveLength(1);
|
|
}
|
|
expect(await exited).toEqual([0, null]);
|
|
} finally {
|
|
controller.abort();
|
|
await processes.shutdown();
|
|
await retention;
|
|
await f.publisher.close();
|
|
}
|
|
},
|
|
);
|
|
|
|
linux("restores old evidence when replacement publication fails", async () => {
|
|
const f = await fixture();
|
|
admitFixturePublisher(f.fingerprint);
|
|
try {
|
|
const verified = await f.publisher.verifyArchive(
|
|
"deb",
|
|
f.release,
|
|
f.archive,
|
|
undefined,
|
|
f.publicKey,
|
|
);
|
|
if (!verified.ok) throw verified.error;
|
|
expect(
|
|
await f.publisher.retainVerifiedArchive(verified.value, f.archive),
|
|
).toEqual({ ok: true, value: undefined });
|
|
const broken = "{interrupted cache metadata";
|
|
await writeFile(join(f.cache, "release.json"), broken);
|
|
const rename = filesystem.rename;
|
|
const denied = Object.assign(new Error("Fixture publication failure"), {
|
|
code: "EIO",
|
|
});
|
|
let backup: string | undefined;
|
|
const publication = vi
|
|
.spyOn(filesystem, "rename")
|
|
.mockImplementation(async (from, to) => {
|
|
if (backup !== undefined && to === f.cache && from !== backup)
|
|
throw denied;
|
|
await rename(from, to);
|
|
if (from === f.cache && typeof to === "string") backup = to;
|
|
});
|
|
syncBuiltinESMExports();
|
|
try {
|
|
const result = await f.publisher.retainVerifiedArchive(
|
|
verified.value,
|
|
f.archive,
|
|
);
|
|
expect(result).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
reason: "verification_unavailable",
|
|
message: expect.stringContaining(f.cache),
|
|
},
|
|
});
|
|
if (result.ok) throw new Error("Expected publication failure");
|
|
expect(result.error.cause).toBeInstanceOf(AggregateError);
|
|
if (result.error.cause instanceof AggregateError)
|
|
expect(result.error.cause.errors).toContain(denied);
|
|
expect(await readFile(join(f.cache, "release.json"), "utf8")).toBe(
|
|
broken,
|
|
);
|
|
expect(
|
|
(await readdir(f.publisher.cacheRoot)).some((name) =>
|
|
name.includes(".backup-"),
|
|
),
|
|
).toBe(false);
|
|
} finally {
|
|
publication.mockRestore();
|
|
syncBuiltinESMExports();
|
|
}
|
|
expect(
|
|
await f.publisher.retainVerifiedArchive(verified.value, f.archive),
|
|
).toEqual({ ok: true, value: undefined });
|
|
expect(await f.publisher.inspectLauncher(f.launcher)).toMatchObject({
|
|
ok: true,
|
|
});
|
|
} finally {
|
|
await f.publisher.close();
|
|
}
|
|
});
|
|
|
|
linux(
|
|
"preserves existing evidence when native cache inspection is unavailable",
|
|
async () => {
|
|
const f = await fixture();
|
|
admitFixturePublisher(f.fingerprint);
|
|
try {
|
|
const verified = await f.publisher.verifyArchive(
|
|
"deb",
|
|
f.release,
|
|
f.archive,
|
|
undefined,
|
|
f.publicKey,
|
|
);
|
|
if (!verified.ok) throw verified.error;
|
|
expect(
|
|
await f.publisher.retainVerifiedArchive(verified.value, f.archive),
|
|
).toEqual({ ok: true, value: undefined });
|
|
await writeFile(join(f.cache, "public-key.asc"), f.publicKey + "\n");
|
|
const metadata = await readFile(join(f.cache, "release.json"));
|
|
const unavailable = vi
|
|
.spyOn(nativeCommands, "execFileOutput")
|
|
.mockRejectedValue(new Error("Fixture native inspection unavailable"));
|
|
expect(
|
|
await f.publisher.retainVerifiedArchive(verified.value, f.archive),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: { reason: "verification_unavailable" },
|
|
});
|
|
expect(await readFile(join(f.cache, "release.json"))).toEqual(metadata);
|
|
expect(
|
|
(await readdir(f.publisher.cacheRoot)).some((name) =>
|
|
name.includes(".backup-"),
|
|
),
|
|
).toBe(false);
|
|
unavailable.mockRestore();
|
|
expect(await f.publisher.inspectLauncher(f.launcher)).toMatchObject({
|
|
ok: true,
|
|
});
|
|
} finally {
|
|
await f.publisher.close();
|
|
}
|
|
},
|
|
);
|
|
|
|
linux(
|
|
"rejects a feed digest not represented in the signed payload",
|
|
async () => {
|
|
const f = await fixture();
|
|
admitFixturePublisher(f.fingerprint);
|
|
try {
|
|
expect(
|
|
await f.publisher.verifyArchive(
|
|
"deb",
|
|
{ ...f.release, main_exe_hash: "a".repeat(64) },
|
|
f.archive,
|
|
undefined,
|
|
f.publicKey,
|
|
),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: {
|
|
reason: "integrity",
|
|
message: expect.stringContaining(f.release.main_exe_hash),
|
|
},
|
|
});
|
|
expect(
|
|
await f.publisher.verifyArchive(
|
|
"deb",
|
|
f.release,
|
|
f.archive,
|
|
AbortSignal.abort(),
|
|
f.publicKey,
|
|
),
|
|
).toMatchObject({ ok: false, error: { reason: "cancelled" } });
|
|
} finally {
|
|
await f.publisher.close();
|
|
}
|
|
},
|
|
);
|
|
|
|
linux(
|
|
"retains a failed publication lease until its real socket owner closes",
|
|
async () => {
|
|
const f = await fixture();
|
|
admitFixturePublisher(f.fingerprint);
|
|
const acquire = leases.acquireHopperReleaseCacheLease;
|
|
let allowCleanup = false;
|
|
vi.spyOn(leases, "acquireHopperReleaseCacheLease").mockImplementation(
|
|
async (input) => {
|
|
const acquired = await acquire(input);
|
|
if (!acquired.acquired) return acquired;
|
|
return {
|
|
acquired: true as const,
|
|
lease: {
|
|
...acquired.lease,
|
|
release: async () => {
|
|
if (!allowCleanup)
|
|
throw new Error("Fixture lease close unavailable");
|
|
await acquired.lease.release();
|
|
},
|
|
},
|
|
};
|
|
},
|
|
);
|
|
try {
|
|
const verified = await f.publisher.verifyArchive(
|
|
"deb",
|
|
f.release,
|
|
f.archive,
|
|
undefined,
|
|
f.publicKey,
|
|
);
|
|
if (!verified.ok) throw verified.error;
|
|
expect(
|
|
await f.publisher.retainVerifiedArchive(verified.value, f.archive),
|
|
).toMatchObject({ ok: false, error: { reason: "cleanup_failed" } });
|
|
const input = {
|
|
cacheDirectory: f.publisher.cacheRoot,
|
|
launcherSha256: f.release.main_exe_hash,
|
|
runId: "fixture-competing-writer",
|
|
};
|
|
expect(await acquire(input)).toMatchObject({ acquired: false });
|
|
await expect(f.publisher.close()).rejects.toMatchObject({
|
|
cleanup: { resources: [expect.stringContaining(".sock")] },
|
|
});
|
|
allowCleanup = true;
|
|
await f.publisher.close();
|
|
const recovered = await acquire(input);
|
|
if (!recovered.acquired)
|
|
throw new Error("Expected the released real cache lease");
|
|
await recovered.lease.release();
|
|
} finally {
|
|
allowCleanup = true;
|
|
await f.publisher.close();
|
|
}
|
|
},
|
|
);
|
|
|
|
it("bounds a stalled metadata fetch and preserves caller cancellation", async () => {
|
|
vi.stubGlobal(
|
|
"fetch",
|
|
vi.fn(
|
|
(_url, options: RequestInit) =>
|
|
new Promise((_resolve, reject) => {
|
|
const signal = options.signal;
|
|
signal?.addEventListener("abort", () => reject(signal.reason), {
|
|
once: true,
|
|
});
|
|
signal?.throwIfAborted();
|
|
}),
|
|
),
|
|
);
|
|
await expect(
|
|
downloadHopperPackage("https://www.hopperapp.com/fixture", {
|
|
timeoutMs: 10,
|
|
}),
|
|
).rejects.toMatchObject({ name: "TimeoutError" });
|
|
const controller = new AbortController();
|
|
const reason = new Error("Caller cancelled metadata inspection");
|
|
const pending = downloadHopperPackage("https://www.hopperapp.com/fixture", {
|
|
signal: controller.signal,
|
|
});
|
|
controller.abort(reason);
|
|
await expect(pending).rejects.toBe(reason);
|
|
});
|
|
it("cancels an oversized chunked metadata response before retaining it", async () => {
|
|
const cancelled = vi.fn();
|
|
vi.stubGlobal(
|
|
"fetch",
|
|
vi.fn(
|
|
async () =>
|
|
new Response(
|
|
new ReadableStream({
|
|
start(controller) {
|
|
controller.enqueue(new Uint8Array(4));
|
|
},
|
|
cancel: cancelled,
|
|
}),
|
|
),
|
|
),
|
|
);
|
|
expect(
|
|
await downloadHopperPackage("https://www.hopperapp.com/fixture", {
|
|
maxBytes: 3,
|
|
}),
|
|
).toEqual({ ok: false, bytes: new Uint8Array() });
|
|
expect(cancelled).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
portableTar(
|
|
"retains the temporary root until its failed hash descriptor closes",
|
|
async () => {
|
|
const f = await fixture("arch");
|
|
admitFixturePublisher(f.fingerprint);
|
|
const events: string[] = [];
|
|
let targetCloseAttempts = 0;
|
|
let failTargetClose = true;
|
|
const targetSize = (await stat(f.launcher)).size;
|
|
let temporaryRoot: string | undefined;
|
|
const originalFileClose = OwnedFileHandle.prototype.close;
|
|
const fileClose = vi
|
|
.spyOn(OwnedFileHandle.prototype, "close")
|
|
.mockImplementation(async function (this: OwnedFileHandle) {
|
|
if ((await this.handle.stat()).size === targetSize) {
|
|
targetCloseAttempts += 1;
|
|
if (failTargetClose) {
|
|
events.push(`hash-close-failed-${targetCloseAttempts}`);
|
|
throw new Error("Fixture extracted launcher close unavailable");
|
|
}
|
|
events.push("hash-close-retried");
|
|
}
|
|
return originalFileClose.call(this);
|
|
});
|
|
const originalRootClose = PrivateRuntimeRoot.prototype.close;
|
|
const originalRootCreate = PrivateRuntimeRoot.create;
|
|
const rootCreate = vi
|
|
.spyOn(PrivateRuntimeRoot, "create")
|
|
.mockImplementation(async (options) => {
|
|
const root = await originalRootCreate.call(PrivateRuntimeRoot, options);
|
|
temporaryRoot = root.path;
|
|
return root;
|
|
});
|
|
const rootClose = vi
|
|
.spyOn(PrivateRuntimeRoot.prototype, "close")
|
|
.mockImplementation(async function (this: PrivateRuntimeRoot) {
|
|
temporaryRoot = this.path;
|
|
events.push("temporary-root-close");
|
|
return originalRootClose.call(this);
|
|
});
|
|
try {
|
|
expect(
|
|
await f.publisher.verifyArchive(
|
|
"arch",
|
|
f.release,
|
|
f.archive,
|
|
undefined,
|
|
f.publicKey,
|
|
),
|
|
).toMatchObject({ ok: false, error: { reason: "cleanup_failed" } });
|
|
expect(targetCloseAttempts).toBeGreaterThanOrEqual(2);
|
|
expect(events).not.toContain("temporary-root-close");
|
|
const attemptsBeforeClose = targetCloseAttempts;
|
|
await expect(f.publisher.close()).rejects.toThrow();
|
|
expect(targetCloseAttempts).toBeGreaterThan(attemptsBeforeClose);
|
|
expect(events).not.toContain("temporary-root-close");
|
|
expect(temporaryRoot).toBeDefined();
|
|
await expect(access(temporaryRoot!)).resolves.toBeUndefined();
|
|
failTargetClose = false;
|
|
await f.publisher.close();
|
|
expect(events.slice(-2)).toEqual([
|
|
"hash-close-retried",
|
|
"temporary-root-close",
|
|
]);
|
|
} finally {
|
|
fileClose.mockRestore();
|
|
rootClose.mockRestore();
|
|
rootCreate.mockRestore();
|
|
await f.publisher.close();
|
|
}
|
|
},
|
|
);
|
|
|
|
linux("retains and retries failed verification cleanup", async () => {
|
|
const f = await fixture();
|
|
admitFixturePublisher(f.fingerprint);
|
|
const cleanup = vi
|
|
.spyOn(PrivateRuntimeRoot.prototype, "close")
|
|
.mockRejectedValueOnce(new Error("Fixture cleanup unavailable"));
|
|
try {
|
|
expect(
|
|
await f.publisher.verifyArchive(
|
|
"deb",
|
|
f.release,
|
|
f.archive,
|
|
undefined,
|
|
f.publicKey,
|
|
),
|
|
).toMatchObject({ ok: false, error: { reason: "cleanup_failed" } });
|
|
expect(cleanup).toHaveBeenCalledOnce();
|
|
await f.publisher.close();
|
|
expect(cleanup).toHaveBeenCalledTimes(2);
|
|
} finally {
|
|
await f.publisher.close();
|
|
}
|
|
});
|
|
|
|
portableTar(
|
|
"reports pending publisher cleanup as a typed failure before retrying the operation",
|
|
async () => {
|
|
const f = await fixture("arch");
|
|
admitFixturePublisher(f.fingerprint);
|
|
const closeRoot = PrivateRuntimeRoot.prototype.close;
|
|
let rootCloseAttempts = 0;
|
|
vi.spyOn(PrivateRuntimeRoot.prototype, "close").mockImplementation(
|
|
async function (this: PrivateRuntimeRoot) {
|
|
rootCloseAttempts += 1;
|
|
if (rootCloseAttempts < 3)
|
|
throw new Error("Fixture cleanup remains unavailable");
|
|
return await closeRoot.apply(this);
|
|
},
|
|
);
|
|
try {
|
|
expect(
|
|
await f.publisher.verifyArchive(
|
|
"arch",
|
|
f.release,
|
|
f.archive,
|
|
undefined,
|
|
f.publicKey,
|
|
),
|
|
).toMatchObject({ ok: false, error: { reason: "cleanup_failed" } });
|
|
|
|
const closeHandle = vi.spyOn(OwnedFileHandle.prototype, "close");
|
|
expect(await f.publisher.inspectLauncher(f.launcher)).toMatchObject({
|
|
ok: false,
|
|
error: { reason: "cleanup_failed" },
|
|
});
|
|
expect(closeHandle).not.toHaveBeenCalled();
|
|
|
|
expect(
|
|
await f.publisher.verifyArchive(
|
|
"arch",
|
|
f.release,
|
|
f.archive,
|
|
undefined,
|
|
f.publicKey,
|
|
),
|
|
).toMatchObject({ ok: true });
|
|
expect(closeHandle).toHaveBeenCalled();
|
|
expect(rootCloseAttempts).toBe(4);
|
|
} finally {
|
|
await f.publisher.close();
|
|
}
|
|
},
|
|
);
|
|
|
|
it("retries a launcher hash descriptor after a transient close failure", async () => {
|
|
const launcher = join(
|
|
await createTestTempDirectory("rea-linux-publisher-hash-"),
|
|
"Hopper",
|
|
);
|
|
const bytes = Buffer.from("fixture launcher bytes");
|
|
await writeFile(launcher, bytes);
|
|
const publisher = new LinuxHopperPublisher({}, async () => undefined);
|
|
const closeCause = new Error("Fixture hash descriptor close unavailable");
|
|
const owners: OwnedFileHandle[] = [];
|
|
const originalClose = OwnedFileHandle.prototype.close;
|
|
const close = vi
|
|
.spyOn(OwnedFileHandle.prototype, "close")
|
|
.mockImplementation(async function (this: OwnedFileHandle) {
|
|
owners.push(this);
|
|
if (owners.length === 1) throw closeCause;
|
|
return originalClose.call(this);
|
|
});
|
|
try {
|
|
await expect(publisher.hashLauncher(launcher)).resolves.toBe(sha256(bytes));
|
|
expect(close).toHaveBeenCalledTimes(2);
|
|
expect(owners).toHaveLength(2);
|
|
expect(owners[1]).toBe(owners[0]);
|
|
expect(owners[0]?.handle.fd).toBe(-1);
|
|
await publisher.close();
|
|
expect(close).toHaveBeenCalledTimes(2);
|
|
} finally {
|
|
close.mockRestore();
|
|
await publisher.close();
|
|
}
|
|
});
|
|
|
|
portableTar(
|
|
"lets overlapping verification and descriptor cleanup settle without re-entering publisher admission",
|
|
async () => {
|
|
const f = await fixture("arch");
|
|
admitFixturePublisher(f.fingerprint);
|
|
const verified = await f.publisher.verifyArchive(
|
|
"arch",
|
|
f.release,
|
|
f.archive,
|
|
undefined,
|
|
f.publicKey,
|
|
);
|
|
if (!verified.ok) throw verified.error;
|
|
expect(
|
|
await f.publisher.retainVerifiedArchive(verified.value, f.archive),
|
|
).toMatchObject({ ok: true });
|
|
|
|
let reachedMetadataClose!: () => void;
|
|
const metadataCloseStarted = new Promise<void>((resolve) => {
|
|
reachedMetadataClose = resolve;
|
|
});
|
|
let releaseMetadataClose!: () => void;
|
|
const metadataCloseGate = new Promise<void>((resolve) => {
|
|
releaseMetadataClose = resolve;
|
|
});
|
|
const originalClose = OwnedFileHandle.prototype.close;
|
|
const close = vi
|
|
.spyOn(OwnedFileHandle.prototype, "close")
|
|
.mockImplementation(async function (this: OwnedFileHandle) {
|
|
const attempt = close.mock.calls.length;
|
|
if (attempt === 2) {
|
|
reachedMetadataClose();
|
|
await metadataCloseGate;
|
|
}
|
|
if (attempt === 3 || attempt === 4 || attempt === 5)
|
|
throw new Error("Fixture descriptor close temporarily unavailable");
|
|
return await originalClose.apply(this);
|
|
});
|
|
try {
|
|
const inspection = f.publisher.inspectLauncher(f.launcher);
|
|
await metadataCloseStarted;
|
|
const hashFailure = await f.publisher.hashLauncher(f.launcher).then(
|
|
() => undefined,
|
|
(cause: unknown) => cause,
|
|
);
|
|
expect(hashFailure).toMatchObject({
|
|
reason: "cleanup_failed",
|
|
cause: expect.any(AggregateError),
|
|
});
|
|
const cleanupOutcomes = (hashFailure as Error & { cause: AggregateError })
|
|
.cause.errors;
|
|
expect(cleanupOutcomes).toHaveLength(2);
|
|
expect(cleanupOutcomes[0]).toMatchObject({ reason: "cleanup_failed" });
|
|
releaseMetadataClose();
|
|
await expect(inspection).resolves.toMatchObject({
|
|
ok: true,
|
|
value: { source: "signed_package_cache" },
|
|
});
|
|
await expect(f.publisher.close()).resolves.toBeUndefined();
|
|
expect(close).toHaveBeenCalledTimes(9);
|
|
} finally {
|
|
releaseMetadataClose();
|
|
close.mockRestore();
|
|
await f.publisher.close();
|
|
}
|
|
},
|
|
);
|
|
|
|
linux(
|
|
"preserves cleanup ownership when verification is cancelled",
|
|
async () => {
|
|
const f = await fixture();
|
|
admitFixturePublisher(f.fingerprint);
|
|
const controller = new AbortController();
|
|
const cleanup = vi
|
|
.spyOn(PrivateRuntimeRoot.prototype, "close")
|
|
.mockImplementation(async () => {
|
|
controller.abort();
|
|
throw new Error("Fixture cleanup unavailable after cancellation");
|
|
});
|
|
try {
|
|
expect(
|
|
await f.publisher.verifyArchive(
|
|
"deb",
|
|
f.release,
|
|
f.archive,
|
|
controller.signal,
|
|
f.publicKey,
|
|
),
|
|
).toMatchObject({
|
|
ok: false,
|
|
error: { reason: "cleanup_failed" },
|
|
});
|
|
await expect(retryLinuxHopperPublisherCleanup()).rejects.toMatchObject({
|
|
cleanup: {
|
|
resources: [expect.stringContaining("rea-hopper-publisher-")],
|
|
},
|
|
});
|
|
} finally {
|
|
cleanup.mockRestore();
|
|
await f.publisher.close();
|
|
await retryLinuxHopperPublisherCleanup();
|
|
}
|
|
},
|
|
);
|