Dev.to AI 🤖 Ai 👁 0 📖 4 min read

How to stop third-party skills from running shell commands in Claude Code

Skills can run shell commands while they load, before Claude reads a single line of them. I use that on purpose. It’s a cheap way to drop git status or the current branch into a skill without spending a tool call on it.

How to stop third-party skills from running shell commands in Claude Code

Skills can run shell commands while they load, before Claude reads a single line of them.

I use that on purpose. It’s a cheap way to drop git status or the current branch into a skill without spending a tool call on it.

Then it clicked that the same feature works just as well inside somebody else’s skill. You clone a repo, you type the skill’s name, and a command runs. No prompt, nothing to approve.

So I built a hook for it. Here’s what’s actually going on, and how to lock it down in about two minutes.

// Detect dark theme var iframe = document.getElementById('tweet-2125768841779130868-452'); if (document.body.className.includes('dark-theme')) { iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=2125768841779130868&theme=dark" }

What happens when a skill loads

Inside a SKILL.md you can write a command two ways: inline, as an exclamation mark followed by the command in backticks, or in a fenced block that opens with three backticks and an exclamation mark.

Claude Code runs those commands first and pastes the output into the skill text. Claude never sees the command, only the result. That’s the whole point: you get live data in the prompt instead of asking Claude to go fetch it.

There is a permission check, but it’s quieter than you’d expect. These commands never prompt you while the skill renders. They’re matched against your permission rules, and a skill can pre-approve its own commands with allowed-tools in its frontmatter.

Read that last part again. A skill you cloned can approve its own curl ... | sh.

This is the one most people miss: PreToolUse hooks don’t fire for these commands at all. If you already run a dangerous-command blocker on PreToolUse, it never sees them.

The hook Claude Code added for this

Claude Code 2.1.332 added a hook event called PreSkillShellCommand. It runs before each of those injected commands, and it can do exactly one thing: block.

If your hook blocks a command, the skill still loads. Claude reads [not run: blocked by hook: <reason>] where the output would have been, so it knows what happened. It can then run the command itself as a normal Bash call, which goes through your usual permission rules and prompts you like anything else.

That’s a good design. Nothing disappears silently, it just stops being automatic.

How to install skill-shell-guard

skill-shell-guard is the hook I built on that event. It is an allowlist, so anything it does not recognize gets refused.

  1. Update Claude Code to 2.1.332 or later. Older versions run injected commands without ever calling the hook, and they print an “Unknown hook event” warning at every start.
  2. Run the install command below.
  3. When the CLI asks where to install, pick User settings. That writes to ~/.claude/settings.json and covers every repo you clone from then on.
npx claude-code-templates@latest --hook security/skill-shell-guard

It needs python3, and that’s the only dependency.

What it lets through: read-only commands, with their options checked. ls, cat, head, grep, wc, git status, git log, git diff, node --version, npm ls, docker ps.

What it blocks: network calls like curl and wget, package scripts like npm run and npx, interpreters and scripts, writes, deletes, and any argument naming a credentials file (.env, ~/.ssh, ~/.aws, *.pem, id_rsa). It also refuses anything bash would expand, so $(...), backticks, globs and redirects are out, except redirecting to /dev/null.

If you need one exact command allowed, put it in allowCommands in ~/.claude/skill-shell-guard.json. That file is never read from the project, so a repository can't allow its own commands.

Is it worth it?

Yes, with one thing said out loud: this is a filter on command text, not a sandbox.

grep -r can still print a secret out of a file it never names. git remote -v will show a token embedded in a remote URL. Commands on the allowlist read whatever file they're handed. The guard stops a skill from calling out to the network or running a script you never read. It doesn't stop an allowed command from reading something it shouldn't.

There is also a blunter option inside Claude Code itself. Set "disableSkillShellExecution": true in your settings and every injected command is replaced with a placeholder instead of running. If you never use this feature, that is simpler than a hook. The guard is for people who want to keep injecting git status and still sleep at night.

Install it at the user level. Two minutes, and it covers every repo you clone from here on.

Links

You can follow me on my social media:

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.