Files
SupplierSystem/server/tests/t3TrustProxyBoundary.test.ts
T
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

89 lines
3.5 KiB
TypeScript
Raw 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 { afterEach, describe, expect, it, vi } from 'vitest';
/**
* T3 独立边界复测 · SEC-3 trust proxy 解析与「实际应用到 Express」—— 2026-10-19,tester。
*
* 与 dev 的 `trustProxyConfig.test.ts` 独立:这里不只读 `config`,还用 `createApp()` 断言
* **Express 实际生效的 trust proxy 值**(app.get('trust proxy')),并补大小写/空白/0/off 等边界。
*/
async function loadConfig(env: Record<string, string | undefined>) {
vi.resetModules();
delete process.env.TRUST_PROXY;
delete process.env.HOST;
for (const [key, value] of Object.entries(env)) {
if (value === undefined) delete process.env[key];
else process.env[key] = value;
}
const mod = await import('../src/config');
return mod.config;
}
async function loadAppTrustProxy(env: Record<string, string | undefined>) {
vi.resetModules();
delete process.env.TRUST_PROXY;
delete process.env.HOST;
for (const [key, value] of Object.entries(env)) {
if (value === undefined) delete process.env[key];
else process.env[key] = value;
}
const mod = await import('../src/index');
const app = mod.createApp();
return app.get('trust proxy');
}
afterEach(() => {
delete process.env.TRUST_PROXY;
delete process.env.HOST;
vi.resetModules();
});
describe('T3/SEC-3:TRUST_PROXY 解析边界', () => {
it('默认(不配置/空白)⇒ loopback,HOST 默认 127.0.0.1(绝不默认 true/1)', async () => {
for (const env of [{}, { TRUST_PROXY: ' ' }]) {
const config = await loadConfig(env);
expect(config.trustProxy).toBe('loopback');
expect(config.host).toBe('127.0.0.1');
}
});
it('false 的多种写法(false/FALSE/off/OFF/0)⇒ false', async () => {
for (const raw of ['false', 'FALSE', 'off', 'OFF', '0']) {
expect((await loadConfig({ TRUST_PROXY: raw })).trustProxy, raw).toBe(false);
}
});
it('数字跳数 / 关键字大小写 / 单个 CIDR / CIDR 列表', async () => {
expect((await loadConfig({ TRUST_PROXY: '2' })).trustProxy).toBe(2);
expect((await loadConfig({ TRUST_PROXY: 'LoopBack' })).trustProxy).toBe('loopback');
expect((await loadConfig({ TRUST_PROXY: 'UNIQUELOCAL' })).trustProxy).toBe('uniquelocal');
expect((await loadConfig({ TRUST_PROXY: '10.1.0.0/16' })).trustProxy).toBe('10.1.0.0/16');
expect((await loadConfig({ TRUST_PROXY: ' 10.0.0.0/8 , 192.168.0.11 ' })).trustProxy).toEqual([
'10.0.0.0/8',
'192.168.0.11',
]);
});
it('显式 true 才全信任;HOST 仅显式覆盖才离开回环', async () => {
expect((await loadConfig({ TRUST_PROXY: 'true' })).trustProxy).toBe(true);
expect((await loadConfig({ TRUST_PROXY: 'TRUE' })).trustProxy).toBe(true);
expect((await loadConfig({ HOST: '0.0.0.0' })).host).toBe('0.0.0.0');
expect((await loadConfig({ HOST: '' })).host).toBe('127.0.0.1');
});
});
describe('T3/SEC-3:createApp 实际把 config 值应用到 Express', () => {
it('默认 ⇒ app.get(trust proxy) = loopback(不是 1/true)', async () => {
const actual = await loadAppTrustProxy({});
expect(actual).toBe('loopback');
expect(actual).not.toBe(1);
expect(actual).not.toBe(true);
}, 60000);
it('TRUST_PROXY=false ⇒ Express false;CIDR 列表 ⇒ Express 数组', async () => {
expect(await loadAppTrustProxy({ TRUST_PROXY: 'false' })).toBe(false);
const actual = await loadAppTrustProxy({ TRUST_PROXY: '10.0.0.0/8,192.168.0.11' });
expect(actual).toEqual(['10.0.0.0/8', '192.168.0.11']);
}, 60000);
});