Skip to content

feat(dir): apply the policy gate to reads that fetch a record by CID - #2237

Merged
muscariello merged 2 commits into
feat/policy-read-gatefrom
feat/policy-pull-gate
Oct 2, 2026
Merged

muscariello merged 2 commits into
feat/policy-read-gatefrom
feat/policy-pull-gate

Conversation

@muscariello

@muscariello muscariello commented Sep 30, 2026 •

Copy link
Copy Markdown
Member

Part of #2208 (second PR), part of #2205. Stacked on #2235: main ← #2226 ← #2229 ← #2235 ← this. Based on feat/policy-read-gate; retargets as the PRs below it merge.

What this adds

#2235 gates the record query builder, which covers search, the catalog and resolve. Reads that fetch a record by CID go straight to the OCI store and bypassed it. This gates them:

  • StoreService.Pull, Lookup and PullReferrer
  • AIFinderService.ExportAgent

Each asks the new IsRecordServable(cid) before touching the store.

  • The caller is told. A record the gate withholds is refused with PermissionDenied (record <cid> is not available under this node's content policy), built by types.RecordExcludedError. That tells the caller the node withholds it, and says nothing about which policy or why: that stays with the audit API (feat(dir): serve excluded records to named auditors, and the enforcement runbook #2253). The gate answers from the index, so a record that is excluded, not yet evaluated or not indexed at all gets the same refusal, and it does not reveal which CIDs the node holds. NotFound (types.RecordNotFoundError, shared by the OCI store and the paths that report a missing record) remains the store's answer for a record that passes the gate but is missing from it, and the answer for any record while enforcement is off or in shadow mode. Pull, Lookup, PullReferrer and ExportAgent all answer the same way; PullReferrer is refused too, rather than answered with an empty list that would read as "no signatures". Search stays silent about what it leaves out.
  • Fails closed. If the gate can't answer (database error), the fetch fails with Internal rather than serving.
  • Nothing changes until a policy is enforced. With no policy enforced, every record is servable, including records the database hasn't indexed yet, so pulls keep working exactly as today. IsRecordServable reads the enforced set once, so it can't decide "nothing enforced" on one read and gate on another.
  • Not counted. Excluded fetches don't increment the pull or lookup counters.
  • At the API layer, not the store. The reconciler's scan, signature and policy tasks read the store directly and must still see unevaluated records in order to evaluate them; gating the store would exclude every new record forever.

Not in this PR

  • Peer RPC (routing/rpc) serves Lookup, Pull, ListReferrers and PullReferrer to remote peers over libp2p, with no database access, so peers can still fetch excluded content. Gating it means threading the check through routing.New → newRemote → rpc.New; that's the next stacked PR.
  • RoutingService.Publish with an explicit CID announces a record without the gate. Query-based publication is already gated (feat(dir): serve only records that comply with every enforced policy #2235); this is announcement, not content, but should follow for consistency.
  • The identity status of a record by CID (IdentityService.GetIdentityStatus; it replaced the naming service's GetVerificationInfo when naming.v1 was removed) is gated in feat(dir): gate explicit-CID publication and verification lookups #2241.

Testing

  • IsRecordServable, 6 cases: no gate and an empty set both serve a record that isn't indexed; with a policy enforced, compliant is served, and non-compliant, never evaluated and not indexed aren't.
  • Controller: for Pull, Lookup, PullReferrer and ExportAgent, the same CID is run twice: held but excluded, and let through by the gate but not in the store. The excluded one gets PermissionDenied with exactly the message above and no policy detail; the other still gets NotFound. The store is never read for an excluded record, and excluded fetches aren't counted. Servable records are served, and a gate error fails closed.
  • Mutation-checked, every mutant compiling. Each makes a test fail: removing the gate from each of the four RPCs, answering an excluded record with NotFound again, answering PullReferrer with an empty response again, failing open on a gate error, and querying even when nothing is enforced.
  • Full server, reconciler, cli and tests modules vet clean. server and reconciler tests pass.

@muscariello muscariello added kind/feature Categorizes issue or PR as related to a new feature. area/dir/server kind/security Categorizes issue or PR as related to security. labels Sep 30, 2026
@muscariello
muscariello requested a review from a team as a code owner September 30, 2026 08:19
@github-actions github-actions Bot added the size/M Denotes a PR that changes 200-999 lines label Sep 30, 2026
muscariello added a commit that referenced this pull request Sep 30, 2026
checkRecordServable returns a gRPC status for the caller, and the tests'
fakes and helpers return statuses and RPC errors unwrapped because the
tests compare codes and messages exactly. Wrapping either would change
the message a caller sees. Follows the repository's existing patterns:
a per-line exemption in the gate, as in ai_finder.go, and a file-level
one in the test, as in store.go.

Fixes the Lint job failure on #2237.

Signed-off-by: Luca Muscariello <[email protected]>
@codecov

codecov Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.59459% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
server/database/gorm/policy_gate.go 91.7% 1 Missing ⚠️
server/types/store.go 50.0% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@muscariello
muscariello added this pull request to stack #2234 September 30, 2026 09:37
muscariello added a commit that referenced this pull request Sep 30, 2026
checkRecordServable returns a gRPC status for the caller, and the tests'
fakes and helpers return statuses and RPC errors unwrapped because the
tests compare codes and messages exactly. Wrapping either would change
the message a caller sees. Follows the repository's existing patterns:
a per-line exemption in the gate, as in ai_finder.go, and a file-level
one in the test, as in store.go.

Fixes the Lint job failure on #2237.

Signed-off-by: Luca Muscariello <[email protected]>
@muscariello
muscariello force-pushed the feat/policy-pull-gate branch from 7b1440d to 01300b9 Compare October 1, 2026 11:20
Comment thread server/types/store.go
// RecordNotFoundError is the error a store returns for a record it does not
// hold. The API returns the same error for a record the policy gate excludes,
// so a caller cannot tell excluded content from absent content.
func RecordNotFoundError(cid string) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really not want the caller to know? I think it's fine to tell the caller that this record didn't pass the policy gate

@muscariello muscariello Oct 1, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, done. A record the gate withholds is now refused with PermissionDenied (record <cid> is not available under this node's content policy) on Pull, Lookup, PullReferrer, ExportAgent, the peer RPC, explicit-CID publication and GetVerificationInfo. The message names no policy and no reason (that stays with the audit API in #2253), and search still leaves excluded records out silently.

A correction to what I first wrote here: the gate answers from the index, so with enforcement on, a CID the node never held gets the same refusal, and the node does not reveal which CIDs it holds. NotFound is left for a record that passes the gate but is missing from the store, and for any record while enforcement is off or in shadow mode.

muscariello added a commit that referenced this pull request Oct 1, 2026
checkRecordServable returns a gRPC status for the caller, and the tests'
fakes and helpers return statuses and RPC errors unwrapped because the
tests compare codes and messages exactly. Wrapping either would change
the message a caller sees. Follows the repository's existing patterns:
a per-line exemption in the gate, as in ai_finder.go, and a file-level
one in the test, as in store.go.

Fixes the Lint job failure on #2237.

Signed-off-by: Luca Muscariello <[email protected]>
@muscariello
muscariello force-pushed the feat/policy-pull-gate branch from 5a5bd58 to d650bf3 Compare October 1, 2026 15:12
muscariello added a commit that referenced this pull request Oct 1, 2026
checkRecordServable returns a gRPC status for the caller, and the tests'
fakes and helpers return statuses and RPC errors unwrapped because the
tests compare codes and messages exactly. Wrapping either would change
the message a caller sees. Follows the repository's existing patterns:
a per-line exemption in the gate, as in ai_finder.go, and a file-level
one in the test, as in store.go.

Fixes the Lint job failure on #2237.

Signed-off-by: Luca Muscariello <[email protected]>
@muscariello
muscariello force-pushed the feat/policy-pull-gate branch from d650bf3 to 44b8242 Compare October 1, 2026 15:52
muscariello added a commit that referenced this pull request Oct 1, 2026
checkRecordServable returns a gRPC status for the caller, and the tests'
fakes and helpers return statuses and RPC errors unwrapped because the
tests compare codes and messages exactly. Wrapping either would change
the message a caller sees. Follows the repository's existing patterns:
a per-line exemption in the gate, as in ai_finder.go, and a file-level
one in the test, as in store.go.

Fixes the Lint job failure on #2237.

Signed-off-by: Luca Muscariello <[email protected]>
@muscariello
muscariello force-pushed the feat/policy-pull-gate branch from 44b8242 to e49c0b4 Compare October 1, 2026 17:51
muscariello added a commit that referenced this pull request Oct 2, 2026
checkRecordServable returns a gRPC status for the caller, and the tests'
fakes and helpers return statuses and RPC errors unwrapped because the
tests compare codes and messages exactly. Wrapping either would change
the message a caller sees. Follows the repository's existing patterns:
a per-line exemption in the gate, as in ai_finder.go, and a file-level
one in the test, as in store.go.

Fixes the Lint job failure on #2237.

Signed-off-by: Luca Muscariello <[email protected]>
@muscariello
muscariello force-pushed the feat/policy-pull-gate branch from e49c0b4 to dfcbafd Compare October 2, 2026 07:34
csirmazbendeguz
csirmazbendeguz previously approved these changes Oct 2, 2026
@muscariello
muscariello dismissed csirmazbendeguz’s stale review October 2, 2026 12:05

The merge-base changed after approval.

muscariello added a commit that referenced this pull request Oct 2, 2026
checkRecordServable returns a gRPC status for the caller, and the tests'
fakes and helpers return statuses and RPC errors unwrapped because the
tests compare codes and messages exactly. Wrapping either would change
the message a caller sees. Follows the repository's existing patterns:
a per-line exemption in the gate, as in ai_finder.go, and a file-level
one in the test, as in store.go.

Fixes the Lint job failure on #2237.

Signed-off-by: Luca Muscariello <[email protected]>
@muscariello
muscariello force-pushed the feat/policy-pull-gate branch from dfcbafd to aee563b Compare October 2, 2026 12:05
muscariello added a commit that referenced this pull request Oct 2, 2026
checkRecordServable returns a gRPC status for the caller, and the tests'
fakes and helpers return statuses and RPC errors unwrapped because the
tests compare codes and messages exactly. Wrapping either would change
the message a caller sees. Follows the repository's existing patterns:
a per-line exemption in the gate, as in ai_finder.go, and a file-level
one in the test, as in store.go.

Fixes the Lint job failure on #2237.

Signed-off-by: Luca Muscariello <[email protected]>
@muscariello
muscariello force-pushed the feat/policy-pull-gate branch from aee563b to 8bcba0f Compare October 2, 2026 12:33
StoreService Pull, Lookup and PullReferrer, and the AI Finder's
ExportAgent, read the OCI store by CID and so bypassed the gate in the
record query builder. Each now asks IsRecordServable first.

A record the gate excludes is refused with PermissionDenied, built by
types.RecordExcludedError: the record is not available under this
node's content policy. The caller is told that much and nothing more:
not which policy, not why. That detail stays with the audit API.

The gate answers from the index, so a record that is excluded, not yet
evaluated, or not indexed at all gets the same refusal, and the refusal
does not reveal which CIDs the node holds. NotFound remains what the
store says for a record that passes the gate but is missing from it,
and the answer for any record while enforcement is off or in shadow
mode. PullReferrer refuses too, rather than answer with an empty list
that would read as "no signatures". Search stays silent about what it
leaves out. The OCI store and the not-found paths share
types.RecordNotFoundError so they cannot drift apart.

When the gate cannot answer, the fetch fails with Internal rather than
serving: it fails closed. Excluded fetches do not count towards pull or
lookup counters.

IsRecordServable reads the enforced set once. With nothing enforced
every record is servable, indexed or not, so pulls of records not yet
indexed keep working until a policy is enforced. The gate stays at the
API layer: the reconciler's tasks read the store directly, and must
still see unevaluated records to evaluate them.

Refs #2208, #2205

Signed-off-by: Luca Muscariello <[email protected]>
checkRecordServable returns a gRPC status for the caller, and the tests'
fakes and helpers return statuses and RPC errors unwrapped because the
tests compare codes and messages exactly. Wrapping either would change
the message a caller sees. Follows the repository's existing patterns:
a per-line exemption in the gate, as in ai_finder.go, and a file-level
one in the test, as in store.go.

Fixes the Lint job failure on #2237.

Signed-off-by: Luca Muscariello <[email protected]>
@muscariello
muscariello added this pull request to the merge queue Oct 2, 2026
Merged via the queue into main with commit 60bcd67 Oct 2, 2026
46 of 47 checks passed
@muscariello
muscariello deleted the feat/policy-pull-gate branch October 2, 2026 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/dir/server kind/feature Categorizes issue or PR as related to a new feature. kind/security Categorizes issue or PR as related to security. size/M Denotes a PR that changes 200-999 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants