Enable and customize Semgrep Assistant
This article walks you through enabling Semgrep Assistant for your deployment and using its features.
- You have completed a Semgrep core deployment.
- You have set rules to Comment or Block mode in your Policies page.
- GitHub
- GitLab
Semgrep Assistant extends normal Semgrep capabilities by providing contextually aware AI-generated suggestions. In order to build that context, it requires GitHub permissions in addition to the standard permissions required for Semgrep.
Semgrep Assistant requires read access to your code in GitHub. This is done through a private Semgrep GitHub app that you install during Assistant setup. This private Semgrep GitHub app:
- Is fully under your control so you can revoke access or specific permissions at any time by visiting Settings > Applications in GitHub.
- Only accesses source code repositories on a file-by-file basis; it does not need or request org-level access to your codebase.
- Can be configured to limit its scope to specific repositories. You do not need to give read access to all repositories in your GitHub organization.
Enable Assistant
- Sign in to Semgrep AppSec Platform.
- Click Settings.
- In the Assistant section, click the Allow code snippets in AI prompts toggle. This launches the Set up Semgrep Assistant prompt.
- Select a source code manager (SCM) by clicking github.com.
- Semgrep provides you with information on why Assistant requires access to your source code. Click Accept & Enable Assistant to proceed.
- You are redirected to the page where you can add a GitHub Private App that grants Semgrep read access to your code.
- Enter your GitHub information. Select whether you're installing the app on an organization or Personal Account, and provide its name.
- Click Review permissions to see the permissions requested by Semgrep.
- Click Register GitHub App to proceed.
- When prompted, click Continue to allow redirection to GitHub to finalize app creation. Follow the instructions to finish creating and installing a private
semgrep-app
.
- You are redirected to Semgrep AppSec Platform's Source Code Managers page. Navigate back to the Deployment page. Under the Assistant section, verify that all of the features are enabled:
- Allow code snippets in AI prompts: Required for Semgrep to auto-triage findings, provide AI remediation guidance, and tag findings with code context.
- Auto-triage for Code: Enable notifications whenever Assistant suggests that a finding may be safe to ignore. You can include notifications in your PR and MR comments, or you can receive them through Slack notifications.
- Autofix suggestions for Code: Enable Assistant-generated autofix suggestions in comments from Assistant. You can also set the minimum confidence level for Assistant-written fixes if the Semgrep rule doesn't include a human-written autofix.
Semgrep Assistant extends normal Semgrep capabilities by providing contextually aware AI-generated suggestions. In order to build that context, it requires GitLab permissions in addition to the standard permissions required for Semgrep.
Semgrep Assistant requires the API scope to run in both GitLab SaaS and GitLab self-managed instances. This can be specified at either the project access token level or personal access token level.
- You can revoke project access tokens or personal access tokens at any time.
- Semgrep Assistant only accesses source code repositories (projects) on a file-by-file basis; it does not need or request org-level access to your codebase.
- The token can be configured to limit its scope to specific projects or individuals. You do not need to give read access to all projects in your GitLab organization.
Enable Assistant
- Sign in to Semgrep AppSec Platform using your GitLab account.
- Click Settings.
- In the Assistant section, click the Allow code snippets in AI prompts toggle. This launches the Set up Semgrep Assistant prompt.
- Follow the on-screen instructions to complete the setup process.
- Navigate back to the Deployment page. Under the Assistant section, verify that all of the features are enabled:
- Allow code snippets in AI prompts: Required for Semgrep to auto-triage findings, provide AI remediation guidance, and tag findings with code context.
- Autofix suggestions for Code: Enable autofix suggestions in comments from Assistant. You can also set the minimum confidence level for Assistant-written fixes if the Semgrep rule doesn't include a human-written autofix.
- Auto-triage for Code: Enable notifications whenever Assistant suggests that a finding may be safe to ignore. You can include notifications in your PR and MR comments, or you can receive them through Slack notifications.
Enable autofix suggestions
Assistant autofix allows you to receive AI-generated code snippets to remediate true positives. Perform the following to enable it:
- Sign in to Semgrep AppSec Platform, and navigate to Settings > Deployment.
- In the Assistant section, click the Autofix suggestions for Code* if it is not yet enabled.
- Optional: Select a confidence level in the drop-down box. This value determines the level of quality at which the autofix code appears as a suggestion. A lower confidence level means that Semgrep Assistant displays the autofix suggestion even when the code quality may be incorrect.
tip
Semgrep recommends setting a low confidence level since even incorrect suggestions may be useful starting points for triage and remediation.
Enable auto-triage
If auto-triage, which allows you to get notifications whenever Assistant indicates a finding may be safe to ignore, isn't enabled for your deployment, you can do so as follows:
- Sign in to Semgrep AppSec Platform, and navigate to Settings > Deployment.
- In the Assistant section, click the Auto-triage for Code if it is not yet enabled.
- Select whether you want alerts included in your PR/MR comments and Slack notifications.
Figure. MR comment from Semgrep Assistant in GitLab.
Missing PR and comments
Semgrep Assistant messages only appear in your PR comments for rules that are set to Comment or Block mode on the Rule Management page. Ensure that:
- You have set rules to Comment or Block mode.
- You have selected PR/MR comments in Semgrep AppSec Platform > Settings > Deployment in the Code section.
Add Memories (beta)
Assistant Memories allows AppSec teams and developers to tailor Assistant's remediation guidance to their organization's standards and defaults on a per-project, per-rule basis. Whenever Assistant gives a suggested fix, you can provide feedback by adding custom instructions.
Memories are enabled by default for all Assistant users.
To add a memory:
- Identify the specific instance of Assistant's suggested fix that you want to modify. These can be found in the finding details page or in the PR or MR comment.
- Click Customize fix to open an input box, and enter your preferred remediation approaches and secure defaults for the project. The suggestion you provide can be as general as "Use AWS Secrets Manager to manage secrets." Figure. Assistant’s suggested fix for a hardcoded secret in the user’s code.
- Click Save and regenerate. Figure. User-provided custom instruction that Assistant uses to improve future guidance.
- Assistant regenerates the suggested fix to reflect the instructions you provided. Figure. Regenerated Assistant fix using the user-provided instructions.
While Assistant Memories is in public beta, memories are scoped to remediation guidance on a per-project and per-rule basis. A saved memory only affects future guidance for findings triggered by the same rule in the same project.
Remove Memories
- Sign in to Semgrep AppSec Platform and navigate to Settings > Deployment.
- In the Assistant section, click the icon next to Customize with memories.
- Click the icon to remove the memory.
Write custom rules (beta)
Semgrep Assistant can help you write custom rules to find issues specific to your codebase.
To do so:
- Sign in to Semgrep AppSec Platform.
- Navigate to Rules > Editor.
- Click the plus button, and under Generate with AI, click ...with Semgrep Assistant.
- In the Generate rule with Semgrep Assistant pop-up window:
- Select the language of your codebase.
- Provide a prompt describing what you want the rule to do in English.
- Optional: provide an example of bad code.
- Optional: provide an example of good code.
- Click Generate to proceed. You'll be redirected to a screen where you can view and copy your rule and test it against the sample bad code snippet you provided.
Use your own OpenAI API key
If you want complete control over how OpenAI handles your data, you can use your OpenAI API key instead of Semgrep's. To provide your OpenAI API key:
- Sign in to Semgrep AppSec Platform and navigate to Settings > Deployment.
- In the Assistant section, click the icon next to AI provider.
- Select Your OpenAI API key, and provide your API key.
Click Save to proceed.
By switching from Semgrep's key to your key, note that you lose access to the following:
- Semgrep’s fine-tuned models that can increase the quality of results.
- Semgrep's Zero Data Retention agreement that prevents OpenAI from saving input or output data.
- Semgrep paying for the cost of your AI usage.
Use your AI provider
If you would like access to the following AI providers for use with your Semgrep organization, click the icon next to the AI provider of your choice to request access:
- Azure OpenAI
- AWS Bedrock
- Google Gemini
Azure OpenAI
To use Azure OpenAI with Semgrep Assistant, you must retrieve the endpoint URL and API key for your model from Azure, then provide it to Semgrep.
- To retrieve the endpoint URL and API key from Azure:
- Log in to Azure OpenAI Studio.
- Navigate to Deployments, and select the deployment you want to use.
- In Endpoint, find and copy both the Target URI and the API key. You will provide both values to Semgrep.
- To configure Semgrep to use Azure OpenAI:
- Sign in to Semgrep AppSec Platform and navigate to Settings > Deployment.
- In the Assistant section, click the icon next to AI provider.
- Select Azure OpenAI.
- Paste the Target URI you copied from Azure into Your Azure OpenAI Endpoint.
- Paste the API key you copied from Azure into Your Azure OpenAI API key.
- Click Save to proceed.
Your Azure OpenAI model is now configured for use in Semgrep.
You can switch to a different Azure OpenAI model any time by repeating these configuration steps using the Target URI and API key for the new model.
Troubleshoot issues with Azure OpenAI
If you see Error 429 - Max Tokens Exceeded:
- Go to Azure OpenAI Studio > Deployments and select your active deployment.
- Under Details, click Edit and increase the Tokens per Minute Rate Limit to the maximum value.
- If the error persists, contact Microsoft Azure support to request a quota upgrade.
If you can't save the endpoint and API key when configuring Semgrep, Semgrep cannot establish a connection with Azure OpenAI.
- Ensure that the endpoint URL is correctly formatted. It should look something like
https://<YOUR_DEPLOYMENT_NAME>.openai.azure.com/openai/deployments/mymodel/chat/completions?api-version=2023-05-06-preview
. - Verify that your API key is correct.
Not finding what you need in this doc? Ask questions in our Community Slack group, or see Support for other ways to get help.