Row-Level Security
PostgreSQL-inspired fine-grained access control. Policies define which rows a user or role can see and modify, enforced transparently by the query engine.
Enable RLS
ENABLE ROW LEVEL SECURITY ON :Project -- Deny-by-default: without policies, no rows visible
Create Policies
Syntax: CREATE POLICY name ON target FOR op [TO role] USING (predicate). The TO role clause is optional - omitting it applies the policy to all users.
-- Users see only their own data
CREATE POLICY own_data ON :Project FOR SELECT TO reader
USING (n.owner = current_user())
-- Policy without a role (applies to everyone)
CREATE POLICY public_read ON :Post FOR SELECT
USING (n.published = true)
-- Multi-tenant isolation
CREATE POLICY tenant ON :Project FOR ALL TO authenticated
USING (n.tenant_id = session('tenant_id'))
-- Restrictive policy (AND logic)
CREATE POLICY no_secret ON :Document FOR SELECT TO reader
USING (n.classification != 'SECRET') AS RESTRICTIVESession Functions
| Function | Description |
|---|---|
| current_user() | The authenticated user's UUID |
| current_username() | The authenticated login username |
| session('key') | Custom session variable (e.g., tenant_id) |
Looking Beyond the Row
A predicate can also look into another collection or along a relationship:EXISTS DOCUMENT IN app_crm.organization_user_roles WHERE user_id = current_user() AND organization_id = d.organization_id AND role = 'admin'or EXISTS ((:User {id: current_user()})-[:ADMIN_OF]->(:Organization {id: n.organization_id})), where d.field / n.field is the row being judged. That is how “only an admin of this row’s organisation may edit it” becomes a policy with the membership kept elsewhere. Parenthesise a lookup to combine it with other conditions; a missing field or a malformed form denies; lookups are not themselves subject to RLS. See the Cypher reference for the full rules.
Write Enforcement
Policies are enforced on writes as well as reads, for nodes, relationships and documents. With RLS enabled, an operation no policy allows is denied.
| Statement | Policy | What happens |
|---|---|---|
| CREATE, MERGE (node or relationship) | INSERT - WITH CHECK | The new node or relationship must pass, judged as the statement leaves it (so CREATE ... SET r = $props counts). A refusal is an error and nothing the statement created remains. |
| SET, REMOVE on a node | UPDATE - USING | Checked for every row before anything is written. One node the caller may read but not update refuses the whole statement. |
| SET, REMOVE on a relationship | UPDATE - USING, then WITH CHECK | A relationship USING does not allow is left as it is. One that was changed must still pass WITH CHECK (USING when there is none), otherwise the statement is refused and the relationship restored. |
| DELETE, DETACH DELETE | DELETE - USING | What the policy does not allow stays, and the summary says how many: "Deleted 2 relationships; 1 relationships left alone by row-level security". |
| UPSERT DOCUMENT, DELETE DOCUMENT, REST /docs writes | INSERT / UPDATE / DELETE on the collection | Checked per document. |
A node or relationship the caller may not read is not matched at all, so it cannot be written, returned or linked: MATCH, MERGE and the write clauses all see the same rows. A node the caller may read but not update is different - naming it in a SET is an error.
Who may write in the first place: a server admin or editor; an editor of an app, inside that app's schema, for graph writes by Cypher and document writes by REST, whatever their server role; and anyone at all for MATCH ... SET on nodes and relationships under RLS and for UPSERT DOCUMENT into a collection under RLS, where the UPDATE policy alone decides. In that last case every node or relationship the statement writes must be under RLS; one that is not refuses it.
Behavior
Deny-by-default: when RLS is enabled, no matching policy means the operation is denied. Policies and the enabled/disabled state persist across server restarts (snapshot v5).
Sync Pairs
When a sync rule links a label to a collection, a single policy on the label automatically applies to the paired collection too.
Where Policies Apply
Collection policies run on the REST document API (get, query, scan, put, delete and batch) and on the Cypher document statements. A document the caller may not see is 404 over REST and simply absent from MATCH DOCUMENT and the graph-document join; a write a policy refuses is 403. Rows are filtered before limit, so a page is never short of rows the caller may see. Upserting an existing document must pass USING on the row as it is and WITH CHECK on its replacement; DELETE DOCUMENT (key and WHERE forms) removes only what the caller may see and delete; UPSERT DOCUMENT ... WHERE ... SET leaves rows its UPDATE policies do not allow untouched. Label SELECT policies filter every Cypher read of nodes: MATCH, traversals, counts, full-text search and OPTIONAL MATCH.
Who is asking is settled once per request. Server admins bypass policies, app admins bypass them inside their own app, and everyone else is judged by every role they hold. FORCE ROW LEVEL SECURITY overrides both bypasses. TO authenticated matches every signed-in principal and TO PUBLIC everyone.
Management
ALTER POLICY name ON :Label USING (predicate) [WITH CHECK (predicate)] -- the predicate you leave out is kept DROP POLICY name ON :Label SHOW POLICIES SHOW POLICIES ON :Label ENABLE / DISABLE / FORCE ROW LEVEL SECURITY ON :Label SIMULATE POLICY AS alice WITH ROLE reader ON :Project -- Collection policies: ON COLLECTION name wherever a label policy takes ON :Label ALTER POLICY things_update ON COLLECTION app_crm.things WITH CHECK (...) DROP POLICY things_update ON COLLECTION app_crm.things SHOW POLICIES ON COLLECTION app_crm.things