SecLab
Access ControlwalkthroughDifficulty 4/528 min

RLS as a second layer of defence

Objective: After this lesson you can enable Row Level Security on a user-owned Postgres table, and know the three ways it is still bypassed.

V8API1:2023A01:2025access-control
Step 1 of 5 · read5 min

The last layer that knows who owns the data

All four previous lessons rest on one thing: somebody remembering to write the right clause. A WHERE clause needs an author, a shared Expression needs a caller, a DTO needs a declaration. Row Level Security is the one layer that needs nobody to remember anything — it runs inside the database, after every query, including a query typed into psql by hand at 3am.

How it works, briefly: you declare a policy on a table, and Postgres adds that policy's condition to every query touching the table. Not "check, then refuse" but a row that fails the condition does not exist as far as that query is concerned. SELECT count(*) returns a smaller number; UPDATE updates nothing; DELETE deletes nothing.

What makes it categorically different from the previous four:

LayerProtectsDoes not protect
A WHERE clauseThe endpoint where somebody wrote itThe second endpoint, background jobs, migration scripts
Deny-by-default in the pipelineRequests arriving over HTTPEverything that does not arrive over HTTP
A shared ExpressionWherever somebody calls itWherever somebody forgot
RLSEvery query against that table, from every sourceSee the final step — there are three bypasses

And here is the part that must be said plainly, because it is this lesson's biggest trap: RLS is the second layer, not the first. Enabling RLS and then dropping the WHERE clause from your code is a mistake: you lose the ability to read the rule from application code, you lose control over performance (the policy joins your query plan), and you place all authorisation in a mechanism that one misconfigured connection turns off entirely.

View path

Comments

Join the discussion
Sign up to comment

Commenting needs an account with at least one completed lesson. That condition is what keeps this thread worth reading: every point belongs to someone who can be asked back, and reputation accrues over time.

Sign upSign in

You can still read every comment below without an account. Signing in brings you back to this exact spot, not to the top of the page.

Loading comments…