Skip to content

Commit 998e896

Browse files
committed
fix(audit): bound private work before enqueue
1 parent 2feda83 commit 998e896

3 files changed

Lines changed: 49 additions & 4 deletions

File tree

‎src/audit/audit-event-writer.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
recordExecutionDecisionFact,
1616
} from "./execution-decision-facts.js";
1717
import {
18+
parseExecutionDecisionWork,
1819
processExecutionDecisionWork,
1920
type ExecutionDecisionWork,
2021
} from "./execution-decision-work.js";
@@ -291,8 +292,12 @@ export function createAuditEventWriter(
291292
return false;
292293
}
293294
try {
295+
const boundedMessage =
296+
message.type === "record-execution-decision-work"
297+
? { ...message, work: parseExecutionDecisionWork(message.work) }
298+
: message;
294299
// Preserve the former Worker boundary's clone and prototype-stripping contract.
295-
queue.push(structuredClone(message));
300+
queue.push(structuredClone(boundedMessage));
296301
schedule();
297302
return true;
298303
} catch (error) {

‎src/audit/execution-decision-work.test.ts‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import path from "node:path";
22
import { DatabaseSync } from "node:sqlite";
3-
import { afterEach, describe, expect, it } from "vitest";
3+
import { afterEach, describe, expect, it, vi } from "vitest";
44
import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js";
55
import { closeOpenClawStateDatabaseForTest } from "../state/openclaw-state-db.js";
66
import { createAuditEventWriter } from "./audit-event-writer.js";
@@ -59,6 +59,46 @@ afterEach(() => {
5959
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
6060

6161
describe("private execution decision work", () => {
62+
it.each(["raw ref", "complete envelope"] as const)(
63+
"rejects an oversized %s before the FIFO clone boundary",
64+
async (oversizedPart) => {
65+
const stateDir = tempDirs.make("openclaw-audit-private-decision-bounds-");
66+
const errors: string[] = [];
67+
const writer = createAuditEventWriter({ stateDir, onError: (error) => errors.push(error) });
68+
const token = createExecutionIdentityAdmissionToken("bounded-private-decision-run", {
69+
contextId: "bounded-private-decision-context",
70+
executionId: "bounded-private-decision-execution",
71+
now: 1_000,
72+
});
73+
const work = decisionWork({
74+
token,
75+
receiptId: "bounded-private-decision",
76+
rawResource: oversizedPart === "raw ref" ? "r".repeat(4_097) : "resource",
77+
rawTarget: "target",
78+
});
79+
if (oversizedPart === "complete envelope") {
80+
const refs = Array.from({ length: 16 }, (_, index) => `${index}:`.padEnd(256, "p"));
81+
work.receipt.enforcement.policyRefs = refs;
82+
work.receipt.enforcement.grantRefs = refs;
83+
work.receipt.enforcement.contextFieldsUsed = refs;
84+
work.receipt.missingEvidence = refs;
85+
work.receipt.remediation = Array.from({ length: 8 }, (_, index) => ({
86+
code: `repair-${index}`,
87+
text: "r".repeat(512),
88+
}));
89+
}
90+
91+
await writer.ready;
92+
const clone = vi.spyOn(globalThis, "structuredClone");
93+
expect(writer.recordExecutionDecisionWork(work)).toBe(false);
94+
expect(clone).not.toHaveBeenCalled();
95+
clone.mockRestore();
96+
await writer.stop();
97+
98+
expect(errors).toEqual(["audit execution decision receipt could not be queued"]);
99+
},
100+
);
101+
62102
it("projects private refs inside the admission FIFO without retaining raw owners", async () => {
63103
const stateDir = tempDirs.make("openclaw-audit-private-decision-");
64104
const database = { env: { OPENCLAW_STATE_DIR: stateDir } };

‎src/audit/execution-decision-work.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ function buildReceipt(params: {
106106
};
107107
}
108108

109-
/** Revalidate the closed queue payload before key or database access. */
110-
function parseExecutionDecisionWork(value: unknown): ExecutionDecisionWork {
109+
/** Revalidate closed work before queue cloning, key access, or database access. */
110+
export function parseExecutionDecisionWork(value: unknown): ExecutionDecisionWork {
111111
if (
112112
!isClosedPayloadRecord(value) ||
113113
!hasOnlyKeys(value, ["workVersion", "token", "receipt", "refs"]) ||

0 commit comments

Comments
 (0)