Security
Overview

Security

AICR implements enterprise-grade security with multi-tenant isolation, policy enforcement, and comprehensive audit trails.

Tenant Isolation

RealmKey System

Every operation is scoped to a tenant via RealmKey:

class RealmKey {
  readonly tenantId: string;
  readonly env: 'lab' | 'stage' | 'prod';
 
  static async fromSlug(slug: string): Promise<RealmKey>;
  static async fromRequest(request: NextRequest): Promise<RealmKey>;
}

Tenant Context

AsyncLocalStorage-based context propagation:

import { withTenantContext } from '@/lib/auth/context';
 
const result = await withTenantContext(
  { userId: 'user-123', tenantId: 'tenant-abc' },
  async () => {
    // All Prisma queries automatically scoped to tenant
    return prisma.task.findMany();
  }
);

Prisma Middleware

Automatic tenant filtering on all database operations:

  • Query filtering adds tenantId to WHERE clauses
  • Create operations inject tenantId
  • Cross-tenant access blocked at database layer

Policy Enforcement

AI Interceptor

All AI model invocations go through 7-step gating:

  1. Context extraction
  2. Policy lookup
  3. Policy evaluation
  4. Risk classification
  5. Approval check
  6. Execution/proposal creation
  7. Evidence recording

Proposal Governance

Significant actions require formal proposals:

  • Automatic approval for low-risk actions
  • Human approval for high-risk actions
  • Audit trail for all decisions

Audit Trail (Spine)

Immutable event log for compliance:

  • All governance events recorded
  • Cryptographic integrity
  • Evidence chain for proposals
  • Compliance reporting

Security Headers

Standard security headers applied:

  • CORS restricted to allowed origins
  • Content-Type enforcement
  • X-Tenant-ID header required for API calls

Best Practices

  1. Always use RealmKey for tenant context
  2. Wrap database operations in withTenantContext
  3. Never expose tenant IDs in URLs
  4. Log all security-relevant events to Spine
  5. Use policy engine for access decisions