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
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Fuzz corpora are exact byte inputs (crash reproducers included): never
# let core.autocrlf rewrite their line endings on checkout.
fuzz/corpus/** -text
89 changes: 89 additions & 0 deletions .github/workflows/fuzz.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: Fuzz

# Coverage-guided fuzzing (#121) of the parsers that turn untrusted input --
# agent output, skill files, config.toml, uploaded documents, runtime
# sensor batches -- into exit codes and dashboard content. Every run first
# replays the seed corpus and the committed crash reproducers in
# fuzz/corpus/<harness>/ (deterministic, blocking). Then each harness fuzzes:
# 60 s and blocking nightly / on dispatch; 30 s and non-blocking on pull
# requests, so a long-standing crash found by chance never fails an unrelated
# PR -- its reproducer is still uploaded. This replaces parser-fuzz.yml.
#
# Reproduce a crash on any platform, without Atheris:
# python fuzz/fuzz_<harness>.py crash-<sha1>
# then add it to fuzz/corpus/<harness>/ with the fix (any file name; renaming
# it crash-<what>.<ext> helps), so tests/test_property_fuzz_targets.py and the
# replay step below run it on every build.

on:
pull_request:
paths:
- "src/secfoo/report/**"
- "src/secfoo/skills/**"
- "src/secfoo/settings.py"
- "src/secfoo/docext.py"
- "src/secfoo/runtime/**"
- "src/secfoo/web/finding_filters.py"
- "fuzz/**"
- ".github/workflows/fuzz.yml"
schedule:
- cron: "17 3 * * *" # nightly, so slow-to-find crashes surface between PRs
workflow_dispatch:

permissions:
contents: read

concurrency:
group: fuzz-${{ github.ref }}
cancel-in-progress: true

jobs:
fuzz:
name: Fuzz (${{ matrix.harness }})
runs-on: ubuntu-latest
timeout-minutes: 15
strategy:
fail-fast: false
matrix:
harness: [parsers, sarif_export, skill_loader, config, docext, runtime_ingest]
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false # PR code is installed and run below

- uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0
with:
python-version: "3.12"

- name: Install package + Atheris
run: pip install -e ".[dev]" "atheris==3.1.0"

- name: Replay the seed corpus and crash reproducers
# -runs=0: run every committed input once and stop. Blocking, and
# deterministic: a fixed crash that comes back fails here.
run: python fuzz/fuzz_${{ matrix.harness }}.py fuzz/corpus/${{ matrix.harness }} -runs=0

- name: Fuzz
id: fuzz
# New inputs libFuzzer finds go to work/ (the first folder), never
# into the committed seed corpus. -timeout catches hangs (a
# pathological PDF), -rss_limit_mb decompression bombs.
continue-on-error: ${{ github.event_name == 'pull_request' }}
run: |
mkdir -p work artifacts
python fuzz/fuzz_${{ matrix.harness }}.py work fuzz/corpus/${{ matrix.harness }} \
-max_total_time=${{ github.event_name == 'pull_request' && 30 || 60 }} \
-timeout=30 -rss_limit_mb=2560 -artifact_prefix=artifacts/ -print_final_stats=1

- name: Report a non-blocking crash
if: ${{ !cancelled() && steps.fuzz.outcome == 'failure' && github.event_name == 'pull_request' }}
run: |
echo "::warning::Fuzzing found a crash in the ${{ matrix.harness }} harness. It may predate this PR; the reproducer is attached as an artifact."

- name: Upload the crash reproducer
if: ${{ !cancelled() && steps.fuzz.outcome == 'failure' }}
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: fuzz-crash-${{ matrix.harness }}
path: artifacts/
if-no-files-found: ignore
20 changes: 0 additions & 20 deletions .github/workflows/parser-fuzz.yml

This file was deleted.

41 changes: 34 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,45 @@ ruff check .

CI runs both on every push and PR, on Python 3.10 and 3.12.

The text parsers (report severities, skill frontmatter, severity filters)
also have property-based tests in `tests/test_property_parsers.py`
(Hypothesis, run as part of `pytest tests`) and a coverage-guided fuzz
harness in `fuzz/fuzz_parsers.py`:
### Fuzzing

Everything that parses untrusted input has a coverage-guided fuzz harness
in `fuzz/`:

| Harness | Target | Contract |
| --- | --- | --- |
| `fuzz_parsers.py` | report severities and ratings, documentation-coverage tables, skill frontmatter, severity filters | malformed reports raise only `MalformedReportError`; the coverage parser never raises |
| `fuzz_sarif_export.py` | `findings export` (SARIF and JSONL) | valid SARIF 2.1.0; JSONL round-trips |
| `fuzz_skill_loader.py` | skill files (`--skill-file`, folders, URLs) | a `Skill` or `SkillLoadError` |
| `fuzz_config.py` | `~/.secfoo/config.toml` | correctly typed config or `ConfigError` |
| `fuzz_docext.py` | uploaded PDF / DOCX / PPTX | text or `""`, within a time limit |
| `fuzz_runtime_ingest.py` | runtime sensor NDJSON batches | every line stored or skipped, never a 500 |

The `Fuzz` workflow first replays every harness's seed corpus and committed
crash reproducers (blocking), then fuzzes each harness: for 30 seconds,
non-blocking, on PRs that touch the parsed code, and for 60 seconds,
blocking, nightly. A crash found on a PR is attached to the run as an
artifact. To run one locally (Linux or macOS):

```sh
pip install atheris
python fuzz/fuzz_parsers.py -max_total_time=60
mkdir -p work
python fuzz/fuzz_config.py work fuzz/corpus/config -max_total_time=60
```

If you change one of those parsers, run the harness for a minute before
opening the PR.
Each harness's `check` function also runs on every platform in
`pytest tests/test_property_fuzz_targets.py` (Hypothesis), which replays
every file in `fuzz/corpus/`. Without Atheris, a harness replays the files
or folders you pass, so a CI crash reproduces anywhere, Windows included:

```sh
python fuzz/fuzz_config.py crash-<sha1>
```

When you fix a crash, add its reproducer to `fuzz/corpus/<harness>/` as
`crash-<what>.<ext>` (or in `fuzz/make_seed_corpus.py`, which writes the
seed corpora) so it's replayed on every run. If you change one of these
parsers, run its harness for a minute before opening the PR.

## Adding or changing a skill

Expand Down
52 changes: 52 additions & 0 deletions fuzz/_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""Shared runner for the harnesses in this folder.

Each harness defines `check(data: bytes) -> None`: it feeds the bytes to one
parser and asserts that parser's contract (a result or its own clean error,
never any other exception). The same `check` runs three ways:

python fuzz/fuzz_config.py WORK_DIR fuzz/corpus/config -max_total_time=60
coverage-guided fuzzing with Atheris (Linux/macOS, `pip install atheris`)

python fuzz/fuzz_config.py crash-<sha1> [more files or folders]
without Atheris installed: replay inputs through `check` -- reproduce a
CI crash on any platform, Windows included

pytest tests/test_property_fuzz_targets.py
Hypothesis drives `check`, and every corpus file is replayed, so the
invariants and the reproducers of fixed crashes run on every platform
"""

from __future__ import annotations

import sys
from collections.abc import Callable
from pathlib import Path

CORPUS_ROOT = Path(__file__).resolve().parent / "corpus"


def _replay(check: Callable[[bytes], None], targets: list[str]) -> int:
paths: list[Path] = []
for target in targets:
path = Path(target)
paths.extend(sorted(p for p in path.rglob("*") if p.is_file()) if path.is_dir() else [path])
for path in paths:
check(path.read_bytes()) # any exception propagates, with its traceback, like a fuzzer crash
print(f"replayed {len(paths)} input(s) without a crash")
return 0


def main(check: Callable[[bytes], None]) -> None:
try:
import atheris
except ImportError:
targets = [arg for arg in sys.argv[1:] if not arg.startswith("-")]
if not targets:
sys.exit("atheris is not installed: pass input files or folders to replay them, "
"or `pip install atheris` (Linux/macOS) to fuzz")
sys.exit(_replay(check, targets))
# Instrument after the harness imported its targets: everything loaded so
# far (secfoo and the libraries it parses with) gets coverage feedback.
atheris.instrument_all()
atheris.Setup(sys.argv, check)
atheris.Fuzz()
1 change: 1 addition & 0 deletions fuzz/corpus/config/crash-defaults-not-a-table.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
defaults = 1
2 changes: 2 additions & 0 deletions fuzz/corpus/config/crash-defaults-timeout-string.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[defaults]
timeout = 'soon'
2 changes: 2 additions & 0 deletions fuzz/corpus/config/crash-invalid-utf8.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[defaults]
agent = "ÿþ"
4 changes: 4 additions & 0 deletions fuzz/corpus/config/crash-mcp-args-number.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[[mcp_servers]]
name = 'a'
command = 'npx'
args = [1, 2]
3 changes: 3 additions & 0 deletions fuzz/corpus/config/crash-mcp-command-number.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[mcp_servers]]
name = 'a'
command = 5
4 changes: 4 additions & 0 deletions fuzz/corpus/config/crash-mcp-env-table.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[[mcp_servers]]
name = 'a'
command = 'x'
env = { A = { B = 1 } }
3 changes: 3 additions & 0 deletions fuzz/corpus/config/crash-mcp-name-list.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[mcp_servers]]
name = [1]
command = 'x'
1 change: 1 addition & 0 deletions fuzz/corpus/config/crash-mcp-servers-string.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mcp_servers = 'x'
3 changes: 3 additions & 0 deletions fuzz/corpus/config/crash-mcp-servers-table.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[mcp_servers]
name = 'a'
command = 'x'
29 changes: 29 additions & 0 deletions fuzz/corpus/config/documented.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[defaults]
agent = "claude"
depth = "quick"
exclude = ["vendor/", "some-cloned-repo/"]
fail_on = "high"
max_cost_usd = 2.00

[agents.claude]
model = "claude-sonnet-4-6"

[agents.api]
max_input_tokens = 150000

[[mcp_servers]]
name = "Atlassian-Rovo-MCP"
command = "npx"
args = ["-y", "mcp-remote@latest", "https://mcp.atlassian.com/v1/mcp/authv2"]

[[mcp_servers]]
name = "wiki"
url = "https://wiki.internal/mcp"
transport = "http"
headers = { Authorization = "Bearer x" }

[skills]
paths = ["~/team-skills"]

[memory]
enabled = false
Empty file added fuzz/corpus/config/empty.toml
Empty file.
Binary file added fuzz/corpus/docext/blank.pdf.bin
Binary file not shown.
Binary file added fuzz/corpus/docext/report.docx.bin
Binary file not shown.
Binary file added fuzz/corpus/docext/summary.pptx.bin
Binary file not shown.
Binary file added fuzz/corpus/docext/text.pdf.bin
Binary file not shown.
Loading
Loading