← Back to Pull requests

Security: grant source-fact sort is linearithmic on hostile order #1164

Closed
unknown closed this pull request without merging on Oct 03, 2026 23:26 UTC
View on GitHub
unknown unknown commented on Oct 03, 2026 19:38 UTC
Author

Summary

C1Z-SEC-007. The grant digest build and repair paths compute every grant row's content hash from its raw stored value:

1. scanGrantContentFactsRawBytes collects one grantSourceFact per sources map entry.
2. sortGrantSourceFacts sorts and dedupes them.

The sort was an insertion sort, and both the list's length and its order come from the stored value. A descending 2^16-entry sources map took 8.9s for one row, with no cancellation check inside the row. A larger map grew quadratically from there. A hostile .c1z reaches these paths through EndSync's digest build and digest repair on the shared workers.

Change

sortGrantSourceFacts now uses slices.SortStableFunc:

  • O(n log n) on any input order. Descending 2^16 entries: 5.6ms (was 8.9s). 2^20: 97ms. 2^22: 0.4s.

  • No allocation per call. This runs on every multi-source grant row.

  • Stable. Collapsing each run of equal keys to its last element is still proto's "last write wins", so the hash ABI is unchanged.
  • Why no cap on the sources count

    An earlier revision also rejected values with more than 2^16 sources. I dropped it:

  • The sort fix alone removes the amplification. The cost now tracks the value's size, as decoding the same value does. A 2^22-entry map is at least about 8MB of stored value and sorts in 0.4s.

  • A cap breaks the raw/decoded parity contract. FuzzGrantContentHashRawMatchesDecoded says the raw scan accepts every value proto.Unmarshal accepts and hashes it like grantContentHashForRecord. A cap would make the raw path reject valid grants that GrantContentHash hashes fine.

  • It would bound one consumer, not the process. Every other unmarshal of the same row allocates a Go map entry per source. A per-record size limit, if wanted, belongs at import.
  • Tests

  • TestSecurity_SortGrantSourceFactsDescendingInput sorts 2^16 descending facts and must finish under 2s. With the insertion sort restored it fails at 8.98s; with this change it takes about 10ms.

  • Existing tests cover correctness of the sort:

  • - TestGrantDigestSpliceMatchesEncode compares raw-scan and decoded-record hashes, including unsorted multi-source maps.
    - TestGrantContentHashRawMatchesDecodedOnDuplicateWireFields covers last-write-wins on duplicate wire keys.
    - The FuzzGrantContentHashRawMatchesDecoded seed corpus.
  • golangci-lint run ./...: 0 issues. go test -race on pkg/dotc1z/engine/pebble passes.

  • go test -short -tags=baton_lambda_support ./...: everything passes except TestC1ZConcurrentClose. It failed once under full-suite load and passed 5 runs in isolation. It is a SQLite concurrent-close test this change does not touch.
  • Diff: 335 lines (+251 / -84) across 4 files Reviewer: pquerna Review duration: 228m
    All Vouch review confidence checks passed
    Residual risk 0.27 = Change Risk (0.65) × (1 − Review Confidence (0.58)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.65 · 335 lines diff across 4 files
    0.65
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    57%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.348
    Shallow Review Depth shallow_review_depth
    0.289
    Sensitive Paths sensitive_paths
    0.065
    Real PR Conversation Timeline
    1 comments
    "Review active on #1164"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 58.0% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 65.4% (HIGH) · Higher = larger blast radius
    65.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 58.0% · Lower = rushed or shallow review
    58.0%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.5% · Safe to merge
    27.5%