fix(binary): admit record input before sealing it for attribution

recordEvidence and the two bundle import paths seal Evidence through
withEvidenceProviderSelection before handing it to the ledger. A sealed
value skips the ledger's ordinary-JSON and heap-headroom admission, so a
session with no authentication allowance accepted a mutable record and a
large record was copied and traversed before the guard ran.

Hold the caller's own value to that admission first, then attribute. The
import paths also refuse with a Result instead of falling through to an
unattributed import when admission fails.
This commit is contained in:
2SinceJuly committed 2026-11-10 07:38:23 +08:00
1 parent 116f9f5591
commit 6570fe022a
4 files changed
+90 -21

No files matched your search

@@ -15,6 +15,7 @@ import type { AnalysisProviderCandidate } from "../AnalysisProvider.js";
import { AnalysisProviderRegistry } from "./AnalysisProviderRegistry.js";
import { BinarySession } from "./BinarySession.js";
import { SessionProviderRouter } from "./SessionProviderRouter.js";
import { InvestigationRecords } from "../investigation/InvestigationRecords.js";
import {
ControllableAnalysisClient,
createBinarySessionTargets,
@@ -238,6 +239,26 @@ it("attributes an imported bundle to the recording session's provider selection"
}
});
it("admits record Evidence before attributing it to the session", async () => {
const [target] = await createBinarySessionTargets();
// A session with no authentication allowance must refuse a mutable record
// rather than copying and sealing it for attribution first.
const session = createTestBinarySession(
createCacheProvider([]),
{},
new InvestigationRecords({ availableAuthenticationBytes: () => 0 }),
);
try {
expect((await session.open(target)).ok).toBe(true);
const refused = session.recordEvidence(observation("no_headroom"));
if (refused.ok) throw new Error("Expected authentication headroom refusal");
expect(refused.error._tag).toBe("AnalysisResourceConstraintError");
expect(session.exportEvidenceBundle().records).toHaveLength(0);
} finally {
await session.dispose();
}
});
it("preserves records through failed disposal and clears them only after its owned cleanup succeeds", async () => {
const [target] = await createBinarySessionTargets();
const client = new ControllableAnalysisClient();
+48 -20
View File
@@ -71,21 +71,20 @@ const SNAPSHOT_MUTATION_RECOVERY =
* Without an active selection the bundle passes through unchanged — there is
* nothing to attribute it to, and dropping the caller's metadata would be a
* loss, not a correction.
*
* Attribution seals its result, and a sealed value skips the owning ledger's
* ordinary-JSON and heap-headroom admission, so the caller must admit first.
*/
const attributeImportedProviderSelection = (
bundle: unknown,
providerSelection: ProviderSelectionProvenance | undefined,
): unknown => {
if (providerSelection === undefined) return bundle;
const authenticated = safeParseImmutableEvidenceBundle(bundle);
if (!authenticated.ok) return bundle;
return createImmutableEvidenceBundle(
authenticated.value.records.map((record) =>
bundle: EvidenceBundle,
providerSelection: ProviderSelectionProvenance,
): EvidenceBundle =>
createImmutableEvidenceBundle(
bundle.records.map((record) =>
withEvidenceProviderSelection(record, providerSelection),
),
authenticated.value.unknowns,
bundle.unknowns,
);
};
/** A missing replay prerequisite is not an Evidence integrity failure. */
const snapshotUnavailable = (reason: string) =>
@@ -159,10 +158,18 @@ export abstract class BinarySessionRecords {
return err(new AnalysisCancelledError("record_evidence"));
// Selection facts belong to the session that admitted the observation, so
// every record stays attributable to the exact engine that produced it.
// Attribution parses to seal the rebound envelope, and a tampered record
// must still come back as the declared `Result` rather than a thrown
// schema error. Authenticate first so the parse inside attribution only
// sees a value the ledger's own reader would have accepted.
// Attribution seals its result, and a sealed record skips the owning
// ledger's ordinary-JSON and heap-headroom admission, so admit the caller's
// own value first — validating a large record must not wait until after it
// has been copied and sealed.
const admitted = this.#records.admitSealedEvidenceInput(
evidence,
"record_evidence",
);
if (!admitted.ok) return admitted;
// A tampered record must still come back as the declared `Result` rather
// than a thrown schema error, so let the ledger reject what it cannot parse
// instead of sealing it here.
const authenticated = tryParseEvidence(evidence);
if (authenticated === undefined)
return this.#records.recordEvidence(evidence);
@@ -250,11 +257,12 @@ export abstract class BinarySessionRecords {
): Result<EvidenceImportReceipt, AnalysisError> {
if (this.#investigationDisposed)
return err(new AnalysisCancelledError("import_evidence_bundle"));
const importable = attributeImportedProviderSelection(
const importable = this.#attributeImportedBundle(
bundle,
this.activeAnalysisBinding()?.providerSelection,
"import_evidence_bundle",
);
const imported = this.#records.mergeEvidenceBundle(importable);
if (!importable.ok) return importable;
const imported = this.#records.mergeEvidenceBundle(importable.value);
if (!imported.ok) return imported;
this.#snapshot.refreshEvidenceMetadata(imported.value.replacements);
if (imported.value.changed) this.#emitSnapshotChanged();
@@ -265,6 +273,25 @@ export abstract class BinarySessionRecords {
});
}
/** Admit, then attribute, an imported bundle the way `recordEvidence` does. */
#attributeImportedBundle(
bundle: unknown,
operation: string,
): Result<unknown, AnalysisError> {
const providerSelection = this.activeAnalysisBinding()?.providerSelection;
if (providerSelection === undefined) return ok(bundle);
const admitted = this.#records.admitSealedEvidenceInput(bundle, operation);
if (!admitted.ok) return admitted;
const authenticated = safeParseImmutableEvidenceBundle(bundle);
if (!authenticated.ok) return ok(bundle);
return ok(
attributeImportedProviderSelection(
authenticated.value,
providerSelection,
),
);
}
protected abstract activeAnalysisBinding(): ActiveAnalysisBinding | undefined;
exportAnalysisSnapshot(): Result<AnalysisSnapshot, AnalysisError> {
@@ -353,11 +380,12 @@ export abstract class BinarySessionRecords {
? undefined
: { target: active.target, profile },
(bundle) => {
const importable = attributeImportedProviderSelection(
const importable = this.#attributeImportedBundle(
bundle,
active?.providerSelection,
"import_analysis_snapshot",
);
const imported = this.#records.mergeEvidenceBundle(importable);
if (!importable.ok) return importable;
const imported = this.#records.mergeEvidenceBundle(importable.value);
if (!imported.ok) return imported;
this.#snapshot.refreshEvidenceMetadata(imported.value.replacements);
return ok(imported.value.recordsAdded);
@@ -65,6 +65,24 @@ export class InvestigationRecords
);
}
/**
* Hold caller input to the same ordinary-JSON and heap-headroom rules the
* ledger applies, before the caller seals it for attribution.
*
* A sealed value skips those checks inside the ledger, so a workflow that
* re-attributes a record must admit the raw input it received here first.
*/
admitSealedEvidenceInput(
input: unknown,
operation: string,
): Result<void, AnalysisError> {
return admitInvestigationAuthentication(
input,
operation,
this.retentionPolicy,
);
}
/** Record immutable Evidence idempotently; reject conflicting content. */
recordEvidence(
evidence: Evidence,
+3 -1
View File
@@ -16,6 +16,7 @@ import type {
AnalysisProvider,
} from "../../src/application/AnalysisProvider.js";
import { BinarySession } from "../../src/application/binary/BinarySession.js";
import { InvestigationRecords } from "../../src/application/investigation/InvestigationRecords.js";
import { SessionProviderRouter } from "../../src/application/binary/SessionProviderRouter.js";
import { AnalysisProviderRegistry } from "../../src/application/binary/AnalysisProviderRegistry.js";
import { OFFICIAL_TOOL_CONTRACTS } from "../../src/contracts/officialToolContracts.js";
@@ -119,8 +120,9 @@ export const createTestProviderRouter = (
export const createTestBinarySession = (
provider: AnalysisProvider | FixtureClientFactory,
options: TestBinarySessionOptions = {},
records: InvestigationRecords = new InvestigationRecords(),
): BinarySession =>
new BinarySession(createTestProviderRouter(provider, options));
new BinarySession(createTestProviderRouter(provider, options), records);
/** Materialize two distinct targets for session lifecycle tests. */
export const createBinarySessionTargets = async (): Promise<