fix(scan): a Semgrep rule pack with its samples is test data #905

Merged
sharang merged 1 commits from fix/rule-samples-test-data into main 2026-11-05 19:11:58 +00:00
Owner

Problem

Our own repository ships Semgrep rules in compliance-agent/rules:

  • Rule samples: next to each rule X.yaml sits the sample X.py / X.cs / X.m / X.js it is tested on, written to hold the flaws the rule must find.
  • Vendored packs: packs under vendor/ bring their own manifests, such as a pom.xml naming old Struts and Spring versions.

Scanned as product code, the samples become code findings, and the vendored manifests become SBOM components with CVEs. Vendor code is left out of code findings but still goes into the SBOM. Every repository that keeps Semgrep rules with tests has the same problem, so this is a general rule, not a path special-case.

Change

  • Detection: test_data::test_data_dirs also lists a directory that holds a Semgrep rule file next to a sample of the same name, the convention semgrep --test uses, with everything below it. It never lists the tree itself.
  • What counts as a rule file: YAML of at most 1 MiB with a rules list naming id, languages and message, which Semgrep requires of every rule. A configuration file next to the code it configures (settings.yaml + settings.py) is not one.
  • Cost: each directory with a candidate YAML is read once, and a YAML file is parsed only when a same-named sample exists.
  • What follows from test data: the pack is pruned from the working copy before any stage runs, so it yields no code findings, no SBOM components and no CVEs. Findings an earlier scan stored there close as test data (#904), and their issues follow.
  • Docs: docs/guide/scanning.md names the rule.

In this repository the rule matches only compliance-agent/rules, checked by simulating it over the tree.

Tests

Unit tests in test_data.rs:

  • Listed: a pack with samples and a nested vendored pack yields only the pack's directory.
  • Not listed: a config next to code, a rule without a sample, and a pack at the tree's root.
  • Rule-file check: large and broken YAML is refused.
  • Nesting: only the top-most directories are kept.

🤖 Generated with Claude Code

## Problem Our own repository ships Semgrep rules in `compliance-agent/rules`: - **Rule samples:** next to each rule `X.yaml` sits the sample `X.py` / `X.cs` / `X.m` / `X.js` it is tested on, written to hold the flaws the rule must find. - **Vendored packs:** packs under `vendor/` bring their own manifests, such as a `pom.xml` naming old Struts and Spring versions. Scanned as product code, the samples become code findings, and the vendored manifests become SBOM components with CVEs. Vendor code is left out of code findings but still goes into the SBOM. Every repository that keeps Semgrep rules with tests has the same problem, so this is a general rule, not a path special-case. ## Change - **Detection:** `test_data::test_data_dirs` also lists a directory that holds a Semgrep rule file next to a sample of the same name, the convention `semgrep --test` uses, with everything below it. It never lists the tree itself. - **What counts as a rule file:** YAML of at most 1 MiB with a `rules` list naming `id`, `languages` and `message`, which Semgrep requires of every rule. A configuration file next to the code it configures (`settings.yaml` + `settings.py`) is not one. - **Cost:** each directory with a candidate YAML is read once, and a YAML file is parsed only when a same-named sample exists. - **What follows from test data:** the pack is pruned from the working copy before any stage runs, so it yields no code findings, no SBOM components and no CVEs. Findings an earlier scan stored there close as *test data* (#904), and their issues follow. - **Docs:** `docs/guide/scanning.md` names the rule. In this repository the rule matches only `compliance-agent/rules`, checked by simulating it over the tree. ## Tests Unit tests in `test_data.rs`: - **Listed:** a pack with samples and a nested vendored pack yields only the pack's directory. - **Not listed:** a config next to code, a rule without a sample, and a pack at the tree's root. - **Rule-file check:** large and broken YAML is refused. - **Nesting:** only the top-most directories are kept. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
sharang added 1 commit 2026-11-05 18:00:19 +00:00
fix(scan): a Semgrep rule pack with its samples is test data
CI / Check (push) Skipped
CI / Detect Changes (pull_request) Skipped
CI / Deploy Agent (pull_request) Skipped
CI / Deploy Dashboard (pull_request) Skipped
CI / Deploy Docs (pull_request) Skipped
CI / Deploy MCP (pull_request) Skipped
CI / Agent integration (pull_request) Successful in 4m25s
CI / Check (pull_request) Successful in 17m16s
e88fa8524d
A repository that ships Semgrep rules keeps, next to each rule X.yaml, the
sample X.py or X.cs it is tested on, written to hold the flaws the rule
must find. Vendored packs bring their own manifests (a pom.xml naming old
Struts and Spring versions). Scanned as product code, the samples become
code findings and the manifests SBOM components with CVEs.

test_data_dirs now also lists a directory with a Semgrep rule file and a
sample of the same name (Semgrep's test convention), with everything below
it, never the tree itself. A YAML file counts as a rule file when it has a
rules list naming id, languages and message, and is at most 1 MiB. As test
data, the pack leaves the working copy before any stage, and findings an
earlier scan stored there close as test data (#904).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sharang merged commit b2b9fd2a02 into main 2026-11-05 19:11:58 +00:00
Sign in to join this conversation.