Documentation Index
Fetch the complete documentation index at: https://docs.semgrep.dev/llms.txt
Use this file to discover all available pages before exploring further.
ruleid: <rule-id>for protecting against false negativesok: <rule-id>for protecting against false positivestodoruleid: <rule-id>for future “positive” rule improvementstodook: <rule-id>for future “negative” rule improvements
- The
--testflag tells Semgrep to run tests in the specified directory. - Annotations are specified as a comment immediately preceding the offending line.
- Semgrep looks for tests based on the rule filename and the languages
specified in the rule. In other words,
path/to/rule.yamlsearches forpath/to/rule.py,path/to/rule.js, and similar, based on the languages specified in the rule.
INFOThe
.test.yaml file extension can also be used for test files. This is necessary when testing YAML language rules.Test rules with Rule-defined fix
Semgrep’s testing mechanism also provides a way to test the behavior of anyfix values defined in the rules.
To define a test for Rule-defined fix behavior:
Create a new Rule-defined fix test file with the
.fixed suffix before the file type extension. For example, name the Rule-defined fix test file of a rule with test code in path/to/rule.py as path/to/rule.fixed.py.Within the Rule-defined fix test file, enter the expected result of applied Rule-defined fix rule to the test code.
semgrep --test, Semgrep applies the Rule-defined fix rule to the original test code (path/to/rule.py), then verifies whether this matches the expected outcome defined in the Rule-defined fix test file (path/to/rule.fixed.py). If there is a mismatch, the line diffs are printed.
INFOHint: Creating a Rule-defined fix test for a rule with Rule-defined fix can take less than a minute with the following flow of commands:These commands apply the Rule-defined fix to the test code. After Semgrep delivers a fix, inspect whether the outcome of this fix is as expected (for example, using
vimdiff rule.py rule.fixed.py).Example
Consider the following rule:rules/detect-eval.yaml, you can create rules/detect-eval.py:
# todoruleid or # todook.
Store rules and test targets in different directories
Creating different directories for rules and tests helps you manage a growing library of custom rules. To store rules and test targets in different directories, use the--config option.
For example, in the directory with the following structure:
rules/detect-eval.fixed.py to represent the expected outcome of applying the fix to the test code:
secure_eval with safe_eval, you can see that lines 5 and 15 do not render as expected.
Validating rules
You can runsemgrep --validate --config [filename] to verify the rule’s configuration. This command runs a combination of Semgrep rules and OCaml checks against your rules to search for issues such as duplicate patterns and missing fields. All rules submitted to the Semgrep Registry are validated.
The semgrep rules are pulled from p/semgrep-rule-lints.
This feature is still experimental and under active development. Your feedback is welcomed!
Enable Rule-defined fix in Semgrep Code
To enable Rule-defined fix for all projects in your Semgrep organization:In Semgrep AppSec Platform, go to Settings > General > Code.