Testing Supabase's REST API and row level security

Supabase gives every table a REST API through PostgREST. The convenience comes with one big responsibility: row level security (RLS) is the only thing standing between the anon key in your frontend and every row in your database. That's what an API test collection for Supabase should prove first.

Step 1: Environment

VariableValue
sbUrlhttps://<project-ref>.supabase.co
sbAnonKeythe anon (public) key, marked as secret anyway
userAEmail, userAPassworda test user, password secret
userBEmail, userBPassworda second test user

Never put the service_role key in a test collection that talks to RLS: it bypasses RLS, and every security test would pass for the wrong reason.

On the collection, set the apikey header with a pre-request script:

JavaScript
pm.request.headers.set('apikey', pm.environment.get('sbAnonKey'));

Step 2: Sign in as user A

HTTP
POST {{sbUrl}}/auth/v1/token?grant_type=password
Content-Type: application/json

{"email": "{{userAEmail}}", "password": "{{userAPassword}}"}
JavaScript
const s = pm.response.json();
pm.test('signed in', () => pm.expect(s.access_token).to.be.ok);
pm.environment.set('userAToken', s.access_token);
pm.environment.set('userAId', s.user.id);

Do the same for user B into userBToken. Each session's access token is a JWT that expires (an hour by default), so sign in at the start of each run as a setup step.

Step 3: Insert and read your own rows

HTTP
POST {{sbUrl}}/rest/v1/notes
Authorization: Bearer {{userAToken}}
Content-Type: application/json
Prefer: return=representation

{"title": "A's private note"}
JavaScript
const row = pm.response.json()[0];
pm.test('row belongs to user A', () => pm.expect(row.user_id).to.equal(pm.environment.get('userAId')));
pm.environment.set('noteId', String(row.id));

With return=representation, the answer is an array of the inserted rows. Without it, a successful insert answers 201 with an empty body.

Step 4: The RLS tests

User B tries to read user A's note:

HTTP
GET {{sbUrl}}/rest/v1/notes?id=eq.{{noteId}}
Authorization: Bearer {{userBToken}}
JavaScript
pm.test('user B cannot see A\'s note', () => {
  pm.expect(pm.response.code).to.equal(200);
  pm.expect(pm.response.json()).to.have.lengthOf(0);
});

RLS doesn't answer 403. It filters, so the result is just empty. A test that expects 403 passes for the wrong reason, or fails while the data is safe.

User B tries to change it:

HTTP
PATCH {{sbUrl}}/rest/v1/notes?id=eq.{{noteId}}
Authorization: Bearer {{userBToken}}
Content-Type: application/json
Prefer: return=representation

{"title": "hacked"}
JavaScript
pm.test('update matched no rows', () => pm.expect(pm.response.json()).to.have.lengthOf(0));

And no user at all, just the anon key:

HTTP
GET {{sbUrl}}/rest/v1/notes?select=id&limit=1
JavaScript
pm.test('anonymous sees nothing', () => pm.expect(pm.response.json()).to.have.lengthOf(0));

These three tests are worth more than the rest of the collection. Run them on every migration. More on this kind of test in testing authorization with the wrong user.

Step 5: Filters and counts

HTTP
GET {{sbUrl}}/rest/v1/notes?select=id,title&title=ilike.*private*&order=id.desc&limit=10
Authorization: Bearer {{userAToken}}
Prefer: count=exact
JavaScript
pm.test('count header', () => pm.expect(pm.response.headers.get('Content-Range')).to.match(/\/\d+$/));

PostgREST errors come as {"code", "message", "details", "hint"}, with Postgres error codes such as 23505 for a unique violation and 42501 for a permission error.