Set up GitHub pull request comments
- You have gained the necessary resource access and permissions required for deployment.
- You have created a Semgrep account and organization.
- You have connected your source code manager.
- Optionally, you have set up SSO.
- You have successfully added a Semgrep job to your CI workflow with diff-aware scanning.
Semgrep can create pull request (PR) comments in your GitHub repository. These comments provide a description of the issue detected by Semgrep and may offer possible solutions. These comments are a means for security teams, or any team responsible for creating standards to help their fellow developers write safe and standards-compliant code.
Automated comments on GitHub pull requests are displayed as follows:
Figure. An inline GitHub pull request comment.
Conditions for PR comment creation
PR comments appear for the following types of scans under these conditions:
Type of scan | Product name | Trigger condition | How to set up |
---|---|---|---|
Static application security testing (SAST) | Semgrep Code | A comment appears when a finding is generated by a rule in Comment or Block mode. This means you can fully customize what comments your developers receive. | Complete the steps in the following sections: |
Software composition analysis (SCA) | Semgrep Supply Chain (SSC) | A comment appears based on the conditions you explicitly set in a Supply Chain policy or when Semgrep detects a license violation. | To receive Supply Chain comments, complete the steps in Confirm account connection and access and set up a policy. To receive license violation comments, enable dependency search. |
Secrets | Semgrep Secrets | A comment appears when a finding is generated by a rule in Comment or Block mode. A comment also appears for invalid findings and validation errors if these conditions are set to Comment or Block mode. | Complete the steps in the following sections: |
Comments from Supply Chain scans include the following information:
- Risk
- A description of the vulnerability, including the types of attack it is vulnerable to.
- Fix
- Indicates what versions to upgrade to, if any, that resolves or eliminates the vulnerability.
- Reference
- A link to additional information about the vulnerability from its source, such as the GitHub Advisory Database and the National Vulnerability Database (NVD), if available.
Steps to set up PR comments
Prerequisites
In addition to finishing the previous steps in your deployment journey, it is recommended to have completed a full scan on your default branch for the repository in which you want to receive comments.
Confirm your Semgrep account's connection
Confirm that you have the correct connection and access:
- In your Semgrep AppSec Platform account, click Settings > Source code managers.
- Check that an entry for your GitHub org exists and is correct.
Confirm repository access
Ensure that Semgrep's GitHub app (semgrep-app
) has sufficient permissions to post PR comments:
- Navigate to your
semgrep-app
settings:- For personal accounts, navigate to the following URL
https://github.com/settings/installations
. - For organization accounts, navigate to the following URL, substituting YOUR_ORG_NAME with the name of your account:
https://github.com/organizations/YOUR_ORG_NAME/settings/installations
.
- For personal accounts, navigate to the following URL
- On the
semgrep-app
row, click Configure. - Check that you have granted the following permission:
Read and write access to actions, pull requests, secrets, security events, and workflows
. - Under Repository access, check that you have included the repositories that you added to Semgrep AppSec Platform. Review the following examples:
Figure. Permissions for all repositories.
Figure. Permissions for select repositories. Ensure the repositories you have onboarded to Semgrep AppSec Platform are selected.
For GitHub Actions users, no further steps need to be undertaken. Continue setting up PR comments by configuring comments for Semgrep Code.
Required environment variables
For CI providers aside from GitHub Actions, additional environment variables must be set:-
SEMGREP_PR_ID
is set to the PR number of the pull request on GitHub Actions. SEMGREP_REPO_NAME
is set to the repository name.SEMGREP_REPO_URL
is set to the repository URL where your project is viewable online.
These values do not have to be fixed or hardcoded. They can be variables passed to the job. For more information, see Sample CI configurations.
Configure comments for Semgrep Code
In addition to setting up the connection between Semgrep and GitHub, you must assign rules to Comment or Block mode. This customization enables you to:
- Manage the amount of PR comments your developers receive.
- Ensure that only rules that meet your criteria, such as high severity or high confidence rules, produce comments visible to developers, reducing noise.
Set rules to Comment or Block mode
The following instructions let you customize what findings or security issues your developers see as comments in their PRs:
- In your Semgrep AppSec Platform account, click Rules > Policies to enter the Policies page. Under Modes , you can quickly see if you have existing rules in either Comment or Block mode.
- Optional: To configure Semgrep Secrets rules, click the Secrets tab.
- Optional: Use the filters to quickly find rules to set to Comment or Block.
- Click the checkbox of the rules you want to set. You can use Ctrl + Click to select rules in bulk.
- Click Change modes.
- Click either Block or Comment.
You have successfully configured PR comments for Semgrep Code.
Rules in Block mode fail the CI job that runs on the PR. Depending on your workflow, this may prevent your PR from merging.
If you are using GitHub Actions to run Semgrep, no extra changes are needed to receive PR comments.
Configure comments for Semgrep Secrets
In addition to setting up the connection between Semgrep and GitHub, you must assign rules to Comment or Block mode. This customization enables you to:
- Manage the amount of PR comments your developers receive.
- Ensure that only rules that meet your criteria, such as high severity or high confidence rules, and result in findings involving valid secrets produce comments visible to developers, reducing noise.
Set rules to Comment or Block mode
The following instructions let you customize what findings or security issues your developers see as comments in their PRs:
- In Semgrep AppSec Platform, go to Rules > Policies > Secrets.
- Under Modes , you can see if you have existing rules in either Comment or Block mode. You can also use the filters to find rules you want to set to Comment or Block.
- Click the checkbox of the rules you want to set. You can use Ctrl + Click to select rules in bulk.
- Click Change modes.
- Click either Block or Comment.
You have successfully configured PR comments for Semgrep Secrets.
Validation state policies
Validation state policies allow you to define how Semgrep handles the following issues:
- Invalid findings: the secret has been revoked, was never functional, or used for a custom or private endpoint that Semgrep can't communicate with. For example, a Semgrep rule that tests GitHub credentials may return an invalid finding if Semgrep can't communicate with an on-premise deployment.
- Validation errors: Semgrep was unable to reach the secrets provider to test the validity of the credential, or Semgrep received an unexpected response from the API
To edit the policy for invalid secrets and errors:
- In Semgrep AppSec Platform, go to Rules > Policies > Secrets.
- Click Validation State Policies.
- Choose the mode, either Comment or Block, that you want Semgrep to set for Invalid findings.
- Choose the mode, either Comment or Block, that you want Semgrep to set for Validation errors.
Rules in Block mode fail the CI job that runs on the PR. Depending on your workflow, this may prevent your PR from merging.
Configure comments for Semgrep Supply Chain
To configure comments for Supply Chain, you must define a Supply Chain policy. This policy lets you set the specific conditions, such as transitivity and reachability, that trigger a comment. These conditions are unique to Supply Chain findings.
See the Policies documentation for more information.
Receive comments in your VPN or on-premise SCM
If you are behind a firewall, are using a virtual private network (VPN), or have network restrictions regarding access, you may need to add the following IP addresses to the ingress allowlist and egress allowlist:
# Ingress IP addresses (from Semgrep to your infrastructure)
# and egress IP addresses (from your infrastructure to Semgrep)
35.166.231.235
52.35.248.246
52.34.137.110
44.225.64.41
Additional egress IP addresses
You must also add CloudFront IP addresses to your egress allowlist. Refer to Locations and IP address ranges of CloudFront edge servers for a list of IP addresses.
Test your configuration
Test that you are able to receive findings by manually triggering a scan through your CI provider.
Receiving PR or MR comments may require additional steps depending on the custom configuration of your VPN or SCM (for example, if you use a static IP without a hostname). Reach out to Semgrep support through the Semgrep Community Slack or send an email to support@semgrep.com for any concerns.
You've set up PR comments! Enable optional features provided in the following sections, or see Next steps.
Optional features
Enable autofix in GitHub repositories
Autofix is a Semgrep feature in which rules contain suggested fixes to resolve findings.
To enable autofix for all projects in your Semgrep AppSec Platform organization, follow these steps:
- In Semgrep AppSec Platform, click Settings on the left sidebar.
- Click Autofix toggle.
Dataflow traces in PR comments
Figure. An inline GitHub pull request comment with dataflow traces.
With dataflow traces, Semgrep Code can provide you with a visualization of the path of tainted, or untrusted, data in specific findings. This path can help you track the sources and sinks of the tainted data as they propagate through the body of a function or a method. For general information about taint analysis, see Taint tracking.
When running Semgrep Code from the command line, you can pass in the flag
--dataflow-traces
to use this feature.
You can view dataflow traces in:
- Semgrep AppSec Platform by going to Semgrep Code's Findings page. For more details, see Path of tainted data in Semgrep Code.
- The PR or MR comments created by Semgrep Code running in your CI/CD system. To enable
this feature, see:
- GitHub users: Dataflow traces in PR comments
- GitLab users: Dataflow traces in MR comments
Get cross-file findings
To get cross-file (interfile) findings in your organization, follow the steps in Perform cross-file analysis.
View the path of tainted data in PR comments
To enable dataflow traces feature in your CI pipeline, fulfill the following prerequisites:
- Set up Semgrep to post GitHub PR comments, as described on this page.
- To obtain meaningful results of dataflow traces in PR comments, use cross-file analysis while scanning your repositories. To enable cross-file analysis, see Perform cross-file analysis.
- Not all Semgrep rules or rulesets make use of taint tracking. Ensure that you have a ruleset that does, such as the default ruleset, added in your Policies. To add this ruleset, navigate to https://semgrep.dev/p/default, and then click Add to Policies.
- You can add additional rules that use taint tracking from Semgrep Registry.
Prevent developers from merging a PR with a reachable vulnerability
Both GitHub and GitLab provide features to prevent or block a PR or MR from merging based on certain conditions. Refer to the links below to prevent PRs or MRs from merging when a reachable finding is detected:
GitHub | Require conversation resolution before merging |
GitLab | Prevent merge unless all threads are resolved |
Next steps
You've finished setting up a core deployment of Semgrep 🎉.
- Explore recommended tasks after deployment in Beyond core deployment.
Additional references
Not finding what you need in this doc? Ask questions in our Community Slack group, or see Support for other ways to get help.