Skip to content
Skip to content
Documentation

Run ASH with pre-commit

ASH's repository defines hooks for the pre-commit framework. This page sets up the ash hook, says what it scans, and what it costs per commit.

Prerequisites:

  • pre-commit, running on Python 3.10 to 3.14, or a language_version as shown below.
  • A git repository with a .pre-commit-config.yaml.
  • Network access to GitHub and PyPI the first time the hook runs.

Add the hook

repos:
  - repo: https://github.com/awslabs/automated-security-helper
    rev: v3.8.0
    hooks:
      - id: ash

Then install the git hook and run it once on every file:

pre-commit install
pre-commit run ash --all-files

The first run builds the hook's environment, with ASH's cdk extra, and installs bandit and checkov, so it takes longer than later runs. When the hook finds something, pre-commit prints Failed with ASH's summary and - exit code: 2, and the commit stops. Exit code 1 means the scan did not complete, for example because a scanner was MISSING; Exit codes lists every cause.

Ignore ASH's output directory in git, so the reports are not committed. ASH leaves its own files there out of the scan whether or not .gitignore lists them, but it scans a file committed there like any other. ash config init writes a .ash/.gitignore that ignores it, or add the line yourself:

.ash/ash_output/

Do not ignore all of .ash/: an ASH config file can live there, as .ash/.ash.yaml.

What the hook runs

The ash hook runs ashv3 --mode=precommit --staged with your args. Precommit mode:

  • runs five scanners by default: bandit, detect-secrets, checkov, cdk-nag, and npm-audit. --add-scanners adds others to that set; --only-scanners replaces the set and runs exactly what it names; --exclude-scanners removes names, whichever source added them. The deprecated --scanners behaves like --add-scanners here. See Choosing the scanners.
  • with --staged (which the hook passes), scans the staged files instead of the whole repository, so a one-file commit pays for one file. See Staged-file scope.
  • prints simplified output and writes reports to .ash/ash_output/.

Precommit mode writes only the reports the hook actually consumes: the text summary and the SARIF report the exit code is computed from, instead of the full report set every other mode writes. That is a default, not a cap -- passing --output-formats, or setting output_formats or configuring anything under reporters in the ASH config file, keeps the full behavior those choices describe, exactly as outside precommit mode.

Staged-file scope (--staged)

--staged reads the staged file list from git diff --cached (added, copied, modified and renamed entries; a rename is scanned at its new path; a deleted file cannot be scanned and is not listed) and makes it the scan's target set:

  • bandit and checkov are handed the staged files instead of the tree; the scanners that pick their own files (detect-secrets, cdk-nag, npm-audit) read the same staged list. For Terraform, checkov also reads a staged file's same-directory .tf/.tfvars siblings, so same-directory variables.tf defaults and locals still resolve; nothing recurses into subdirectories, so a local module that lives in one is read only when its own files are staged.
  • a scanner none of whose files are staged is skipped, with a log line saying so. For bandit and checkov that decision is made before their tools are even looked up (the converted tree has to be empty too), so a commit that stages nothing they scan does not pay for their version probes -- and does not need their tools installed.
  • converters (Jupyter notebooks, archives) convert staged inputs only, and are skipped when nothing staged matches them.
  • suppressions and ignore_paths resolve against the same scan root as a whole-repository run, so a suppression that matches a finding in a full scan matches it here too.
  • findings in files that are not staged are out of scope by definition: the hook judges the commit, not the repository. The reports are filtered to the staged files, so even what a tool read for context (the Terraform siblings above, or the large-commit fallback below) never adds findings. Keep a whole-repository scan in CI for the rest.
  • a commit staging more files than one command line can carry (about 500) makes bandit and checkov fall back to scanning the directory, with a log line: the reports still hold staged-file findings only, and what degrades is the time win.
  • with nothing staged, the scan logs that and exits 0 without starting any scanner.

