Harden untrusted-input handling, refactor check engine, and enforce golangci-lint#19
Merged
Conversation
Threat model: codeguard runs in CI on untrusted PRs, so repo config and scanned content are attacker-controllable. This wave closes a set of hardening gaps and clears the mechanical/security lint backlog. Hardening: - glob matcher no longer panics on malformed config patterns (regexp.Compile + treat-as-no-match) and memoizes compiled patterns via sync.Map (utils.go) - validate base_ref before git (reject leading '-', charset allowlist, --end-of-options) to block option injection (diff/changed_files/ diff_command/mcp_tools/ai-semantic) - git & govulncheck subprocesses use CommandContext with timeouts and bounded io.LimitReader output to prevent hangs and OOM - MCP HTTP server sets ReadHeaderTimeout (+Read/Write/Idle) against Slowloris - recover() around each check section so one panic can't abort the whole scan - fix close/cancel leaks (triage body, http_post close, mcp ctx cancel; httpretry now drains before close); GitHub comment client threads ctx and a client timeout - cap cache/config reads; tighten dir/file perms to 0750/0600 Tooling & quality: - wire golangci-lint (16-linter v2 config) into CI, non-blocking for now - %w error wrapping; sha1 -> sha256 for cache/identity keys; remove dead code, redundant conversions, and loop-var copies; prealloc where bounded - justified //nolint:gosec on intentional subprocess/file/jitter sites (production gosec findings now zero) Build, vet, gofmt clean; 430 tests pass. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…ilder, CLI cleanup Behavior-preserving refactors (output byte-for-byte identical, 430 tests pass): - runner/checks: replace the hardcoded section-dispatch if-ladder with a data-driven sectionRegistry ([]sectionDef) iterated by Build; all 8 sections covered, panic-recovery (safeRun) and ordering preserved (new registry.go) - checks/quality: add warnFinding() builder and migrate 21 hand-written FindingInput struct literals onto it, incl. the three per-language performance scanners and the three near-identical AI-quality emitters - cli: extract parseFlags helper and name exit-code constants (exitOK/exitError) in new exit.go; migrate the five commands.go handlers Note: the per-language performance scanners were found to use genuinely different strategies (Go AST / Python indent / TS brace-depth), so no shared line-scanner was forced. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…hing Lock in the H1/H2 hardening: - ValidateBaseRef rejects option-injection refs (leading '-') and out-of-charset values; accepts legitimate refs and the stdin sentinel - MatchPattern returns false (never panics) on malformed globs, matches valid glob semantics, and is idempotent across the sync.Map cache 476 tests pass (was 430). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
- fix remaining errcheck, govet-shadow, gocritic, unparam findings - scoped //nolint:contextcheck on the git helpers that use a contained timeout (deeper ctx threading tracked as a follow-up) - //nolint:prealloc on the genuinely unbounded finding accumulators - exclude revive's documentation/naming style rules (exported-doc, package-comments, unused-parameter); the correctness/safety linters gate CI. Documenting the public pkg/codeguard API is a tracked follow-up - exclude test fixtures from gosec/errcheck/prealloc/bodyclose - remove continue-on-error from the CI golangci-lint step: lint is now blocking. golangci-lint run reports 0 issues. Build/vet/gofmt clean; 476 tests pass. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
…forcement Note the data-driven sectionRegistry + safeRun panic recovery, the base_ref/ git subprocess hardening contract (and why the contextcheck nolints are deliberate), and that golangci-lint is now CI-blocking with revive style rules excluded. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The config is golangci-lint v2 schema, but action@v6 installs v1.64.8, which
rejects it ("additional properties 'version'/'settings' not allowed"). Bump to
@v8 (installs golangci-lint v2, runs on Node 24) and pin version v2.12.2 to
match local. `golangci-lint config verify` passes; run reports 0 issues.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A hardening, refactor, and quality pass over the codebase (branch scanned itself the whole way — build/vet/gofmt clean, 476 tests pass, golangci-lint 0 issues and now CI-blocking). Threat model throughout: codeguard runs in CI on untrusted PRs, so repo config and scanned content are attacker-controllable.
135 files changed, +1027/−617.
Hardening (H1–H10)
MatchPatternusesregexp.Compile+ async.Mapcache and returns false (never panics) on a malformed config glob (runner/support/utils.go).base_refoption-injection blocked — validated byValidateBaseRef(rejects leading-, charset allowlist, keeps thestdinsentinel) before reaching git, with--end-of-optionson git diff/show/worktree.exec.CommandContextwith contained timeouts and bounded (io.LimitReader) output — no more hangs or OOM on a huge diff.ReadHeaderTimeout(+Read/Write/Idle) against Slowloris.safeRun; a panic degrades that section to a warning instead of aborting the whole scan.http_postclose, MCP ctx cancel;httpretrynow drains before close; GitHub comment client threads ctx + a client timeout.Refactor (Q2/Q3/Q8) — behavior-preserving
runner/checks/registry.go); all 8 sections covered, ordering + panic-recovery preserved.warnFindingbuilder collapses 21 hand-writtenFindingInputstruct literals (incl. the three per-language performance scanners and AI-quality emitters). Note: the per-language scanners use genuinely different strategies (Go AST / Python indent / TS brace-depth), so no shared line-scanner was forced.parseFlagshelper + named exit-code constants.Quality & tooling (Q1/Q4/Q5/Q6/Q7)
go vetonly). Fixed a latent bug: the config had been on the v1issues.exclude-rulesschema, silently ignored under golangci-lint v2.%werror wrapping; sha1→sha256 for cache/identity keys; removed dead code, redundant conversions, loop-var copies; prealloc where bounded.gosecfindings driven to zero (justified//nolinton intentional subprocess/file/jitter sites).ValidateBaseRefand the panic-safeMatchPattern(430 → 476 tests).Config decisions worth a look
exported,package-comments,unused-parameter) are excluded — the correctness/safety linters gate CI.tests/is excluded from gosec/errcheck/prealloc/bodyclose (intentional fixtures).//nolint:contextcheckmarkers in the git layer are deliberate: the contained timeout is the chosen design; full caller-ctx threading is a tracked follow-up (don't remove the nolint without actually threading ctx).Knowledge captured
.claude/knowledge/architecture-boundaries.md— the section registry +safeRun, the git/base_refhardening contract (and why the contextcheck nolints are deliberate), and the golangci-lint enforcement + revive exclusions.Tracked follow-ups (intentionally not in this PR)
pkg/codeguardAPI (reviveexportedcurrently excluded rather than satisfied).ctxthreading through the git helpers (currently contained timeouts + scoped nolint).Verification
go build ./...✓ ·go vet ./...✓ ·gofmt -lclean ·go test ./...= 476 passed ·golangci-lint run= 0 issues.🤖 Generated with Claude Code