Files
reverse-engineering-anything/src/process/DetachedPgpSignature.ts
T
Alex GorevskiandCopilot 4e28166626 fix(cancellation): preserve caller abort reason in cancellation errors (#6144)
* fix(cancellation): preserve caller abort reason in cancellation errors

Several application, provider, and MCP boundaries constructed
AnalysisCancelledError, ArtifactReaderFailure, or
DetachedPgpSignatureError for an already-observed or newly-observed abort
without passing the caller's signal.reason as the error cause. The typed
result then reported cancellation with an undefined reason even though the
exact object supplied to AbortController.abort(reason) was still available.

Pass signal.reason as the cause at each producing cancellation site:
JavaScript artifact reconstruction and file abort checks, derived MCP
operation checkpoints, detached PGP verification, ASAR listing admission,
Dart AOT inspection and its provider guard, evidence-file analysis views,
binary layout and recorded crash admission, WASM inspection admission,
and the Kuna shared assembly-index wait.

Cancellation classification and cleanup behavior are unchanged.

Fixes morluto/rea#5935
Fixes morluto/rea#5944
Fixes morluto/rea#6100
Fixes morluto/rea#6101
Fixes morluto/rea#6110
Fixes morluto/rea#6113
Fixes morluto/rea#6121
Fixes morluto/rea#6131
Fixes morluto/rea#6132

* test(cancellation): assert the exact caller reason identity

Copied reasons satisfied toMatchObject, so the tests would pass even if the
caller's abort reason were replaced by a structurally equal copy. Capture the
rejection and assert the cause is the same object.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 35b247e9-c793-4b8f-979a-807c27112cf5

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 35b247e9-c793-4b8f-979a-807c27112cf5
2026-10-22 04:11:15 +08:00

103 lines
3.0 KiB
TypeScript

import { err, ok, type Result } from "../domain/result.js";
export class DetachedPgpSignatureError extends Error {
readonly _tag = "DetachedPgpSignatureError";
constructor(
readonly reason: "invalid_signature" | "wrong_publisher" | "cancelled",
message: string,
options?: ErrorOptions,
) {
super(message, options);
this.name = this._tag;
}
}
/** Verify the complete binary payload and its signing subkey's primary identity. */
export const verifyDetachedPgpSignature = async (
bytes: Uint8Array,
detachedSignature: Uint8Array,
armoredPublicKey: string,
primaryFingerprint: string,
signal?: AbortSignal,
): Promise<
Result<
{
readonly primaryFingerprint: string;
readonly signingFingerprint: string;
},
DetachedPgpSignatureError
>
> => {
const cancelled = () =>
err(
new DetachedPgpSignatureError(
"cancelled",
"Package verification was cancelled.",
{ cause: signal?.reason },
),
);
const isAborted = () => signal?.aborted === true;
if (isAborted()) return cancelled();
try {
// Keep the cryptographic implementation out of unrelated CLI cold starts.
const pgp = await import("openpgp");
const key = await pgp.readKey({ armoredKey: armoredPublicKey });
if (isAborted()) return cancelled();
const fingerprint = key.getFingerprint().toUpperCase();
if (fingerprint !== primaryFingerprint)
return err(
new DetachedPgpSignatureError(
"wrong_publisher",
"The package signing key does not belong to the pinned publisher.",
),
);
const signature = await pgp.readSignature({
binarySignature: detachedSignature,
});
if (isAborted()) return cancelled();
if (
signature.packets.length !== 1 ||
signature.packets[0]?.signatureType !== pgp.enums.signature.binary
)
return err(
new DetachedPgpSignatureError(
"invalid_signature",
"Expected one detached binary package signature.",
),
);
const verified = await pgp.verify({
message: await pgp.createMessage({ binary: bytes }),
signature,
verificationKeys: key,
format: "binary",
});
const signed = verified.signatures[0];
if (signed === undefined || verified.signatures.length !== 1)
return err(
new DetachedPgpSignatureError(
"invalid_signature",
"The package has no unique verified signature.",
),
);
await signed.verified;
const signingKey = await key.getSigningKey(
signed.keyID,
signature.packets[0].created,
);
if (isAborted()) return cancelled();
return ok({
primaryFingerprint: fingerprint,
signingFingerprint: signingKey.getFingerprint().toUpperCase(),
});
} catch (cause: unknown) {
if (isAborted()) return cancelled();
return err(
new DetachedPgpSignatureError(
"invalid_signature",
"The package signature could not be verified.",
{ cause },
),
);
}
};