Skip to content

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

Python

Keep provider keys out of tool children's environment (#1267)

Rach Pradhan99ef76627fce
diff --git a/src/agent_eval.zig b/src/agent_eval.zig
index adfbc266..08386dc2 100644
--- a/src/agent_eval.zig
+++ b/src/agent_eval.zig
@@ -17,7 +17,7 @@ const providerClass = scoring.providerClass;
const signScore = scoring.signScore;
const telemetry = @import("telemetry.zig");
-const runCapped = @import("jobs.zig").runCapped;
+const runCappedWithOptions = @import("jobs.zig").runCappedWithOptions;
const judgeTask = @import("subagent.zig").judgeTask;
const eval_memory = @import("eval_memory.zig");
const trace = @import("trace.zig");
@@ -72,7 +72,7 @@ pub fn runEval(self: *Agent, note: []const u8) !ExecResult {
const commitment_id = std.fmt.bufPrint(&commitment_buf, "eval-{d}-{d}", .{ eval_turn, self.eval_iter + 1 }) catch "";
if (behavior) |bt| bt.recordExpectedAction(eval_turn, commitment_id, .{ .kind = "eval" }, .{ .pass = true }, "eval-driven loop verifier");
- const run = runCapped(self.gpa, self.io, &.{ "/bin/sh", "-c", cmd }, 64 * 1024, 16 * 1024, 0) catch |e| {
+ const run = runCappedWithOptions(self.gpa, self.io, &.{ "/bin/sh", "-c", cmd }, 64 * 1024, 16 * 1024, 0, .{ .environ_map = @import("tool_env.zig").get() }) catch |e| { // #1267
// The command never ran, so the commitment can never be verified by
// the normal exit-code/score path below; resolve it here instead of
// leaving a dangling turn_committed that a scorer would misread as
diff --git a/src/hooks.zig b/src/hooks.zig
index bccb62ae..fde91a2d 100644
--- a/src/hooks.zig
+++ b/src/hooks.zig
@@ -129,6 +129,7 @@ pub fn runHookCmd(gpa: Allocator, io: Io, command: []const u8, payload: []const
const none: HookRun = .{ .code = null, .stderr = &.{} };
var child = std.process.spawn(io, .{
.argv = &.{ "/bin/sh", "-c", command },
+ .environ_map = @import("tool_env.zig").get(), // #1267: no provider keys
.stdin = .pipe,
.stdout = .ignore,
.stderr = .pipe,
diff --git a/src/jobs.zig b/src/jobs.zig
index 4a10c90d..dcd93d4a 100644
--- a/src/jobs.zig
+++ b/src/jobs.zig
@@ -34,6 +34,7 @@ pub fn runCappedCwd(gpa: Allocator, io: Io, argv: []const []const u8, stdout_cap
pub fn toolRunOptions(cwd: ?[]const u8) CappedRunOptions {
return .{
.cwd = if (cwd) |path| .{ .path = path } else .inherit,
+ .environ_map = @import("tool_env.zig").get(), // #1267: no provider keys
.kill_process_tree = true,
};
}
@@ -307,6 +308,7 @@ pub fn spawnJobOpts(gpa: Allocator, io: Io, cmd: []const u8, opts: SpawnOpts) !*
var child = try std.process.spawn(io, .{
.argv = &argv,
.cwd = if (opts.cwd) |path| .{ .path = path } else .inherit,
+ .environ_map = @import("tool_env.zig").get(), // #1267: no provider keys
.stdin = .ignore,
.stdout = .pipe,
.stderr = .pipe,
diff --git a/src/local_tools.zig b/src/local_tools.zig
index dbb83f4a..eef2e3c4 100644
--- a/src/local_tools.zig
+++ b/src/local_tools.zig
@@ -182,9 +182,15 @@ fn jsonString(arena: Allocator, s: []const u8) ![]const u8 {
return aw.writer.buffered();
}
+/// Local tools are model-invoked children like `shell`: same scrubbed
+/// environment (#1267).
+fn toolEnvOpts() process_runner.CappedRunOptions {
+ return .{ .environ_map = @import("tool_env.zig").get() };
+}
+
fn dryRun(io: Io, gpa: Allocator, runner: []const u8, entry_rel: []const u8) bool {
const argv = [_][]const u8{ runner, entry_rel, "--dry-run" };
- const run_res = process_runner.runCapped(gpa, io, &argv, 32 * 1024, 4 * 1024, 8_000) catch return false;
+ const run_res = process_runner.runCappedWithOptions(gpa, io, &argv, 32 * 1024, 4 * 1024, 8_000, toolEnvOpts()) catch return false;
defer gpa.free(run_res.stdout);
defer gpa.free(run_res.stderr);
return run_res.term == .exited and run_res.term.exited == 0;
@@ -200,7 +206,7 @@ pub fn exec(gpa: Allocator, io: Io, name: []const u8, input: Value) !tools_mod.T
aw.writer.flush() catch {};
const payload = if (aw.writer.buffered().len == 0) "{}" else aw.writer.buffered();
const argv = [_][]const u8{ inst.runner, inst.entry_path, payload };
- const run_res = process_runner.runCapped(gpa, io, &argv, 256 * 1024, 16 * 1024, 30_000) catch |err| {
+ const run_res = process_runner.runCappedWithOptions(gpa, io, &argv, 256 * 1024, 16 * 1024, 30_000, toolEnvOpts()) catch |err| {
return .{ .text = try std.fmt.allocPrint(gpa, "local tool failed: {s}", .{@errorName(err)}), .is_error = true };
};
defer gpa.free(run_res.stdout);
diff --git a/src/startup.zig b/src/startup.zig
index 0eb78d68..3e038ed1 100644
--- a/src/startup.zig
+++ b/src/startup.zig
@@ -266,6 +266,10 @@ pub fn runSubcommand(io: Io, gpa: Allocator, arena: Allocator, init: std.process
// the login flow writes instead of "/.kimi/...".
oauth.initHome(keys_cli.homeEnv(init.environ_map) orelse "");
job_registry.home = keys_cli.homeEnv(init.environ_map) orelse ""; // #199: ownership records for background jobs
+ // #1267: tool children get graff's environment minus its credentials.
+ // After router_config.load (its key is one of them), before `serve` and
+ // the other subcommands below can spawn a tool.
+ @import("tool_env.zig").init(arena, init.environ_map);
// #557: same pin for GRAFF_PRICES_PATH, before `graff models` is dispatched
// below — the hydration points (router_catalog, models_cache) all sit well
// under the last call that still holds the environment.
diff --git a/src/test_hooks.zig b/src/test_hooks.zig
index 06fa8fb1..7b1c5f55 100644
--- a/src/test_hooks.zig
+++ b/src/test_hooks.zig
@@ -391,4 +391,5 @@ test {
_ = @import("mcp_rpc.zig");
_ = @import("subagent_brief.zig"); // structured delegation brief + harness environment header
_ = @import("idle_wake_sources.zig"); // shared idle auto-turn sources for REPL / TUI / ACP
+ _ = @import("tool_env.zig"); // #1267/#1268: tool children's environment
}
diff --git a/src/tool_env.zig b/src/tool_env.zig
new file mode 100644
index 00000000..8dff6667
--- /dev/null
+++ b/src/tool_env.zig
@@ -0,0 +1,162 @@
+//! The environment tool children run in: the `shell` tool's foreground and
+//! background jobs, local tools, lifecycle hooks, and the eval-loop command.
+//! Built once at startup from graff's own environment (startup.zig, before
+//! any subcommand can spawn one) and read-only afterwards.
+//!
+//! #1267: those children used to inherit everything, provider keys included,
+//! so one `env` in a tool call copied OPENAI_API_KEY into the transcript and
+//! the traces. The credential variables graff itself reads are removed.
+//! GRAFF_TOOL_ENV_PASS (comma-separated names) keeps any of them for the rare
+//! command that really needs one.
+//!
+//! Children graff launches as graff (learning runs) and MCP servers do not
+//! come through here: they keep the full environment, because they are the
+//! ones that legitimately read those keys. Subagents run in-process.
+
+const std = @import("std");
+const builtin = @import("builtin");
+const Allocator = std.mem.Allocator;
+const Map = std.process.Environ.Map;
+const provider = @import("provider.zig");
+
+pub const pass_env = "GRAFF_TOOL_ENV_PASS";
+
+/// Secrets graff reads that are not a provider_specs `env_key`.
+const extra_secrets = [_][]const u8{
+ "CODEGRAFF_API_KEY", // also a provider env_key; listed so it can never drift out
+ "GRAFF_TELEMETRY_KEY",
+ "GRAFF_MCP_TOKEN",
+ "GRAFF_SERVE_TOKEN",
+ "HARNESS_SERVE_TOKEN",
+};
+
+var g_map: ?Map = null;
+
+/// The map to hand `environ_map` when spawning a tool child. Null before
+/// `init` (unit tests, the in-process library) or if building it ran out of
+/// memory; the child then inherits graff's environment as it always did.
+pub fn get() ?*const Map {
+ if (g_map) |*m| return m;
+ return null;
+}
+
+/// Build the tool environment from graff's own. `arena` must outlive every
+/// spawn (the process arena). Called after router_config.load so a project
+/// router's key is scrubbed too.
+pub fn init(arena: Allocator, parent: *const Map) void {
+ g_map = build(arena, parent) catch null;
+}
+
+pub fn build(gpa: Allocator, parent: *const Map) Allocator.Error!Map {
+ var m = try parent.clone(gpa);
+ errdefer m.deinit();
+ const pass = parent.get(pass_env) orelse "";
+ for (provider.provider_specs) |spec| scrub(&m, spec.env_key, pass);
+ if (provider.additional_router) |spec| scrub(&m, spec.env_key, pass);
+ for (extra_secrets) |name| scrub(&m, name, pass);
+ return m;
+}
+
+fn scrub(m: *Map, name: []const u8, pass: []const u8) void {
+ if (!passed(pass, name)) _ = m.swapRemove(name);
+}
+
+/// Whether `name` is on the comma-separated pass-through list. Windows
+/// variable names are case-insensitive, like the Map itself there.
+fn passed(pass: []const u8, name: []const u8) bool {
+ var it = std.mem.tokenizeScalar(u8, pass, ',');
+ while (it.next()) |raw| {
+ const entry = std.mem.trim(u8, raw, " \t");
+ const same = if (builtin.os.tag == .windows) std.ascii.eqlIgnoreCase(entry, name) else std.mem.eql(u8, entry, name);
+ if (same) return true;
+ }
+ return false;
+}
+
+fn fakeParent(gpa: Allocator) !Map {
+ var parent = Map.init(gpa);
+ errdefer parent.deinit();
+ try parent.put("PATH", "/usr/bin:/bin");
+ try parent.put("HOME", "/tmp");
+ try parent.put("OPENAI_API_KEY", "sk-fake-openai");
+ try parent.put("XAI_API_KEY", "xai-fake");
+ try parent.put("CODEGRAFF_API_KEY", "cg_sk_fake");
+ try parent.put("GRAFF_TELEMETRY_KEY", "telemetry-fake");
+ return parent;
+}
+
+test "#1267: tool env drops the provider keys graff reads and keeps the rest" {
+ const gpa = std.testing.allocator;
+ var parent = try fakeParent(gpa);
+ defer parent.deinit();
+ try parent.put("MYROUTER_API_KEY", "router-fake");
+ const saved_router = provider.additional_router;
+ defer provider.additional_router = saved_router;
+ provider.additional_router = .{ .id = "myrouter", .display_name = "My Router", .kind = .openai, .auth = .bearer, .url = "http://127.0.0.1:9/v1/chat/completions", .env_key = "MYROUTER_API_KEY", .default_model = "m" };
+
+ var m = try build(gpa, &parent);
+ defer m.deinit();
+ for ([_][]const u8{ "OPENAI_API_KEY", "XAI_API_KEY", "CODEGRAFF_API_KEY", "GRAFF_TELEMETRY_KEY", "MYROUTER_API_KEY" }) |name|
+ try std.testing.expect(m.get(name) == null);
+ try std.testing.expectEqualStrings("/usr/bin:/bin", m.get("PATH").?);
+ try std.testing.expectEqualStrings("/tmp", m.get("HOME").?);
+ try std.testing.expectEqualStrings("sk-fake-openai", parent.get("OPENAI_API_KEY").?); // graff keeps its own
+}
+
+test "#1267: GRAFF_TOOL_ENV_PASS keeps the named keys only" {
+ const gpa = std.testing.allocator;
+ var parent = try fakeParent(gpa);
+ defer parent.deinit();
+ try parent.put(pass_env, " XAI_API_KEY ,NOT_SET");
+ var m = try build(gpa, &parent);
+ defer m.deinit();
+ try std.testing.expectEqualStrings("xai-fake", m.get("XAI_API_KEY").?);
+ try std.testing.expect(m.get("OPENAI_API_KEY") == null);
+ try std.testing.expect(m.get("NOT_SET") == null);
+}
+
+/// Install `build(fake parent)` as the tool environment for one test and
+/// return what it replaced.
+fn installFake(gpa: Allocator) !?Map {
+ var parent = try fakeParent(gpa);
+ defer parent.deinit();
+ const saved = g_map;
+ g_map = try build(gpa, &parent);
+ return saved;
+}
+
+test "#1267: a real tool child (both shell paths) cannot see the key" {
+ if (builtin.os.tag == .windows or builtin.os.tag == .wasi) return error.SkipZigTest;
+ const gpa = std.testing.allocator;
+ const io = std.testing.io;
+ const jobs = @import("jobs.zig");
+ const saved = try installFake(gpa);
+ defer {
+ g_map.?.deinit();
+ g_map = saved;
+ }
+
+ // Subagent foreground path: runCapped with the tool run options.
+ const sh = jobs.shellArgv("env");
+ const run = try jobs.runCappedWithOptions(gpa, io, &sh, 64 * 1024, 4096, 10_000, jobs.toolRunOptions(null));
+ defer gpa.free(run.stdout);
+ defer gpa.free(run.stderr);
+ try std.testing.expect(std.mem.indexOf(u8, run.stdout, "PATH=/usr/bin:/bin") != null);
+ try std.testing.expect(std.mem.indexOf(u8, run.stdout, "sk-fake-openai") == null);
+ try std.testing.expect(std.mem.indexOf(u8, run.stdout, "OPENAI_API_KEY") == null);
+
+ // Root path: a job-registry shell, as `shell` action=run spawns it.
+ jobs.g_jobs = .{};
+ defer jobs.jobsReap(gpa, io);
+ const id = (try jobs.spawnJob(gpa, io, "env")).id;
+ const waited = try jobs.waitForeground(gpa, io, id, 10_000);
+ defer switch (waited) {
+ .running => |r| gpa.free(r.output),
+ .done => |d| gpa.free(d.output),
+ .cancelled => |c| gpa.free(c.output),
+ };
+ try std.testing.expect(waited == .done);
+ try std.testing.expect(std.mem.indexOf(u8, waited.done.output, "PATH=/usr/bin:/bin") != null);
+ try std.testing.expect(std.mem.indexOf(u8, waited.done.output, "xai-fake") == null);
+ try std.testing.expect(std.mem.indexOf(u8, waited.done.output, "OPENAI_API_KEY") == null);
+}