mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 19:51:20 -05:00
ab84ba6839
* fix(process): preserve producer-owned inspection cancellation Prototype the explicit inspection origin and retained file/batch owner protocol. Downstream adapter migration and lease binding remain required before integration. * fix(process): type inspection ownership handoffs precisely * fix(process): retain plain inspection preparation cancellation * fix(process): retain explicit inspection cleanup and cancellation facts * fix(process): require explicit inspection facts and cleanup scope * refactor(process): remove redundant cleanup forwarding wrapper * fix(firmware): retain inspection cancellation and cleanup ownership * fix(wakaru): retain typed inspection outcomes and caller scope * fix(ghidra): preserve inspection facts and launch cleanup scope * fix(providers): preserve typed process inspection outcomes * refactor(process): remove obsolete inspection cancellation re-export * fix(native): preserve inspection preparation cleanup and cancellation * fix(registration): retain typed inspection cleanup during Git discovery * fix(process): preserve inspection deadline origin in adapter handoff * fix(process): consume the canonical inspection selector in binding * fix(native): preserve inspection deadlines and cleanup retry completion * test(process): narrow the retained Darwin preparation owner * fix(providers): preserve preparation ownership and timeout policy Narrow preparation facts through their canonical owner, preserve actual cleanup capabilities and recorded cancellation, and keep enclosing command deadlines authoritative. Retain raw inspection records through Java refusal and configuration projections without changing availability fallback policy. * refactor(process): use one inspection failure type owner * fix(providers): retain recorded secondary failures when rebinding * fix(ghidra): preserve normalized inspection facts through public projection * fix(process): normalize native process listing cancellation * docs(development): clarify failure provenance and cleanup verification * fix(process): preserve direct inspection cancellation facts * fix(process): retain inspection owners through termination * fix(process): preserve command inspection cleanup provenance * fix(desktop): preserve inspection facts after cleanup retry * refactor(process)!: migrate desktop capture with inspection producers * fix(desktop)!: preserve capture failure origins and cleanup owners (#10115) * fix(desktop): preserve capture failure origins and cleanup * fix(desktop): type keyring cleanup cancellation * fix(kuna)!: retain copy cleanup before retiring workspaces (#10114) * fix(kuna): own copy cancellation and descriptor cleanup * fix(kuna): close copy descriptors before workspace removal * test(kuna): type copy lifecycle gates for project library * docs(kuna): explain retained copy-file cleanup and staging ownership * docs(kuna): separate cache publication, integrity and cleanup guidance * fix(desktop): retain sampling inspection owners before release * fix(process): preserve capture file read cancellation origin * fix(process): retain capture inspection cancellation and cleanup owners * fix(process): retain sampling inspection failures in capture scope * fix(process): retain inspection failures through mandatory cleanup * fix(process): distinguish sampler shutdown from capture cancellation * fix(browser): retain startup EOF before later cancellation * fix(process): bind capture inspection cleanup to the owning run * test(process): migrate cancellation fixtures to typed owners Record explicit preparation and command cancellation facts, retain exact opaque reasons and physical causes, and guard the mocked version probe from native preparation. Keep the live Wakaru command control while adding a recording equivalent for bounded source verification. * fix(process): own sampling reads and settle inspection peers * fix(errors): retain authored recovery through cleanup composition * fix(process): retain all sampling inspection leaves for final cleanup * test(browser): report recording cleanup signal outcome * fix(process): keep inspection error context out of implicit JSON * fix(desktop): retain normalized sampling cleanup ownership * test(process): assert tagged startup cancellation
181 lines
6.2 KiB
TypeScript
181 lines
6.2 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
import { writeFile } from "node:fs/promises";
|
|
import { join } from "node:path";
|
|
|
|
import { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js";
|
|
import { assertArtifactReadActive } from "../artifacts/ArtifactFileOperation.js";
|
|
import {
|
|
dearmorPublicKeys,
|
|
isArmoredKey,
|
|
validSignature,
|
|
type ValidSignature,
|
|
} from "../domain/desktop/aptRepositories.js";
|
|
import { verifyingKey } from "../domain/desktop/openPgpKeys.js";
|
|
import { PrivateRuntimeRoot } from "../process/PrivateRuntimeRoot.js";
|
|
import { readHostFile } from "./HostFileRead.js";
|
|
import type { QueryRunner } from "./HostQuery.js";
|
|
|
|
const FILE_MAX_BYTES = 1024 * 1024;
|
|
|
|
const released = (release: readonly string[]): string =>
|
|
release[0] ?? "its release file";
|
|
|
|
/** A candidate key file, or a key a sources entry holds, that verified a release, and the bytes of it to retain. */
|
|
export interface VerifiedKey {
|
|
readonly path: string;
|
|
readonly bytes: Buffer;
|
|
readonly sha256: string;
|
|
readonly format: "armored" | "binary";
|
|
/** The verifying primary key's fingerprint. */
|
|
readonly fingerprint: string;
|
|
}
|
|
|
|
/** A key a deb822 sources entry holds in its Signed-By field, named by the entry's sources file. */
|
|
export interface HeldKey {
|
|
readonly path: string;
|
|
readonly bytes: Buffer;
|
|
}
|
|
|
|
/** Runs gpgv on one candidate keyring at a time, written into a private folder it owns. */
|
|
class GpgvKeyCheck {
|
|
#root: PrivateRuntimeRoot | undefined;
|
|
#keyrings = 0;
|
|
readonly #reads = new Map<string, Buffer | "missing" | "unreadable">();
|
|
|
|
constructor(
|
|
private readonly runQuery: QueryRunner,
|
|
private readonly signal: AbortSignal | undefined,
|
|
) {}
|
|
|
|
/** The private folder, once a keyring was written into it. */
|
|
get root(): PrivateRuntimeRoot | undefined {
|
|
return this.#root;
|
|
}
|
|
|
|
async #read(path: string): Promise<Buffer | "missing" | "unreadable"> {
|
|
const read =
|
|
this.#reads.get(path) ??
|
|
(await readHostFile(path, FILE_MAX_BYTES, this.signal));
|
|
this.#reads.set(path, read);
|
|
return read;
|
|
}
|
|
|
|
/** The first candidate key that verifies the release, or why none did. */
|
|
async firstVerifying(
|
|
candidates: readonly string[],
|
|
release: readonly string[],
|
|
): Promise<VerifiedKey | { readonly reason: string }> {
|
|
const unreadable: string[] = [];
|
|
for (const path of candidates) {
|
|
const bytes = await this.#read(path);
|
|
if (bytes === "unreadable") unreadable.push(path);
|
|
if (typeof bytes === "string") continue;
|
|
const verified = await this.#verifying({ path, bytes }, release);
|
|
if (verified !== undefined) return verified;
|
|
}
|
|
return {
|
|
reason: `no candidate key (${candidates.join(", ")}) verifies ${released(release)}${unreadable.length === 0 ? "" : `; unreadable: ${unreadable.join(", ")}`}`,
|
|
};
|
|
}
|
|
|
|
/** The key a sources entry holds when it verifies the release, or why not. */
|
|
async heldVerifying(
|
|
key: HeldKey,
|
|
release: readonly string[],
|
|
): Promise<VerifiedKey | { readonly reason: string }> {
|
|
return (
|
|
(await this.#verifying(key, release)) ?? {
|
|
reason: `the key the Signed-By field of ${key.path} holds does not verify ${released(release)}`,
|
|
}
|
|
);
|
|
}
|
|
|
|
/** The key with the bytes to retain when it verifies the release, why it cannot count, or undefined when it does not verify. */
|
|
async #verifying(
|
|
key: { readonly path: string; readonly bytes: Buffer },
|
|
release: readonly string[],
|
|
): Promise<VerifiedKey | { readonly reason: string } | undefined> {
|
|
const signature = await this.#verify(key.bytes, release);
|
|
if (signature === "missing")
|
|
return {
|
|
reason:
|
|
"gpgv is not installed, so no key could be checked against the repository's signature",
|
|
};
|
|
if (signature === undefined) return undefined;
|
|
const kept = verifyingKey(key.bytes, signature.primary);
|
|
if (!kept.ok)
|
|
return {
|
|
reason: `${key.path} verifies ${released(release)}, but ${kept.error}`,
|
|
};
|
|
return {
|
|
path: key.path,
|
|
...kept.value,
|
|
sha256: createHash("sha256").update(kept.value.bytes).digest("hex"),
|
|
fingerprint: signature.primary,
|
|
};
|
|
}
|
|
|
|
/** The good signature gpgv reports with the key, whatever its exit status; `missing` without gpgv. */
|
|
async #verify(
|
|
key: Buffer,
|
|
release: readonly string[],
|
|
): Promise<ValidSignature | "missing" | undefined> {
|
|
const keyring = isArmoredKey(key) ? dearmorPublicKeys(key) : key;
|
|
if (keyring === undefined) return undefined;
|
|
this.#root ??= await PrivateRuntimeRoot.create({ prefix: "rea-apt-keys-" });
|
|
const path = join(this.#root.path, `keyring-${String(this.#keyrings)}.gpg`);
|
|
this.#keyrings += 1;
|
|
await writeFile(path, keyring, { mode: 0o600, flag: "wx" });
|
|
const outcome = await this.runQuery(
|
|
"gpgv",
|
|
[
|
|
"--homedir",
|
|
this.#root.path,
|
|
"--status-fd",
|
|
"1",
|
|
"--keyring",
|
|
path,
|
|
...release,
|
|
],
|
|
this.signal,
|
|
);
|
|
if (outcome === "missing") return "missing";
|
|
return typeof outcome === "object"
|
|
? validSignature(outcome.stdout)
|
|
: undefined;
|
|
}
|
|
}
|
|
|
|
/** Use a gpgv key check, then remove its private folder; a failed removal keeps the folder's owner in the thrown cleanup failure. */
|
|
export const withGpgvKeyCheck = async <Value>(
|
|
runQuery: QueryRunner,
|
|
signal: AbortSignal | undefined,
|
|
use: (
|
|
check: Pick<GpgvKeyCheck, "firstVerifying" | "heldVerifying">,
|
|
) => Promise<Value>,
|
|
): Promise<Value> => {
|
|
const check = new GpgvKeyCheck(runQuery, signal);
|
|
let outcome:
|
|
| { readonly kind: "completed"; readonly value: Value }
|
|
| { readonly kind: "failed"; readonly cause: unknown };
|
|
try {
|
|
outcome = { kind: "completed", value: await use(check) };
|
|
} catch (cause: unknown) {
|
|
outcome = { kind: "failed", cause };
|
|
}
|
|
const root = check.root;
|
|
if (root !== undefined) {
|
|
const resources = new ArtifactResourceScope();
|
|
const removed = await resources.release({
|
|
kind: "private-runtime-root",
|
|
resource: root.path,
|
|
root,
|
|
});
|
|
if (removed.kind === "failed")
|
|
throw resources.cleanupFailure(outcome, [removed]);
|
|
}
|
|
if (outcome.kind === "failed") throw outcome.cause;
|
|
assertArtifactReadActive(signal);
|
|
return outcome.value;
|
|
};
|