Skip to content

Commit af1bcfa

Browse files
committed
docs(security): expand CODE_ANALYSIS with Rust checks, dependency policy, and SLSA L1 release steps
1 parent e6dc88a commit af1bcfa

1 file changed

Lines changed: 197 additions & 0 deletions

File tree

CODE_ANALYSIS.md

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
# Code Analysis & Hardening
2+
3+
This document defines the *required* static checks, tests, and release-time integrity steps for this repository. It is Rust-specific and aligns with our SLSA Level 1 baseline.
4+
5+
## Goals
6+
- Catch correctness & security issues before merge.
7+
- Keep dependencies healthy and policy-compliant.
8+
- Produce traceable releases (SBOM + provenance) and signed artifacts.
9+
10+
## 1. Per-PR Required Checks
11+
12+
Run locally and in CI; PRs must pass all of these.
13+
14+
### 1.1 Formatting
15+
```bash
16+
cargo fmt --all -- --check
17+
```
18+
19+
### 1.2 Lints (deny warnings)
20+
```bash
21+
cargo clippy --all-targets --all-features -- -D warnings
22+
```
23+
***Policy***: Prefer `clippy::pedantic` where practical; allow per-line `#[allow]` with justification.
24+
25+
### 1.3 Tests + minimal coverage
26+
```bash
27+
cargo test --all --all-features
28+
```
29+
If using coverage (recommended):
30+
```bash
31+
# Linux example with llvm-cov
32+
cargo llvm-cov --workspace --all-features --lcov --output-path target/lcov.info
33+
# Enforce a floor (tune threshold):
34+
cargo llvm-cov --fail-under-lines 70
35+
```
36+
37+
### 1.4 Dependency Policy: `cargo-deny`
38+
39+
```bash
40+
cargo deny check licenses bans advisories sources
41+
```
42+
* licenses: must be OSI-approved or whitelisted.
43+
* bans: forbid duplicated critical crates or disallowed crates.
44+
* advisories: no unresolved vulnerability advisories.
45+
* sources: only allowed registries (e.g., crates.io) and pinned git sources.
46+
47+
### 1.5 Vulnerability Scan: `cargo-audit`
48+
49+
```bash
50+
cargo audit
51+
```
52+
Fail on known vulnerabilities unless an ***expiring*** ignore is documented.
53+
54+
### 1.6 Semgrep (optional but recommended)
55+
56+
```bash
57+
semgrep scan --config p/r2c-security-audit --error
58+
```
59+
60+
Pin rule packs in CI for determinism; document local overrides.
61+
62+
### 1.7 CodeQL (recommended)
63+
64+
Run GitHub Advanced Security CodeQL on PRs and weekly; treat new alerts as blockers.
65+
66+
### 1.8 Unsafe Code Policy
67+
* Crates set `#![forbid(unsafe_code)]` by default.
68+
* If unsafe is necessary, isolate in a small module with:
69+
* Extensive documentation of invariants
70+
* Unit tests and `cargo miri test` where applicable.
71+
72+
---
73+
74+
## 2. Hardening (Continuous)
75+
76+
### 2.1 Miri (UB checks)
77+
78+
```bash
79+
cargo +nightly miri setup
80+
cargo +nightly miri test
81+
```
82+
83+
### 2.2 Sanitizers (where supported)
84+
85+
```bash
86+
RUSTFLAGS="-Z sanitizer=address" \
87+
RUSTDOCFLAGS="-Z sanitizer=address" \
88+
cargo +nightly test -Zbuild-std --target x86_64-unknown-linux-gnu
89+
```
90+
91+
### 2.3 Fuzzing (critical parsers/logic)
92+
93+
```bash
94+
cargo fuzz run <target> -- -runs=0
95+
```
96+
97+
---
98+
99+
## 3. Release-Time Integrity (SLSA L1)
100+
101+
On tagged releases:
102+
103+
### 3.1 SBOM (CycloneDX)
104+
105+
```bash
106+
cargo install cyclonedx-bom --locked
107+
cyclonedx-bom -o target/sbom.json
108+
```
109+
110+
### 3.2 Provenance (documented, repeatable build)
111+
* Build with a pinned toolchain (`rust-toolchain.toml`) and reproducible flags where possible:
112+
113+
```bash
114+
export SOURCE_DATE_EPOCH="$(git log -1 --pretty=%ct)"
115+
RUSTFLAGS="-C link-arg=-Wl,--build-id=none -C debuginfo=0"
116+
cargo build --release --locked
117+
```
118+
119+
* Capture build metadata (builder, commit, inputs, outputs) into `docs/PROVENANCE.md` and attach to the release.
120+
121+
### 3.3 Signatures & Checksums
122+
123+
```bash
124+
shasum -a 256 target/release/<bin> > target/release/<bin>.sha256
125+
gpg --detach-sign --armor target/release/<bin>
126+
```
127+
(or Sigstore/cosign if you prefer keyless signing).
128+
129+
---
130+
131+
## 4. Local Developer Shortcuts
132+
133+
Common `make` targets (optional but encouraged):
134+
135+
```Makefile
136+
fmt: ; cargo fmt --all
137+
lint: ; cargo clippy --all-targets --all-features -- -D warnings
138+
test: ; cargo test --all --all-features
139+
audit: ; cargo deny check licenses bans advisories sources && cargo audit
140+
miri: ; cargo +nightly miri test
141+
coverage: ; cargo llvm-cov --workspace --all-features --fail-under-lines 70
142+
sbom: ; cyclonedx-bom -o target/sbom.json
143+
release-prep: fmt lint test audit sbom
144+
```
145+
For offline checks:
146+
```bash
147+
make fmt lint test
148+
cargo deny check --offline
149+
cargo audit --db ~/.cargo/advisory-db # if you maintain a local mirror
150+
```
151+
152+
---
153+
154+
## 5. Exceptions & Waivers
155+
* Allowed only with:
156+
* Link to issue explaining risk and timeline
157+
* Scoped `#[allow(...)]` or tool config ignore with **expiry date**
158+
* Security exceptions require maintainer approval.
159+
160+
## 6. Example CI Outline (GitHub Actions)
161+
(YAML abbreviated; adapt to your repo)
162+
```yaml
163+
name: CI
164+
on:
165+
pull_request:
166+
push:
167+
branches: [main]
168+
169+
jobs:
170+
checks:
171+
runs-on: ubuntu-latest
172+
permissions:
173+
contents: read
174+
security-events: write # for CodeQL (if enabled)
175+
steps:
176+
- uses: actions/checkout@v4
177+
- uses: dtolnay/rust-toolchain@stable
178+
with: { components: rustfmt, clippy }
179+
- run: cargo fmt --all -- --check
180+
- run: cargo clippy --all-targets --all-features -- -D warnings
181+
- run: cargo test --all --all-features --locked
182+
- uses: EmbarkStudios/cargo-deny-action@v2
183+
- uses: actions-rs/audit-check@v1
184+
with: { token: ${{ secrets.GITHUB_TOKEN }} }
185+
# Optional:
186+
# - uses: github/codeql-action/init@v3
187+
# - uses: github/codeql-action/analyze@v3
188+
# - run: semgrep scan --config p/r2c-security-audit --error
189+
```
190+
191+
## 7. Artifacts to Keep
192+
193+
* `target/lcov.info` (coverage)
194+
* `target/sbom.json` (SBOM)
195+
* `docs/PROVENANCE.md` capturing build inputs/outputs for each release
196+
* Release checksums and signatures
197+

0 commit comments

Comments
 (0)