Operations Security - osama1998H/Moca GitHub Wiki
Security
Authentication methods, permission layers, and security architecture.
Security Layers
- Network: TLS termination at reverse proxy (Caddy/NGINX)
- Authentication: Session-based, JWT tokens, API keys
- Authorization: RBAC + FLS + RLS (see Permission Engine)
- Tenant Isolation: Schema-per-tenant with
search_pathenforcement - Input Validation: MetaType-driven field validation, parameterized queries
- Rate Limiting: Redis-backed sliding window per endpoint
- Encryption at Rest: Optional field encryption for
Passwordfields and encrypted backup artifacts - Audit Trail: Request logging with user/site context
Authentication Methods
| Method | Use Case | Status |
|---|---|---|
| Session (cookie) | Browser-based Desk access | Implemented |
| JWT Bearer token | API access | Implemented |
| API Key + Secret | Server-to-server | Planned (MS-18) |
| OAuth2 SSO | Browser login against a generic OAuth2 identity provider | Implemented |
| OIDC SSO | Browser login with discovery and ID token verification | Implemented |
| SAML 2.0 SSO | Enterprise browser login via SP metadata + ACS | Implemented |
For browser-authenticated clients:
POST /api/v1/auth/loginsetsmoca_sidandmoca_ridasHttpOnlycookies- login and refresh responses return only
access_tokenandexpires_in; the refresh token stays inmoca_rid POST /api/v1/auth/refreshcan read the refresh token frommoca_ridwhen the request body omitsrefresh_token, and it rotates the cookie on successPOST /api/v1/auth/logoutclears both cookies
Password Security
- bcrypt hashing with configurable work factor
- Password policy enforcement (min length, complexity)
- user account passwords remain hashed
- MetaType fields of type
Passwordcan additionally be encrypted at rest whenMOCA_ENCRYPTION_KEYis configured for the server
Field Encryption
When MOCA_ENCRYPTION_KEY is present, Moca enables transparent encryption for MetaType fields whose field_type is Password:
- values are encrypted before save using AES-256-GCM
- encrypted values are stored with an
enc:v1:prefix for versioning and idempotency - values are transparently decrypted when documents are loaded through the document manager
This protects application secrets stored in document data. It does not replace bcrypt hashing for login passwords, and it does not retroactively re-encrypt old rows unless they are saved again.
Single Sign-On
Enterprise SSO is configured per site with the builtin SSOProvider DocType. Supported provider_type values are OAuth2, OIDC, and SAML.
client_secretandsp_private_keyarePasswordfields, so they are encrypted at rest whenMOCA_ENCRYPTION_KEYis enabledauto_create_usercontrols whether first-time SSO users are provisioned automaticallydefault_roleis assigned during auto-provisioning when a new user is created- OAuth2 and OIDC flows start at
GET /api/v1/auth/sso/authorize?provider={name} - SAML uses
GET /api/v1/auth/saml/metadata?provider={name}for SP metadata andPOST /api/v1/auth/saml/acsfor assertion consumption - successful SSO login creates the
moca_sidsession cookie and redirects to/deskor the caller-provided relativeredirect_to - state tokens are stored in Redis and expire after 10 minutes
See REST API Reference for the browser endpoints.
Backup Encryption
moca backup create --encrypt encrypts backup files before they are written to sites/{site}/backups/. Restore auto-detects .enc files and requires the same key material. See Backup & Restore for the CLI flow and key resolution order.
SQL Injection Prevention
- All queries use parameterized statements via pgx
- QueryBuilder generates parameterized SQL ($1, $2, ...)
- No string interpolation in SQL