Security: invariant prefix-skip scans make guaranteed forward progress on hostile keys #1163
<!-- review-superseded: {"report_comment_id": 5971757660, "run_id": "37141266902", "run_attempt": "1"} -->
<details>
<summary>Superseded — see the <a href="https://github.com/ConductorOne/baton-sdk/pull/1163#issuecomment-5971757660">current review report</a> for commit <code>a933f7f11af7</code></summary>
General PR Review: Security: invariant prefix-skip scans make guaranteed forward progress on hostile keys
Blocking Issues: 0 | Suggestions: 1 | Threads Resolved: 0
Criteria: Criteria status: loaded .claude/skills/ci-review.md from trusted base 768d840ca236.
_Review mode: full_
View review run
Review Summary
The PR changes the three prefix-skip scans in
pkg/dotc1z/engine/pebble/ingest_facts.go (I3, I7, I8). Each one now rejects keys whose byte 2 is not 0x00, and the dangling scan also rejects flag components other than "0" or "1". Each scan now builds its SeekGE target from the current key's raw bytes (key[:skipLen]) instead of decoding and re-encoding the components, so the iterator always moves forward. A new red/green test covers this, and ingest_repair.go only gets a doc comment.I scanned the full diff (3 files) for security and correctness. I checked the splice offsets against codec.DecodeTupleStringAlias. That function returns the offset of the next separator, and the escape rules (0x00→0x01 0x01, 0x01→0x01 0x02) mean an encoded component never contains a bare 0x00. So headerLen+ridEnd+1 and headerLen+off always end on the component's own separator. KeyUpperBound can't return nil there, because the spliced prefix ends in 0x00, or is clamped to a key that starts with the v3 header. I also checked that canonical grant and entitlement keys always have bytes after rid, so the new ridEnd >= len(tail) rejection only applies to malformed keys. Legacy single-component entitlement keys were already rejected by the existing next >= len(tail) check. I confirmed the doc claim about ForEachDanglingGrantPrincipal: its prefix includes the separator, and a valid escape sequence decodes and re-encodes to the same bytes.
Repo criteria applied, triage:
The scan cost stays O(distinct) seeks, and the change removes an allocation per seek, so the cost-curve rule needs no new benchmark. There are no exported API, proto, default, dependency, or version changes. Malformed keys that were silently tolerated now fail the invariant pass. That is intentional and matches the policy of #1160.
Security Issues
None found.
Correctness Issues
None found.
Suggestions
stripped: comps[2] == idFlagStripped and re-encoded with id.flagComponent() (identity.go:47-52), so canonical "0"/"1" keys round-tripped exactly and were never folded. The fold only affected non-canonical flags such as "2". Because of this, the "two-flag distinctness" check in the control subtest (ingest_facts_security_test.go:~146) would pass before the fix too, so it is not a regression guard for a fixed bug. Please correct the PR description so nobody sets out to remediate a legitimate-key I8 miscount that never existed.</details>
<!-- review-superseded: {"report_comment_id": 5972028679, "run_id": "37143158106", "run_attempt": "1"} -->
<details>
<summary>Superseded — see the <a href="https://github.com/ConductorOne/baton-sdk/pull/1163#issuecomment-5972028679">current review report</a> for commit <code>495ce7998c05</code></summary>
General PR Review: Security: invariant prefix-skip scans make guaranteed forward progress on hostile keys
Blocking Issues: 0 | Suggestions: 1 | Threads Resolved: 0
Criteria: Criteria status: loaded .claude/skills/ci-review.md from trusted base 768d840ca236.
_Review mode: full_
View review run
Review Summary
The PR changes the three prefix-skip scans in
pkg/dotc1z/engine/pebble/ingest_facts.go (I3, I7, I8). Each one now rejects keys whose byte 2 is not 0x00, and the dangling scan also rejects flag components other than "0" or "1". Each scan now builds its SeekGE target from the current key's raw bytes (key[:skipLen]) instead of decoding and re-encoding the components, so the iterator always moves forward. A new red/green test covers this, and ingest_repair.go only gets a doc comment.I scanned the full diff (3 files) for security and correctness. I checked the splice offsets against codec.DecodeTupleStringAlias. That function returns the offset of the next separator, and the escape rules (0x00→0x01 0x01, 0x01→0x01 0x02) mean an encoded component never contains a bare 0x00. So headerLen+ridEnd+1 and headerLen+off always end on the component's own separator. KeyUpperBound can't return nil there, because the spliced prefix ends in 0x00, or is clamped to a key that starts with the v3 header. I also checked that canonical grant and entitlement keys always have bytes after rid, so the new ridEnd >= len(tail) rejection only applies to malformed keys. Legacy single-component entitlement keys were already rejected by the existing next >= len(tail) check. I confirmed the doc claim about ForEachDanglingGrantPrincipal: its prefix includes the separator, and a valid escape sequence decodes and re-encodes to the same bytes.
Repo criteria applied, triage:
The scan cost stays O(distinct) seeks, and the change removes an allocation per seek, so the cost-curve rule needs no new benchmark. There are no exported API, proto, default, dependency, or version changes. Malformed keys that were silently tolerated now fail the invariant pass. That is intentional and matches the policy of #1160.
Security Issues
None found.
Correctness Issues
None found.
Suggestions
stripped: comps[2] == idFlagStripped and re-encoded with id.flagComponent() (identity.go:47-52), so canonical "0"/"1" keys round-tripped exactly and were never folded. The fold only affected non-canonical flags such as "2". Because of this, the "two-flag distinctness" check in the control subtest (ingest_facts_security_test.go:~146) would pass before the fix too, so it is not a regression guard for a fixed bug. Please correct the PR description so nobody sets out to remediate a legitimate-key I8 miscount that never existed.---
Reviewed commit: a933f7f11af7
<!-- review-state: {"last_reviewed_sha": "a933f7f11af768d229f79c42e93e6e79253c15d5", "base_sha": "768d840ca23627e6acf250d1fe5eb9d0accaa64d", "workflow_ref": "ConductorOne/github-workflows/.github/workflows/general-pr-review.yaml@refs/heads/main", "run_id": "37141266902", "run_attempt": "1", "summary_marker": "### General PR Review:", "verdict_mode": "baseline", "publication": "completed", "started_at": "2026-10-03T17:39:01Z", "working_comment_id": 5971744849, "working_comment_updated_at": "2026-10-03T17:40:30Z"} -->
</details>
No blocking issues found — see the full review report
<!-- review-publication: {"run_id": "37141266902", "run_attempt": "1", "workflow_ref": "ConductorOne/github-workflows/.github/workflows/general-pr-review.yaml@refs/heads/main", "summary_marker": "### General PR Review:", "verdict_mode": "baseline", "report_comment_id": 5971757660} -->
<!-- review-superseded: {"report_comment_id": 5972028679, "run_id": "37143158106", "run_attempt": "1"} -->
<details>
<summary>Superseded — see the <a href="https://github.com/ConductorOne/baton-sdk/pull/1163#issuecomment-5972028679">current review report</a> for commit <code>495ce7998c05</code></summary>
General PR Review: Security: invariant prefix-skip scans make guaranteed forward progress on hostile keys
Blocking Issues: 0 | Suggestions: 0 | Threads Resolved: 0
Criteria: Criteria status: loaded .claude/skills/ci-review.md from trusted base 768d840ca236.
_Review mode: incremental since a933f7f_
View review run
Review Summary
The new commits move all four referential-invariant scans (I3
ForEachDistinctGrantEntitlementResource, I7 ForEachDistinctEntitlementResource, I8 ForEachDanglingGrantEntitlement, I9 ForEachDanglingGrantPrincipal) onto one helper, forEachKeyGroup (ingest_facts.go:136). The helper bounds the iterator to the whole 2–3 byte family and rejects keys without the header separator or with fewer than n separator-terminated components. After each group it seeks to upperBoundOf(key[:off]), where off is one past the n-th separator. That target is always strictly greater than the current key, so every scan makes progress. I scanned the full PR diff (3 files) for security and correctness issues and checked the helpers it relies on:codec.KeyUpperBound copies its input, so the uncapped key[:off] slice cannot alias or mutate the iterator buffer.DecodeTupleStringAlias returns the separator offset as end.key[:off] always ends in 0x00, so the bound is never nil.[v3,typeIndex,idxGrantByPrincipal] (keys.go:185,265,273), so widening I9 to the 3-byte family adds no false rejections. The same holds for grant and entitlement keys.For well-formed keys, grouping is byte-identical to the previous decode→re-encode skip. The tests cover each hostile shape against every scan that reads it, plus a group-once control that includes I9.
Repo criteria triage:
Verdict: LOW, so no escalation. No exported API, proto, serialized-state, default or dependency surface changes. The cost-curve rule for compaction-adjacent paths is satisfied because the cost is unchanged (O(distinct groups) seeks, stated in the PR). The only behavior change is that truncated or non-canonical keys now return errors, which the PR documents.
Security Issues
None found.
Correctness Issues
None found.
Suggestions
None.
Resolved prior findings
TestIngestScansVisitEachGroupOnce), not a regression guard.</details>
General PR Review: Security: invariant prefix-skip scans make guaranteed forward progress on hostile keys
Blocking Issues: 0 | Suggestions: 0 | Threads Resolved: 0
Criteria: Criteria status: loaded .claude/skills/ci-review.md from trusted base 768d840ca236.
_Review mode: incremental since a933f7f_
View review run
Review Summary
The new commits move all four referential-invariant scans (I3
ForEachDistinctGrantEntitlementResource, I7 ForEachDistinctEntitlementResource, I8 ForEachDanglingGrantEntitlement, I9 ForEachDanglingGrantPrincipal) onto one helper, forEachKeyGroup (ingest_facts.go:136). The helper bounds the iterator to the whole 2–3 byte family and rejects keys without the header separator or with fewer than n separator-terminated components. After each group it seeks to upperBoundOf(key[:off]), where off is one past the n-th separator. That target is always strictly greater than the current key, so every scan makes progress. I scanned the full PR diff (3 files) for security and correctness issues and checked the helpers it relies on:codec.KeyUpperBound copies its input, so the uncapped key[:off] slice cannot alias or mutate the iterator buffer.DecodeTupleStringAlias returns the separator offset as end.key[:off] always ends in 0x00, so the bound is never nil.[v3,typeIndex,idxGrantByPrincipal] (keys.go:185,265,273), so widening I9 to the 3-byte family adds no false rejections. The same holds for grant and entitlement keys.For well-formed keys, grouping is byte-identical to the previous decode→re-encode skip. The tests cover each hostile shape against every scan that reads it, plus a group-once control that includes I9.
Repo criteria triage:
Verdict: LOW, so no escalation. No exported API, proto, serialized-state, default or dependency surface changes. The cost-curve rule for compaction-adjacent paths is satisfied because the cost is unchanged (O(distinct groups) seeks, stated in the PR). The only behavior change is that truncated or non-canonical keys now return errors, which the PR documents.
Security Issues
None found.
Correctness Issues
None found.
Suggestions
None.
Resolved prior findings
TestIngestScansVisitEachGroupOnce), not a regression guard.---
Reviewed commit: 495ce7998c05
<!-- review-state: {"last_reviewed_sha": "495ce7998c052ba1c0b1558a7654d26a72135ce4", "base_sha": "768d840ca23627e6acf250d1fe5eb9d0accaa64d", "workflow_ref": "ConductorOne/github-workflows/.github/workflows/general-pr-review.yaml@refs/heads/main", "run_id": "37143158106", "run_attempt": "1", "summary_marker": "### General PR Review:", "verdict_mode": "baseline", "publication": "completed", "started_at": "2026-10-03T18:09:55Z", "working_comment_id": 5972016392, "working_comment_updated_at": "2026-10-03T18:11:13Z"} -->
No blocking issues found — see the full review report
<!-- review-publication: {"run_id": "37143158106", "run_attempt": "1", "workflow_ref": "ConductorOne/github-workflows/.github/workflows/general-pr-review.yaml@refs/heads/main", "summary_marker": "### General PR Review:", "verdict_mode": "baseline", "report_comment_id": 5972028679} -->
Approved these changes.
pquerna
c1-squire-dev[bot]
Summary
C1Z-SEC-006 (CWE-835). The syncer's referential invariants (I3, I7, I8, I9) run as group-skip scans over the Pebble keyspace. Each scan visits the first key of a group, then
SeekGEs past the group, so a pass costs one seek per distinct group.The skip target was built by decoding the key's tuple components and re-encoding them. For two non-canonical shapes the re-encoded prefix sorts below the key, so
SeekGElanded on the same key and the scan spun at full CPU until its context was cancelled:[v3|type]family. The decoder assumed a separator at byte 2, and the re-encoded prefix always has one."0"/"1". The scan folded any such value to"0".A hostile
.c1zreaches these scans through the syncer and the compaction worker.Changes
forEachKeyGroup(ingest_facts.go) is the one group-skip loop for all four scans. It owns the iterator bounds, the ctx check, component decoding, malformed-key rejection and the skip. The skip seeks to the upper bound of the visited key's own prefix. That bound sorts after every key sharing the prefix, so the scan advances whatever the key bytes are. Pagination cursors and grant-digest partition repair already derive their seeks from raw key bytes; these scans were the exception.malformed keyerror. The helper covers the whole record family and rejects:- a key without the tuple separator after the header;
- a key with fewer than the scan's separator-terminated components.
I8 also rejects a flag other than
"0"/"1".ForEachDanglingGrantPrincipal) was not exploitable, because its iterator already started at the separator. It used the same decode→re-encode skip, though, so it moves onto the helper too, and no scan keeps that pattern.Behavior changes
Tests
TestSecurity_IngestScansRejectNonCanonicalKeysruns each hostile shape against every scan that reads it:- bad header byte (I3, I7, I8, I9);
- flag
"2"(I8);- a grant key that ends inside the identity (I8).
Each must return
malformedwithin 10s. To check that the test can fail, I reverted the skip to decode→re-encode and dropped the header check; the four header cases then failed at the deadline.TestIngestScansVisitEachGroupOncewrites two grants per entitlement and per principal. I3, I7, I8 and I9 each visit every group once, and stripped and opaque identities on one resource stay distinct.TestIngestScansFailLoudOnMalformedKeys,TestIngestScanSurfaceAfterCloseReturnsClosing, and thepkg/syncreferential invariant tests.golangci-lint run ./...: 0 issues.go test -short -tags=baton_lambda_support ./...: passes.go test -race: passes onpkg/dotc1z/engine/pebbleandpkg/sync.