Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: agent commands never get the app's own keys

ogsamratb32324b031ca
diff --git a/apps/server/src/opencode-plugins/managed-policy.test.ts b/apps/server/src/opencode-plugins/managed-policy.test.ts
index 05dfc69e..129f73df 100644
--- a/apps/server/src/opencode-plugins/managed-policy.test.ts
+++ b/apps/server/src/opencode-plugins/managed-policy.test.ts
@@ -8,6 +8,7 @@ import managedPolicy from "./managed-policy.js";
import managedPolicyNext from "./managed-policy-next.js";
import { DESTRUCTIVE_MARKER } from "../git-command-policy.js";
import { annotateShellOutput, prepareShellCommand, readGitIdentity } from "./managed-policy-client.js";
+import { SHELL_SECRET_ENV } from "./shell-secret-env.js";
type EvaluateCall = { action: string; input: Record<string, unknown> };
@@ -295,6 +296,21 @@ describe("managed-policy engine plugin: git workflows", () => {
expect(pty.env.OMNIRUSH_SESSION).toBeUndefined();
});
+ test("shell.env blanks the app's own secrets for every shell call and terminal", async () => {
+ policyServer({ account: null });
+ for (const name of SHELL_SECRET_ENV) setEnv(name, `secret-${name.toLowerCase()}-0123456789`);
+ const hooks = await managedPolicy({ directory: "/" }) as unknown as Record<string, (event: unknown, output: unknown) => Promise<void>>;
+ for (const event of [{ cwd: "/", callID: "call_1", sessionID: "ses_1" }, { cwd: "/" }]) {
+ const output: { env: Record<string, string> } = { env: {} };
+ await hooks["shell.env"](event, output);
+ // What the engine gives the command: its own environment with the hook's values on top.
+ const shellEnv = { ...process.env, ...output.env };
+ for (const name of SHELL_SECRET_ENV) expect(shellEnv[name]).toBe("");
+ expect(Object.values(shellEnv).some((value) => typeof value === "string" && value.includes("0123456789"))).toBe(false);
+ }
+ expect([...SHELL_SECRET_ENV]).toEqual(expect.arrayContaining(["OPENCODE_SERVER_PASSWORD", "OPENCODE_SERVER_USERNAME", "OMNIRUSH_SERVER_TOKEN", "OMNIRUSH_POLICY_TOKEN", "OMNIRUSH_ACCESS_TOKEN"]));
+ });
+
test("the next-engine plugin applies the same rules through its structural hooks", async () => {
const { calls } = policyServer({ account: { name: "Sam Example", email: "[email protected]" } });
const repo = await temporaryRepository();
diff --git a/apps/server/src/opencode-plugins/managed-policy.ts b/apps/server/src/opencode-plugins/managed-policy.ts
index 8f124a2b..89d429e5 100644
--- a/apps/server/src/opencode-plugins/managed-policy.ts
+++ b/apps/server/src/opencode-plugins/managed-policy.ts
@@ -1,5 +1,6 @@
import { annotateShellOutput, check, checkManagedTool } from "./managed-policy-client.js";
import { applyEnginePath } from "../engine-shell-path.js";
+import { blankShellSecrets } from "./shell-secret-env.js";
type ToolBeforeInput = { tool: string; sessionID?: string; callID?: string };
type ToolAfterInput = { tool: string; sessionID?: string; callID?: string; args?: unknown };
@@ -17,6 +18,8 @@ export default async function managedPolicy(input?: { directory?: string }) {
output.output = annotateShellOutput(event.callID, output.output);
},
"shell.env": async (event: { cwd: string; callID?: string; sessionID?: string }, output: { env: Record<string, string> }) => {
+ // The app's own secrets never reach a command the agent runs (shell-secret-env.ts).
+ blankShellSecrets(output.env);
if (enginePath) output.env.PATH = enginePath;
// The call and session the shell runs for: the install log's wrappers note them, so an
// install is tied to the very call that ran it (parallel calls, a sub-agent's, one in the background).
diff --git a/apps/server/src/opencode-plugins/shell-secret-env.ts b/apps/server/src/opencode-plugins/shell-secret-env.ts
new file mode 100644
index 00000000..b588c6f4
--- /dev/null
+++ b/apps/server/src/opencode-plugins/shell-secret-env.ts
@@ -0,0 +1,21 @@
+/**
+ * The app's own per-run secrets that the engine holds in its environment: the
+ * engine server's login, the app server's bearer, the policy token and the
+ * local gateway's key. A command the agent runs (a shell call or a terminal)
+ * gets the engine's environment plus the `shell.env` hook's values, so each is
+ * set to "" there: no command can read one or use it against the local servers.
+ */
+export const SHELL_SECRET_ENV = Object.freeze([
+ "OPENCODE_SERVER_PASSWORD",
+ "OPENCODE_SERVER_USERNAME",
+ "OPENCODE_PASSWORD",
+ "OMNIRUSH_SERVER_TOKEN",
+ "OMNIRUSH_POLICY_TOKEN",
+ "OMNIRUSH_ACCESS_TOKEN",
+]);
+
+/** Blanks every app secret in a shell's env (the engine's `shell.env` hook output). */
+export function blankShellSecrets(env: Record<string, string> | undefined | null): void {
+ if (!env) return;
+ for (const name of SHELL_SECRET_ENV) env[name] = "";
+}