Cyber Monday: 2XCYBER doubles your top-up, up to $2,000.Double my top-up

nc://semgrep

v0.1.0Pinned
Backnodecode-ai
ThomSharktools: add semgrep, supabase, stripe, docker and kubectl cells (#186)c504079· 2 Dec1 commit
test1 filecell.lisp4.7 KBcli.lisp11.1 KBreport.lisp11.2 KBtools.lisp9.9 KB

nodecode-semgrep

Semgrep static analysis as tools the model calls: scan local code with Semgrep's registry rulesets or your own rule files, and read the findings one line each, worst first. The code a finding matched is never shown, so a scan for secrets does not put the secrets it found into the conversation.

Written for Nodecode from Semgrep's documented command line, JSON and SARIF output. No code of Semgrep's is in this cell and the CLI is not vendored: you install it.

Install

  1. Install Semgrep so semgrep is on PATH (quickstart: pipx install semgrep, brew install semgrep, or python3 -m pip install semgrep), or set command to the binary's absolute path. The cell needs a Semgrep that writes --sarif-output (tested with 1.187.0). No account is needed.
  2. Add the cell: nodecode add nc://semgrep.
  3. Check it with /semgrep, which answers Semgrep's version and whether a login applies.

Tools

Tool What it does
semgrep_scan semgrep scan over absolute paths with registry or local rules; a summary line, then one line per finding: severity, rule id, file:line, the rule's own message, CWE and OWASP class
semgrep_validate semgrep scan --validate over local rule files: valid, or each problem with its rule and message; nothing is scanned

semgrep_scan arguments:

  • paths (required): absolute paths of files or folders. ~ is read against your home.
  • config: registry rulesets p/NAME (p/default, p/owasp-top-ten, p/secrets, p/python...), registry rules r/RULE-ID, or absolute paths of local rule files or folders. The section's default_config when omitted.
  • severity: low, medium, high or critical, the floor a finding must reach. Semgrep's INFO and LOW count as low, WARNING and MEDIUM as medium, ERROR and HIGH as high, CRITICAL as critical. What the floor leaves out is counted.
  • include, exclude: path globs (--include, --exclude), each value its own flag.
  • exclude_rules: rule ids to skip (--exclude-rule).
  • include_ignored: also scan what .gitignore leaves out (--no-git-ignore).
  • max_target_bytes, jobs, rule_timeout, max_findings: lower than the section's value, never higher (a section value of 0 for jobs, rule_timeout or max_target_bytes sets no ceiling; a section max_target_bytes of 0 with no call value is passed as --max-target-bytes=0, so Semgrep's own 1 MB default does not apply).

What it refuses and hides

  • The matched code. extra.lines, extra.fix and extra.metavars are never read, and neither is extra.message: Semgrep fills a rule's $METAVARIABLES into the message with the matched code, so a rule saying Hard-coded token $V would answer with the token. A finding shows the rule's own text instead, read from a SARIF copy of the same run (each rule's fullDescription), its $METAVARIABLES left unfilled. A rule whose text does not come back is listed without a message.
  • Scan errors about a scanned file name the file and line, never the message, which can quote the code. Errors about a rule or a config (a type naming a rule, pattern, YAML, schema, language or config) carry their message. An error counts as a file error when it has a path, or a first span's file and a type that is not about rules, so an unknown type keeps its message out.
  • Raw output. If Semgrep writes something other than its JSON report (a cut or broken document), the answer says how long it was and does not show it.
  • config auto is refused: it needs metrics on and sends the project's languages to semgrep.dev. URLs and anything other than p/NAME, r/RULE-ID or an absolute path are refused too.
  • Flags from the model. Every value rides its flag as one argument (--include=VALUE), a value that starts with a dash or holds a control character is refused, and the targets come after --. Relative or missing paths are refused before anything runs.
  • A login on this machine. Every run gets a folder of its own (mode 0700, removed when the run ends) holding the SARIF copy and, unless use_login is on, a Semgrep settings file of its own, with SEMGREP_APP_TOKEN taken out of the run's environment. A semgrep login then changes nothing a tool does: no Pro rules or Pro engine, registry rules fetched anonymously. Every run also sets SEMGREP_SEND_METRICS=off and drops an inherited SEMGREP_RULES, SEMGREP_BASELINE_COMMIT and SEMGREP_FORCE_COLOR, which would add rules or hide findings behind the call's back. A SEMGREP_APP_TOKEN in the environment is scrubbed from every answer either way, in one pass and before any part of the answer is cut to max_output_chars, so a token the cut would split leaves no prefix behind.
  • Missing CLI and timeouts are words: exit 127 with how to install Semgrep, or the run killed past timeout_seconds with its whole process tree.

Every scan runs with --json --metrics=off --disable-version-check --quiet. A registry ruleset is downloaded from semgrep.dev on each scan; the scanned code never leaves the machine.

Settings

The semgrep section of ~/.nodecode/config.jsonc (see config.example.jsonc):

Setting Default Meaning
command ["semgrep"] the argv prefix that runs Semgrep
default_config ["p/default"] the rules a scan uses when its call names none
severity "low" the floor a scan reports when its call names none
timeout_seconds 900 one run's ceiling; its process tree is killed past it
max_output_chars 20000 how much one answer carries
max_findings 200 finding lines one answer lists, and a call's ceiling
max_target_bytes 1000000 files larger than this are skipped, and a call's ceiling; 0 sets no limit
jobs 0 rules or files at once, and a call's ceiling; 0 leaves Semgrep's own
rule_timeout 5 seconds one rule may spend on one file, and a call's ceiling; 0 sets no limit
use_login false let a semgrep login or SEMGREP_APP_TOKEN apply to scans

"enabled": false vetoes the section and installs nothing.

Tests

bash .github/test-cell.sh tools/nodecode-semgrep

The tests run offline against a stubbed CLI: the command lines the tools build, what is refused before anything runs, the run's environment, and the report, above all that a secret planted in the matched lines, the filled message, the fix, a metavariable and a file error reaches no answer.

License

MIT. Semgrep is a trademark of Semgrep, Inc.; this cell is not affiliated with it.

Add nc://semgrep

  1. 1

    Install Nodecode

    Once per machine.

    $ curl -fsSL https://nodecode.ai/install.sh | sh
  2. 2

    Add the cell

    Pinned at c504079.

    $ nodecode add nc://semgrep

    Or tell the agent add the semgrep cell.