firestore-security-rules-generation
Firestore Security Rules patterns for user-scoped access, RBAC, and field validation. PROACTIVELY activate for: (1) implementing user-scoped data access rules, (2) setting up role-based access with custom claims, (3) validating fields and enforcing immutability. Triggers: "security rules", "rbac", "firestore rules"
What this skill does
# Firestore Security Rules Generation
## Overview
Firestore Security Rules define who can access what data and under what conditions. Rules are enforced at the database level, providing a critical security layer.
## Rules Syntax Fundamentals
### Basic Structure
```
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Rules go here
}
}
```
### Match Patterns
```
// Exact match
match /users/alice { }
// Wildcard (single document)
match /users/{userId} { }
// Recursive wildcard (all documents in subcollections)
match /users/{userId}/{document=**} { }
```
## Authentication Patterns
### User-Scoped Access
**Pattern**: Users can only access their own data
```
match /users/{userId} {
allow read, write: if request.auth.uid == userId;
}
match /profiles/{profileId} {
allow read: if true; // Public read
allow write: if request.auth.uid == resource.data.ownerId;
}
```
### Authenticated Only
```
match /posts/{postId} {
allow read: if request.auth != null;
allow create: if request.auth != null;
}
```
## Role-Based Access Control (RBAC)
### Custom Claims Pattern
**Set Claims** (Server-Side):
```typescript
await adminAuth.setCustomUserClaims(uid, { role: 'admin' });
```
**Rules**:
```
// Helper functions
function isAuthenticated() {
return request.auth != null;
}
function hasRole(role) {
return isAuthenticated() && request.auth.token.role == role;
}
function isAdmin() {
return hasRole('admin');
}
// Admin-only collection
match /admin-data/{document} {
allow read, write: if isAdmin();
}
// Role-based read access
match /posts/{postId} {
allow read: if resource.data.visibility == 'public'
|| isAdmin()
|| hasRole('moderator');
}
```
## Field Validation
### Required Fields
```
function hasRequiredFields(fields) {
return request.resource.data.keys().hasAll(fields);
}
match /posts/{postId} {
allow create: if hasRequiredFields(['title', 'content', 'authorId', 'createdAt']);
}
```
### Data Type Validation
```
match /posts/{postId} {
allow create: if request.resource.data.title is string
&& request.resource.data.title.size() > 0
&& request.resource.data.title.size() <= 200
&& request.resource.data.viewCount is int
&& request.resource.data.viewCount >= 0;
}
```
### Immutable Fields
```
function isImmutable(field) {
return request.resource.data[field] == resource.data[field];
}
match /posts/{postId} {
allow update: if isImmutable('authorId')
&& isImmutable('createdAt');
}
```
## Complete Example
```
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// HELPER FUNCTIONS
function isAuthenticated() {
return request.auth != null;
}
function isOwner(userId) {
return isAuthenticated() && request.auth.uid == userId;
}
function hasRole(role) {
return isAuthenticated() && request.auth.token.role == role;
}
function isAdmin() {
return hasRole('admin');
}
function hasRequiredFields(fields) {
return request.resource.data.keys().hasAll(fields);
}
function isImmutable(field) {
return request.resource.data[field] == resource.data[field];
}
// USERS COLLECTION
match /users/{userId} {
allow read: if isOwner(userId) || isAdmin();
allow create: if isOwner(userId)
&& hasRequiredFields(['email', 'displayName', 'createdAt']);
allow update: if isOwner(userId)
&& isImmutable('createdAt')
&& (!('role' in request.resource.data) || isImmutable('role'));
allow delete: if isAdmin();
}
// POSTS COLLECTION
match /posts/{postId} {
allow read: if resource.data.status == 'published'
|| isOwner(resource.data.authorId)
|| isAdmin();
allow create: if isAuthenticated()
&& request.resource.data.authorId == request.auth.uid
&& hasRequiredFields(['title', 'content', 'authorId', 'status', 'createdAt'])
&& request.resource.data.status in ['draft', 'published', 'archived']
&& request.resource.data.title is string
&& request.resource.data.title.size() > 0
&& request.resource.data.title.size() <= 200;
allow update: if (isOwner(resource.data.authorId) || isAdmin())
&& isImmutable('authorId')
&& isImmutable('createdAt');
allow delete: if isOwner(resource.data.authorId) || isAdmin();
}
}
}
```
## Deployment
```bash
# Deploy rules only
firebase deploy --only firestore:rules
# Validate rules before deploying
firebase firestore:rules:release
```
## Best Practices
**Do**:
- Default deny, explicitly allow
- Validate all user input (types, sizes, ranges)
- Use helper functions for reusable logic
- Test rules locally before deploying
- Document complex rules with comments
- Use custom claims for RBAC (not Firestore lookups)
- Enforce immutable fields (createdAt, authorId)
**Don't**:
- Use `allow read, write: if true` in production
- Perform Firestore lookups in rules (slow, limited to 10 per request)
- Store sensitive data in custom claims (1000 byte limit)
- Skip field validation
- Use rules for rate limiting (use Cloud Functions)
---
**Related Skills**: `firebase-authentication-patterns`, `firebase-admin-sdk-server-integration`
Related in Security
mac-ops
IncludedComprehensive macOS workstation operations — diagnose kernel panics, identify failing drives, audit launchd startup items, decode wake reasons, triage TCC permission denials, manage APFS snapshots, recover from no-boot. Use for: Mac is slow, slow bootup, won't boot, kernel panic, kernel_task hot, mds_stores CPU, photoanalysisd, cloudd, login loop, gray screen, sleep wake failure, drive failing, IO errors, APFS snapshots eating space, Time Machine local snapshots, Spotlight indexing, launchd, LaunchAgent, LaunchDaemon, login items, TCC permissions, Full Disk Access, Screen Recording denied, Gatekeeper, quarantine, com.apple.quarantine, app is damaged, helper tool, /Library/PrivilegedHelperTools, pmset, wake reasons, dark wake, sysdiagnose, panic.ips, DiagnosticReports, configuration profile, MDM profile, remote diagnostics over SSH.
a11y-audit
IncludedRun accessibility audits on web projects combining automated scanning (axe-core, Lighthouse) with WCAG 2.1 AA compliance mapping, manual check guidance, and structured reporting. Output is configurable: markdown report only, markdown plus machine-readable JSON, or markdown plus issue tracker integration. Use this skill whenever the user mentions "accessibility audit", "a11y audit", "WCAG audit", "accessibility check", "compliance scan", or asks to check a web project for accessibility issues. Also trigger when the user wants to verify WCAG conformance or map findings to a specific standard (CAN-ASC-6.2, EN 301 549, ADA/AODA).
erpclaw
IncludedAI-native ERP system with self-extending OS. Full accounting, invoicing, inventory, purchasing, tax, billing, HR, payroll, advanced accounting (ASC 606/842, intercompany, consolidation), and financial reporting. 413 actions across 14 domains, 43 expansion modules. Constitutional guardrails, adversarial audit, schema migration. Double-entry GL, immutable audit trail, US GAAP.
assess
IncludedAssesses and rates quality 0-10 across multiple dimensions (correctness, maintainability, security, performance, testability, simplicity) with pros/cons analysis. Compares against project conventions and prior decisions from memory. Produces structured evaluation reports with actionable improvement suggestions. Use when evaluating code, designs, architectures, or comparing alternative approaches.
spring-boot-security-jwt
IncludedProvides JWT authentication and authorization patterns for Spring Boot 3.5.x covering token generation with JJWT, Bearer/cookie authentication, database/OAuth2 integration, and RBAC/permission-based access control using Spring Security 6.x. Use when implementing authentication or authorization in Spring Boot applications.
code-hardcode-audit
IncludedDetect hardcoded values, magic numbers, and leaked secrets. TRIGGERS - hardcode audit, magic numbers, PLR2004, secret scanning.