Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ inputs:
extra-args:
description: "Extra arguments appended verbatim to `secfoo run`."
default: ""
changed-only:
description: >-
On pull_request events, review only the paths changed since the PR base
(using git merge-base). Passes github.event.pull_request.base.sha
automatically; other events stay full scans. Requires a checkout with
fetch-depth: 0. Pair with extra-args: "--fail-scope changed" to gate on
changed findings only.
default: "false"
baseline:
description: >-
Path to a findings baseline JSON (e.g. .secfoo/baseline.json). Matching
open findings are excluded from --fail-on but still exported; expired
entries count again. The file must be present in the checkout.
default: ""
version:
description: "secfoo version to install from PyPI. Empty = latest."
default: ""
Expand Down Expand Up @@ -159,6 +173,9 @@ runs:
IN_TIMEOUT: ${{ inputs.timeout }}
IN_EXCLUDE: ${{ inputs.exclude }}
IN_EXTRA_ARGS: ${{ inputs.extra-args }}
IN_CHANGED_ONLY: ${{ inputs.changed-only }}
IN_BASELINE: ${{ inputs.baseline }}
PR_BASE_SHA: ${{ github.event.pull_request.base.sha }}
IN_RESULT_FILE: ${{ inputs.result-file }}
# Non-interactive: no project-name prompt, no "scan again?" prompt.
SECFOO_PROJECT_NAME: ${{ github.repository }}
Expand All @@ -172,6 +189,10 @@ runs:
[ -n "$IN_FAIL_ON" ] && args+=(--fail-on "$IN_FAIL_ON")
[ -n "$IN_MAX_COST" ] && args+=(--max-cost "$IN_MAX_COST")
[ -n "$IN_TIMEOUT" ] && args+=(--timeout "$IN_TIMEOUT")
if [ "$IN_CHANGED_ONLY" = "true" ] && [ -n "$PR_BASE_SHA" ]; then
args+=(--changed-since "$PR_BASE_SHA")
fi
[ -n "$IN_BASELINE" ] && args+=(--baseline "$IN_BASELINE")
if [ -n "$IN_EXCLUDE" ]; then
IFS=',' read -ra excludes <<< "$IN_EXCLUDE"
for x in "${excludes[@]}"; do x="${x## }"; x="${x%% }"; [ -n "$x" ] && args+=(--exclude "$x"); done
Expand Down Expand Up @@ -240,11 +261,14 @@ runs:
shell: bash
env:
SARIF_FILE: ${{ inputs.sarif-file }}
IN_BASELINE: ${{ inputs.baseline }}
run: |
set -euo pipefail
# The runner's store only holds this job's scan, so no --project filter
# is needed: every exported finding belongs to the checked-out repo.
secfoo findings export --format sarif --output "$SARIF_FILE"
export_args=(--format sarif --output "$SARIF_FILE")
[ -n "$IN_BASELINE" ] && export_args+=(--baseline "$IN_BASELINE")
secfoo findings export "${export_args[@]}"
echo "sarif-file=$SARIF_FILE" >> "$GITHUB_OUTPUT"

