
Curated Semgrep rule repository for GitLab SAST, providing static analysis patterns to detect security vulnerabilities across multiple programming languages with CI/CD integration.
This is the central Semgrep rule repository that hosts the Semgrep rules for the GitLab semgrep analyzer.
The repository is structured as follows:
.
├── mappings
│ ├── find_sec_bugs.yml
│ ├── eslint.yml
│ └── ...
├── rules
│ ├── lpgl
│ │ ├── java
│ │ │ ├── webview
│ │ │ │ ├── rule-ignore_ssl_certificate_error.yml
│ │ │ │ ├── rule-ignore_ssl_certificate_error.java
│ │ │ │ └── ...
│ │ │ └── ...
│ │ ├── python
│ │ │ └── ...
│ │ └── ...
│ ├── lpgl-cc
│ │ ├── java
│ │ │ └── ...
│ │ └── ...
│ └── ...
├── c
│ ├── buffer
│ │ ├── rule-strcpy.yml
│ │ ├── test-strcpy.c
│ │ ├── rule-memcpy.yml
│ │ └── test-memcpy.c
│ └── ...
└── javascript
│ └── ...
└── ...
The structure above follows the pattern:
rules/<license>/<language>/<ruleclass>/rule-<rulename>\.(yml|<ext>)
where:
<license> is the license of all the rules underneath<language> the target programming language<ruleclass> a descriptive name for the class of rules underneath<rulename> a descriptive name for the actual rule<ext> the usual file extension for <language>Older rules follow the <language>/<ruleclass>/rule-... pattern, and the
newer one above should be preferred whenever possible.
The mappings directory includes the rule-pack configuration.
The Makefile defines a few targets that are helpful when working on rules:
$ make help
TARGETS:
test test all rules with Semgrep
watch watch for file changes and auto-run affected tests
help prints this message
Rules contained in this repository have to adhere to the following format:
" for strings, otherwise the YAML literal block |---The mappings directory in this repository contains YAML configuration files that map native analyzer ids (e.g. Bandit, Brakeman, etc) to the corresponding Semgrep rules.
The intention of mapping files is to, first and foremost, separate analyzer-specific information from the actual rules and, secondly, to provide a non-intrusive way of generating rule-packs or rule-sets (across language or analyzer boundaries) for different purposes.
Mapping files are located under the mappings/ directory where the filename refers
to the rulepack and/or analyser that is represented by the set of rules used in the
respective file. If you want the rules to be included in the GitLab standard ruleset
and the rule does not fit into one of the rule-packs (or analysers) that are already
available in the mappings/ directory, you can add your mappings to the
mappings/gitlab_<license>_<language>.yml file where <license> is a suitable
license dictated by the source from which the rules is sourced and <language>
is a placeholder of the language to which the rule refers to.
If you want to integrate a new rule that was developed from scratch, you can
add a corresponding mapping to mappings/gitlab_ee_<language>.yml file.
When determining what license should apply to a particular rule you are mapping,
refer to this internal guidance.
Mappings are also used to automatically assemble rule-packs. The snippet below
illustrates an example with mapping files for the bandit analyzer. The native_id
section includes some information about the
native analyzer id, i.e., meta-information that the original analyser (in this case bandit)
attaches to the finding it produces. The actual rule mappings are defined in the mappings
section. Each mapping maps a native analyser rule id (in the example below B301) to a set of semgrep files
in this repository that resemble, or are ideally de facto equivalent to, that particular native rule.
bandit:
native_id:
type: "bandit_test_id"
name: "Bandit Test ID: $ID"
value: "$ID"
mappings:
- id: "B301"
rules:
- path: "python/deserialization/rule-pickle"
primary_id: "bandit.B301-1"
id: "bandit.B301-1"
- path: "python/deserialization/rule-cpickle"
primary_id: "bandit.B301-2"
id: "bandit.B301-2"
- path: "python/deserialization/rule-dill"
primary_id: "bandit.B301-3"
id: "bandit.B301-3"
- path: "python/deserialization/rule-shelve"
primary_id: "bandit.B301-4"
id: "bandit.B301-4"
# ...
The anatomy of a mapping file is explained in more detail below.
gl-sast-report.json) for deduplication purposes.gl-sast-report.json produced by the GitLab Semgrep analyzer and made available in the Vulnerabiliy Report.B301 which refers to one of the rules of the python analyzer bandit). The rules array points to files in the repository to which this rule refers to. In other words, the logic off B301 is implemented in the four files listed in the snippet above.We use two different types of identifiers id and primary_id to support rule splitting: multiple semgrep rules (id) can be mapped to a single native analyser’s rule (primary_id ).
The rules and test-cases in this repository are partially sourced from the sources listed below:
The details are listed in the headers of all the rule end test-files including the licensing information and proper attribution.
If you know about a pattern that isn't present in this repo or refinements that could be applied to the rules in this repository, you can contribute by opening an issue, or even submit an improvement to the rule files/test cases in this repository.
We apply the following semantic versioning scheme to this repository:
New SAST rule releases must be incorporated into the semgrep analyzer to take effect. To request a new semgrep release, create a release issue using the instructions in the SAST release issue template.
We would like to thank the following authors very much for their valuable contributions.
| Author | MRs/Issues |
|---|---|
| @masakura | !99, !107 |
| @niklas.volcz | !183 |
| @pieter39 | !668 |