Loading...
Loading...
Use when building secure AI pipelines or hardening LLM integrations. Defense-in-depth implements 8 validation layers from edge to storage with no single point of failure.
npx skill4agent add yonatangross/orchestkit defense-in-depth┌─────────────────────────────────────────────────────────────────────────┐
│ Layer 0: EDGE │ WAF, Rate Limiting, DDoS, Bot Detection │
├─────────────────────────────────────────────────────────────────────────┤
│ Layer 1: GATEWAY │ JWT Verify, Extract Claims, Build Context │
├─────────────────────────────────────────────────────────────────────────┤
│ Layer 2: INPUT │ Schema Validation, PII Detection, Injection│
│ │ + Tavily Prompt Injection Firewall (opt.) │
├─────────────────────────────────────────────────────────────────────────┤
│ Layer 3: AUTHORIZATION │ RBAC/ABAC, Tenant Check, Resource Access │
├─────────────────────────────────────────────────────────────────────────┤
│ Layer 4: DATA ACCESS │ Parameterized Queries, Tenant Filter │
├─────────────────────────────────────────────────────────────────────────┤
│ Layer 5: LLM │ Prompt Building (no IDs), Context Separation│
├─────────────────────────────────────────────────────────────────────────┤
│ Layer 6: OUTPUT │ Schema Validation, Guardrails, Hallucination│
├─────────────────────────────────────────────────────────────────────────┤
│ Layer 7: STORAGE │ Attribution, Audit Trail, Encryption │
├─────────────────────────────────────────────────────────────────────────┤
│ Layer 8: OBSERVABILITY │ Logging (sanitized), Tracing, Metrics │
└─────────────────────────────────────────────────────────────────────────┘@dataclass(frozen=True)
class RequestContext:
"""Immutable context that flows through the system"""
# Identity
user_id: UUID
tenant_id: UUID
session_id: str
permissions: frozenset[str]
# Tracing
request_id: str
trace_id: str
# Metadata
timestamp: datetime
client_ip: strasync def authorize(ctx: RequestContext, action: str, resource: Resource) -> bool:
# 1. Check permission exists
if action not in ctx.permissions:
raise Forbidden("Missing permission")
# 2. Check tenant ownership
if resource.tenant_id != ctx.tenant_id:
raise Forbidden("Cross-tenant access denied")
# 3. Check resource-level access
if not await check_resource_access(ctx.user_id, resource):
raise Forbidden("No access to resource")
return Trueclass TenantScopedRepository:
def __init__(self, ctx: RequestContext):
self.ctx = ctx
self._base_filter = {"tenant_id": ctx.tenant_id}
async def find(self, query: dict) -> list[Model]:
# ALWAYS merge tenant filter
safe_query = {**self._base_filter, **query}
return await self.db.find(safe_query)llm-safety-patterns| Pattern | Source | Application |
|---|---|---|
| Defense in Depth | NIST | Multiple validation layers |
| Zero Trust | Google BeyondCorp | Every request verified |
| Least Privilege | AWS IAM | Minimal permissions |
| Complete Mediation | Saltzer & Schroeder | Every access checked |
llm-safety-patternssecurity-checklistobservability-monitoringowasp-top-10auth-patternsinput-validationsecurity-scanning| Decision | Choice | Rationale |
|---|---|---|
| Context object | Immutable dataclass | Prevents accidental mutation, ensures consistent identity flow |
| Tenant isolation | Query-level filtering | Defense in depth - application layer + database constraints |
| LLM prompt security | No identifiers in prompts | IDs flow around LLM, not through it - prevents prompt injection leaks |
| Audit logging | Sanitized structured logs | Compliance requirements while preventing PII exposure |