- name: Upload SARIF to code scanning
Expand Down
74 changes: 68 additions & 6 deletions docs/CI.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,55 @@ pipeline can gate on.
Flags that matter in CI: `--json` (one JSON document on stdout, progress on
stderr, no interactive prompts), `--rescan` (never ask "scan this commit
again?"), `--project-name` (so the dashboard shows a repo name rather than a
runner path), `--fail-on`, `--max-cost`, `--exclude`, `--timeout`. The full
list is in [REFERENCE.md](REFERENCE.md#ci-gates-and-machine-readable-output).
runner path), `--fail-on`, `--max-cost`, `--exclude`, `--timeout`, and, for
pull requests, `--changed-since`, `--fail-scope` and `--baseline` (below).
The full list is in
[REFERENCE.md](REFERENCE.md#ci-gates-and-machine-readable-output).

## Adopting secfoo in an existing repo

A first full audit of a mature codebase usually finds dozens of issues. If
you turn on `--fail-on high` immediately, every PR goes red and the check
gets disabled. The baseline workflow avoids that without throwing the
findings away:

1. Run once over the whole repository and write a baseline:

```bash
secfoo run --skill sast --skill secret-scanning --agent api --target . --rescan
secfoo findings baseline write .secfoo/baseline.json --reason "initial triage"
```

Commit `.secfoo/baseline.json`. It is plain JSON — edit an entry's
`reason` or give it an `expires` date (`YYYY-MM-DD`) when you know when
the exception should be revisited. `baseline write` keeps those edits on
a later rewrite when you omit `--reason`/`--expires`.

2. On subsequent PRs, review only what changed and gate on the diff:

```bash
secfoo run --changed-since origin/main --fail-scope changed \
--fail-on high --baseline .secfoo/baseline.json
```

Known findings stop failing the build; expired baseline entries start
failing again and are listed in the run output. Nothing is hidden: the
findings remain in the store, in the SARIF/JSONL exports, and the SAST
dashboard marks them **Accepted in baseline**.

3. As you fix the known findings, run `secfoo findings baseline check` to
list entries that are now closed or missing, and prune them from the
JSON. Then tighten `--fail-on` (or drop `--fail-scope`) once the baseline
is small enough.

`--changed-since` uses git's merge base, so a long-lived branch does not
pick up unrelated commits from `main`. It requires a local git checkout
with the base ref available: use `fetch-depth: 0` on GitHub Actions (the
Action's `changed-only` input wires `github.event.pull_request.base.sha`
automatically) and `GIT_DEPTH: "0"` on GitLab. If your CI already knows the
diff, pass it with `--changed-files` instead (newline-separated paths).
The two flags are mutually exclusive, and a missing ref or a non-git target
is an error before any agent is started.

## Which agent in CI

Expand Down Expand Up @@ -78,13 +125,18 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # needed for changed-only / --changed-since on PRs
- uses: secfoo-com/secfoo@main
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }} # or ANTHROPIC_API_KEY / GEMINI_API_KEY
with:
skills: sast,secret-scanning
fail-on: high
max-cost: "2.00"
changed-only: true # PR runs review the diff
baseline: .secfoo/baseline.json # commit this file after the first run
extra-args: "--fail-scope changed" # findings in unchanged files stay visible, not gating
```

Pin to a release tag (`secfoo-com/secfoo@v0.3.1`) once you are happy with
Expand All @@ -104,6 +156,8 @@ and, without the Action, [examples/ci/github-actions-manual.yml](../examples/ci/
| `max-cost` | | USD cap on agent-reported spend. |
| `timeout` | | Per-skill timeout in seconds. |
| `exclude` | | Comma-separated paths to skip. |
| `changed-only` | `false` | On `pull_request` events, review only the paths changed since the PR base (via git merge-base). Passes `github.event.pull_request.base.sha` automatically; other events stay full scans. Needs `fetch-depth: 0`. Pair with `extra-args: "--fail-scope changed"` to gate on changed findings only. |
| `baseline` | | Path to a committed findings baseline JSON. Matching findings are excluded from `fail-on` but still exported; expired entries count again. |
| `extra-args` | | Appended to `secfoo run` verbatim. |
| `version` | latest | secfoo version from PyPI. |
| `python-version` | `3.12` | Python used to install secfoo. |
Expand Down Expand Up @@ -177,18 +231,26 @@ Full example: [examples/ci/pre-commit-config.yaml](../examples/ci/pre-commit-con
secfoo:
stage: test
image: python:3.12-slim
variables:
GIT_DEPTH: "0" # merge-base needs full history on merge requests
before_script:
- apt-get update -qq && apt-get install -y -qq --no-install-recommends git >/dev/null
- pip install --quiet secfoo
script:
- |
# On merge requests, review only what changed; branch pipelines are full scans.
extra=()
if [ -n "${CI_MERGE_REQUEST_DIFF_BASE_SHA:-}" ]; then
extra+=(--changed-since "$CI_MERGE_REQUEST_DIFF_BASE_SHA" --fail-scope changed)
fi
secfoo run --agent api --target . --depth quick --json --rescan \
--project-name "$CI_PROJECT_PATH" \
--skill sast --skill secret-scanning \
--fail-on high --max-cost 2.00 > secfoo-result.json || code=$?
- secfoo findings export --format gitlab-sast -o gl-sast-report.json
- secfoo findings export --format gitlab-secret-detection -o gl-secret-detection-report.json
- secfoo findings export --format sarif -o secfoo.sarif
--fail-on high --max-cost 2.00 \
--baseline .secfoo/baseline.json "${extra[@]}" > secfoo-result.json || code=$?
- secfoo findings export --format gitlab-sast -o gl-sast-report.json --baseline .secfoo/baseline.json
- secfoo findings export --format gitlab-secret-detection -o gl-secret-detection-report.json --baseline .secfoo/baseline.json
- secfoo findings export --format sarif -o secfoo.sarif --baseline .secfoo/baseline.json
- exit ${code:-0}
artifacts:
when: always
Expand Down
58 changes: 57 additions & 1 deletion docs/REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,14 @@ Open rows keyed with the legacy fingerprint are **rekeyed** automatically
on the first rescan that computes a region hash. There is no post-hoc
embedding similarity on finding text.

A **diff-scoped run** (`--changed-since` / `--changed-files`, below) keeps
this identity: the code-region hash is computed from the checkout exactly as
in a full scan. A rename contributes both the old and the new path to the
changed set, so a finding in a moved file is still recognised. Crucially,
stale-finding closure is limited to the scanned paths — a finding in a file
the run did not look at keeps its `open` state instead of being closed. Each
finding also carries a `scope` (`changed` or `context`) for the CI gate.

### Structured findings (SAST, SCA, secret scanning)

SCA and secret-scanning runs get the same open/closed lifecycle as SAST,
Expand All @@ -300,6 +308,13 @@ records:
code-free description of the vulnerable construct ("YAML loader without
a safe loader applied to an uploaded config file"). Because it has no
paths or identifiers, findings can be compared across repos.
- **`scope`** (#109): `changed` in a full scan, or `changed`/`context` in a
diff-scoped run. A `context` finding is still stored and exported but can
be excluded from `--fail-on` with `--fail-scope changed`.
- **`baseline_accepted`** (#109): JSON `{"reason", "expires"}` when a run
with `--baseline` saw the finding as an active baseline entry. The SAST
dashboard shows an **Accepted in baseline** badge; it never hides the
finding. Expired entries clear the badge on the next such run.
- **`locations`** (SAST and secret scanning): every site the report's
Location cell names, as a list of `{"path", "start_line", "end_line"}`.
The cell may hold `path:line`, `path:start-end`, `path:line,line`,
Expand All @@ -324,6 +339,7 @@ stored, even when a report includes more.
secfoo findings export # open findings, every skill, JSON Lines to stdout
secfoo findings export -s sast --status all # one skill, open and closed
secfoo findings export --no-locations -o findings.jsonl # drop paths/lines/region hashes
secfoo findings export --baseline .secfoo/baseline.json -o findings.jsonl # annotate baseline state
```


