返回提示词目录第 1428 页

审计并修复云数据库权限规则

检查数据库与存储的访问边界和字段权限,输出修正规则、模拟器测试及上线核验步骤。

完整提示词

<context>
In Firebase the client talks to the database directly, so the security rules are the whole backend authorization layer, and anyone can extract the project config from the app and query it with their own script. The usual holes: test-mode rules (`allow read, write: if true` or a time-based rule that was extended), `if request.auth != null` used as if it meant "the owner" (any signed-up user, including anonymous auth, passes), writes that accept any field so a user can set `role: "admin"` or `credits: 9999`, ownership taken from `request.resource.data.ownerId` on create without comparing it to `request.auth.uid`, `list` queries allowed where only `get` should be, Realtime Database rules where a `.read` higher in the tree cascades down and cannot be revoked by children, and Storage buckets open for any authenticated write with no size or content type limit. Rules are not filters: a query must be constrained so it can only return allowed documents. Admin SDK code on servers bypasses rules entirely, so those paths need their own checks.
</context>

<task>
Audit the rules for: {{project}}

1. Read `firestore.rules`, `database.rules.json`, `storage.rules` and `firebase.json`, and the client code that reads and writes data, to build a map of collections or paths, who accesses them and how.
2. For each match block or path, decide whether reads (`get` and `list` separately) and writes (`create`, `update`, `delete` separately) match the intended access. Flag catch-all matches (`/{document=**}`), `if true`, auth-only checks, cascading RTDB grants, and expired or soon-to-expire time-based rules.
3. Check writes for field control: allowed keys (`request.resource.data.keys().hasOnly(...)` or `diff().affectedKeys()`), types and sizes, immutable fields (owner, created time, role), and server-owned fields such as balances or roles that clients must never set.
4. Check Storage paths for owner scoping, size limits and content type checks.
5. Note any Cloud Functions or server code using the Admin SDK on user input, since rules do not protect those paths, and whether App Check is enabled.
6. Write fixed rules using helper functions (`isSignedIn`, `isOwner`, `hasRole`) and the smallest change that keeps legitimate client queries working.
7. Write emulator tests with `@firebase/rules-unit-testing` covering an allowed and a denied case per rule, including another user's document, privilege-field writes and unauthenticated access. Run them with the emulator.
</task>

<constraints>
- Do not deploy rules; give the deploy command for the user to run after review.
- Do not test against the live project or other people's projects.
- Do not suggest hiding the config as protection.
- Do not invent the app's access model; ask when the intended access for a path is unclear.
{{> guardrails/investigate-before-answering}}
{{> guardrails/verify-before-done}}
</constraints>

<output_format>
## Data and rules map
Table: path, intended access, current rule, verdict.
## Findings
Ranked list: severity, path, what an attacker can do, fix.
## Fixed rules
The full revised rules files in code blocks.
## Tests
One line per test case.
## Verification
Emulator commands run and actual results.
## Rollout
Order of deployment and which client queries to check before deploying.
</output_format>

可替换内容

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

分类与标签

编程开发、云数据库安全、权限审计、安全规则、模拟器测试

前往 AI 图片创作