返回提示词目录第 1311 页

审计并修复Supabase访问权限

排查数据库、存储与边缘函数的权限暴露,产出修复迁移、跨用户访问测试及配置清单。

完整提示词

<context>
Every table, view and function in a schema Supabase exposes through its API (by default `public`) can be called by anyone holding the anon key, which ships in every client and is public by design. So the database's own controls are the authorization layer. The recurring holes: tables with RLS disabled, or enabled with a policy like `using (true)`; policies that trust `auth.jwt() -> 'user_metadata'`, which users can edit themselves; views, which run with the owner's rights and ignore RLS unless created with `security_invoker = true`; `security definer` functions in exposed schemas that anon can call through `rpc()` because `execute` is granted to `public` by default; the `service_role` key, which bypasses RLS, ending up in client bundles, mobile apps or public env variables; public storage buckets holding private files, or `storage.objects` policies not scoped to the owner's folder; and edge functions deployed with JWT verification off and no other check. Supabase's built-in security advisor catches some of these but not the logic of each policy.
</context>

<task>
Audit and fix this Supabase project: {{project}}

1. Read the migrations or a schema dump (`supabase db dump --schema-only` against the local stack, not production) and list every table, view, function and bucket in exposed schemas.
2. For each table: RLS enabled, and each policy's `using` and `with check` reviewed per operation against the intended access. Flag `true` policies, policies on `user_metadata`, missing `with check` on insert or update, and missing policies that make a feature fail silently.
3. Views: recreate with `security_invoker = true` or move them out of the exposed schema.
4. Functions: list `security definer` functions in exposed schemas; set `search_path`, revoke `execute` from `anon` and `public` where not needed, and add explicit checks on `auth.uid()` inside.
5. Keys: search client code, env files, build config and mobile projects for the service role key or database password (report by file, never print values). Confirm it is used only in server code.
6. Storage: list buckets, flag public ones that hold user files, and write `storage.objects` policies scoped to `auth.uid()` folders with size and type limits set on the bucket.
7. Edge functions: check each for JWT verification or its own auth, input validation, and use of the service role key only after an authorization check.
8. Write a migration with the fixes and SQL or pgTAP tests that run as `anon`, as user A and as user B, proving cross-user access is denied. Run them against the local Supabase stack.
</task>

<constraints>
- Work against the local stack or a branch database; never run changes against production.
- Never print keys or passwords; refer to them by name and location.
- If the service role key was exposed, say it must be rotated and that rotation will break servers using it until they are updated.
- Keep legitimate queries working; list any client query the new policies will change.
{{> guardrails/investigate-before-answering}}
{{> guardrails/verify-before-done}}
</constraints>

<output_format>
## Exposure map
Table: object, type, exposed to anon (yes or no), RLS or check, verdict.
## Findings
Ranked list: severity, object, attack, fix.
## Migration
The SQL migration in a code block.
## Tests
One line per test and the role it runs as.
## Verification
Commands run and actual results.
## Dashboard settings
Settings to change outside the repo (auth, API exposed schemas, key rotation), as a checklist.
</output_format>

可替换内容

  • {{project}}
  • {{> guardrails/investigate-before-answering}}
  • {{> guardrails/verify-before-done}}

分类与标签

编程开发、数据库权限、行级安全、密钥排查、安全测试

前往 AI 图片创作