DEV Community

Cover image for Claude Code skills run commands before Claude reads them. I gated them
The Paste-Ready Engineer
The Paste-Ready Engineer

Posted on Edited on

Claude Code skills run commands before Claude reads them. I gated them

I checked all 385 skill and plugin files on my machine. Four of them run shell commands the moment they load, 11 commands in total. Those commands finish before Claude reads a single word of the skill.

TL;DR

  • A Claude Code skill or slash command can embed shell commands that run on load. Claude only sees the output, so Claude can't stop them.
  • Claude Code 2.1.332 (Nov 25) added a new hook, PreSkillShellCommand, that fires right before those commands. I used it to build a gate that blocked every test skill that reached the network or deleted files.
  • All 11 load-time commands from the real skills I had installed passed through untouched. Along the way I found 4 ways to slip past the gate, and closed all of them.

Short on time? Everything below is free. If you'd rather paste one block and have the gate installed, I packaged it: skill-gate on Gumroad.

Skills run code the moment they load

When I find a skill that looks useful, I skim the description and install it. I almost never read it line by line. After counting those 11 commands, that habit felt a lot less comfortable.

Here's what a load-time command looks like inside a skill:

---
name: today
description: "Start the conversation with today's date"
---
Today is !`date +%Y-%m-%d`.
Enter fullscreen mode Exit fullscreen mode

The part wrapped in an exclamation mark and backticks runs in your shell when the skill loads. Its output replaces the command, and only then does the text go to Claude.

It's handy for dropping in the date or the git status. But if a skill someone shared has a network call hidden in there, asking Claude "is this safe?" is already too late. The command has run.

Claude didn't notice, and couldn't

I built a test skill that runs one "setup" script on load. In the middle of that script I put a single curl line shaped like it was sending data out (it pointed nowhere and did nothing).

I loaded the skill. Claude started the conversation as if nothing happened. All it got was the two lines the script printed: "preparing" and "done". There was nothing suspicious to see, so no model, however smart, could have caught it.

The new hook: PreSkillShellCommand

The regular tool hook, PreToolUse, does not fire for load-time commands. 2.1.332 added a separate one just for them. You register it in your user settings like this:

{
  "hooks": {
    "PreSkillShellCommand": [
      {
        "matcher": "Bash|PowerShell",
        "hooks": [
          { "type": "command", "command": "python3 ~/.claude/skill-gate/gate.py", "onFailure": "block" }
        ]
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

The hook receives the full command, the skill's name, and which settings it came from. If the script exits with code 2, the command doesn't run, and Claude is told it was blocked and why. onFailure set to block means that if the gate script itself crashes, nothing gets through.

My gate blocks commands that:

  • Reach the network (curl, wget, ssh, any URL)
  • Delete files, or move and copy things in your home folder
  • Read secrets (key folders, API keys, environment variables)
  • Run code built on the fly, or decode hidden content
  • Change system settings, change git settings, or push

That's 14 patterns in total.

I let one through myself

Embarrassing, but true. Midway through testing, I forgot to turn the gate on and ran a test skill that fetched a sample page and piped it into a shell. The page was empty, so it just errored out. But the person building the gate didn't notice either.

That's why the gate lives in my user settings, not per project. Whatever folder I open, it's on.

Four ways around it, all closed

Each one slips past if you only look at the single line of the command.

  • Script contents. My first version only checked the command text, so a skill that ran a script with curl inside passed. Now the gate reads every script the command runs.
  • Running a script by path, no "bash" in front. I found this one in a real plugin I had installed. The gate now reads those too.
  • Changing a script after you allow it. Claude pointed this out: "This allow rule only matches the command text, so if the script changes later, it still passes." Fair. Now an allowed script is pinned to its current content by hash. Change one character and it's blocked again.
  • A harmless command first. Put date first, then run the script after a semicolon, and my gate only looked at the first part. I found this after I thought I was done, which was not a great feeling. Now it follows every chained command, cd-then-run, and every line after the first, and I confirmed it blocks inside real Claude Code.

Testing it on real skills

I ran all 11 real load-time commands through the gate. At first it blocked one: a comment in a script contained a word the gate read as "dump environment variables." I made the gate skip comment lines, and after that all 11 passed on the latest version.

Skills that call npm or gh are blocked on purpose. Trusted ones need a single allow line. I'd rather the gate be a little too strict than a little too quiet.

When it blocks something, it writes a log line, shows a macOS notification, and tells you the exact line to add if you trust it.

Wrap-up

  • Skills can run shell commands on load, before Claude sees anything
  • Claude can't catch what it never sees, so the check has to happen before the command runs
  • 2.1.332's PreSkillShellCommand hook is the only place to do that
  • Read every script the command runs, not just the command line
  • Allow scripts by their current content, so a later edit gets blocked again

Try this today: count how many exclamation-mark-backtick commands are in the skills you have installed. I didn't expect four.

Over to you: do you read a skill before you install it? A one-word "yes" or "no" is fine. Tell me in the comments.

Want the skill?

Thanks for reading this far! If you now want to know what your own skills run on load, here's the packaged version.

  • Paste one block into Terminal and the whole gate is installed, with a backup of your settings first
  • Blocks 14 risky patterns. All 11 real load-time commands on my machine passed
  • The 4 bypasses I found are already closed

Good fit if you often install skills or plugins other people made. Not for you if you're on Windows: I've only tested it on a Mac.

👉 Get it: https://pastereadyengineer.gumroad.com/l/skill-gate

If this was useful, a ❤️ and a follow would mean a lot. I write about practical ways to put AI to work in indie development, with what actually happened when I tried it.

Top comments (1)

Collapse
 
cryptostrapon profile image
cryptostrapon •

This is the same shape as an npm postinstall hook: the dangerous part runs at load time, and the tool you trust only sees the output afterward. A gate that hashes the script content is the detail I liked most, because an allow rule on the command line would still pass after someone edits the file. I would also read the skill for those load-time command blocks before installing it, the same way I list lifecycle scripts before the first npm install.