生产中的行级安全性
三种 RLS 模式覆盖 95% 的用例:所有权、多租户、分层组织。使用 pg_prove 和调试测试。
什么是 RLS#
行级安全性是本机 PostgreSQL 功能,它通过评估可能取决于当前角色和 JWT 声明的 SQL 表达式来动态过滤返回(或接受)的行。 Aurabase 通过 request.jwt.claims 注入用户声明并公开帮助程序 auth.uid()、 auth.role()、 auth.tenant()、 auth.has_role()。
- 角色名称。 规范数据库角色是
aura_anon、aura_authenticated和aura_service_role。在托管集群上,组别名anon、authenticated和service_role也映射到它们(请注意,BYPASSRLS严格保留在aura_service_role上)。 - 对于通过 Aurabase创建的表,RLS 默认处于启用状态。 使用
/raw创建的任何表、迁移、Studio 或表 API 都会自动接收ENABLE和FORCE ROW LEVEL SECURITY(除非DISABLE ROW LEVEL SECURITY在同一个 DDL 批处理中显式执行):公共/匿名查询返回[],直到您添加策略。通过直接 PostgreSQL 连接(psql、 Prisma、Drizzle)创建的表永远不会通过该钩子,因此它们不会获得 no RLS ,而是 无法访问公钥:aura_anon和aura_authenticated对它们没有任何特权直到你同意为止。要将此类表公开给 SDK,请首先启用 RLS,然后授予并添加策略:ALTER TABLE posts ENABLE ROW LEVEL SECURITY; GRANT SELECT, INSERT, UPDATE, DELETE ON posts TO aura_authenticated; CREATE POLICY ...。切勿在 RLS 关闭的表上授予aura_anon或aura_authenticated:公钥的每个持有者或每个注册用户都将读取(并且在写入授权的情况下修改)其所有行。 - 公钥没有用户。对于公钥和
service_role密钥,auth.uid()是NULL;它仅返回登录用户的 ID。使用to aura_authenticated将策略限制为登录用户。
所有权——每个用户都有自己的线路#
使用案例:个人资料、注释、帖子,其中每一行都属于通过 user_id 或 author_id的唯一用户。
alter table posts enable row level security;
create policy "posts_own_read" on posts
for select to aura_authenticated
using (auth.uid() = author_id);
create policy "posts_own_write" on posts
for all to aura_authenticated
using (auth.uid() = author_id)
with check (auth.uid() = author_id);
将 for all 与 using + with check 组合以在一项策略中涵盖 SELECT/UPDATE/DELETE。 INSERT 仅使用 with check。
多租户——按组织划分的范围#
使用案例:B2B SaaS,其中每个用户都属于一个组织,并可以查看其组织的所有数据。 JWT 携带 app_metadata.tenant, auth.tenant() 帮助器返回该值(NULL 当不存在或不是 UUID 时,因此策略拒绝)。
create policy "orders_tenant_scope" on orders
for all to aura_authenticated
using (tenant_id = auth.tenant())
with check (tenant_id = auth.tenant());
-- 性能不可或缺的指标
create index idx_orders_tenant on orders(tenant_id);
分层组织——按角色划分的权限#
使用案例:组织中的角色(管理员、成员、查看者)具有不同权限的 SaaS。我们将 auth.tenant() 与 auth.has_role()结合起来。
create policy "invoices_read" on invoices
for select to aura_authenticated
using (
tenant_id = auth.tenant()
and (auth.has_role('admin') or auth.has_role('billing'))
);
create policy "invoices_write" on invoices
for all to aura_authenticated
using (tenant_id = auth.tenant() and auth.has_role('admin'))
with check (tenant_id = auth.tenant());
pg_prove — 每个策略一个断言#
在 CI 中作为真实用户测试您的策略
策略是代码,所以要测试它们。 aura db raw ... --as authenticated --user <uuid> 在 PostgreSQL 内运行 SQL,并为该用户设置 auth.uid() 和 auth.role()(添加 --dry-run 以回滚写回),因此答案正是您的应用程序将看到的内容:
set -euo pipefail
ALICE=$(aura auth users create --email alice@example.com --password 'Passw0rd!long1' --json | jq -r .id)
BOB=$(aura auth users create --email bob@example.com --password 'Passw0rd!long1' --json | jq -r .id)
aura db raw "insert into posts(author_id, title) values ('$ALICE', 'hello')"
# 鲍勃不能看到爱丽丝的帖子
test "$(aura db raw "select count(*) as n from posts" --as authenticated --user "$BOB" --json | jq -r '.[0].n')" = "0"
# 爱丽丝必须看到它
test "$(aura db raw "select count(*) as n from posts" --as authenticated --user "$ALICE" --json | jq -r '.[0].n')" = "1"
# 访客什么也看不到
test "$(aura db raw "select count(*) as n from posts" --as anon --json | jq -r '.[0].n')" = "0"
在 aura db push之后在 CI 中运行它。 Studio 的 RLS 模拟器(数据库 › 表 › RLS 模拟器)在回滚的 PostgreSQL 事务中运行相同的检查,并在结果旁边注明; pgTAP 和 pg_prove 需要直接 PostgreSQL 连接,并非每个部署都会发布该连接。
当政策没有如你所愿时#
- 结果为空? 对具有 RLS 且没有匹配的
SELECT策略的表进行匿名或登录读取会返回[]以及meta.hint(指定要添加的角色和策略)。aura db raw "select * from posts" --as anon显示访问者所看到的内容。 - RLS 已禁用?
aura db table list用[RLS]标记表,并且 Advisor 对禁用 RLS 的表和带有 RLS 但没有策略的表发出警告。关闭 RLS (aura db table rls posts off) 使持有公钥的任何人都可以读取该表。 - 视图。 视图以读取器的权限进行读取(
security_invoker = true应用于您创建的每个视图),因此适用其表的策略。 - 加起来的策略:同一张表上的多个策略+操作+角色以 OR 组合。如有必要,在单个策略中使用 AND 进行限制
- 缺少 GRANT 权限:即使使用策略,角色也必须具有
grant select on posts to aura_authenticated - 声明未注入:来自 Postgres、
select current_setting('request.jwt.claims', true)— null 表示您位于直接数据库中,而不是通过网关