supabase
Handles the full Supabase workflow from schema changes to deployment, with built-in security guardrails that catch common traps like RLS...
firestore-security-rules-generation
Firestore Security Rules patterns for user-scoped access, RBAC with custom claims, multi-tenant isolation, field validation, immutable fields, and testing strategies. Includes rules syntax and best practices. Keywords: "security rules", "rbac", "multi-tenant", "validation", "firestore rules", "ac...
Full skill instructions
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_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
// Rules go here
}
}
// Exact match
match /users/alice { }
// Wildcard (single document)
match /users/{userId} { }
// Recursive wildcard (all documents in subcollections)
match /users/{userId}/{document=**} { }
allow read; // get() and list() operations
allow write; // create(), update(), delete() operations
// Or be specific:
allow get; // Read single document
allow list; // Query/list documents
allow create; // Create document
allow update; // Update document
allow delete; // Delete document
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;
}
match /posts/{postId} {
allow read: if request.auth != null;
allow create: if request.auth != null;
}
match /posts/{postId} {
allow read: if true; // Anyone can read
allow create: if request.auth != null; // Must be logged in to create
allow update, delete: if request.auth.uid == resource.data.authorId;
}
Set Claims (Server-Side):
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');
}
function hasAnyRole(roles) {
return isAuthenticated() && request.auth.token.role in roles;
}
match /moderation/{docId} {
allow read, write: if hasAnyRole(['admin', 'moderator']);
}
Data Structure:
{
id: "post1",
tenantId: "tenant_abc",
content: "...",
}
Set Tenant Claim (Server-Side):
await adminAuth.setCustomUserClaims(uid, { tenantId: 'tenant_abc' });
Rules:
function belongsToTenant(tenantId) {
return isAuthenticated() && request.auth.token.tenantId == tenantId;
}
match /posts/{postId} {
allow read, write: if belongsToTenant(resource.data.tenantId);
}
// For creates (resource.data doesn't exist yet)
match /posts/{postId} {
allow create: if isAuthenticated()
&& request.resource.data.tenantId == request.auth.token.tenantId;
}
tenants/tenant_abc/posts/post1
tenants/tenant_abc/users/user1
Rules:
match /tenants/{tenantId}/{document=**} {
allow read, write: if request.auth.token.tenantId == tenantId;
}
function hasRequiredFields(fields) {
return request.resource.data.keys().hasAll(fields);
}
match /posts/{postId} {
allow create: if hasRequiredFields(['title', 'content', 'authorId', 'createdAt']);
}
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;
}
match /posts/{postId} {
allow create, update: if request.resource.data.status in ['draft', 'published', 'archived'];
}
function isImmutable(field) {
return request.resource.data[field] == resource.data[field];
}
match /posts/{postId} {
allow update: if isImmutable('authorId')
&& isImmutable('createdAt');
}
match /posts/{postId} {
allow create: if request.resource.data.title.size() >= 1
&& request.resource.data.title.size() <= 200
&& request.resource.data.content.size() >= 10
&& request.resource.data.content.size() <= 10000;
}
match /comments/{commentId} {
allow create: if exists(/databases/$(database)/documents/posts/$(request.resource.data.postId));
}
match /posts/{postId}/comments/{commentId} {
allow read: if exists(/databases/$(database)/documents/posts/$(postId));
allow create: if request.auth != null
&& exists(/databases/$(database)/documents/posts/$(postId));
}
match /posts/{postId} {
// Anyone can read published posts
allow read: if resource.data.status == 'published';
// Author can read their own posts (any status)
allow read: if request.auth.uid == resource.data.authorId;
// Admins can read all
allow read: if isAdmin();
}
match /posts/{postId} {
allow create: if request.auth != null
&& request.time < resource.data.lastPostTime + duration.value(1, 'm'); // 1 minute cooldown
}
Note: Firestore rules don't have built-in rate limiting. Use Cloud Functions for accurate rate limiting.
match /posts/{postId} {
// Prevent actual deletion
allow delete: if false;
// Only allow marking as deleted
allow update: if request.auth.uid == resource.data.authorId
&& request.resource.data.diff(resource.data).affectedKeys().hasOnly(['deletedAt']);
}
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();
}
// ========================================================================
// COMMENTS COLLECTION
// ========================================================================
match /comments/{commentId} {
allow read: if true;
allow create: if isAuthenticated()
&& request.resource.data.authorId == request.auth.uid
&& exists(/databases/$(database)/documents/posts/$(request.resource.data.postId))
&& request.resource.data.content.size() > 0
&& request.resource.data.content.size() <= 1000;
allow update: if isOwner(resource.data.authorId)
&& isImmutable('authorId')
&& isImmutable('postId');
allow delete: if isOwner(resource.data.authorId) || isAdmin();
}
}
}
// firestore.rules.spec.ts
import { assertFails, assertSucceeds } from '@firebase/rules-unit-testing';
import { initializeTestEnvironment } from '@firebase/rules-unit-testing';
let testEnv;
beforeAll(async () => {
testEnv = await initializeTestEnvironment({
projectId: 'test-project',
firestore: {
rules: fs.readFileSync('firestore.rules', 'utf8'),
},
});
});
test('User can read their own profile', async () => {
const alice = testEnv.authenticatedContext('alice');
await assertSucceeds(alice.firestore().collection('users').doc('alice').get());
});
test('User cannot read other profiles', async () => {
const alice = testEnv.authenticatedContext('alice');
await assertFails(alice.firestore().collection('users').doc('bob').get());
});
# Deploy rules only
firebase deploy --only firestore:rules
# Validate rules before deploying
firebase firestore:rules:release
✅ Do:
❌ Don't:
allow read, write: if true in production (except truly public data)Issue: resource.data is null on create
Solution: Use request.resource.data for creates
Issue: "Property is undefined" error
Solution: Check field exists first: 'field' in resource.data
Issue: Rules too slow
Solution: Avoid exists() and get() calls, use custom claims instead
Related Skills: firebase-authentication-patterns, firebase-admin-sdk-server-integration
Token Estimate: ~1,400 tokens
Handles the full Supabase workflow from schema changes to deployment, with built-in security guardrails that catch common traps like RLS...
Comprehensive guides and best practices for Neon Serverless Postgres, covering setup, connection methods, authentication, and platform APIs.
Guide for setting up and using Firebase Authentication. Use this skill when the user's app requires user sign-in, user management, or secure data access using auth rules.
A skill to evaluate how secure Firestore security rules are. Use this when Firestore security rules are updated to ensure that the generated rules are extremely secure and robust.
Official skill for integrating Firebase AI Logic (Gemini API) into web applications. Covers setup, multimodal inference, structured output, and security.
Complete Better Auth server and client setup with database adapters, session management, plugins, and security configuration.
Deploy and manage projects on Vercel using token-based authentication. Use when working with Vercel CLI using access tokens rather than interactive login — e.g. "deploy to vercel", "set up vercel", "add environment variables to vercel".
Complete Cloudflare platform integration with decision trees for compute, storage, AI, networking, security, and infrastructure-as-code.
Run Azure compliance and security audits with azqr plus Key Vault expiration checks. Covers best-practice assessment, resource review, policy/compliance validation, and security posture checks. WHEN: compliance scan, security audit, BEFORE running azqr (compliance cli tool), Azure best practices, Key Vault expiration check, expired certificates, expiring secrets, orphaned resources, compliance assessment.
Send, read, and manage Gmail messages, drafts, labels, and account settings.
Comprehensive website auditing across 230+ rules in 21 categories including SEO, performance, security, and accessibility.
Shared authentication, CLI syntax, and output formatting patterns for gws Google Workspace commands.
Discover your celebrity doppelgänger with StarByFace!
GeoSpy: Pricing, Features, FAQs, and Alternatives for AI Teams
Detect AI-generated voices to protect against audio fraud.
Ensure Your Content's Originality with AI Plagiarism Checker
Upscale images by 400% without quality loss
Protect your social media from copyright disputes
Accurately Detect AI-Generated Content with TheChecker.AI
Chrome extension that detects and flags AI-generated content