DeepTeam is a framework to red team LLMs and AI agents.
feat(docs): add Code Scanning
tanayvaswani2c45f67d5ab2
diff --git a/docs/content/docs/code-scanning-github-bot.mdx b/docs/content/docs/code-scanning-github-bot.mdxnew file mode 100644index 00000000..06347ea2--- /dev/null+++ b/docs/content/docs/code-scanning-github-bot.mdx@@ -0,0 +1,65 @@+---+title: Code Scanning GitHub Bot+sidebar_label: GitHub Bot+---++`deepteam` can review every pull request and comment its findings as **`deepteam[bot]`**. The scan runs in your own CI using your `OPENAI_API_KEY`, and the comment is posted by the bot through GitHub Actions [OIDC](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect), so there are no bot tokens to store.++:::info+The workflow only needs `contents: read` and `id-token: write`, not `pull-requests: write`. The comment is posted server-side by the bot, and the OIDC token is bound to the exact repository and pull request the workflow ran for.+:::++## Quick Summary++There are **TWO** ways to set up the bot:++- [Hosted GitHub App](/docs/code-scanning-github-bot#hosted-github-app) - install the app and it configures everything for you.+- [Manual Setup](/docs/code-scanning-github-bot#manual-setup) - add the scan workflow to your repository yourself.++## Hosted GitHub App++The quickest path is the hosted DeepTeam GitHub App, which sets everything up for you:++1. Install the app at [github.com/apps/deepteam](https://github.com/apps/deepteam), selecting the repositories you want scanned.+2. Enter your email when redirected. The bot automatically opens a pull request that adds the scan workflow to each repository.+3. Merge that pull request and add an `OPENAI_API_KEY` [repository secret](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions).++From then on, every pull request is scanned and commented on automatically.++## Manual Setup++If you'd rather not use the hosted onboarding, you can commit the workflow yourself:++```yaml title=".github/workflows/deepteam-code-scan.yml"+name: DeepTeam Code Scan++on:+ pull_request:+ types: [opened, synchronize, ready_for_review]++permissions:+ contents: read+ id-token: write++jobs:+ scan:+ runs-on: ubuntu-latest+ steps:+ - uses: actions/checkout@v4+ with:+ fetch-depth: 0+ - uses: actions/setup-python@v5+ with:+ python-version: "3.12"+ - run: pip install deepteam+ - name: Scan & comment as deepteam[bot]+ env:+ OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}+ run: deepteam scan . --diff "origin/${{ github.base_ref }}..HEAD" --comment+```++Then add an `OPENAI_API_KEY` repository secret and install the [DeepTeam GitHub App](https://github.com/apps/deepteam) on the repository so the bot is authorized to comment.++:::note+If your Confident AI account is in the EU region, set `CONFIDENT_BASE_URL: "https://eu.api.confident-ai.com"` under the step's `env`. The hosted app configures this for you automatically.+:::diff --git a/docs/content/docs/code-scanning-introduction.mdx b/docs/content/docs/code-scanning-introduction.mdxnew file mode 100644index 00000000..b9c794e1--- /dev/null+++ b/docs/content/docs/code-scanning-introduction.mdx@@ -0,0 +1,122 @@+---+title: Introduction to Code Scanning+sidebar_label: Introduction+---++`deepteam` offers a powerful way to statically scan your source code for AI-application security vulnerabilities. Instead of attacking a running system like [red teaming](/docs/red-teaming-introduction) does, code scanning reads your source files and flags risky constructs such as `eval` or `exec` on LLM output, prompt injection sinks, SSRF, and excessive agency.++:::info+Code scanning is built on the same vulnerability taxonomy as red teaming. Every finding is classified into one of `deepteam`'s [vulnerabilities](/docs/red-teaming-vulnerabilities), so the two interfaces share the same vocabulary.+:::++## Quick Summary++Code scanning can be run in 4 different ways:++- [Command Line](/docs/code-scanning-introduction#running-a-scan) - scan a file, directory, or git diff from the CLI.+- [Configuration File](/docs/code-scanning-introduction#configuration-file) - declare your scan settings in `.deepteam-code-scan.yaml`.+- [Python API](/docs/code-scanning-introduction#python-api) - run the scanner programmatically for full control.+- [GitHub Bot](/docs/code-scanning-github-bot) - comment findings on every pull request as `deepteam[bot]`.++Here's how you can run a scan on the current directory:++```bash+deepteam scan .+```++By default the report is printed as Markdown and the command exits with code `1` if any findings remain, which makes it a ready-made CI gate.++:::tip DID YOU KNOW?+Code scanning is particularly useful for **CI/CD pipelines**, where you want to catch AI-security issues on every pull request before they are merged.+:::++## Running a Scan++The `deepteam scan` command takes a `path` argument, which can be a file or a directory and is defaulted to `.`.++### Basic Usage++```bash+deepteam scan .+```++### Scanning a Git Diff++Use the `--diff` flag to scan only the files changed between two git refs, which is ideal for pull request checks:++```bash+deepteam scan . --diff "main..HEAD"+```++### Command Line Flags++There are **EIGHT** optional command line flags:++- [Optional] `--diff`: scan only files changed between two git refs, e.g. `"main..HEAD"`.+- [Optional] `-c`, `--config`: path to a config YAML. Auto-discovered at the scan root if not set.+- [Optional] `-f`, `--format`: output format, one of `markdown`, `sarif`, or `json`. Defaulted to `markdown`.+- [Optional] `--min-severity`: only report findings at or above `low`, `medium`, `high`, or `critical`.+- [Optional] `-m`, `--model`: evaluation model, e.g. `gpt-4o`. Overrides the config.+- [Optional] `-o`, `--output`: write the report to a file instead of stdout.+- [Optional] `--fail-on-findings` / `--no-fail-on-findings`: exit with code `1` if any findings remain. Defaulted to `True`.+- [Optional] `--comment`: post findings so `deepteam[bot]` comments on the pull request, see [GitHub Bot](/docs/code-scanning-github-bot).++## Configuration File++You can declare your scan settings in a `.deepteam-code-scan.yaml` file at your scan root, which is picked up automatically.++```yaml title=".deepteam-code-scan.yaml"+min_severity: medium++instruction: |+ This is a customer-support agent. Treat hardcoded API keys and any+ eval/exec on LLM output as critical.++exclude:+ - "tests/**"+```++There are **SEVEN** optional parameters when creating a code scan configuration:++- [Optional] `min_severity`: minimum severity to report, one of `low`, `medium`, `high`, or `critical`. Defaulted to `low`.+- [Optional] `instruction`: project-specific guidance passed to the scanner. Defaulted to `None`.+- [Optional] `include`: glob patterns a file must match to be scanned. Defaulted to `None`.+- [Optional] `exclude`: glob patterns to skip. Defaulted to `None`.+- [Optional] `vulnerabilities`: restrict the scan to a subset of vulnerability names. Defaulted to a curated set of code-visible categories.+- [Optional] `model`: default evaluation model. Defaulted to `None`.+- [Optional] `diffs_only`: only scan changed files. Defaulted to `False`.++## Python API++For full control, you can use the scanner programmatically. Create a `code_scan_example.py` file and paste the following code:++```python title="code_scan_example.py"+from deepteam.code_scanner import CodeScanner, collect_files++# Collect scannable chunks from a path (binaries and lockfiles are skipped)+chunks = collect_files(".", exclude=["tests/**"])++scanner = CodeScanner(+ model="gpt-4o",+ instruction="This is a customer-support agent.",+)+findings = scanner.scan(chunks)++for finding in findings:+ print(finding.severity, f"{finding.filePath}:{finding.lineStart}", finding.vulnerability)+```++To scan only what changed between two git refs, use `collect_changed_files` instead:++```python title="code_scan_example.py"+from deepteam.code_scanner import CodeScanner, collect_changed_files++chunks = collect_changed_files(".", base="main", head="HEAD")+findings = CodeScanner(model="gpt-4o").scan(chunks)+```++Each finding carries `filePath`, `lineStart`, `lineEnd`, `vulnerability`, `vulnerabilityType`, `severity`, `reason`, and `recommendation`. The helpers `to_markdown`, `to_sarif`, `to_json`, and `filter_by_severity` are also available from `deepteam.code_scanner` for rendering and filtering.++:::info+You'll need to set your `OPENAI_API_KEY` as an environment variable before running a scan, since `deepteam` uses an LLM to detect and classify vulnerabilities. To use **ANY** custom LLM of your choice, [check out this part of the docs](https://deepeval.com/guides/guides-using-custom-llms).+:::diff --git a/docs/content/docs/meta.json b/docs/content/docs/meta.jsonindex 464f8f11..8e991291 100644--- a/docs/content/docs/meta.json+++ b/docs/content/docs/meta.json@@ -38,6 +38,10 @@"guardrails-introduction","(guards)",+ "---[FileSearch]Code Scanning---",+ "code-scanning-introduction",+ "code-scanning-github-bot",+"---[Boxes]Others---","data-privacy"]