Skip to content
Merged
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
23 changes: 22 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ inputs:
result-file:
description: "Where to write the machine-readable run summary (the `--json` document)."
default: "secfoo-result.json"
changed-only:
description: >-
On pull_request events, review only files changed since the PR base SHA
(--changed-since) and scope the fail gate to changed files (--fail-scope changed).
Requires checking out full history (fetch-depth: 0).
default: "false"
baseline:
description: >-
Path to a JSON baseline file (e.g. .secfoo/baseline.json). Active entries
are excluded from --fail-on and tagged baselineState: unchanged in SARIF.
default: ""

outputs:
exit-code:
Expand Down Expand Up @@ -160,6 +171,9 @@ runs:
IN_EXCLUDE: ${{ inputs.exclude }}
IN_EXTRA_ARGS: ${{ inputs.extra-args }}
IN_RESULT_FILE: ${{ inputs.result-file }}
IN_CHANGED_ONLY: ${{ inputs.changed-only }}
IN_BASELINE: ${{ inputs.baseline }}
PR_BASE_SHA: ${{ github.event.pull_request.base.sha }}
# Non-interactive: no project-name prompt, no "scan again?" prompt.
SECFOO_PROJECT_NAME: ${{ github.repository }}
run: |
Expand All @@ -172,6 +186,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" --fail-scope changed)
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 +258,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=(findings export --format sarif --output "$SARIF_FILE")
[ -n "$IN_BASELINE" ] && export_args+=(--baseline "$IN_BASELINE")
secfoo "${export_args[@]}"
echo "sarif-file=$SARIF_FILE" >> "$GITHUB_OUTPUT"

- name: Upload SARIF to code scanning
Expand Down
37 changes: 35 additions & 2 deletions docs/CI.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ and, without the Action, [examples/ci/github-actions-manual.yml](../examples/ci/
| `upload-sarif` | `true` | Upload to code scanning. |
| `sarif-category` | `secfoo` | Code scanning category, so other tools' results are kept. |
| `result-file` | `secfoo-result.json` | Where the `--json` document is written. |
| `changed-only` | `false` | On `pull_request` events, review only files changed since `github.event.pull_request.base.sha` (`--changed-since` + `--fail-scope changed`). Requires `fetch-depth: 0` on checkout. |
| `baseline` | | Baseline JSON file (e.g. `.secfoo/baseline.json`) to exclude known findings from `--fail-on` and set SARIF `baselineState`. |

**Outputs**: `exit-code`, `critical`, `high`, `medium`, `low`, `cost-usd`,
`result-file`, `sarif-file`. For example, to comment on a PR only when
Expand Down Expand Up @@ -143,6 +145,29 @@ GitHub Advanced Security on private ones). Without it, set
fail-on: high
```

## Adopting secfoo in an existing repo

Two features keep CI green without turning off the gate on legacy codebases:

1. **Record a baseline once** after an initial full scan, commit it, and tighten later:

```bash
secfoo run --skill sast --skill secret-scanning --target .
secfoo findings baseline write .secfoo/baseline.json \
--reason "Pre-adoption backlog" --expires 2026-12-31
```

On later runs, pass `--baseline .secfoo/baseline.json` (or `baseline: .secfoo/baseline.json` in the Action). Active entries are excluded from `--fail-on`, expired entries start counting again and are listed in the run summary, SARIF exports mark baselined results `baselineState: "unchanged"` (and new findings `"new"`), JSONL exports include `"baseline": {reason, expires}`, and the dashboard marks them **Accepted in baseline**. Periodically run `secfoo findings baseline check .secfoo/baseline.json` to spot closed findings that can be pruned.

2. **Focus PRs on the diff** with `--changed-since <ref>` (or `--changed-files <path>` for precomputed lists) and `--fail-scope changed`:

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

Only changed paths (via `git diff --name-only --merge-base`) are targeted for review, out-of-scope findings keep their open/closed state, and any context finding in an unchanged file (`scope: context`) is ignored by `--fail-scope changed`. On GitHub Actions, set `changed-only: "true"` with `fetch-depth: 0`; on GitLab merge requests, use `CI_MERGE_REQUEST_DIFF_BASE_SHA`.

## Pre-commit

secfoo ships [`.pre-commit-hooks.yaml`](../.pre-commit-hooks.yaml) with two hooks:
Expand Down Expand Up @@ -182,13 +207,21 @@ secfoo:
- pip install --quiet secfoo
script:
- |
diff_args=""
if [ -n "${CI_MERGE_REQUEST_DIFF_BASE_SHA:-}" ]; then
diff_args="--changed-since $CI_MERGE_REQUEST_DIFF_BASE_SHA --fail-scope changed"
fi
baseline_args=""
if [ -f .secfoo/baseline.json ]; then
baseline_args="--baseline .secfoo/baseline.json"
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=$?
--fail-on high --max-cost 2.00 $diff_args $baseline_args > 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
- secfoo findings export --format sarif ${baseline_args:-} -o secfoo.sarif
- exit ${code:-0}
artifacts:
when: always
Expand Down
7 changes: 7 additions & 0 deletions docs/REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,9 @@ 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 --format sarif --baseline .secfoo/baseline.json -o findings.sarif
secfoo findings baseline write .secfoo/baseline.json --reason "Pre-adoption" --expires 2026-12-31
secfoo findings baseline check .secfoo/baseline.json # report closed/expired entries to prune
```


