Skip to content

Harden the RLS access walk: assert member_of classification, minimum-depth tie resolution, and cycle bound as tested invariants #1735

Closed

Description

@malibio

Overview

The cloud access predicate decides whether a user may read or write a node by walking the graph upward to the nearest restricted collection. Whether that walk follows member_of (a node's classification) or only has_child (a node's position in the outline) is the difference between a correct access model and a broad, silent read leak.

The shipped predicate in nodespace-cloud already does the right thing. This issue is not a bug report — it is about locking that behavior in. The correct implementation currently rests on a comment and on tests that were written for a visibility concern (sub-collections not appearing in sync pulls), not for the security property. The security property itself is untested, so a well-intentioned "simplify the walk to just follow the tree" refactor would reintroduce the leak with no test failing.

Problem Statement

The predicate seeds its recursive CTE from both member_of and has_child in-edges of the subject node, recurses upward through has_child unconditionally and through member_of only into collection nodes, bounds depth at 64, and gates on the minimum-depth restricted-ancestor set.

Each of those four properties is load-bearing, and each is currently unpinned:

  1. member_of in the seed. Without it, this ordinary authoring shape evaluates as open to the entire tenant:

    Date page 2026-07-25            (open — no restricted ancestor)
      └── has_child → Note "Q3 comp bands"
                        └── member_of → Collection "Compensation"  (restrictedToMembers = true)
    

    Writing a note where you are working and filing it into a collection afterwards is the normal path — filing does not move the note in the outline. A has_child-only walk leaks every such node, and makes an outline move a silent access change.

  2. member_of recursion restricted to collection nodes. Collection nesting is real containment; a content node that happens to be member_of something is not an access parent of anything. Dropping the node_type = 'collection' guard would let content memberships transitively govern unrelated nodes.

  3. Minimum-depth tie resolution. Ancestry is a DAG, not a tree, so several restricted collections can tie at the minimum depth (a diamond). Membership in any one of them grants. Gating on all restricted ancestors instead of the minimum-depth set would break ADR-037's nearest-boundary nesting semantics; denying on tie would make explicit grants silently ineffective.

  4. Depth bound + set semantics. UNION (not UNION ALL) dedupes so a cyclic graph terminates; the depth cap is the backstop.

Additionally, the read and write predicates must perform the same walk, differing only in the final permission check. If they drift, read and write disagree about where the boundary is.

Proposed Solution

Pin all four properties with adversarial tests in the cross-tenant / privilege-escalation suite, and make the read/write walk shared rather than duplicated.

Test cases to add (each must fail if the corresponding property is removed):

  • A node member_of a restricted collection, reachable in the outline only from an open ancestor, is not readable by a tenant user who is not a member. This is the primary regression test — it is the case a has_child-only walk gets wrong.
  • The same node is readable by a member of that collection.
  • A content node that is member_of a collection does not transitively govern nodes that are has_child children of it.
  • A node under two restricted collections at equal minimum depth is readable by a member of either (tie grants).
  • A node under a restricted collection at depth 1 and a different restricted collection at depth 3 requires membership in the depth-1 collection (nearest wins; farther boundary does not grant).
  • A relationship cycle in the ancestry graph terminates rather than looping or timing out.
  • The write predicate agrees with the read predicate on boundary location for every case above, differing only in requiring modify/admin.

Shared walk: the read predicate, the write predicate, and the restricted-ancestor existence helper currently repeat the same recursive CTE three times. Factor the traversal so the walk cannot drift between them while the final checks stay distinct.

Architecture Impact

  • ADR-037 (../nodespace-docs/decisions/037-collection-access-model.md) §5 previously described the walk as following the has_child tree. It has been amended to state that the walk unions both edge types. This issue does not reverse it — it implements and pins what the amended ADR requires.
  • ADR-059 (../nodespace-docs/decisions/059-access-classification-not-outline-position.md) is the governing decision: access follows classification, not outline position; ancestry is a DAG; minimum-depth ties grant; access is inherited but membership is never derived.
  • architecture/tenant-isolation-rls.md records this as load-bearing security property P6, which is what this issue asserts by test.
  • The companion nodespace-docs changes are already committed; no further docs work is required for this issue.

Acceptance Criteria

  • Adversarial test asserts a node member_of a restricted collection but reachable only from an open outline ancestor is unreadable by a non-member
  • Adversarial test asserts the same node is readable by a member
  • Test asserts member_of recursion does not follow through content nodes
  • Test asserts equal-minimum-depth ties grant on membership in either collection
  • Test asserts a nearer restricted boundary overrides a farther one
  • Test asserts a cyclic ancestry graph terminates
  • Test asserts read and write predicates locate the same boundary
  • The recursive walk is defined once and shared by the read predicate, write predicate, and restricted-ancestor helper
  • Each test fails when its corresponding property is removed from the predicate (verified by temporarily reverting)
  • Tests pass (bun run test:all)
  • Code passes bun run quality:fix

Technical Specifications

Reference Files

  • Tenant schema: db/tenant-schema.sql in nodespace-cloud — user_can_access_node, user_can_write_node, has_restricted_ancestor
  • Membership view: collection_members (derived from relationships ⋈ auth_identities); not granted to authenticated, read only through SECURITY DEFINER helpers
  • Security properties: ../nodespace-docs/architecture/tenant-isolation-rls.md — P6

