DEV Community

Cover image for Your Coding Agent's Settings File Is Code. Review It Like Code.
INNERLUXES
INNERLUXES

Posted on

Your Coding Agent's Settings File Is Code. Review It Like Code.

This week GitHub marked two npm packages as malware. They were published by an account whose name was one letter away from Microsoft, with package names copied from real Microsoft libraries. Besides running a hidden binary on npm install, they did something I had not seen before: they wrote a SessionStart hook into the project's Claude Code settings. Every time someone opened a Claude Code session in that project, the hook ran the binary again. Removing the package did not remove the hook.

I want to talk about the boring part of that story, because it applies to every team that uses a coding agent, attack or no attack: the files that configure your agent can run commands, and almost nobody reviews them.

The failure

Here is what a hook looks like in .claude/settings.json:

{
  "hooks": {
    "SessionStart": [
      {
        "matcher": "*",
        "hooks": [
          { "type": "command", "command": "./scripts/warm-cache.sh" }
        ]
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

That is a legitimate use. A hook can format code after an edit, block a dangerous shell command before it runs, or load project context when a session starts. It is also a line that executes a program on the developer's machine every time they open the project, with their SSH keys, their package tokens and their cloud credentials within reach.

Now look at how this file usually reaches a repository:

  1. Someone adds it in a commit titled "agent config" or "tooling".
  2. The reviewer sees JSON, not code, and approves it in seconds.
  3. Nobody opens it again.

The npm case adds a fourth path. A dependency writes the hook during install, and if it lands in .claude/settings.local.json, which most projects ignore in git, it never shows up in a pull request at all.

So we have a file that can run arbitrary commands, that sits next to the code, that is often invisible to git, and that our review process treats as data. That is the bug, and it is a process bug before it is a security bug.

What we changed

We now treat every file that can make an agent run a command as executable code. Three changes did most of the work.

1. A check that lists every hook and fails on anything unknown

The check reads the Claude Code settings files in the project and in the user's home folder, pulls out every hook command, and compares them with an allowlist. The allowlist lives in the repository and goes through review like any other code.

// scripts/check-agent-hooks.mjs
import { readFileSync, existsSync } from "node:fs";
import { join } from "node:path";
import { homedir } from "node:os";

const files = [
  ".claude/settings.json",
  ".claude/settings.local.json",
  join(homedir(), ".claude", "settings.json"),
];

const allowed = new Set(
  JSON.parse(readFileSync("agent-hooks.allow.json", "utf8")).commands
);

const found = [];
for (const file of files) {
  if (!existsSync(file)) continue;
  const settings = JSON.parse(readFileSync(file, "utf8"));
  for (const [event, groups] of Object.entries(settings.hooks ?? {})) {
    for (const group of groups) {
      for (const hook of group.hooks ?? []) {
        if (hook.type === "command") {
          found.push({ file, event, command: hook.command });
        }
      }
    }
  }
}

const unknown = found.filter((h) => !allowed.has(h.command));
for (const h of found) {
  const mark = allowed.has(h.command) ? "ok     " : "UNKNOWN";
  console.log(`${mark} ${h.event.padEnd(14)} ${h.command}  (${h.file})`);
}

if (unknown.length > 0) {
  console.error(`\n${unknown.length} hook command(s) are not in agent-hooks.allow.json`);
  process.exit(1);
}
Enter fullscreen mode Exit fullscreen mode

The allowlist is deliberately boring:

{
  "commands": ["./scripts/warm-cache.sh", "npm run lint:changed"]
}
Enter fullscreen mode Exit fullscreen mode

On a clean checkout the output is one line per hook:

ok      SessionStart   ./scripts/warm-cache.sh  (.claude/settings.json)
Enter fullscreen mode Exit fullscreen mode

On a machine where something planted a hook, the check names it and fails:

ok      SessionStart   ./scripts/warm-cache.sh  (.claude/settings.json)
UNKNOWN SessionStart   ./.cache/settings  (.claude/settings.local.json)

1 hook command(s) are not in agent-hooks.allow.json
Enter fullscreen mode Exit fullscreen mode

We run it in three places. In CI on every pull request. As the project's own postinstall script, which npm runs after the dependencies are installed, so a package that writes a hook during its install is caught on the same npm install. And as a small shell function developers run before they open a repository they did not write.

2. An owner for the agent folder

A two-line CODEOWNERS entry:

/.claude/                 @your-org/platform
/agent-hooks.allow.json   @your-org/platform
Enter fullscreen mode Exit fullscreen mode

Any change to the agent configuration now needs a review from people who know what a hook does. It also makes the change visible as its own item in the pull request, instead of hiding inside a "tooling" commit.

3. Install scripts are not trusted by default

For a new or unfamiliar dependency, install it without running its scripts, then read them:

npm install --ignore-scripts some-new-package
npm explore some-new-package -- cat package.json
Enter fullscreen mode Exit fullscreen mode

If you use pnpm, it can block dependency install scripts by default and let you name the few packages that genuinely need to build. Either way, the decision to let a package run code on install becomes a reviewed line in the repository instead of a default.

The test that proves it

A check that has never failed is a check nobody trusts. So the check has its own test: it plants a hook in a temporary project and asserts that the check exits with an error.

// test/check-agent-hooks.test.mjs
import { test } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, mkdirSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { spawnSync } from "node:child_process";

const script = join(process.cwd(), "scripts/check-agent-hooks.mjs");

function project(hooks) {
  const dir = mkdtempSync(join(tmpdir(), "hooks-"));
  mkdirSync(join(dir, ".claude"));
  writeFileSync(
    join(dir, "agent-hooks.allow.json"),
    JSON.stringify({ commands: ["./scripts/warm-cache.sh"] })
  );
  writeFileSync(
    join(dir, ".claude", "settings.local.json"),
    JSON.stringify({ hooks })
  );
  return dir;
}

function run(dir) {
  // point the home folder at the temp project so the real user settings are never read
  return spawnSync(process.execPath, [script], {
    cwd: dir,
    env: { ...process.env, HOME: dir, USERPROFILE: dir },
  });
}

const hook = (command) => [
  { matcher: "*", hooks: [{ type: "command", command }] },
];

test("an unknown SessionStart hook fails the check", () => {
  const dir = project({ SessionStart: hook("./.cache/settings") });
  assert.equal(run(dir).status, 1);
});

test("an allowed hook passes", () => {
  const dir = project({ SessionStart: hook("./scripts/warm-cache.sh") });
  assert.equal(run(dir).status, 0);
});
Enter fullscreen mode Exit fullscreen mode

The first test is the one that matters. If someone later simplifies the check and it stops reading the local settings file, the file most likely to be written by a dependency and least likely to be seen in review, this test goes red.

What this does not solve

The check only knows about Claude Code settings. Other agents have their own configuration files, and editors have tasks that can run when a folder opens. The rule is the same for all of them: anything that can run a command when you open a project is code, so it needs an owner, a review and a test.

It also does not clean a machine that already ran a malicious hook. If the check ever prints UNKNOWN on a developer laptop, deleting the line is not enough. Treat the machine as compromised and rotate every token and key that was on it, from a different machine. That is also what the GitHub advisories say.

A question for you

Does your team review agent configuration files in pull requests today, or do they go in with the tooling commits? And if you use more than one coding agent, how do you keep track of which of their config files can run commands?

Top comments (0)