Expand Down Expand Up @@ -851,6 +854,10 @@ 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-scope all\|changed` | Controls `--fail-on` filtering for diff-scoped runs: `all` (default) or `changed` (excludes context findings tagged `scope: context`). |
| `--changed-since <ref>` | Scope review to paths changed since git merge-base with `<ref>`, leaving out-of-scope findings open. Errors before runs if `<ref>` is missing or target is not a git repo. |
| `--changed-files <path>` | Newline-separated list of changed paths (alternative to `--changed-since`). |
| `--baseline <path>` | Baseline JSON file (`secfoo findings baseline write`); active matches do not trip `--fail-on`, expired entries are listed and counted again, and dashboard rows show **Accepted in baseline**. |
| `--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
18 changes: 16 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
- uses: actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # v5.6.0
with:
python-version: "3.12"
Expand All @@ -28,12 +30,24 @@ jobs:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
# Keep going on exit 2 (gate tripped) so the SARIF still uploads.
run: |
diff_args=()
if [ -n "${{ github.event.pull_request.base.sha }}" ]; then
diff_args+=(--changed-since "${{ github.event.pull_request.base.sha }}" --fail-scope changed)
fi
baseline_args=()
if [ -f .secfoo/baseline.json ]; then
baseline_args+=(--baseline .secfoo/baseline.json)
fi
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 \
"${diff_args[@]}" "${baseline_args[@]}" > secfoo-result.json || echo "code=$?" >> "$GITHUB_OUTPUT"

- name: Export SARIF
run: secfoo findings export --format sarif -o secfoo.sarif
run: |
export_args=(findings export --format sarif -o secfoo.sarif)
[ -f .secfoo/baseline.json ] && export_args+=(--baseline .secfoo/baseline.json)
secfoo "${export_args[@]}"

- uses: github/codeql-action/upload-sarif@5895b2c9be147bc7f1c75ce72c5cb577febb1907 # v4
with:
Expand Down
14 changes: 11 additions & 3 deletions examples/ci/gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,22 @@ secfoo:
script:
# Exit codes: 0 clean, 1 a run failed, 2 the --fail-on gate tripped, 3 the --max-cost gate tripped.
- |
diff_args=""
if [ -n "${CI_MERGE_REQUEST_DIFF_BASE_SHA:-}" ]; then
diff_args="--changed-since $CI_MERGE_REQUEST_DIFF_BASE_SHA --fail-scope changed"
fi
baseline_args=""
if [ -f .secfoo/baseline.json ]; then
baseline_args="--baseline .secfoo/baseline.json"
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=$?
--fail-on high --max-cost 2.00 $diff_args $baseline_args > 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
- secfoo findings export -o secfoo-findings.jsonl
- secfoo findings export --format sarif ${baseline_args:-} -o secfoo.sarif
- secfoo findings export ${baseline_args:-} -o secfoo-findings.jsonl
- exit ${code:-0}
artifacts:
when: always
Expand Down
Loading
Loading