Firestore REST API & Security
tosijs-platform provides a secure REST API for Firestore access through Cloud Functions. This approach keeps your client bundle small and centralizes security logic on the server.
Why REST Instead of Firestore SDK?
Traditional Approach (Firestore SDK in browser):
- ❌ Large bundle size (~300KB+ for Firebase SDK)
- ❌ Security rules in separate
.rulesfile - ❌ Limited validation capabilities
- ❌ Hard to implement field-level permissions
tosijs-platform Approach (REST API via Cloud Functions):
- ✅ Tiny client code (just fetch calls)
- ✅ Server-side validation and transformation
- ✅ Fine-grained access control (role-based, field-level)
- ✅ TypeScript throughout
- ✅ Easy to test and debug
Endpoints
/doc - Single Document Operations
CRUD operations for individual documents.
GET - Read Document
// Client code
const post = await fb.service.post['post-id'].get()
// Equivalent to
GET /doc/post/post-id
Authorization: Bearer <firebase-id-token>
Access Control:
- Checks user roles
- Finds highest role with
readpermission - Applies read filter (field masking, transformation)
- Returns filtered document or error
POST - Create Document
// Client code
const newPost = await fb.service.post.post({
title: 'My Post',
content: 'Hello world',
})
// Equivalent to
POST /doc/post
Authorization: Bearer <firebase-id-token>
Content-Type: application/json
{
"title": "My Post",
"content": "Hello world"
}
Processing:
- Validates data via collection's
validate()function - Checks unique field constraints
- Verifies user has
writepermission - Creates document with auto-generated ID
- Returns created document
PUT - Replace Document
// Client code
await fb.service.post['post-id'].put({
title: 'Updated Title',
content: 'New content',
})
// Equivalent to
PUT /doc/post/post-id
Authorization: Bearer <firebase-id-token>
Content-Type: application/json
{
"title": "Updated Title",
"content": "New content"
}
Processing:
- Validates new data
- Checks unique constraints (excluding current doc)
- Verifies write permission
- Replaces entire document
- Returns updated document
PATCH - Update Document
// Client code
await fb.service.post['post-id'].patch({
title: 'Just update the title',
})
// Equivalent to
PATCH /doc/post/post-id
Authorization: Bearer <firebase-id-token>
Content-Type: application/json
{
"title": "Just update the title"
}
Processing:
- Fetches existing document
- Merges with provided fields
- Validates merged data
- Checks unique constraints
- Updates document
- Returns updated document
DELETE - Delete Document
// Client code
await fb.service.post['post-id'].delete()
// Equivalent to
DELETE /doc/post/post-id
Authorization: Bearer <firebase-id-token>
Processing:
- Verifies write permission
- Deletes document
- Returns success
/docs - Collection Queries
Query and list documents from a collection.
GET - List Documents
// Client code
const posts = await fb.service.post.get()
// With limit
const latest = await fb.service.post.get(10)
// With specific fields
const titles = await fb.service.post.get(10, ['title', 'date'])
// With sorting
const byDate = await fb.service.post.get(10, false, 'date(desc)')
// Equivalent to
GET /docs/post?limit=10&fields=title,date&sort=date(desc)
Authorization: Bearer <firebase-id-token>
Parameters:
limit- Maximum number of documents (default: 100)fields- Array of field names orfalsefor all fieldssort- Sort expression:field(asc)orfield(desc)
Processing:
- Checks
listpermission for user's role - Applies list filter to each document
- Filters out documents that return errors
- Applies field filtering if specified
- Sorts results
- Returns array of documents
/user - Current User Info
Get current user's roles and permissions.
// Client code
const user = await fb.service.user.get()
// Returns
{
uid: 'user-id',
email: 'user@example.com',
roles: ['author', 'editor']
}
Security Model
Collection Configuration
Collections are defined in functions/src/ with security and validation rules:
import { COLLECTIONS } from './collections'
import { ROLES } from './roles'
import { ALL } from './access'
COLLECTIONS.post = {
// Unique field constraints
unique: ['path', 'title'],
// Server-side validation
validate: async (data, userRoles) => {
if (!data.title?.trim()) {
return new Error('Title is required')
}
// Transform data
data.slug = data.title.toLowerCase().replace(/\s+/g, '-')
return data
},
// Role-based access control
access: {
[ROLES.public]: {
// Public can read published posts only
read: async (post) => {
if (!post.date) {
return new Error('Not published')
}
// Hide author's email
delete post.authorEmail
return post
},
// Public can see published posts in lists
list: async (post) => {
return post.date ? post : undefined
},
},
[ROLES.author]: {
// Authors can read all posts
read: ALL,
// Authors can see all posts
list: ALL,
// Authors can create/edit their own posts
write: async (post, userRoles, userId) => {
if (post.author === userId) {
return post
}
return new Error('Can only edit own posts')
},
},
[ROLES.admin]: {
// Admins have full access
read: ALL,
write: ALL,
list: ALL,
},
},
}
Built-in Roles
Defined in functions/src/roles.ts:
export const ROLES = {
public: 'public', // Unauthenticated users
author: 'author', // Can create content
editor: 'editor', // Can edit others' content
admin: 'admin', // Can manage users and content
developer: 'developer', // Can access technical features
owner: 'owner', // Full access
}
Roles are hierarchical - access rules are checked in order from most restrictive to least restrictive.
Access Configuration Options
1. ALL - Full Access
read: ALL // Any user with this role can read
2. Field Map - Restrict Fields
read: {
title: true,
content: true,
author: true,
// authorEmail is hidden
}
3. Function - Dynamic Access
read: async (doc, userRoles, userId) => {
// Return modified doc
if (doc.isPrivate && doc.author !== userId) {
return new Error('Access denied')
}
delete doc.internalNotes
return doc
}
// For list operations
list: async (doc, userRoles, userId) => {
// Return doc to include in list
// Return undefined to exclude
// Return Error to exclude silently
if (doc.isPublished) {
return doc
}
return undefined
}
Validation Function
The validate function runs before any write operation:
validate: async (data, userRoles) => {
// Check required fields
if (!data.name) {
return new Error('Name is required')
}
// Transform data
data.slug = slugify(data.name)
data.updatedAt = new Date().toISOString()
// Prevent privilege escalation
if (data.role === 'owner' && !userRoles.includes('owner')) {
return new Error('Cannot create owner users')
}
// Return modified data
return data
}
Unique Constraints
Enforce uniqueness across the collection:
COLLECTIONS.user = {
unique: ['email', 'username'],
// ...
}
Unique checks:
- Run after validation
- Case-sensitive
- Checked on POST, PUT, PATCH
- Exclude current document on updates
Document Caching
For frequently-read, rarely-changing documents (like configuration), enable TTL caching:
COLLECTIONS.config = {
cacheLatencySeconds: 300, // Cache for 5 minutes
access: {
[ROLES.public]: {
read: ALL,
list: ALL,
},
// ...
},
}
How it works:
- Documents are cached in memory after the first read
- Subsequent reads return cached data until TTL expires
- Cache is per Cloud Function instance
- Cache clears on cold starts (new instances)
When to use:
- Configuration documents that change rarely
- Settings that can tolerate 1-5 minutes of staleness
- High-traffic reads where Firestore latency matters
When NOT to use:
- User-specific data
- Frequently updated documents
- Data where freshness is critical
Example savings:
- Without cache: 10K requests/day × 5ms = ~50 seconds latency, ~$0.06 Firestore costs
- With 5-minute cache: ~288 Firestore reads/day, negligible latency and costs
Sub-Collections
Define access for nested collections:
// Top-level posts
COLLECTIONS.post = { /* ... */ }
// Comments on posts
COLLECTIONS['post/comment'] = {
access: {
[ROLES.public]: {
read: ALL,
list: ALL,
},
[ROLES.author]: {
write: ALL,
},
},
}
// Access as:
// /doc/post/{postId}/comment/{commentId}
// /docs/post/{postId}/comment
Client Usage
The firebase.ts client creates a dynamic proxy:
// Auto-generates REST calls
fb.service.post.get() // GET /docs/post
fb.service.post['123'].get() // GET /doc/post/123
fb.service.post.post({ title: 'Hi' }) // POST /doc/post
fb.service.post['123'].put({ ... }) // PUT /doc/post/123
fb.service.post['123'].patch({ ... }) // PATCH /doc/post/123
fb.service.post['123'].delete() // DELETE /doc/post/123
// Sub-collections
fb.service.post['123'].comment.get() // GET /docs/post/123/comment
Error Handling
Errors share one shape. error is a stable code; message is prose that may be
reworded:
{ "error": "schema", "message": "schema validation failed",
"details": [{ "path": "title", "message": "required" }] }
Switch on error, never on message. The full list of codes, their HTTP statuses and when
each fires is the Errors table in BETA.md, which is the 0.2.x
contract (a test keeps that table and the code set identical). A 404 not-found also covers
"not visible to you" for non-privileged callers, by design.
Performance Considerations
Caching
Use the built-in cache system for expensive queries:
import { cachedQuery } from './cached-query'
const result = await cachedQuery(
'blog-latest', // Cache key
60 * 60 * 1000, // TTL: 1 hour
async () => { // Query function
return await getDocs(req, res, 'post', 10)
}
)
Pagination
Limit queries to avoid timeouts:
// Good: limited query
const posts = await fb.service.post.get(20)
// Bad: unlimited query (can timeout)
const allPosts = await fb.service.post.get(false)
Field Filtering
Request only needed fields:
// Good: only get needed fields
const titles = await fb.service.post.get(10, ['title', 'date'])
// Less efficient: get all fields
const posts = await fb.service.post.get(10)
Security Best Practices
- Always validate server-side - Never trust client data
- Use validation functions - Transform and sanitize before storage
- Field-level filtering - Hide sensitive data from unauthorized users
- Unique constraints - Prevent duplicate emails, usernames, etc.
- Role hierarchy - Order access rules from restrictive to permissive
- Error messages - Don't leak sensitive information
- Rate limiting - Consider adding rate limits for production
Testing Collections
Test your collection configuration:
// In functions/src/collections.ts
COLLECTIONS.test = {
validate: async (data) => {
if (data.isInvalid) {
return new Error('Invalid data')
}
data.processed = true
return data
},
unique: ['uniqueField'],
access: {
[ROLES.public]: {
read: async (data) => {
delete data.secret
return data
},
write: ALL,
list: ALL,
},
},
}
Then test with your client or curl:
# Create test document
curl -X POST https://YOUR-PROJECT.cloudfunctions.net/doc/test \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "Content-Type: application/json" \
-d '{"uniqueField": "test", "secret": "hidden"}'
# Read it back (secret should be removed)
curl https://YOUR-PROJECT.cloudfunctions.net/doc/test/DOCUMENT_ID \
-H "Authorization: Bearer YOUR_TOKEN"
Further Reading
- functions/src/access.ts - Access control implementation
- functions/src/doc.ts - Document endpoint
- functions/src/docs.ts - Collection query endpoint
- functions/src/roles.ts - Role definitions