What the tools read is the working-tree copy of each staged file. Under the pre-commit framework the two are identical, because it stashes unstaged changes around the hooks; a direct ash --mode precommit --staged run after git add -p, or after editing a file it had staged, scans content that differs from what the commit will contain.

--staged works only with --mode precommit; any other mode refuses it (exit 5). Without the flag, a precommit scan still covers the whole repository: on every ASH v3 release the flag is opt-in, the hooks in this repository pass it for you, and it is planned to become the precommit default in ASH v4.

This is not what --changed-files-only does. --changed-files-only scans everything and only filters the reports against a <base_ref>...HEAD diff afterwards, so it saves no scan time and cannot see staged-but-uncommitted content; --staged is the scan-time scope built for the hook.

The hook's environment includes the cdk extra that cdk-nag needs. cdk-nag and npm-audit also need Node.js and npm on PATH, but only when the repository has files they scan: CloudFormation templates for cdk-nag, including the *.template.json files cdk synth writes to cdk.out even when .gitignore lists cdk.out, and a package.json with an npm, yarn, or pnpm lockfile beside it for npm-audit. In a repository with neither, both are SKIPPED and the hook runs without Node.js. In one that has them, Node.js and npm must be on PATH, or ASH reports the scanner that has files to scan as MISSING and the hook fails with exit code 1 on every commit. To run without Node.js there, exclude the two scanners:

repos:
  - repo: https://github.com/awslabs/automated-security-helper
    rev: v3.8.0
    hooks:
      - id: ash
        args: [--exclude-scanners, cdk-nag, --exclude-scanners, npm-audit]

They are then SKIPPED, and the hook passes or fails on the other three. ash-simple-scan is the same hook under another name.

Do not add --no-fail-on-incomplete-scanners to the hook: with it, a commit passes when a scanner did not run, and the output says only Passed.

Choosing the scanners

The selection precommit mode runs is resolved in this order, later entries winning:

  1. The mode default: the five scanners above.
  2. The ASH config file: precommit.only_scanners replaces the default set; precommit.add_scanners adds to it. Setting both keys is refused.
  3. The command line: --only-scanners replaces everything above, mode default and config keys alike; --add-scanners, or the deprecated --scanners, adds to whatever stands. --only-scanners cannot be combined with either additive flag.
  4. --exclude-scanners applies last: an excluded name does not run, whichever source included it.

So a repository that wants its hook to run only bandit and detect-secrets states it once in its ASH config instead of in every clone's hook args:

project_name: my-project
precommit:
  only_scanners:
    - bandit
    - detect-secrets

A committer can still widen one run with --add-scanners, or override the file entirely with --only-scanners. --scanners keeps working with a deprecation warning; it is the same additive selection --add-scanners now names explicitly.

Upgrading to v3.8.0 with checkov extra formats configured

An ASH config that sets checkov.additional_formats: [cyclonedx_json] can crash checkov on some repositories. From v3.8.0, a scanner that crashes correctly fails the run with exit code 1 instead of passing silently, so a hook that passed on v3.7.x can start failing after the upgrade. Remove that format from the config, or exclude checkov from the hook with --exclude-scanners checkov. Once checkov runs without crashing, it can also report a real finding the crashed runs never surfaced.

Add Python packages to the hook

additional_dependencies in your configuration replaces the hook's own list rather than adding to it. The ash hook's list is .[cdk], which installs the cdk extra from the hook's clone of ASH, so a list of your own has to include .[cdk] as well. Without it, cdk-nag is MISSING in a repository with CloudFormation templates.

When the hook runs

The hook has no stages setting, so it runs on every commit. To run it less often, set one:

repos:
  - repo: https://github.com/awslabs/automated-security-helper
    rev: v3.8.0
    hooks:
      - id: ash
        stages: [pre-push]

With stages: [pre-push], install that hook type with pre-commit install --hook-type pre-push, and a failing scan blocks git push. With stages: [manual], the hook runs only when you ask: pre-commit run --hook-stage manual ash.

