Skip to main content

CLI Command

The promptfoo code-scans command scans code changes for LLM-related security vulnerabilities, helping you identify prompt injection risks, jailbreaks, PII exposure, and other security issues before they reach production.

Quick Start​

Install promptfoo globally:

npm install -g promptfoo

Install and run promptfoo with Node.js 24 or later for full-repository scans. On Node.js 22.22 or later, you can scan changed lines with promptfoo code-scans run --diffs-only.

During a full scan, the hosted scanner can read eligible text files throughout the selected directory at the Git revision being compared (--compare, default HEAD). The CLI builds a private snapshot from committed regular files. It excludes working-tree edits, untracked files, symlinks, submodules, generated or oversized files, and recognized credential filenames and content. Each file is limited to 500 KiB. Full scans stop if eligible paths exceed 30,000 files or 256 MiB in total. Full scans send the commit IDs for the comparison and omit commit subjects and author names. These checks cannot identify every secret. Use --diffs-only to share only the Git diff.

Full scans use the read-only filesystem MCP server installed with promptfoo. Keep the installation outside the repository being scanned and use a trusted Git executable on your PATH. If you installed with --omit=optional, reinstall with optional dependencies or use --diffs-only.

If Yarn Plug'n'Play reports a missing ESM loader, set pnpEnableEsmLoader: true in the external installation's .yarnrc.yml and reinstall.

Authenticate with your promptfoo account:

promptfoo auth login

Run a scan on your current branch:

promptfoo code-scans run

Running Time​

Depending on the size of your PR and codebase, the scan can take anywhere from a minute or two to 20 minutes or more. That said, most PRs take between 3 and 10 minutes.

Command Options​

Basic Usage​

promptfoo code-scans run [repo-path] [options]

Options​

OptionDescriptionDefault
repo-pathPath to repositoryCurrent directory (.)
--api-key <key>Promptfoo API keyFrom promptfoo auth or PROMPTFOO_API_KEY env var
--base <ref>Base branch/commit to compare againstAuto-detects either main or master
--compare <ref>Branch/commit to scanHEAD
--config <path>Path to config fileNone
--guidance <text>Custom guidance to tailor the scanNone
--guidance-file <path>Load guidance from a fileNone
--api-host <url>Promptfoo API host URLhttps://api.promptfoo.app
--diffs-onlyScan only PR diffs, don't explore full repofalse
--jsonOutput results as JSON (see schema)false
-f, --format <format>Output format (text, json, or sarif)text
--github-pr <owner/repo#number>Post comments to GitHub PR (used with Promptfoo GitHub Action)None

Examples​

Scan diffs for current branch, comparing against main (or master):

promptfoo code-scans run

Scan diffs for specific branch against main:

promptfoo code-scans run --compare feature/new-llm-integration

Scan diffs between two commits:

promptfoo code-scans run --base ffa1b2d3 --compare a9c7e5b6

Scan with custom config:

promptfoo code-scans run --config custom-scan-config.yaml

Get JSON output:

promptfoo code-scans run --json

See JSON Output Schema for the response format.

Get SARIF output for security tooling:

promptfoo code-scans run --format sarif > promptfoo-code-scan.sarif

SARIF output includes location-backed findings that GitHub Code Scanning can display.

Configuration File​

To use a configuration file, pass its path explicitly:

promptfoo code-scans run --config .promptfoo-code-scan.yaml

For example, this .promptfoo-code-scan.yaml limits the scan to the diff:

# Minimum severity level to report (low|medium|high|critical)
# Both minSeverity and minimumSeverity are supported
minSeverity: medium

# Limit the hosted scanner to the diff. The default is a full scan.
diffsOnly: true

# Optional: Custom guidance to tailor the scan to your needs
guidance: |
Focus on authentication and authorization vulnerabilities.
Treat any PII exposure as high severity.

# Or load guidance from a file (path relative to config file)
# guidanceFile: ./scan-guidance.md

# Optional: Promptfoo API host URL
# apiHost: https://api.promptfoo.dev

Custom Guidance​

You can provide custom guidance to tailor scans to your specific needs. See the overview for what guidance can do.

Via command line:

# Inline guidance
promptfoo code-scans run --guidance "Focus on authentication vulnerabilities in the /src/auth directory"

# Load from file
promptfoo code-scans run --guidance-file ./scan-guidance.md

Via config file:

# Inline guidance
guidance: |
Focus on authentication and authorization vulnerabilities.
Treat any PII exposure as high severity.

# Or load from file
guidanceFile: ./scan-guidance.md

Authentication​

The code scanner supports multiple authentication methods (checked in order):

  1. CLI argument: --api-key <key>
  2. Environment variable: PROMPTFOO_API_KEY=<key>
  3. Promptfoo auth: promptfoo auth login
  4. GitHub OIDC (when used in the Promptfoo GitHub Action): Automatic

Using promptfoo auth​

# Login once
promptfoo auth login

# Then run scans without --api-key
promptfoo code-scans run

Using environment variable​

export PROMPTFOO_API_KEY=your-api-key
promptfoo code-scans run

Using --api-key argument​

promptfoo code-scans run --api-key your-api-key

JSON Output Schema​

When using --json, the scan outputs a JSON object to stdout with the following structure:

Response Object​

FieldTypeDescription
successbooleanWhether the scan completed successfully
reviewstringOverall review summary of the scan
commentsComment[]Array of findings (see below)
commentsPostedbooleanWhether comments were posted to a PR
skipReasonstringSet when the scan was intentionally skipped (e.g. fork PR awaiting maintainer approval); comments will be empty
errorstringError message if the scan failed

Comment Object​

FieldTypeDescription
filestringFile path where the issue was found, or null
linenumberLine number of the finding, or null
startLinenumberStart line for multi-line findings, or null
findingstringDescription of the security issue
fixstringSuggested fix for the issue
severitystringcritical, high, medium, low, or none
aiAgentPromptstringPrompt for AI coding agents to fix the issue

Example​

{
"success": true,
"review": "The PR introduces an LLM-powered support chat feature. The main security concerns are around prompt injection via user messages and insufficient output validation.",
"comments": [
{
"file": "src/chat/handler.ts",
"line": 42,
"startLine": 40,
"finding": "User input is passed directly to the LLM prompt without sanitization, allowing prompt injection attacks.",
"fix": "Sanitize user input and use a system prompt that instructs the model to ignore injected instructions.",
"severity": "critical",
"aiAgentPrompt": "In src/chat/handler.ts around line 42, add input sanitization before passing user messages to the LLM. Use a system prompt with injection-resistant instructions."
},
{
"file": "src/chat/handler.ts",
"line": 87,
"startLine": null,
"finding": "LLM responses are rendered as raw HTML without escaping, which could allow cross-site scripting if the model is manipulated.",
"fix": "Escape or sanitize LLM output before rendering it in the UI.",
"severity": "high",
"aiAgentPrompt": "In src/chat/handler.ts at line 87, escape the LLM response output before inserting it into the DOM to prevent XSS."
}
]
}

See Also​