Skip to content

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

Python

Refuse shell commands that contain a NUL byte (#1270)

Rach Pradhan2b2c95440ef3
diff --git a/src/exec_bash.zig b/src/exec_bash.zig
index 404ce33f..ca184db7 100644
--- a/src/exec_bash.zig
+++ b/src/exec_bash.zig
@@ -292,8 +292,21 @@ test {
_ = server_port;
}
+/// #1270: spawn copies argv into NUL-terminated C strings, so a NUL inside
+/// the command (JSON `\u0000`) ended the string there: `make test\u0000 &&
+/// git push` ran only `make test` and reported its success. Refuse before
+/// anything is gated or spawned, naming the offset so the model can fix it.
+pub fn nulRejection(gpa: Allocator, cmd: []const u8) !?ToolOutput {
+ const at = std.mem.indexOfScalar(u8, cmd, 0) orelse return null;
+ return .{
+ .text = try std.fmt.allocPrint(gpa, "command refused: it contains a NUL byte at offset {d}. The shell would stop reading there and run only the text before it. Remove the NUL and retry.", .{at}),
+ .is_error = true,
+ };
+}
+
pub fn exec(ctx: ToolCtx, call: tools.ToolCall) !ToolOutput {
const cmd = strField(call.input, "command") orelse return missingArg(ctx.gpa, "command");
+ if (try nulRejection(ctx.gpa, cmd)) |refused| return refused;
if (try @import("publish_gate.zig").beforeExec(ctx, cmd)) |denied| return denied;
// Respect the subagent gate before running even a read-only probe.
const approved = if (ctx.from_sub) (if (ctx.approvals) |ap| ap.allowed(ctx.io, cmd) else true) else true;
diff --git a/src/shell_tool.zig b/src/shell_tool.zig
index 7302ca37..4b4a91a9 100644
--- a/src/shell_tool.zig
+++ b/src/shell_tool.zig
@@ -192,6 +192,22 @@ test "shell interact dispatch names the missing PTY" {
try std.testing.expect(std.mem.indexOf(u8, out.text, "not a PTY") != null);
}
+test "#1270: a NUL byte in a shell command is refused with its offset, never run" {
+ const gpa = std.testing.allocator;
+ var client: std.http.Client = undefined;
+ const ctx: ToolCtx = .{ .gpa = gpa, .io = std.testing.io, .client = &client, .provider = undefined, .registry = null, .from_sub = false, .approvals = null, .tracer = null };
+ for ([_][]const u8{ "shell", "bash" }) |name| {
+ var c = try parseCall(gpa, name, "{\"command\":\"true\\u0000 && false\"}");
+ defer c.parsed.deinit();
+ try std.testing.expectEqual(@as(?usize, 4), std.mem.indexOfScalar(u8, runCommand(c.call).?, 0));
+ const out = try exec(ctx, c.call);
+ defer gpa.free(out.text);
+ try std.testing.expect(out.is_error);
+ try std.testing.expect(!out.pending);
+ try std.testing.expect(std.mem.indexOf(u8, out.text, "NUL byte at offset 4") != null);
+ }
+}
+
test "catalog advertises shell, not the three bash names" {
const schema = @import("schema.zig");
var saw_shell = false;