What it costs

The first run builds the hook environment and installs the scanner tools, which takes 65 to 150 seconds. The environment takes about 650 MB under ~/.cache/pre-commit, and bandit and checkov about 240 MB under ~/.local/share/uv.

Every later run's cost scales with the size of the repository unless --staged narrows the scan: without the flag, each commit scans the whole tree, however small the change. With --staged, which the hook passes, the scan covers the staged files and the time grows with the commit instead. checkov is the slowest scanner. Warm-run times, measured on x86_64 Linux with 8 or more vCPUs, Python 3.12, and warm caches. The 10-file column is the median of three runs on one machine; the ~900-file column comes from an earlier measurement on a different machine, so compare rows within a column rather than across:

Warm run 10-file repository ~900-file Python repository
--mode precommit on the whole repository (no --staged) ~6.6 s ~18 s
--mode precommit --staged, one .py file staged ~2.8 s ~5.7 s
The same, with --only-scanners bandit,detect-secrets ~2.8 s ~3.4 s
--staged with the express daemon warm (--express) ~1.4 s not measured

For scale, ash --version alone takes about 0.2 seconds on the same machine. What is left of the fastest runs above is the floor every scan pays: starting the interpreter, importing ASH, loading the configuration, and setting up the scanners. A --staged run whose staged files match no scanner still pays that floor; the scanners are skipped, and the wall time is unchanged.

Part of that floor is ASH's own startup (the interpreter and the eager imports), paid again on each commit. Express mode removes it: add --express to the hook's args, and the first commit starts a resident warm daemon paired with the hook's own environment, which every later hook run forks instead of starting cold (Linux and macOS only). The pairing matters: the daemon serves exactly the ASH installation that started it, and pre-commit's hook environment is its own installation, so a daemon started from your shell's ash is silently ignored by the hook -- start it with the hook's flag, not from the shell. The hook's default --staged already scopes the scan to the files the commit touches, so the daemon and --staged together are the fastest hook shape.

Pre-commit and CI together

The pre-commit hook and a CI scan answer different questions, so most repositories run both.

Pre-commit hook (ash) CI scan
Mode --mode precommit local by default, or container
Scanners The five in What the hook runs, plus any you add with --scanners Every scanner your configuration enables, including the ones precommit mode leaves out, such as semgrep, opengrep, grype, trivy, and gitleaks
Runs On the developer's machine, per commit or push On a clean runner, per pull request or push
Output Simplified, no progress display The full summary and report files, for upload as artifacts or SARIF
Cost A few seconds per commit after the first run, growing with the staged set (What it costs) Installs the scanner tools each build, or uses an image you prepared

Scanner coverage lists which scanners each mode runs. A scanner that precommit mode skips is not run by the ash hook at all, so a clean commit does not mean a clean CI scan. To get the container image's full scanner set before pushing, use the ash-full-scan hook.

The CI pages run ASH directly rather than through the hook, and use its exit code as the gate:

If you want CI to enforce the same hook configuration, pre-commit run ash --all-files works on a runner that has pre-commit, Python 3.10 to 3.14, and network access to GitHub and PyPI. If the repository has CloudFormation templates or a package.json with a lockfile, the runner also needs Node.js and npm, or the hook exits 1 on every run (see Behavior for the --exclude-scanners alternative). It runs only the precommit scanner set, so keep a direct ASH scan in the pipeline as well; the hook does not replace it.

Use an ASH you already installed

If ASH is installed with uv tool install or in a virtual environment outside the repository, a local hook runs that installation instead of building one per repository:

repos:
  - repo: local
    hooks:
      - id: ash
        name: ASH
        entry: ash --mode precommit
        language: system
        pass_filenames: false

