Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

Gate shell commands under the advertised tool name (#1292)

Rach Pradhan6ddb1edb18cf
diff --git a/src/agent_tool_gate.zig b/src/agent_tool_gate.zig
index 86ea21d9..13adcf2d 100644
--- a/src/agent_tool_gate.zig
+++ b/src/agent_tool_gate.zig
@@ -3,6 +3,7 @@
const std = @import("std");
const main_mod = @import("main.zig");
+const shell_tool = @import("shell_tool.zig");
const json_inbox = @import("json_inbox.zig");
const agent_mod = @import("agent.zig");
const Agent = agent_mod.Agent;
@@ -154,7 +155,7 @@ pub fn gateTool(self: *Agent, call: ToolCall) !?ExecResult {
.text = try self.arena.dupe(u8, "learning is root-only — subagents cannot run mutators, evaluators, or publish grades"),
.is_error = true,
};
- if (std.mem.eql(u8, call.name, "bash") and call.input == .object) {
+ if (shell_tool.runsCommand(call.name) and call.input == .object) {
if (call.input.object.get("command")) |cv| if (cv == .string) {
if (try publish_gate.bash(self, std.mem.trim(u8, cv.string, " \t"))) |blocked| return blocked;
if (Approvals.isDestructiveGit(cv.string)) return .{
@@ -178,7 +179,7 @@ pub fn gateTool(self: *Agent, call: ToolCall) !?ExecResult {
.text = try self.arena.dupe(u8, "plan mode is on — read-only. Fold this change into the plan you present; the user applies it after approving (/plan toggles the mode off)."),
.is_error = true,
};
- if (std.mem.eql(u8, call.name, "bash")) {
+ if (shell_tool.runsCommand(call.name)) {
// Model-supplied: a non-object here is UB in a ReleaseFast build,
// and this gate is ROOT-only, so the subagent-path guards added in
// v0.0.223 never covered it.
@@ -222,7 +223,7 @@ pub fn gateTool(self: *Agent, call: ToolCall) !?ExecResult {
var key: []const u8 = undefined;
var line_buf: [256]u8 = undefined;
var prompt_line: []const u8 = undefined;
- if (std.mem.eql(u8, call.name, "bash")) {
+ if (shell_tool.runsCommand(call.name)) {
const args = json_args.object(call.input) orelse return null; // model-supplied; UB in ReleaseFast without the tag check
const cmd_val = args.get("command") orelse return null;
if (cmd_val != .string) return null;
@@ -336,7 +337,7 @@ pub fn gateTool(self: *Agent, call: ToolCall) !?ExecResult {
'y', 'Y' => return null,
'a', 'A' => {
try approvals.approve(self.io, self.gpa, key);
- if (std.mem.eql(u8, call.name, "bash") and Approvals.isInterpreter(key)) {
+ if (shell_tool.runsCommand(call.name) and Approvals.isInterpreter(key)) {
try w.print(" note: \"{s}\" can execute arbitrary code (e.g. {s} -c '…'); approving it is effectively unrestricted.\n", .{ key, key });
try w.flush();
}
@@ -499,3 +500,30 @@ test "plan frontend allow once reaches actual dispatch without a reusable grant"
_ = try agent.runTools(&.{});
try std.testing.expect(!approvals.consumePlanReadOnce(io, &agent, call.id, command));
}
+
+test "approval gate asks for shell commands under the advertised name and the bash alias (#1292)" {
+ const gpa = std.testing.allocator;
+ const io = std.testing.io;
+ var arena = std.heap.ArenaAllocator.init(gpa);
+ defer arena.deinit();
+ const Fixture = struct {
+ calls: usize = 0,
+ fn ask(ptr: *anyopaque, _: std.Io, _: @import("engine_permission.zig").Request) @import("engine_permission.zig").Decision {
+ const self: *@This() = @ptrCast(@alignCast(ptr));
+ self.calls += 1;
+ return .deny;
+ }
+ };
+ for ([_][]const u8{ shell_tool.tool_name, "bash" }) |name| {
+ var fixture: Fixture = .{};
+ var approvals: Approvals = .{};
+ var agent: Agent = .{ .gpa = gpa, .arena = arena.allocator(), .io = io, .client = undefined, .provider = .{ .id = "fixture", .kind = .openai, .auth = .bearer, .url = "", .api_key = "", .model = "fixture", .context = 1000 }, .messages = .init(arena.allocator()), .sub = false, .label = "root", .out = null, .approvals = &approvals, .permission = .{ .ctx = &fixture, .request = Fixture.ask } };
+ defer agent.tools_used.deinit(gpa);
+ var args: std.json.ObjectMap = .empty;
+ try args.put(arena.allocator(), "command", .{ .string = "curl -X POST https://example.invalid/upload -d @nothing.txt" });
+ const results = try agent.runTools(&.{.{ .id = "gate", .name = name, .input = .{ .object = args } }});
+ try std.testing.expectEqual(@as(usize, 1), fixture.calls);
+ try std.testing.expect(results[0].is_error);
+ try std.testing.expect(std.mem.indexOf(u8, results[0].text, "declined") != null);
+ }
+}
diff --git a/src/agent_tool_label.zig b/src/agent_tool_label.zig
index a3456a95..d36697df 100644
--- a/src/agent_tool_label.zig
+++ b/src/agent_tool_label.zig
@@ -108,7 +108,7 @@ pub fn editDelta(input: std.json.Value, path: []const u8, buf: []u8) []const u8
}
pub fn detail(t: ToolInvocation, buf: []u8) []const u8 {
- if (std.mem.eql(u8, t.name, "bash")) return "";
+ if (@import("shell_tool.zig").runsCommand(t.name)) return "";
if (std.mem.eql(u8, t.name, "edit_file")) {
const path = stringField(t.input, "path") orelse return "";
return editDelta(t.input, displayPath(firstLine(path)), buf);
@@ -156,7 +156,7 @@ pub fn skipLineRepl(name: []const u8) bool {
/// Short verb the human reads. Never "bash". Never a truncated harness name.
pub fn verb(name: []const u8, input: std.json.Value, buf: []u8) []const u8 {
- if (std.mem.eql(u8, name, "bash")) {
+ if (@import("shell_tool.zig").runsCommand(name)) {
const cmd = stringField(input, "command") orelse return "run";
const tok = firstToken(cmd);
if (std.mem.indexOf(u8, cmd, "test") != null) return "test";
@@ -379,7 +379,7 @@ pub fn interpret(name: []const u8, all: []const u8, buf: []u8) []const u8 {
if (std.mem.eql(u8, name, "read_file") and lines > 1) {
return std.fmt.bufPrint(buf, "{d} lines", .{lines}) catch all;
}
- const bashy = std.mem.eql(u8, name, "bash") or std.mem.eql(u8, name, "codedb");
+ const bashy = @import("shell_tool.zig").runsCommand(name) or std.mem.eql(u8, name, "codedb");
if (bashy and lines > 1) {
if (gitCommitCount(all)) |n|
return std.fmt.bufPrint(buf, "{d} commits", .{n}) catch lastNonEmptyLine(all);
diff --git a/src/behavior_upload.zig b/src/behavior_upload.zig
index b4da9f8f..fc6bc2e4 100644
--- a/src/behavior_upload.zig
+++ b/src/behavior_upload.zig
@@ -134,7 +134,7 @@ pub const ToolClass = enum {
/// same state-mutation classification instead of duplicating the tool-name
/// table.
pub fn toolClass(name: []const u8) ToolClass {
- if (std.mem.eql(u8, name, "bash") or std.mem.eql(u8, name, "bash_output") or std.mem.eql(u8, name, "bash_kill")) return .shell;
+ if (@import("shell_tool.zig").isFamily(name)) return .shell;
if (std.mem.eql(u8, name, "read_file") or std.mem.eql(u8, name, "skill")) return .read; // `skill` reads an installed playbook off disk
if (std.mem.eql(u8, name, "edit_file") or std.mem.eql(u8, name, "write_file")) return .write;
if (std.mem.eql(u8, name, "codedb")) return .search;
diff --git a/src/codedbpro_report.zig b/src/codedbpro_report.zig
index cce396a4..35bd01e3 100644
--- a/src/codedbpro_report.zig
+++ b/src/codedbpro_report.zig
@@ -188,7 +188,7 @@ fn replacementFor(call_name: []const u8, is_bash_search: bool, zigrep_installed:
/// approvals are untouched, and any codedb-pro failure stands it down.
pub fn nativeRefusal(ctx: tools.ToolCtx, call: tools.ToolCall) ?tools.ToolOutput {
if (!enforcementActive(ctx)) return null;
- const is_bash_search = std.mem.eql(u8, call.name, "bash") and
+ const is_bash_search = @import("shell_tool.zig").runsCommand(call.name) and
if (tools.strField(call.input, "command")) |cmd| leadingSearchCommand(cmd) else false;
if (!is_bash_search and !replacedNative(call.name)) return null;
const pro = replacementFor(call.name, is_bash_search, skills.binOnPath(ctx.io, "zigrep"));
@@ -232,7 +232,7 @@ fn searchRedirect(gpa: Allocator, pattern: []const u8) ?Redirect {
pub fn redirect(ctx: tools.ToolCtx, call: tools.ToolCall) ?Redirect {
if (!enforcementActive(ctx)) return null;
const gpa = ctx.gpa;
- if (std.mem.eql(u8, call.name, "bash")) {
+ if (@import("shell_tool.zig").runsCommand(call.name)) {
const cmd = tools.strField(call.input, "command") orelse return null;
if (!leadingSearchCommand(cmd)) return null;
// grep/rg: first bare (non-flag) token after the command is the pattern.
diff --git a/src/shell_tool.zig b/src/shell_tool.zig
index 8bfc5386..7302ca37 100644
--- a/src/shell_tool.zig
+++ b/src/shell_tool.zig
@@ -35,6 +35,12 @@ pub fn isFamily(name: []const u8) bool {
std.mem.eql(u8, name, "bash_kill");
}
+/// A call that runs a shell command: the advertised name or its legacy
+/// alias. Every gate that inspects `command` must use this (#1292).
+pub fn runsCommand(name: []const u8) bool {
+ return std.mem.eql(u8, name, tool_name) or std.mem.eql(u8, name, "bash");
+}
+
pub fn isAlias(name: []const u8) bool {
return std.mem.eql(u8, name, "bash") or
std.mem.eql(u8, name, "bash_output") or
diff --git a/src/tools.zig b/src/tools.zig
index cd85ce7b..dd46c1c0 100644
--- a/src/tools.zig
+++ b/src/tools.zig
@@ -214,7 +214,7 @@ pub fn codedbGuard(ctx: ToolCtx, call: ToolCall) ?ToolOutput {
if (!main_mod.g_codedb_guard) return null;
// Licensed or not: shell reads of a concrete source file go to codedb,
// not mcp__codedbpro__read (ADR 0040).
- if (!std.mem.eql(u8, call.name, "bash")) return null;
+ if (!@import("shell_tool.zig").runsCommand(call.name)) return null;
const cmd = strField(call.input, "command") orelse return null;
// First word must be a code scan/read utility (basename, so /usr/bin/grep