Skip to content

Commit 84a61b2

Browse files
committed
fix(audit): scope plugin hook ownership
1 parent 9beee70 commit 84a61b2

3 files changed

Lines changed: 66 additions & 17 deletions

File tree

‎src/agents/agent-tools.before-tool-call.decision.test.ts‎

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -316,17 +316,44 @@ describe("generic tool action decision receipts", () => {
316316
});
317317

318318
it.each([
319-
{ label: "returns adjusted params", result: { params: { ownerAdjusted: true } } },
320-
{ label: "returns void", result: undefined },
319+
{
320+
label: "returns adjusted params",
321+
result: { params: { ownerAdjusted: true } },
322+
matcher: undefined,
323+
toolName: undefined,
324+
expectedHandlerCalls: 1,
325+
expectedGenericReceipts: 0,
326+
expectedOwnerReceipts: 1,
327+
},
328+
{
329+
label: "returns void",
330+
result: undefined,
331+
matcher: undefined,
332+
toolName: undefined,
333+
expectedHandlerCalls: 1,
334+
expectedGenericReceipts: 0,
335+
expectedOwnerReceipts: 1,
336+
},
337+
{
338+
label: "does not match the tool",
339+
result: undefined,
340+
matcher: ["exec"],
341+
toolName: "read",
342+
expectedHandlerCalls: 0,
343+
expectedGenericReceipts: 1,
344+
expectedOwnerReceipts: 0,
345+
},
321346
] as const)(
322-
"does not follow an owner-native plugin allow receipt with a generic allow fact when it $label",
323-
async ({ result }) => {
347+
"routes generic attribution only when a plugin hook $label",
348+
async ({ result, ...testCase }) => {
324349
const registry = createEmptyPluginRegistry();
350+
const handler = vi.fn(() => result);
325351
addTestHook({
326352
registry,
327353
pluginId: "owner-plugin",
328354
hookName: "before_tool_call",
329-
handler: (() => result) as PluginHookRegistration["handler"],
355+
handler: handler as PluginHookRegistration["handler"],
356+
...(testCase.matcher ? { matcher: [...testCase.matcher] } : {}),
330357
});
331358
setActivePluginRegistry(registry);
332359
initializeGlobalHookRunner(registry);
@@ -337,7 +364,11 @@ describe("generic tool action decision receipts", () => {
337364
return true;
338365
});
339366
const tool = wrapToolWithBeforeToolCallHook(
340-
assembledTool("data", "hook_allow_subject", vi.fn().mockResolvedValue({ content: [] })),
367+
assembledTool(
368+
"data",
369+
testCase.toolName ?? "hook_allow_subject",
370+
vi.fn().mockResolvedValue({ content: [] }),
371+
),
341372
);
342373

343374
try {
@@ -346,12 +377,21 @@ describe("generic tool action decision receipts", () => {
346377
clearOwnerSink();
347378
}
348379

349-
expect(works).toEqual([]);
350-
expect(ownerReceipts).toHaveLength(1);
351-
expect(ownerReceipts[0]).toMatchObject({
352-
decision: { outcome: "allowed", reasonCode: "plugin_hook_allowed" },
353-
source: { owner: "plugin-hook" },
354-
});
380+
expect(handler).toHaveBeenCalledTimes(testCase.expectedHandlerCalls);
381+
expect(works).toHaveLength(testCase.expectedGenericReceipts);
382+
expect(ownerReceipts).toHaveLength(testCase.expectedOwnerReceipts);
383+
if (testCase.expectedGenericReceipts === 1) {
384+
expect(works[0]?.receipt).toMatchObject({
385+
decision: { outcome: "allowed", reasonCode: "generic_action_attributed" },
386+
source: { owner: "tool-action" },
387+
});
388+
}
389+
if (testCase.expectedOwnerReceipts === 1) {
390+
expect(ownerReceipts[0]).toMatchObject({
391+
decision: { outcome: "allowed", reasonCode: "plugin_hook_allowed" },
392+
source: { owner: "plugin-hook" },
393+
});
394+
}
355395
},
356396
);
357397

‎src/agents/agent-tools.before-tool-call.policy.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,15 @@ export async function runBeforeToolCallHook(args: {
331331
}
332332
const hookEventParams = isPlainObject(policyAdjustedParams) ? policyAdjustedParams : {};
333333
const callerIdentity = getGatewayToolCallerIdentity();
334+
let ownerDecisionMarked = false;
334335
const receipt =
335336
callerIdentity?.executionIdentityToken && callerIdentity.receiptAuthority
336337
? {
337338
token: callerIdentity.executionIdentityToken,
338339
assertAuthority: callerIdentity.receiptAuthority,
340+
markOwnerDecision: () => {
341+
ownerDecisionMarked = true;
342+
},
339343
}
340344
: undefined;
341345
const hookResult = await hookRunner.runBeforeToolCall(
@@ -410,7 +414,7 @@ export async function runBeforeToolCallHook(args: {
410414
blocked: false as const,
411415
params: finalParams,
412416
};
413-
if (hasBeforeToolCallHooks || finalApprovalResolution) {
417+
if (ownerDecisionMarked || finalApprovalResolution) {
414418
markPrivateDecision(allowed, "ownerDecision");
415419
}
416420
if (finalApprovalResolution) {

‎src/plugins/hooks.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,7 @@ export function createHookRunner(
14591459
receipt?: Readonly<{
14601460
token: ExecutionIdentityAdmissionToken;
14611461
assertAuthority: () => boolean | void;
1462+
markOwnerDecision?: () => void;
14621463
}>,
14631464
): Promise<PluginHookBeforeToolCallResult | undefined> {
14641465
return runModifyingHook<"before_tool_call", PluginHookBeforeToolCallResult>(
@@ -1495,22 +1496,26 @@ export function createHookRunner(
14951496
},
14961497
shouldStop: (result) => result.block === true,
14971498
terminalLabel: "block=true",
1498-
onHandlerResult: ({ hook, result }) =>
1499+
onHandlerResult: ({ hook, result }) => {
1500+
receipt?.markOwnerDecision?.();
14991501
recordBeforeToolCallDecision({
15001502
event,
15011503
hook,
15021504
token: receipt?.token,
15031505
result,
15041506
receiptAuthority: receipt?.assertAuthority,
1505-
}),
1506-
onHandlerError: (hook, failOpen) =>
1507+
});
1508+
},
1509+
onHandlerError: (hook, failOpen) => {
1510+
receipt?.markOwnerDecision?.();
15071511
recordBeforeToolCallDecision({
15081512
event,
15091513
hook,
15101514
token: receipt?.token,
15111515
failOpen,
15121516
receiptAuthority: receipt?.assertAuthority,
1513-
}),
1517+
});
1518+
},
15141519
},
15151520
event.toolName,
15161521
);

0 commit comments

Comments
 (0)