← Back to Pull requests

Security: duplicate-identity grant fold is linear; manifest sync_runs decode is bounded #1165

Closed
unknown closed this pull request without merging on Oct 04, 2026 00:20 UTC
View on GitHub
unknown unknown commented on Oct 03, 2026 23:40 UTC
Author

Summary

C1Z-SEC-008, two findings on .c1z import:

1. Duplicate-identity grant fold was quadratic. mergeDuplicateGrantValues folds a group of grant rows that share one structural identity. It runs in the id-index migration and in the bulk import's resolveDuplicateGrants. It merged pairwise, rebuilding the annotation union (new seen-map and full re-sort) and re-cloning the sources map at every step. A v1 c1z can hold any number of rows with distinct external ids that fold to one identity, so one group of 8192 rows took 13.5s inside a single uninterruptible call.
2. The manifest header's sync_runs decode had no bound of its own. Only the 16 MiB manifest cap limited it. Decoding allocates a message per run and a map entry per stats key, so a header read could cost hundreds of MiB of heap. Header reads happen on every open, through engine dispatch.

Changes

Duplicate-identity fold: one pass, same bytes

  • grantRecordFold applies the existing per-field rules one row at a time: identity winner, needs_expansion OR, annotation union, sources merge by key, expansion id union with Shallow AND. Each row costs time proportional to its own size, so a group is linear in its total size (8192 rows: 0.07s).

  • Same bytes as the pairwise fold. As before, a field that only one row carries keeps that row's value as stored, and the dedupe-and-sort union runs only once a second row contributes. Artifacts merged by earlier SDK versions and by this one are therefore identical.

  • The pairwise helpers are gone; recordIdentityInfoWins and minNonEmptyString remain the shared rules.
  • Manifest sync_runs: bounded decode, never rejected

  • A header read decodes the projection only while its encoded size stays within maxManifestSyncRunsBytes (1 MiB). Past that it drops the whole projection.

  • It drops the projection rather than failing because the projection is advisory, and both readers already handle its absence:

  • - selectSourceSyncFromManifest falls back to unpacking the payload.
    - c1zSyncIDBestEffort is used only for logs.
  • Real projections are tiny. A v3 Pebble c1z holds one sync: StartNewSync wipes the keyspace, and the retention options apply only to SQLite. So a written projection is one run of roughly 0.1–6 KB (about 2 KB for a typical connector with stats), and the 1 MiB budget is only reached by crafted headers. Reading such a file without its projection is safer than refusing to open it.

  • Dropping the whole projection rather than truncating it keeps the compactor from choosing the "latest" sync out of a partial list.

  • The bound is on bytes, not run count, because one run's SyncStatsRecord maps can be as large as millions of runs.
  • Tests

  • TestMergeDuplicateGrantValuesMatchesPairwiseFold is a differential test. It runs 2000 seeded random groups of 2–5 rows covering single-contributor fields, duplicate annotations, colliding sources, empty expansion ids and discovered_at ties. Each group's merged bytes must equal those of the previous pairwise fold, which lives in the test file as the reference. Canonicalizing a single contributor (the earlier revision of this PR) fails it at group 1.

  • TestSecurity_MergeDuplicateGrantValuesLargeGroup folds an 8192-row group with a distinct annotation and source per row, under a 5s bound, and checks that reversing the input order gives the same bytes. The pairwise fold takes 16.3s on the same input.

  • TestManifestSyncRunsProjectionBudget writes envelopes with WriteEnvelope and reads them back with ReadManifestHeader:

  • - 2 runs, and exactly as many runs as fit the budget, read back whole;
    - one run more reads back with no error and no projection, other header fields intact;
    - with the bound removed, the over-budget case decodes every run and fails.
  • Existing tests also pass: TestMergeDuplicateGrantValuesFoldOrderIndependent and the migration and bulk-import suites.

  • golangci-lint run ./...: 0 issues. go test -short -tags=baton_lambda_support ./...: passes. go test -race passes on pkg/dotc1z/engine/pebble and pkg/dotc1z/format/v3.
  • Diff: 513 lines (+384 / -129) across 6 files Reviewer: pquerna Review duration: 40m
    All Vouch review confidence checks passed
    Residual risk 0.32 = Change Risk (0.71) × (1 − Review Confidence (0.54)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.71 · 513 lines diff across 6 files
    0.71
    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%
    54%
    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.373
    Shallow Review Depth shallow_review_depth
    0.289
    Sensitive Paths sensitive_paths
    0.071
    Real PR Conversation Timeline
    1 comments
    "Review active on #1165"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 54.2% 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: 70.9% (HIGH) · Higher = larger blast radius
    70.9%
    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: 54.2% · Lower = rushed or shallow review
    54.2%
    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: 32.4% · Safe to merge
    32.4%