Files
SupplierSystem/server/tests/tenantRlsGuardDynamic.test.ts
Ubuntu 5fa2bbfd55
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
fix(audit-20261019): T1-T9 审计修复、存储归属与审计日志列宽
- T1 资金/库存/报价/自采 P1(BIZ-1/2/3/4/10/13/14)
- T2 主系统反向事件墓碑/改派/孤儿处理(BIZ-6/8/9)
- T3 选分公司 ticket、trust proxy、角色权限收口(SEC-2/3/4)
- T4 缺列/RLS/tenant 名单/FK/迁移执行器(DATA-1..8)
- T5 401 登出/分页/映射口径/blob/ZIP 中文名(FE-1..4/BIZ-15)
- T6 测试脚本目标守卫/config fail-fast/容器配置(OPS-2..8)
- T7 前后端测试与 lint 门禁清零
- T8 应付卡片按关联订单日(口径 A)
- T9 存储对象全链路归属校验、敏感列禁写、审计 target_id 超长修复
2026-10-19 16:16:51 +08:00

68 lines
2.6 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from 'vitest';
/**
* P2(T4 红队复攻,2026-10-19):启动 RLS 自检必须覆盖迁移后的 75 张,而不是硬编码 15 张。
* 真实 PG:全部开启 ⇒ ok=true;临时关掉任意一张(ai_prompt_overrides)⇒ ok=false;finally 恢复。
*/
type PgClient = {
connect: () => Promise<void>;
end: () => Promise<void>;
query: (sql: string, params?: unknown[]) => Promise<{ rows: Array<Record<string, unknown>>; rowCount?: number }>;
};
async function loadPgClient(): Promise<PgClient | null> {
const { loadEnv } = await import('../src/env');
loadEnv();
if (!process.env.DATABASE_URL) return null;
const pg = (await import('pg')).default as unknown as {
Client: new (cfg: { connectionString: string }) => PgClient;
};
const client = new pg.Client({ connectionString: process.env.DATABASE_URL });
try {
await client.connect();
} catch {
return null;
}
return client;
}
describe('P2:inspectTenantRls 动态覆盖(真库)', () => {
it('全部含 branch_code 的表开启 ⇒ ok=true;关掉 ai_prompt_overrides ⇒ br002 ok=false', async (ctx) => {
const client = await loadPgClient();
if (!client) {
ctx.skip();
return;
}
const { inspectTenantRls } = await import('../src/services/tenantRlsGuard');
const target = 'tenant_br002.ai_prompt_overrides';
try {
const before = await inspectTenantRls();
expect(before.length).toBeGreaterThan(0);
expect(before.every((row) => row.ok), JSON.stringify(before)).toBe(true);
// 动态覆盖:br002 的 need 应等于其含 branch_code 的表数(迁移后 75),而不是旧 15
const br002 = before.find((row) => row.schema === 'tenant_br002')!;
expect(br002.rlsOn).toBeGreaterThanOrEqual(75);
expect(br002.policies).toBeGreaterThanOrEqual(br002.rlsOn);
await client.query(`ALTER TABLE ${target} DISABLE ROW LEVEL SECURITY`);
const bad = await inspectTenantRls();
expect(bad.find((row) => row.schema === 'tenant_br002')?.ok, '关掉任意一张必须 ok=false').toBe(false);
} finally {
await client.query(`ALTER TABLE ${target} ENABLE ROW LEVEL SECURITY`).catch(() => undefined);
await client.query(`ALTER TABLE ${target} FORCE ROW LEVEL SECURITY`).catch(() => undefined);
await client.end();
}
const after = await (async () => {
const c2 = await loadPgClient();
if (!c2) return [] as Awaited<ReturnType<typeof inspectTenantRls>>;
try {
return await inspectTenantRls();
} finally {
await c2.end();
}
})();
expect(after.every((row) => row.ok)).toBe(true);
});
});