Skip to main content
Terra is built with security as a foundational principle. This document outlines our security architecture, threat model, and practices to ensure government-grade compliance and enterprise readiness.

🛡️ Table of Contents

  1. Security Architecture
  2. Threat Model
  3. Authentication & Authorization
  4. Data Protection
  5. Audit & Compliance
  6. Vulnerability Reporting
  7. Security Best Practices

🏗️ Security Architecture

Defense-in-Depth Strategy

Terra implements multiple layers of security controls:

Security Components

Middleware Security (src/middleware.ts)

  • Purpose: First line of defense for all HTTP requests
  • Controls:
    • CSP header injection with nonce-based script execution
    • Role-based route protection (admin vs. user)
    • Session decryption and validation
    • Multi-tenant domain routing
    • Redirect validation (prevents open redirects)
  • Fail-Secure: Defaults deny access, requires explicit session

Auth Guards (src/lib/auth-guards.ts)

  • requireAdmin() - Blocks non-admin users
  • checkFormAccess() - Verifies form ownership/team membership
  • verifySubmissionAccess() - Ensures submission ownership

Security Utilities (src/lib/security/)

  • Integration Validator - Prevents XSS in third-party integration IDs
  • Submission Guard - Verifies resource ownership for sensitive operations
  • Path Sanitization - Prevents path traversal in file operations

🎯 Threat Model

Assets Protected

Threat Actors

1. External Attackers (Untrusted)

  • Goal: Data theft, service disruption, unauthorized access
  • Mitigations:
    • Rate limiting on public endpoints
    • DDoS protection (CloudFlare)
    • Input validation on all server actions
    • CSP to prevent XSS
    • SQL injection prevention (parameterized queries via Supabase)

2. Malicious Insiders (Low-Privilege Users)

  • Goal: Privilege escalation, access other users’ data
  • Mitigations:
    • RBAC enforcement in middleware
    • Row-level security in database
    • Submission ownership verification
    • Audit logging of all actions
    • Fail-secure auth guards

3. Compromised Accounts (Legitimate Users)

  • Goal: Abuse stolen credentials
  • Mitigations:
    • Session expiration (configurable)
    • MFA enforcement (WorkOS)
    • Anomaly detection in audit logs
    • IP tracking and geo-blocking (optional)

4. Supply Chain Attacks (Dependencies)

  • Goal: Inject malicious code via npm packages
  • Mitigations:
    • Automated dependency scanning (npm audit, Snyk)
    • Lock files (pnpm-lock.yaml)
    • Regular security updates
    • Minimal dependency footprint

Attack Vectors & Mitigations


🔐 Authentication & Authorization

Authentication Flow (WorkOS SSO)

Role-Based Access Control (RBAC)

Authorization Enforcement

Middleware-Level (Route Protection)
Action-Level (Resource Protection)
Database-Level (Row-Level Security)

🔒 Data Protection

Encryption

In Transit

  • TLS 1.3 for all HTTPS connections
  • Certificate pinning (optional for high-security deployments)
  • No downgrade attacks - HSTS enforced

At Rest

  • Database: AES-256 encryption (Supabase managed)
  • File Storage: AES-256 encryption (Supabase Storage)
  • Sensitive Fields: Additional application-level encryption for:
    • Bank account numbers
    • Routing numbers
    • Social security numbers
    • Plaid access tokens

In Use

  • Memory: Secrets loaded from environment variables, never hardcoded
  • Logs: PII automatically redacted by logger (src/lib/logger.ts)

Data Classification

PII Handling

Terra follows GDPR/CCPA principles:
  1. Data Minimization - Only collect necessary data
  2. Purpose Limitation - Data used only for stated purpose
  3. Storage Limitation - Configurable retention policies
  4. Integrity & Confidentiality - Encryption + access controls
  5. Right to Erasure - Users can delete their data
PII Redaction in Logs:

📊 Audit & Compliance

Audit Logging

