Skip to content

Add optional Claude Code hook for blocking malicious packages#172

Closed
David Larsen (dc-larsen) wants to merge 2 commits into
mainfrom
add-socket-gate-hook
Closed

Add optional Claude Code hook for blocking malicious packages#172
David Larsen (dc-larsen) wants to merge 2 commits into
mainfrom
add-socket-gate-hook

Conversation

@dc-larsen
Copy link
Copy Markdown
Contributor

Summary

Adds a PreToolUse hook (hooks/socket-gate.ts) for Claude Code that intercepts package install commands and queries the public Socket MCP server before allowing installation.

  • Blocks packages with supply chain score below 20 (known malware, typosquats, high-risk signals)
  • Supports npm, PyPI, Cargo, RubyGems, Go, and NuGet ecosystems
  • No API key, no CLI, no registration — uses https://mcp.socket.dev/ directly
  • Fails open on network, parse, and timeout errors so a Socket outage never blocks legitimate work
  • Standalone Node.js script using --experimental-strip-types — no build step, no extra deps

Replaces #168 (which used the Socket CLI + API key). Design simplified after review feedback.

Files

File Description
hooks/socket-gate.ts The hook script
hooks/socket-gate.test.ts 23 tests (8 unit, 15 integration)
README.md Setup and usage docs under "Claude Code Hook (Optional)"

Supported commands

Ecosystem Commands
npm npm install, npm i, npm add, yarn add, pnpm add, bun add
PyPI pip install, pip3 install, uv add, uv pip install, poetry add, pipenv install
Cargo cargo add, cargo install
RubyGems gem install, bundle add
Go go get, go install
NuGet dotnet add package, nuget install

How it works

  1. Claude Code emits a PreToolUse JSON payload on stdin for every Bash call
  2. The hook extracts the package name from recognized install commands
  3. It POSTs an MCP initialize then a tools/call for the depscore tool against https://mcp.socket.dev/
  4. If supplyChain < 20, it returns deny with a reason that includes the score and a socket.dev review link
  5. Anything else (unrecognized command, non-install, network error, parse error) returns allow

Test results

ℹ tests 23
ℹ pass 23
ℹ fail 0

Inspired by Jimmy Vo's dependency hook.

Adds a PreToolUse hook (hooks/socket-gate.ts) that intercepts package
install commands across npm, PyPI, Cargo, RubyGems, Go, and NuGet, and
queries the public Socket MCP server at https://mcp.socket.dev/ to
block packages with a supply chain score below 20 (known malware,
typosquats, high-risk supply chain signals).

- No API key, CLI, or registration required
- Fails open on network, parse, and timeout errors
- 23 tests (8 unit + 15 integration), all passing

Inspired by https://blog.jimmyvo.com/posts/claudes-dependency-hook/
John-David Dalton (jdalton) added a commit that referenced this pull request Jun 1, 2026
…all docs

Picks up #172 (a PreToolUse hook that blocks installs of packages scoring
below the Socket supply-chain threshold across npm/PyPI/Cargo/gem/Go/NuGet,
failing open on errors) and #111 (Factory install instructions). The hook
is a self-contained, dependency-free copy-paste artifact for ~/.claude/hooks;
stdout is its IPC channel so the raw writes carry a logger-guard opt-out.
Adapted to fleet conventions (export helpers, undefined over null, sorted
methods) and re-tested as test/socket-gate.test.ts under vitest with a
stubbed fetch — no live network — covering the parsers + allow/deny/error
decisions.

Co-authored-by: dc-larsen <dc-larsen@users.noreply.github.com>
Co-authored-by: factory-ben <factory-ben@users.noreply.github.com>
@jdalton
Copy link
Copy Markdown
Collaborator

Landed in main as part of ae237a0 (feat(hooks): add optional socket-gate Claude Code hook + Factory install docs), with you credited as co-author. The hook carried over as hooks/socket-gate.ts, adapted to fleet conventions (exported helpers, undefined-over-null, sorted methods), and the test was re-implemented as test/socket-gate.test.ts under vitest with a stubbed fetch (no live network) covering the parsers + allow/deny/error decisions. Verified running live against the public server. Thanks — nice guardrail!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants