-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(core): add agent tool authorization guard #1390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -106,7 +106,12 @@ import { | |
| type EnqueueEvalScoringArgs, | ||
| enqueueEvalScoring as enqueueEvalScoringHelper, | ||
| } from "./eval"; | ||
| import type { AgentHooks, OnToolEndHookResult, OnToolErrorHookResult } from "./hooks"; | ||
| import type { | ||
| AgentHooks, | ||
| AgentToolGuard, | ||
| OnToolEndHookResult, | ||
| OnToolErrorHookResult, | ||
| } from "./hooks"; | ||
| import { stripDanglingOpenAIReasoningFromModelMessages } from "./model-message-normalizer"; | ||
| import { AgentTraceContext, addModelAttributesToSpan } from "./open-telemetry/trace-context"; | ||
| import { | ||
|
|
@@ -1044,6 +1049,7 @@ export class Agent { | |
| private readonly workspaceToolkitOptions: AgentOptions["workspaceToolkits"]; | ||
| private readonly workspaceSkillsPromptOption: AgentOptions["workspaceSkillsPrompt"]; | ||
| private readonly configuredHooks?: AgentHooks; | ||
| private readonly toolGuard?: AgentToolGuard; | ||
| private readonly maxStepsConfigured: boolean; | ||
| private defaultObservability?: VoltAgentObservability; | ||
| private readonly toolManager: ToolManager; | ||
|
|
@@ -1078,6 +1084,7 @@ export class Agent { | |
| this.workspaceToolkitOptions = options.workspaceToolkits; | ||
| this.workspaceSkillsPromptOption = options.workspaceSkillsPrompt; | ||
| this.configuredHooks = options.hooks; | ||
| this.toolGuard = options.toolGuard; | ||
| this.maxStepsConfigured = options.maxSteps !== undefined; | ||
| const globalWorkspace = AgentRegistry.getInstance().getGlobalWorkspace(); | ||
| const workspaceOption = options.workspace === undefined ? globalWorkspace : options.workspace; | ||
|
|
@@ -6421,6 +6428,46 @@ export class Agent { | |
| return parseResult.data; | ||
| } | ||
|
|
||
| private async assertToolGuardAllows( | ||
| tool: BaseTool | ProviderTool, | ||
| args: any, | ||
| oc: OperationContext, | ||
| options?: ToolExecuteOptions, | ||
| ): Promise<void> { | ||
| if (!this.toolGuard) { | ||
| return; | ||
| } | ||
|
|
||
| const result = await this.toolGuard({ | ||
| agent: this, | ||
| tool: tool as any, | ||
| context: oc, | ||
| args, | ||
| options, | ||
| }); | ||
|
|
||
| const denied = | ||
| result === false || | ||
| (typeof result === "object" && | ||
| result !== null && | ||
| (result.denied === true || result.allowed === false)); | ||
| if (!denied) { | ||
| return; | ||
| } | ||
|
|
||
| const reason = | ||
| typeof result === "object" && result !== null && typeof result.reason === "string" | ||
| ? result.reason | ||
| : "Tool execution denied by toolGuard."; | ||
|
|
||
| throw new ToolDeniedError({ | ||
| toolName: tool.name, | ||
| message: reason, | ||
| code: "TOOL_FORBIDDEN", | ||
| httpStatus: 403, | ||
| }); | ||
| } | ||
|
|
||
| private createToolExecutionFactory( | ||
| oc: OperationContext, | ||
| hooks: AgentHooks, | ||
|
|
@@ -6623,6 +6670,7 @@ export class Agent { | |
| try { | ||
| await this.waitForSpeculativeInputGuardrail(oc); | ||
| await oc.traceContext.withSpan(toolSpan, async () => { | ||
| await this.assertToolGuardAllows(tool, args, oc, executionOptions); | ||
| await runToolStartHooks(); | ||
|
Comment on lines
+6665
to
6666
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win Call the tool end hook once for denied local calls. When Remove the duplicate invocation. Add a test that asserts Also applies to: 6734-6736 🤖 Prompt for AI Agents |
||
| }); | ||
|
|
||
|
|
@@ -6683,7 +6731,8 @@ export class Agent { | |
| return oc.traceContext.withSpan(toolSpan, async () => { | ||
| try { | ||
| await this.waitForSpeculativeInputGuardrail(oc); | ||
| // Call tool start hook - can throw ToolDeniedError | ||
| // Call tool guard and start hook - both can throw ToolDeniedError | ||
| await this.assertToolGuardAllows(tool, args, oc, executionOptions); | ||
| await runToolStartHooks(); | ||
|
|
||
| // Execute tool with merged options | ||
|
|
@@ -7242,6 +7291,7 @@ export class Agent { | |
| `Provider tool "${tool.name}" received arguments that do not match callTool input.`, | ||
| ); | ||
| } | ||
| await this.assertToolGuardAllows(tool, callInput, oc, executionOptions); | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Outdated
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P1: For provider tools this guard runs after the tool has already been executed, so it does not actually authorize the call. In Additionally, provider tools exposed directly to the model are passed through untouched in Prompt for AI agents |
||
| await hooks.onToolStart?.({ | ||
| agent: this, | ||
| tool: tool as any, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.