귀하의 RLS 정책이 잘못된 이유로 테스트를 통과했습니다

작성자

카테고리:

← 피드로
DEV Community · Dev Encyclopedia · 2026-08-21 개발(SW)
Cover image for Your RLS Policy Passed Its Test For the Wrong Reason

Dev Encyclopedia

A manual psql check answers exactly one question: does this policy work right now, against today’s schema, with today’s roles. It says nothing about tomorrow.

Three ordinary changes are enough to quietly break tenant isolation without anyone noticing at review time. A migration that drops and recreates a table loses RLS entirely, since it’s a per-table flag, not something that travels with column definitions. A new service role for a background job can skip the policy if nobody remembers to apply it. And the most common one: someone grants BYPASSRLS during an incident and never revokes it.

Most guides point you at pgTAP here and stop. pgTAP is fine, but it’s a separate SQL-based framework with its own runner. If your backend is already on Jest, you don’t need a second test framework, you need a Jest test that actually proves a leak can’t happen. The core pattern: seed a row as tenant A, query as tenant B, assert the result is empty. Run it through a dedicated low-privilege role, since table owners and superusers bypass RLS by default even with FORCE enabled for the owner.

I break down the full pattern, the queryAsTenant helper, testing WITH CHECK on INSERT/UPDATE, catching accidental BYPASSRLS grants, and wiring it into GitHub Actions here: https://devencyclopedia.com/blog/postgres-rls-testing-jest

If you’re doing this across more than one or two tables, I also built RLSBuilder, a browser tool that generates the CREATE POLICY SQL and a matching Jest test from the same three inputs so they can’t drift apart: https://devencyclopedia.com/tools/rls-builder

원문에서 계속 ↗