Every mutation operation is logged:
Audit Log Schema:
  • Who: user_id, user_email, user_role
  • What: action_type (create, update, delete, etc.)
  • Which: entity_type, entity_id
  • When: created_at (UTC timestamp)
  • Where: ip_address, user_agent
  • Why: metadata (context)
  • Changes: changes (before/after diff)
Audit Log Retention: 2 years minimum (configurable via migration 079)

Compliance Frameworks

SOC 2 Type II (System and Organization Controls)

  • CC6.1 - Logical access controls (RBAC, RLS)
  • CC6.6 - Audit logging and monitoring
  • CC6.7 - Encryption at rest and in transit
  • CC7.2 - Change management (audit logs + version control)

ISO 27001 (Information Security Management)

  • A.9 - Access control (authentication, authorization)
  • A.10 - Cryptography (TLS, AES-256)
  • A.12 - Operations security (logging, monitoring)
  • A.14 - System acquisition and development (secure SDLC)

GDPR (General Data Protection Regulation)

  • Article 25 - Privacy by design and default
  • Article 32 - Security of processing (encryption)
  • Article 33 - Breach notification (monitoring + alerts)
  • Article 35 - Data protection impact assessment (threat model)

HIPAA (Health Insurance Portability and Accountability Act)

  • ⚠️ Not HIPAA-certified - Additional BAA required for PHI
  • Technical safeguards in place (encryption, access controls)
  • Audit controls (comprehensive logging)

Compliance Reporting

Automated compliance reports available:
  • User access logs (who accessed what, when)
  • Form modification history (version control)
  • Submission exports (with audit trail)
  • Security event summary (failed logins, access denied)

🚨 Vulnerability Reporting

Responsible Disclosure Policy

We take security vulnerabilities seriously. If you discover a security issue: DO:
  • ✅ Report privately via GitHub Security Advisories
  • ✅ Provide detailed reproduction steps
  • ✅ Allow 90 days for remediation before public disclosure
  • ✅ Work with us to verify the fix
DON’T:
  • ❌ Publicly disclose before we’ve had a chance to fix
  • ❌ Exploit vulnerabilities beyond proof-of-concept
  • ❌ Access or modify other users’ data
  • ❌ Perform DOS/DDOS attacks

Reporting Channels

  1. GitHub Security Advisory (Preferred)
  2. Email: security@withunify.org (if GitHub unavailable)
    • Use PGP key: [Link to public key]
    • Include: Description, impact, reproduction steps

Response Timeline

Security Bounty

Currently no formal bug bounty program, but we recognize security researchers:
  • Public acknowledgment (with permission)
  • Swag/merchandise for significant findings
  • Potential monetary reward for critical vulnerabilities (case-by-case)

🔧 Security Best Practices (For Developers)

Code Review Checklist

Before submitting a PR, verify:
  • Authentication: All server actions have auth guards
  • Authorization: Resource ownership verified (checkFormAccess, verifySubmissionAccess)
  • Input Validation: All user input validated (zod schemas)
  • Output Encoding: User-generated content sanitized (DOMPurify)
  • SQL Injection: Using Supabase client (no raw SQL strings)
  • XSS Prevention: No dangerouslySetInnerHTML without sanitization
  • Path Traversal: File paths sanitized (sanitizeStoragePath)
  • Secrets: No hardcoded API keys or passwords
  • Logging: No PII in log messages
  • Error Messages: Generic errors to users (details in logs)
  • Rate Limiting: Considered for new public endpoints
  • Audit Logging: Mutations logged via audit-logger.ts

Secure Coding Patterns

✅ GOOD: Server Action with Auth

❌ BAD: Missing Auth / Validation

Security Testing

Run security checks locally:

📅 Security Update Policy

  • Critical vulnerabilities: Patched within 7 days
  • High vulnerabilities: Patched within 30 days
  • Dependency updates: Monthly review, quarterly updates
  • Security audits: Quarterly internal review, annual external audit

📚 Additional Resources


Last Updated: 2026-01-08 Security Contact: security@withunify.org Version: 1.0