# xgrep

A fast, Semgrep-compatible code scanner written in Go.

xgrep scans codebases using Semgrep YAML rule syntax and tree-sitter for language-aware,
AST-based pattern matching. It optimizes for **accuracy** — when it reports a
vulnerability, it should be real and exploitable — and adds code-intelligence and
AI-agent features on top of scanning. See the
[design goals](docs/01-getting-started/index.md#design-goals).

## Installation

Install xgrep from the [`@mondoohq/xgrep`](https://www.npmjs.com/package/@mondoohq/xgrep)
npm package, which ships prebuilt binaries:

```bash
# Run it straight from npx (no install)
npx @mondoohq/xgrep scan .

# Or add the `xgrep` command to your PATH
npm install -g @mondoohq/xgrep
xgrep scan .
```

> Prebuilt binaries are available for Linux and Windows (amd64 and arm64).
> A Homebrew formula for macOS is coming soon.

## Quick start

xgrep ships with a built-in rule corpus, so no rules file is needed to get started:

![Scanning a Flask app with xgrep — a tainted HTTP parameter reaching a shell command is reported as a command-injection finding](docs/assets/scan.gif)

```bash
# Scan the current directory with the built-in rules (runs the security and secrets rules)
xgrep scan .

# Choose a category (default: security,secrets)
xgrep scan --category correctness .

# High-signal, exploitable-only: drop hardening/advisory findings and non-source
# trees (tests, benchmarks, examples). Recommended for embedding or scanning a
# focused surface such as an AI-agent skill.
xgrep scan --category security --subcategory vuln --xgrepignore .
```

```bash
# Machine-readable output
xgrep scan --json .
xgrep scan --sarif .                          # GitHub Code Scanning
xgrep scan --gitlab -o gl-sast-report.json .  # GitLab SAST

# Bring your own rules: point -f at a rule file or a directory of rules
xgrep scan -f rules.yaml src/
```

A scan target can also be a **remote git repository** — xgrep clones it (shallow,
default branch) into a temp directory and scans it, no manual clone needed:

```bash
xgrep scan github.com/expressjs/express            # host/owner/repo shorthand
xgrep scan https://github.com/expressjs/express    # or a full HTTPS/SSH URL
xgrep scan github.com/expressjs/express --ref 4.18.2   # a branch, tag, or commit
```

See the [remote-repository section](docs/02-scanning/cli-reference.md#scanning-a-remote-repository)
for `--ref`, `--depth`, and `--full-clone`.

## Secrets scanning

Secrets scanning is **on by default** — every `xgrep scan` also looks for committed
credentials. xgrep ships **270+ detectors across 150+ providers** (cloud keys,
source-forge and CI tokens, payment/AI/messaging credentials, PEM private keys),
tuned for precision so well-known example values are filtered out.

![xgrep reporting a committed GitHub token and Stripe key, each with revoke-and-rotate remediation guidance](docs/assets/secrets.gif)

```bash
xgrep scan --category secrets .                  # scan the working tree for secrets only
xgrep --history --category secrets .             # walk the full git history — catches secrets
                                                 # committed and later deleted (still compromised)
xgrep --validate --category secrets .            # probe providers to confirm which secrets are live
```

Every finding tells you what leaked and how to respond — revoke, rotate, and move the
value to a secret store. Full details in [Secrets scanning](docs/02-scanning/secrets.md).

## CI/CD integration

`xgrep ci` is a single command that auto-detects your CI provider, scans only what a
pull/merge request changed, and writes a report for the platform to ingest — no hosted
backend, no upload. Distribute it via the prebuilt
[npm package](https://www.npmjs.com/package/@mondoohq/xgrep).

![xgrep ci on a simulated GitHub Actions pull request: it detects the CI environment, runs a diff-aware scan against the merge-base, writes a SARIF report, and flags the command injection introduced by the change](docs/assets/ci.gif)

**GitHub Actions** → GitHub Code Scanning (SARIF):

```yaml
permissions:
  contents: read
  security-events: write
steps:
  - uses: actions/checkout@v4
    with: { fetch-depth: 0 }       # history is needed for diff-aware PR scans
  - run: npm install -g @mondoohq/xgrep
  - run: xgrep ci -o results.sarif
  - if: always()
    uses: github/codeql-action/upload-sarif@v3
    with: { sarif_file: results.sarif }
```

**GitLab CI** → Vulnerability Report (GitLab SAST):

```yaml
xgrep-sast:
  image: node:lts
  script:
    - npm install -g @mondoohq/xgrep
    - xgrep ci -o gl-sast-report.json   # GitLab SAST format is auto-selected under GitLab CI
  artifacts:
    reports:
      sast: gl-sast-report.json
```

Install options, diff-aware PR scans, exit-code gating, and gradual rollout are in
[CI integration](docs/05-integrations/ci.md).

## Documentation

Full documentation lives in [`docs/`](docs/README.md):

- **[Getting started](docs/01-getting-started/index.md)** — install and run your first scan.
- **[Scanning](docs/02-scanning/index.md)** — CLI reference, output formats, supported
  languages, file filtering, secrets scanning, and Semgrep compatibility.
- **[Rules](docs/03-rules/index.md)** — writing, syntax, taint analysis, and testing rules.
- **[Code intelligence](docs/04-code-intelligence/index.md)** — `xgrep inspect` and the code graph.
- **[Integrations](docs/05-integrations/index.md)** — MCP, LSP, and CI.
- **[AI agents](docs/06-ai-agents/index.md)** — using xgrep as an agent backend (see also
  [`AGENTS.md`](AGENTS.md)).
