DefectDojo is a well-known vulnerability management tool. It allows you to gather security issues from other tools, including Semgrep. By integrating Semgrep findings into DefectDojo, security teams can more easily monitor their overall security posture.
Follow these steps to prepare DefectDojo and generate Semgrep findings in the proper format:
1
In DefectDojo:i. Create your product.
ii. In that DefectDojo product, create an engagement, called semgrep. This is a CI/CD engagement type and the name designates the CI/CD tool used.
2
Run semgrep as semgrep ... --json > report.json to generate a JSON report.
To run API DefectDojo operations such as GET, POST, and DELETE, an API token is necessary. To get it, follow the API guide.Once you have a token, store it as an environment variable named DEFECT_DOJO_API_TOKEN:
The DefectDojo API uses the /api/v2/import-scan/ endpoint for the first import and the /api/v2/reimport-scan endpoint for following imports.These endpoints take the following parameters:
file: The Semgrep scan findings report or export in JSON format.
scan_type: A descriptive name for the scan type. In this example, the scan type is “Semgrep JSON Report`”.
product_name: The name of the product in DefectDojo to send the Semgrep findings report to.
engagement_name: The name of the engagement you created the preceding “Integration” section. In this example, semgrep.
INFOThe DefectDojo API allows identifying the parameters either by name or by ID. This example follows the By name approach.
Here is an example snippet of a Python function using this endpoint:
The full version of this Python script can be found here. Feel free to use this in your own environment after reviewing the script to make sure it works for you.
Integrating Semgrep and DefectDojo in a CI pipeline
To prevent tampering with findings, it is crucial to import scan results to DefectDojo in the same pipeline or CI job as the scan itself.The following is an example of a GitLab job importing Semgrep findings to DefectDojo:
TIPAs a good security practice, this pipeline includes checksum validation for the import script, to ensure that the script has not been tampered with.
There are some environment variables defined in the gitlab-ci.yml file, such as:
DEFECTDOJO_URL
PRODUCT
IMPORT_SEMGREP_TO_DEFECTDOJO_SHA_CHECKSUM
They must be defined in the GitLab pipeline. Settings->CI/CD->Variables:
The content for this last variable was generated with the following command:
shasum -a 256 -U import_semgrep_to_defect_dojo.py
This command generates a unique checksum, taking as input the content of the script, and it will be used to verify that the script has not changed.In the pipeline, the integrity of the script is verified with the following commands:
echo $IMPORT_SEMGREP_TO_DEFECTDOJO_SHA_CHECKSUM > sha-import-dd.tmpshasum -a 256 -U -c sha-import-dd.tmp
If the script has not changed since the checksum was generated, the pipeline will continue normal execution. Otherwise it will stop and return an error.Example DefectDojo screenshot, after a pipeline execution: