/semgrep:add-and-validate-guardian-developer-semgrep-rulefrom Semgrep Guardian
Guides users through writing custom Semgrep rules and adding them to Guardian at user or repo scope, then validates by creating a file that triggers the rule.
Ships in Semgrep Guardian. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
guardian scans every file claude writes or edits. a custom rule makes it catch something the deployment's ruleset does not — a project convention, a deprecated internal api, a footgun this team keeps hitting.
rules live in two places, both read on every scan:
~/.semgrep/guardian-rules/ — user scope, follows the user into every repository<repo root>/.semgrep/guardian-rules/ — repo scope, follows the code, so everyone who clones it gets the ruleadd rules with add_developer_semgrep_rule rather than writing into those
directories by hand: it resolves the scope's directory and will not write over
a rule already there. it does not check the rule itself or count it against the
deployment's limit — that happens when a scan reads the rules, and step 5 is
how you find out.
a file can hold one rule or a rules: list. a file that fails to parse is
rejected whole, so rules kept in one file are sent together or not at all. this
skill writes one rule per file, which keeps that blast radius at one rule.
call list_developer_semgrep_rules before anything else. it reports both
directories, the rule files already there, whether they are being sent, and the
deployment's limit.
guardian.developer_rules_enabled.login tool), then retry.note the existing rule ids; yours has to be new.
ask for code that should match, and if they have it, code that looks similar and should not. you need the language, what makes the bad case bad as a code shape rather than a feeling, and what nearby code must keep passing. write both snippets down — step 5 uses them as fixtures.
rules:
- id: no-direct-requests-get
message: use the retrying client in lib/http.py, not requests.get directly.
severity: WARNING
languages: [python]
patterns:
- pattern: requests.get(...)
- pattern-not-inside: |
def _retrying_get(...):
...
pattern matches one shape. ... stands for any sequence (arguments,
statements, elements), and $x is a metavariable — the same name twice means
the same code twice.patterns: is and, pattern-either: is or. cut false positives with
pattern-not, pattern-not-inside, metavariable-regex.languages: takes python, javascript, typescript, go, java, ruby,
generic, and the rest of semgrep's list. severity: is ERROR, WARNING, or INFO.id must be new and specific — no-requests-get-in-handlers, not http.
against another local rule, the later file is dropped whole; against the
deployment's ruleset, your rule is the one dropped.two things about how guardian runs these, which should shape the rule:
use askuserquestion. the choice is who the rule is for:
<repo>/.semgrep/guardian-rules/ and is shared with the team once committed.~/.semgrep/guardian-rules/, and nobody else sees it.default to repo when the rule names anything specific to the project, user when it is about how this person likes to write code.
call add_developer_semgrep_rule with the rule text, the chosen scope, and a
name to write it as — name the file after the rule's id, e.g. no-eval.yaml.
the tool writes what you give it; it can only refuse the name or the place:
"x" must be a file name rather than a path — pass a bare name.<path> already exists; pass a different name — that file is somebody's
working rule. pick another name, or edit that file if the user meant to
replace it.repo scope needs a git repository, and this is not one — offer user scope.the add succeeding says nothing about whether the rule is usable or fits: the
tool writes the file first and checks nothing. so call
list_developer_semgrep_rules straight away, before any other write, and find
the new file in it:
id. it is left out of
every scan without a word, so fix it in place now; the fixture below would
just go through.n developer rules on disk, over the limit of m — the deployment's cap, and
your file is what crossed it. going over does not mean the extra rule is
ignored: no rules are sent, and guardian blocks writes until the count is
back under. delete the file you just added, at the path the add returned,
and list again to confirm rules are being sent. then tell the user: to keep
this rule, retire another (the remove-guardian-developer-semgrep-rule
skill) and add it again.do not write the fixture until the listing shows every rule being sent and your file among them without an error.
guardian checks only the shape, not whether semgrep will accept the rule. a rule the scanner rejects shows up in the next step as a failed scan carrying the scanner's parse error rather than as a block. that is the signal to fix the yaml and try again.
now test it the way it will actually be used, by writing code that trips it. tell the user first that the write is meant to be blocked, so the block does not read as a failure.
guardian-rule-check.py. use write on a new file: an edit only reports
findings the edit introduced, and a new file has no baseline to subtract.
give it a real source extension; markdown is never scanned, and files outside
the working directory are not either.message. that
block is the rule working. a scan that fails instead means the scanner
would not take the rule — read its error, fix the file in place, and write
the fixture again.if nothing fires, the rule is already on disk — edit it in place in the scope's
directory and write the fixture again; every scan re-reads the rules, so nothing
needs restarting. work through, in order: does list_developer_semgrep_rules
say the rules are being sent, does languages: match the fixture's extension,
and is the pattern too specific (drop the pattern-not clauses and narrow again
from a rule that fires).
run list_developer_semgrep_rules once more and tell the user:
rules: list with one entry or a single bare
rule. either works; a second rule in the same file, or a second yaml document
after ---, makes the whole file unusable.list_developer_semgrep_rules reports the cap and how many of the rules on
disk fit under it.message and metadata all the same.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub semgrep/guardian --plugin semgrep5 skills in this plugin
/install-mfwInstall the mfw (Semgrep Malware Firewall) CLI via the curl|sh installer and walk the user through setup — trust store, PATH shims, shell integration, and the background daemon — asking questions at real decision points. Detects when the user's shell rc files are *managed* (symlinked into nix / home-manager / chezmoi / GNU stow / a dotfiles git repo) and, instead of clobbering them, adds mfw's source line to the right source-of-truth. Use when the user wants to install, set up, or onboard mfw on this machine./recommend-guardian-policy-rulesRecommend Semgrep rules to add to the organization's Guardian policy for a repository — pull the rules Guardian runs now, gather candidates from the repo's existing findings and the org's custom rules, rank them for Guardian, let the user pick, and turn the picks on through the Semgrep MCP. Use when someone wants Guardian to catch more, asks which rules Guardian should run, or wants to tune or customize the Guardian policy for a repo./reconnectRe-check Semgrep Guardian's connection and recover it — verify the credential live, and log in again if it is expired. Use when Guardian reported that it timed out checking credentials or could not reach Semgrep, when scans seem not to be running, or when the user asks to reconnect Guardian.Other plugins' skills for the same job