ash must be on PATH when git runs the hook. ASH installs bandit and checkov with the uv installed with it, or with a uv on PATH. pass_filenames: false is required. Without it, pre-commit appends the staged file names, and ASH rejects the first one as an unknown command, a usage error, with exit code 5.

This hook uses the extras and tools of that installation. When the repository has CloudFormation templates, cdk-nag needs the cdk extra in that installation and Node.js on PATH; when it has npm lockfiles, npm-audit needs npm on PATH. A scanner that lacks them is MISSING and every commit fails with exit code 1. Add --exclude-scanners cdk-nag --exclude-scanners npm-audit to entry to leave the two out.

Pre-commit on another Python

pre-commit builds a Python hook's environment with the interpreter it runs under, and ASH installs on Python 3.10 to 3.14 only. If pre-commit itself runs on another version, building the hook environment fails; on Python 3.9 the error is No matching distribution found for hatchling>=1.32.4. Install pre-commit on a supported interpreter:

uv tool install --python 3.12 pre-commit

Or point the hook at a newer Python that is on your PATH:

repos:
  - repo: https://github.com/awslabs/automated-security-helper
    rev: v3.8.0
    hooks:
      - id: ash
        language_version: python3.12

default_language_version at the top of the file does the same for every Python hook.

A frozen rev is flagged as a secret

pre-commit autoupdate --freeze replaces the tag with a 40-character commit SHA, and detect-secrets reports that line of .pre-commit-config.yaml as a high-entropy string, which fails the hook with exit code 2. Suppress that finding for that file, with the reason:

global_settings:
  suppressions:
    - path: .pre-commit-config.yaml
      rule_id: SECRET-HEX-HIGH-ENTROPY-STRING
      reason: pre-commit autoupdate --freeze pins hooks to commit SHAs

Put it in your ASH config file, or, if the repository has none, in a new .ashrc.yaml at its root. Suppress findings covers expiration dates and narrower matches.

The ash-full-scan hook

.pre-commit-hooks.yaml also defines ash-full-scan, which runs ashv3 --mode=container --simple. It builds ASH's container image from the source that pre-commit installed, then scans the repository inside it with every scanner the image carries. It needs a container runtime on the host: finch, docker, nerdctl, or podman, found in that order. The first run builds the image and takes several minutes, so give the hook stages: [pre-push] or stages: [manual]:

repos:
  - repo: https://github.com/awslabs/automated-security-helper
    rev: v3.8.0
    hooks:
      - id: ash-full-scan
        stages: [manual]
pre-commit run --hook-stage manual ash-full-scan --all-files

Run ASH in a container covers container mode.

Run the hook in Nix mode

Nix mode needs a current release

Nix mode is in the latest release; v3.7.x and older reject --mode nix. This recipe runs the ASH on PATH, so install that release or newer, or use a source checkout.

.pre-commit-hooks.yaml defines no Nix hook: ash and ash-simple-scan run precommit mode and ash-full-scan runs container mode, and ASH takes one --mode per run. To scan with the scanners that ASH's flake.nix supplies, add a local hook that calls ash scan --mode nix on an ash already on PATH, and give it a slow stage like ash-full-scan:

repos:
  - repo: local
    hooks:
      - id: ash-nix
        name: ASH scan in Nix mode
        entry: ash scan --mode nix --no-progress
        language: system
        pass_filenames: false
        stages: [manual]
uv tool install 'automated-security-helper[cdk] @ git+https://github.com/awslabs/automated-security-helper.git@v3.8.0'
pre-commit run --hook-stage manual ash-nix --all-files

This is a different hook from ash: it runs the scanners of a normal scan, not the five of precommit mode, so it takes longer and belongs on manual or pre-push. It uses the installation on PATH, so install ASH with the cdk extra for cdk-nag. An installed release finds flake.nix at its own release tag with nothing set, so the hook works on a pre-push and from an IDE or GUI client as well, where a variable exported in your shell may not be set. Install ASH covers Nix mode's requirements.