Resource ownership checks
Local fixture: Node.js 22.16.0, ECMAScript modules, synthetic data only. Run node main.mjs in a disposable directory. This is not a production security implementation.
Evaluate resource ownership
function canRead(principal, book) { return Boolean(principal && book && principal.tenant === book.tenant && principal.id === book.owner);}const book = {tenant:'north',owner:'u1'};for (const p of [null,{tenant:'north',id:'u1'}, {tenant:'north',id:'u2'},{tenant:'south',id:'u1'}]) { console.log(canRead(p,book));}Expected output:
falsetruefalsefalseA matching user ID in another tenant is still denied.
How it works
Build a trusted principal from verified authentication, then scope the data query to authorized tenant and owner values. Use parameter binding for values, and constrain modifications with the same scope as reads. An update that matches no records should not expose another tenant’s data. Returning 404 for inaccessible resources can deliberately avoid revealing existence; document consistent response behavior. In the following policy, a reader can read only their own record within their tenant; there is no privileged bypass. The local model is not a database isolation mechanism. The second fixture uses actual in-memory SQLite (experimental in this Node version), with synthetic rows and no persistent files.
Test a tenant-scoped SQL lookup
import {DatabaseSync} from 'node:sqlite';import assert from 'node:assert/strict';const db = new DatabaseSync(':memory:');try { db.exec('CREATE TABLE books (tenant TEXT, id TEXT, owner TEXT, title TEXT, PRIMARY KEY(tenant,id))'); const insert = db.prepare('INSERT INTO books VALUES (?, ?, ?, ?)'); insert.run('north','b1','u1','Rivers'); insert.run('south','b1','u2','Mountains'); const read = db.prepare('SELECT title FROM books WHERE tenant=? AND id=? AND owner=?'); assert.equal(read.get('north','b1','u1').title,'Rivers'); assert.equal(read.get('south','b1','u1'),undefined); assert.equal(read.get('north','b1','u2'),undefined); console.log('3 scoped query checks passed');} finally { db.close(); }Expected output:
3 scoped query checks passedThe actual SQL engine filters by all three predicates; this does not verify a PostgreSQL driver.
Common mistakes
Do not check only the owner ID when IDs can overlap across tenants. Do not trust tenant fields supplied by the requester. Do not fetch a record and serialize it before authorizing access.
Practice and next steps
Extend the scoped query to update a title. Assert that an update for a different tenant changes zero rows, then verify both original records remain intact.
Further reading: Primary documentation. Related staged reference: Object-level authorization.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Resource ownership checks — knowledge check
Apply the security boundary and distinguish the example from a production control.
Two tenants both have book b1. Which scope is needed here?
What does an unpredictable resource ID prove?
Why bind query values?