Adding a new scan rule
When the user wants the scanner to detect a new vulnerability pattern, follow this workflow. Loaded on-demand, not into every session — see scanner/src/sast/CLAUDE.md for the same content baked into the SAST tree.
Decision: where does this rule live?
| Shape of the rule | Module pattern |
|-------------------|----------------|
| Language-specific (e.g. Python sink, Kotlin force-unwrap) | Add to or create scanner/src/sast/<language>.js |
| Framework-specific hardening (e.g. Express auth, FastAPI defaults) | scanner/src/sast/<framework>-hardening.js |
| Cross-cutting vuln class (CSRF, prototype pollution, mass assignment) | New top-level scanner/src/sast/<topic>.js |
| Posture annotator (mutates existing findings) | scanner/src/posture/ — see scanner/src/posture/CLAUDE.md |
If the rule is "X but with light cross-file context," still emit from sast/ and let the cross-file annotators in posture/cross-lang-*.js enrich it. Don't put cross-file logic into the SAST module.
Steps
- Write the detector. Export a
scan<Name>(fileContents, opts)function returningFinding[]. Required fields:id,severity,file,line,vuln,cwe,description,remediation. Setfamilyandparserwhen you know them;posture/finding-defaults.jsbackfills, but detector-set wins.
- Wire into the engine. Open
scanner/src/engine.js. Add the import to the existing block (alphabetical) and call it insiderunFullScannext to similar rules. Append the results tofinalFindings.
- Add the fixture pair. Create
scanner/test/fixtures/<rule-name>/vulnerable/andscanner/test/fixtures/<rule-name>/clean/. Each holds one small file demonstrating the vulnerable / fixed shape. The vulnerable file must produce a finding; the clean file must not.
- Add the test. Either extend an existing topical test (e.g.
test/python-sinks.test.js) or createtest/<rule-name>.test.jsmodelled on the smallest existing test. Wire the file into the matching scoped script inscanner/package.json(test:sast,test:dataflow, etc.).
- Verify. Use the scoped script that covers what you touched (
npm run test:smoke,npm run test:sast,npm run test:dataflow, …). Always includenpm run test:lifecycle— it catches a rule that ships without anengine.jswire-up. The fullnpm testis the CI gate but is usually overkill for a single new rule.
- Rebuild the bundle if anything outside
src/will consume it.npm run build. Thennpm run smoketo check the CLI path. If bundle smoke disagrees with unit-test smoke, you have anengine.jswiring miss — unit tests run againstsrc/directly, but the bundle re-binds at build time.
Common pitfalls
- Comments confuse regex detectors. Always go through
blankComments()fromscanner/src/sast/_comment-strip.jsbefore scanning a file body. Otherwise a vuln pattern inside a// example:block fires. - Snippet attribution. For multi-line sinks (
exec('ping ' + req.body.host, …)), the match offset and the readable sink line can diverge. Use the actual sink expression for the finding'ssnippet, notlines[regex.lastIndex]. - Severity floor. Don't emit
criticalwithout strong evidence.annotateExploitabilitywill lifthightocriticalwhen production context warrants. A flood of false criticals drowns real ones. - Family field. If you can't pick a
family, the rule probably covers too much. Split it. - Bench-shape isolation. If your rule needs answer-key signals to fire correctly (file naming, label markers), it goes under
scanner/src/sast/bench-shape/and stays gated byAGENTIC_SECURITY_BENCH_SHAPE=1. Never make a production rule depend on bench shape.
Verify the wiring
After step 5 the lifecycle test should pass with the new file. If it fails with "exported X has no external call site," you forgot step 2 — go back and add the import + call in engine.js.
If npm run smoke reports the new rule fires in clean/ (false positive on the clean fixture), tighten the regex/AST match. If it doesn't fire in vulnerable/, your match shape is wrong — look at the actual fixture content with the actual detector and trace where it diverges.
When to stop
You're done when:
- Both fixtures behave as expected
- The scoped test passes
npm run test:lifecyclepassesnpm run build && npm run smokeproduces the same finding count via the CLI as the unit test reported