Overview
SnackBase security operates on multiple layers to ensure data protection:| Layer | Purpose | Mechanism |
|---|---|---|
| Authentication | Verify user identity | JWT tokens, Argon2id password hashing |
| Account Isolation | Separate tenant data | Row-level filtering via account_id |
| Authorization | Control user actions | RBAC + Permission system |
| Field-Level Security | Hide sensitive data | Field-level access control |
| Audit Logging | Track all actions | Immutable audit logs (coming soon) |
Authentication vs Authorization
Understanding the distinction is critical:| Aspect | Authentication | Authorization |
|---|---|---|
| Question | Who are you? | What can you do? |
| Mechanism | JWT tokens, passwords | Roles, permissions, rules |
| Timing | Once per session | Every request |
| Failure Result | 401 Unauthorized | 403 Forbidden |
Example Scenario
Role-Based Access Control (RBAC)
SnackBase uses RBAC as the foundation of authorization.RBAC Hierarchy
Default Roles
| Role | Description | Typical Permissions |
|---|---|---|
| admin | Full administrative access | All operations on all collections |
| editor | Content creator/manager | Create, Read, Update on specific collections |
| viewer | Read-only access | Read on specific collections |
Custom Roles
You can create custom roles for any purpose:Permission System
Permissions define what operations a user can perform on which collections.Permission Matrix
For a role with permissions:| Collection | Create | Read | Update | Delete |
|---|---|---|---|---|
| posts | ✅ | ✅ | ✅ | ❌ |
| comments | ✅ | ✅ | ✅ | ✅ |
| users | ❌ | ✅ | ❌ | ❌ |
Permission Structure
Wildcard Collections
Use* to grant permissions on all collections:
Permission Caching
Permissions are cached for 5 minutes to improve performance:Rule Engine
SnackBase includes a powerful rule engine for fine-grained access control.Rule Syntax
Rules use a custom DSL (Domain Specific Language):Built-in Functions
| Function | Description | Example |
|---|---|---|
@has_role(role) | User has specific role | @has_role("admin") |
@has_any_role([roles]) | User has any of these roles | @has_any_role(["admin", "moderator"]) |
@owns_record() | User created this record | @owns_record() |
@is_superadmin() | User is superadmin | @is_superadmin() |
Rule Evaluation Context
Rules have access to:| Variable | Description | Example |
|---|---|---|
user | Current user object | user.id, user.email |
record | Record being accessed | record.created_by, record.status |
context | Request context | context.account_id |
Permission Rules Example
- They created the post AND status is draft/pending, OR
- They have admin role
Field-Level Security
SnackBase supports field-level access control to hide sensitive data.Field Visibility
Restrict which fields a role can see:Field-Level Rules
Apply rules to specific fields:Account Isolation
Account isolation is the foundation of SnackBase security.Multi-Tenant Isolation
All data is automatically isolated byaccount_id:
Enforcement Layers
Account isolation is enforced at multiple layers:| Layer | Mechanism | Example |
|---|---|---|
| Database | account_id column in WHERE clause | WHERE account_id = ? |
| Repository | Automatic filtering in queries | posts.find_all(context) |
| API Middleware | Validates account in token | Token contains account_id |
| Hooks | Built-in account_isolation_hook | Cannot be disabled |
Cross-Account Access Prevention
Attempting to access another account’s data:account_id parameters.
Security Best Practices
1. Principle of Least Privilege
Grant minimum required permissions:2. Use Rules for Fine-Grained Control
Leverage the rule engine for complex scenarios:3. Implement Field-Level Security
Hide sensitive fields by default:4. Regular Permission Audits
Periodically review and update permissions:- Remove unused roles
- Tighten overly permissive rules
- Document permission rationale
- Use audit logs (when available) to track access
5. Use Wildcards Carefully
Wildcard permissions (*) are powerful but dangerous:
6. Test Permission Changes
Always test permission changes in development:7. Monitor and Alert
Monitor for suspicious activity:- Repeated failed authorization attempts
- Unusual access patterns
- Permission escalation attempts
- Cross-account access attempts
Common Security Scenarios
Scenario 1: User Can Only Edit Their Own Posts
Scenario 2: Moderators Can Edit All Comments
Scenario 3: Public Read, Private Write
Summary
| Concept | Key Takeaway |
|---|---|
| Security Layers | Authentication → Account Isolation → Authorization → Field-Level Security → Audit |
| Authentication vs Authorization | Authentication = Who are you? Authorization = What can you do? |
| RBAC | Users → Roles → Permissions → Collections |
| Permission System | CRUD permissions per collection, wildcard support, 5-minute cache |
| Rule Engine | Custom DSL for fine-grained control with built-in functions |
| Field-Level Security | Hide sensitive fields, field-specific rules |
| Account Isolation | Automatic via account_id, enforced at multiple layers |
| Best Practices | Least privilege, use rules, hide sensitive data, audit permissions |