[Feature Request] Built-in Claude Code (and other AI coding agent) PreToolUse hook subcommand

Open
#45 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Tech stack
shell
Domain
cli, security

Research direction

Start with the existing package-manager matching that sfw supports and the sfw --help entry point, then review the proposed Claude Code settings.json hook and its stdin JSON payload. Define the hook subcommand's pass-through, blocking exit, stderr message, and retry hint behavior; done means matching installs are blocked while sfw-prefixed commands pass through.

Written by the indexing model from the issue text.

Description

Summary

Add a built-in subcommand — e.g. sfw hook claude — that reads a Claude Code PreToolUse JSON event on stdin and gates the tool call, blocking package-install commands that aren't already prefixed with sfw.

Why this matters

LLM coding agents (Claude Code, Cursor, Aider, Codex, etc.) routinely run npm install, pip install, uvx <tool>, cargo add etc. on the developer's behalf. They're arguably the highest-risk path for malicious package install today:

  • Agents can be steered into installing typosquatted or compromised packages via prompt injection
  • Agents iterate fast and don't pause for human review on every install
  • Reviewers of agent diffs often miss a subtle dependency tucked into a refactor

Wrapping these agents' package-manager calls with sfw is therefore more valuable than wrapping a careful human's. Claude Code's hook mechanism fires before tool execution and can block calls — with a small wrapper script, sfw already serves as that gate today, but every team has to roll the same script themselves.

Current workaround (what teams are already doing)

A bash script invoked from ~/.claude/settings.json:

#!/bin/bash
input=$(cat)
cmd=$(printf '%s' "$input" | jq -r '.tool_input.command // ""')
echo "$cmd" | grep -qE '(^|[[:space:];&|]+)sfw[[:space:]]' && exit 0
echo "$cmd" | grep -qE 'npm[[:space:]]+(install|i|add|exec|ci)|npx[[:space:]]+[^-]|yarn[[:space:]]+(add|install)|pnpm[[:space:]]+(add|install|i|dlx)|pip3?[[:space:]]+install|uv[[:space:]]+(pip[[:space:]]+install|add|tool[[:space:]]+install|run)|uvx[[:space:]]+[^-]|cargo[[:space:]]+(install|add)' && {
  echo "Socket Firewall guard: prefix with sfw — sfw $cmd" >&2
  exit 2
}
exit 0

…plus the settings.json wiring:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [{ "type": "command", "command": "/path/to/sfw-gate.sh" }]
      }
    ]
  }
}

It works, but it's per-team boilerplate that drifts out of sync as sfw adds support for new package managers.

Proposal

Ship the gate as a first-class sfw subcommand so settings.json becomes one line:

{
  "type": "command",
  "command": "sfw hook claude"
}

The subcommand:

  • Reads PreToolUse JSON from stdin
  • Allows pass-through if the inner command is already sfw-prefixed
  • Blocks (exit 2 with a stderr message) any matching install/exec command
  • Returns a retry hint the agent can act on: Re-run as: sfw <original command>

Pattern matching can stay in lockstep with the package managers sfw already supports — that's the main maintenance win over per-team scripts.

Prior art

rtk hook claude (from another tool, rust token killer) ships exactly this pattern — single subcommand that drops into the Claude Code hook slot with no wrapper.

Benefits

  • Zero per-team boilerplate for sfw + Claude Code adoption
  • One canonical implementation that stays current as new package managers are added
  • Lower bar for security-minded teams to harden their agent setups
  • Discoverable: sfw --help becomes the entry point

Open questions

  • Generalize to sfw hook <agent> to support Cursor / Aider / Codex / Continue.dev? Hook payload shapes differ across agents.
  • Should the block message include the proposed sfw-prefixed retry command verbatim? (Helps the agent auto-retry without a human round-trip.)
  • Should there be an env var or flag for "warn but don't block" mode for teams rolling this out gradually?
  • Worth detecting nested invocations (bash -c 'npm install foo', heredocs, etc.) inside the inner command?

Happy to submit a starter PR if there's interest in the direction.

Dominant language
No language data
Stars
271
Forks
8
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from SocketDev/sfw-free

All issues in SocketDev/sfw-free

Similar issues

More CLI issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.