Implementation Details

  • The restriction flag is read at properties->'collection'->>'restrictedToMembers' — core stores schema fields nested under the node-type namespace, so the synced JSON is {"collection":{"restrictedToMembers":true}}, not a top-level key.
  • Compare with text equality (= 'true'), never ::boolean. Untrusted client-supplied properties (e.g. "yes please") would raise inside a SECURITY DEFINER policy and 500 every query under that collection.
  • The function parameter is named for_user_id, not user_id, to avoid colliding with the collection_members.user_id column inside the query.
  • Person nodes are gated by a separate co-member check, not by this walk — the open-by-default branch would otherwise expose PII to every tenant user.
  • The walk is on the hot read path, evaluated per row and per Realtime WAL broadcast. Keep the depth bound and confirm the traversal stays indexed; a regression here degrades every query, not just restricted ones.

Related Issues

Activity

  1. mstomar125 commented on Jul 28, 2026

    @mstomar125
    Contributor

    Finding while writing the pinning tests: the predicate has a real interior-content leak

    Verifying user_can_access_node empirically against live tenant_demo (BEGIN..ROLLBACK) before locking it in surfaced a case where the shipped predicate does not do the right thing — the exact silent-leak class this issue exists to prevent.

    Behavior: a content root filed into a restricted collection via member_of (the normal grouping edge #1742 gates) leaks its has_child descendants to non-members. The root is correctly gated; its interior children are not.

    Restricted root contained via outsider access to its child
    member_of only TRUE — leaks
    has_child only FALSE (gated)
    both FALSE (gated)
    (control) the root itself FALSE (gated)

    Reproducible:

    SET search_path = <SCHEMA>, public, extensions;
    INSERT INTO nodes VALUES ('R','collection','R','{"collection":{"restrictedToMembers":true}}',1),('out','person','O','{}',1),('P','text','root','{}',1),('D','text','child','{}',1);
    INSERT INTO auth_identities (person_node_id, supabase_uid) VALUES ('out','dddddddd-0000-0000-0000-0000000000d1');
    INSERT INTO relationships (in_node,out_node,relationship_type) VALUES ('R','P','member_of'),('P','D','has_child');
    SELECT user_can_access_node('P','dddddddd-...d1');  -- f  (root gated ✓)
    SELECT user_can_access_node('D','dddddddd-...d1');  -- t  (child LEAKS ✗)

    Cause: the recursive walk follows member_of upward only when the current node is a collection (... OR (member_of AND na.node_type = 'collection')). So a content root's restriction never propagates down its subtree. The clause is duplicated verbatim in all three twin predicates — user_can_access_node, user_can_write_node, has_restricted_ancestor.

    Reachability: add_to_collection creates member_of only (no has_child), so this is the normal shape of every multi-paragraph document filed into a collection — 11,729 such structures exist in tenant_demo today. It's currently dormant only because tenant_demo has 0 restricted collections; restricting any collection (or filing a doc into a restricted one) exposes the children of every document in it.

    Fix being implemented (option a)

    Enforce ADR-059 §2 at the read layer: a filed root's restriction covers its whole subtree. Change the recursion clause in all three predicates from na.node_type = 'collection' to na.node_type <> 'person' — follow member_of up from content roots and collections, but not from person (grantee) nodes (grantee access is checked separately via collection_members, so this is safe). Read-layer enforcement gates regardless of how the edge was created (stronger than fixing only the write path).

    Shipping with the pinning tests this issue asks for (leak now gated; a member of the collection still reads the child; open-collection content unaffected; collection-nesting + has_child paths unchanged; person exemption), live-verified vs tenant_demo + adversarial review. PR to nodespace-cloud to follow.

  2. mstomar125 commented on Jul 28, 2026

    @mstomar125
    Contributor

    Resolved by nodespace-cloud#99 (squash-merged d1e5a2a0), deployed + verified live on tenant_demo.

    The access walk now follows member_of upward from content roots and collections (not collections only) in all three twin predicates — user_can_access_node, user_can_write_node, has_restricted_ancestor — via na.node_type <> 'person'. A content root filed into a restricted collection now restricts its whole has_child subtree (ADR-059 §2), closing the read and write leak; person (grantee) member_of edges stay unwalked (grantee access is decided via collection_members).

    The four load-bearing walk properties this issue asked to lock in are now pinned by db/fixes/2026-07-28-access-walk-member-of-from-content-test.sql: member_of in the seed, has_child unconditional, member_of-from-non-person recursion, and the nearest-restricted (min-depth) gate. The test fails on the old clause and passes on the fixed one, so a future 'simplify the walk' refactor trips a test instead of silently reopening the leak.

    Verification: live vs tenant_demo (BEGIN..ROLLBACK) — outsider read/write of an interior node under a member_of-restricted root flips TRUE→FALSE; members retain access; open content, has_child gating, collection nesting, and min-depth all unaffected. Two-way verified (live tests + adversarial review: the change is a monotonic tightening, structurally unable to introduce a leak).

    Follow-up (operational): the db/fixes migration is applied to tenant_demo; apply it to any other live tenants per the cloud runbook (new tenants get it from the canonical schema at /provision).

  3. self-assigned this
    on Jul 28, 2026
  4. mstomar125 commented on Aug 1, 2026

    @mstomar125
    Contributor

    ✅ Live-tenant migration applied + verified (2026-08-01). Deployed to all 4 complete tenants — tenant_demo, tenant_deutnet, tenant_nodespace_internal, tenant_public — via the recheck→apply→post-verify flow (rolled-back recheck on real data, then committed CREATE OR REPLACE (no data change), then behavioral -test.sql post-verify all green). Fix-marker confirmed in each tenant's live function definitions. tenant_b skipped (incomplete stub schema — no auth_identities/access functions). New tenants remain auto-covered via canonical. (cloud-side access-walk migration 2026-07-28-access-walk-member-of-from-content.sql.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions