tenant-guard.extension.ts
3.03 KB
-
feat(tenant): 纵深防御 — Prisma 扩展按 ALS 上下文自动注入 tenantId/source_unit · 927021a0
- tenant-context.ts:请求级 AsyncLocalStorage(tenantId/sourceUnits) - tenant-guard.extension.ts:$extends 对 14 张 scoped 表的列表/聚合读查询自动注入 tenantId(+ source_unit 模型注品牌);findUnique 不注(破坏唯一键,by-id 走显式守卫); 无 ALS 上下文(系统任务)不注;$queryRaw 不经扩展(召回引擎自带隔离闸)。env PAC_TENANT_GUARD=warn 可切观察模式 - prisma.service:构造器 return this.$extends(...) 透明接入(services 不改) - tenant-scope.interceptor:handler 在 tenantAls.run 内执行(subscribe 包裹) 验证(已删一次性 CLI):注错 tenant→0、限品牌→该品牌数、系统任务不注、findUnique 不破。 关键:Prisma 惰性查询,await 须在 als.run 内 — interceptor 的 handler 正满足。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
luoqi committed