Expand Down Expand Up @@ -381,6 +397,42 @@ score. CWE tags use `external/cwe/cwe-N`. Findings without a CWE still get a rul
`--no-locations` removes the same structured location fields as JSONL and omits
SARIF locations; free-text finding titles and descriptions are not redacted.

With `--baseline <file>`, every exported finding is annotated without being
hidden: SARIF results get a `baselineState` of `"unchanged"` for an active
entry and `"new"` otherwise; JSON Lines records get the same `baselineState`
plus `baseline: {reason, expires}` (with `null` values when the entry has
none). Expired entries read as `"new"`. The GitLab report formats ignore
baseline annotations. See [CI.md](CI.md#adopting-secfoo-in-an-existing-repo)
for the workflow that writes and maintains the file.

### Findings baseline

```bash
secfoo findings baseline write .secfoo/baseline.json --reason "issue #42" --expires 2026-12-31
secfoo findings baseline check .secfoo/baseline.json
secfoo run --baseline .secfoo/baseline.json --fail-on high
```

`baseline write` records the fingerprint, skill, severity and title of every
**open** finding in a small JSON file. `--reason` and `--expires` are applied
to all entries when given; the file is plain JSON and meant to be edited by
hand afterwards. Re-running `write` without those flags preserves an
existing entry's edited reason and expiry. Only `--skill` and `--project`
limit which findings are written.

`baseline check` lists entries whose finding is now closed or no longer in
the store, plus entries whose expiry has passed, so the file can be pruned.
It never changes the file itself.

`secfoo run --baseline <file>` excludes active entries from the `--fail-on`
count. It does not suppress the run, the stored finding, or any export.
Expired entries are reported in the run output and count toward the gate
again; an invalid file (bad JSON, missing `entries`, or a non-`YYYY-MM-DD`
expiry) fails with exit code `2` before any agent starts. In `--json` mode
the summary adds `gates.findings.expired_entries` and a `baseline` object
with the path and active/expired entry counts. Combine with
`--fail-scope changed` to gate only on findings in files the PR touched.

### Runtime evidence

Runtime evidence records what a running app actually did with a vulnerable
Expand Down Expand Up @@ -799,7 +851,11 @@ secfoo run --skill sast --skill secret-scanning --agent api --target . \

| Flag | Effect |
|---|---|
| `--fail-on critical\|high\|medium\|low` | Exit `2` if any run reports a finding at that severity or worse. Counts come from the report contract's `### [SEVERITY] Fn:` headings, not from prose. |
| `--fail-on critical\|high\|medium\|low` | Exit `2` if any run reports a finding at that severity or worse. Counts come from the report contract's `### [SEVERITY] Fn:` headings, not from prose. Active baseline entries and (with `--fail-scope changed`) `context` findings are excluded from the count. |
| `--fail-scope all\|changed` | With `--fail-on`, `changed` counts only findings in files the PR touched; `all` (default) counts them all. Findings in unchanged files stay stored and exported as `scope: context` — only the gate ignores them. Requires `--changed-since` or `--changed-files` to have any effect. |
| `--changed-since <ref>` | Diff-aware run: review the paths changed between `ref` and HEAD, using git's **merge base**. The brief names the changed files and asks the agent to review those paths and their callers. Needs a local git checkout with that ref available; a missing ref or non-git target fails with exit code `2` before runs start. Mutually exclusive with `--changed-files`. |
| `--changed-files <paths>` | Diff-aware run from a newline-separated list of paths supplied by CI. Same scope behaviour as `--changed-since`; needs a local git target because structured findings are scoped by path. |
| `--baseline <file>` | Exclude findings whose fingerprint has an active entry in this baseline from `--fail-on` (see [Findings baseline](#findings-baseline)). Expired entries are listed and count again. |
| `--max-cost <usd>` | Exit `3` if the summed agent-reported spend for this invocation exceeds the cap. Evaluated after the runs complete. |
| `--max-input-tokens <n>` | `api` agent only. Checked **before** the model is called: if the assembled prompt (brief + target source) is larger than `n` input tokens, the run fails without sending anything, and the error says how large the prompt was and which limit applied. Token counts come from LiteLLM's tokenizer for the model, or ~4 characters per token when it has none. Falls back to `[agents.api].max_input_tokens`. Other agents read the target themselves, so they warn and ignore it. |
| `--json` | Write one JSON document to stdout (per-skill status, severity counts, tokens, USD, gate verdicts, resolved exit code); progress and notes go to stderr. Also suppresses the interactive project-name prompt. |
Expand Down
13 changes: 11 additions & 2 deletions examples/ci/github-actions-manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
fetch-depth: 0 # --changed-since needs the PR base and merge base
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
with:
python-version: "3.12"
Expand All @@ -26,14 +28,21 @@ jobs:
id: scan
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
PR_BASE_SHA: ${{ github.event.pull_request.base.sha }}
# Keep going on exit 2 (gate tripped) so the SARIF still uploads.
# PR_BASE_SHA is empty on push events; use --changed-files there if
# your workflow computes the diff itself.
run: |
extra=()
[ -n "$PR_BASE_SHA" ] && extra+=(--changed-since "$PR_BASE_SHA" --fail-scope changed)
secfoo run --agent api --target . --depth quick --json --rescan \
--project-name "$GITHUB_REPOSITORY" \
--skill sast --skill secret-scanning --fail-on high > secfoo-result.json || echo "code=$?" >> "$GITHUB_OUTPUT"
--skill sast --skill secret-scanning --fail-on high \
--baseline .secfoo/baseline.json "${extra[@]}" > secfoo-result.json \
|| echo "code=$?" >> "$GITHUB_OUTPUT"

- name: Export SARIF
run: secfoo findings export --format sarif -o secfoo.sarif
run: secfoo findings export --format sarif -o secfoo.sarif --baseline .secfoo/baseline.json

- uses: github/codeql-action/upload-sarif@5895b2c9be147bc7f1c75ce72c5cb577febb1907 # v4
with:
Expand Down
5 changes: 5 additions & 0 deletions examples/ci/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
fetch-depth: 0 # --changed-since needs the PR base and merge base

- uses: secfoo-com/secfoo@main
env:
Expand All @@ -30,6 +32,9 @@ jobs:
fail-on: high # exit 2 -> red check when a High+ finding appears
max-cost: "2.00" # exit 3 if the agent reports more than $2 of spend
exclude: vendor,fixtures
changed-only: true # PR runs review the diff; push/schedule stay full
baseline: .secfoo/baseline.json # commit this file after the first run
extra-args: "--fail-scope changed" # unchanged files stay visible, not gating

# Prefer Claude Code / Codex / Gemini as the reviewer? Install the CLI
# on the runner and point the action at it instead of `api`:
Expand Down
Loading