← Back to Pull requests

Security: page tokens come only from iterator keys (C1Z-SEC-010) #1169

Closed
unknown closed this pull request without merging on Oct 06, 2026 13:17 UTC
View on GitHub
unknown unknown commented on Oct 05, 2026 14:10 UTC
Author

Summary

C1Z-SEC-010. The Pebble paginators mint each next-page cursor from the iterator key of the last row they return, and the next page starts strictly after it (rangeAfter appends 0x00). Five list RPCs overrode that cursor whenever they filled a page mid-scan, rebuilding it from the last record's value fields instead:

  • ListResources (primary keyspace and by_parent index)

  • ListGrantsForEntitlement (reader v2 and v3)

  • ListGrantsForEntitlements (reader v2 and v3)
  • In a hostile .c1z a record's value need not match its key. A row whose value names an id that sorts before its key, landing at a page boundary, mints a cursor that sorts before rows already served. The next page re-serves them, forever. Every exhaustive pager over a foreign file loops and grows until OOM: the syncer's external-resource ingestion, baton resources/csv/diff, and the explorer.

    Change

    The RPCs no longer mint cursors; they return the engine's.

  • Resource-type filters are key prefixes. The resource type leads both the primary key and the by_parent index key's child component, so ListResources with ResourceTypeId bounds the iterator to that type's prefix instead of scanning every resource and discarding other types. Each listing reads only rows of the requested type.

  • Other filters run inside the iterator. iteratePrimaryPageWithKey takes a keep func(T) bool (nil keeps every record). A page holds limit kept records, and its cursor is the iterator key of the last one. The grant RPCs pass their principal-type filter as keep. PaginateGrantsByPrincipalResourceType already filtered this way.

  • The RPCs become a single call that returns the engine's next cursor. Gone: cursorFor, the refill loop, brokeEarly, and the 4× over-fetch heuristic.

  • One implementation per grant RPC. The v2 and v3 ListGrantsForEntitlement(s) bodies were line-for-line copies. Each pair now shares listGrantsForEntitlement / listGrantsForEntitlements, which return []*v3.GrantRecord; v2 converts the result.

  • - The batch RPC requests exactly as many rows as are left, so a full page always ends on the engine page's last row, and the engine's cursor already marks it.
    - When an entitlement is exhausted on a full page, the existing next-entitlement branch runs, so the next call no longer starts with an empty page.
  • One grant iterator. iterateGrantPrimaryPage was a copy of the generic iterator for GrantRecord; it now uses the generic one.

  • No API change. PaginateResources, PaginateResourcesByParent and PaginateGrantsByEntitlement keep their signatures and wrap unexported versions that take the type filter or keep.
  • With this, pagination terminates for any stored bytes. Pebble pages start strictly after the last returned key. SQLite tokens are lastRowid+1, minted only when a later row exists. The earlier revision's next == pageToken guards in 11 caller loops (syncer, explorer, cmd/baton) are therefore removed. They checked the engine's contract from outside, caught only an exact repeat (not an A→B→A cycle), and covered some loops but not others.

    Tests

    TestSecurity_PageTokensAdvancePastHostileRows plants, in each keyspace, a row whose value names an id sorting before every key, as the last row of the first page. It then pages exhaustively and requires every row exactly once:

  • ListResources, by resource type, by parent, and by parent and type;

  • ListGrantsForEntitlement, and filtered by principal resource type;

  • ListGrantsForEntitlements.
  • On main, every subtest re-serves the first page until the page bound (r1 r2 r0 r1 r2 r0 …).

    TestListResourcesTypeFilterPagination pages ListResources by type, with and without a parent, over user/us/group children of one parent. It must return exactly the requested type, which covers the scan prefixes' trailing separator (us is a name prefix of user). It replaces TestPostFilterPaginationDoesNotSkip.

    The grant post-filter no-skip tests, pagination tests, v3 record-preservation tests and cross-engine parity test pass unchanged.

  • golangci-lint v2.13.2: 0 issues.

  • go test -short -tags=baton_lambda_support ./...: passes.

  • go test -race ./pkg/dotc1z/engine/pebble: passes.
  • Diff: 532 lines (+399 / -133) across 6 files Reviewer: c1-squire-dev[bot] Review duration: 1387m
    All Vouch review confidence checks passed
    Residual risk 0.30 = Change Risk (0.76) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.76 · 532 lines diff across 6 files
    0.76
    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%
    60%
    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.376
    Shallow Review Depth shallow_review_depth
    0.289
    Sensitive Paths sensitive_paths
    0.076
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #1169"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% 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: 76.1% (HIGH) · Higher = larger blast radius
    76.1%
    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: 60.5% · Lower = rushed or shallow review
    60.5%
    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: 30% · Safe to merge
    30%