Files
SupplierSystem/server/tests/trustProxyConfig.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

94 lines
3.9 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 { readFileSync } from 'node:fs';
import path from 'node:path';
import { afterEach, describe, expect, it, vi } from 'vitest';
/**
* SEC-3(2026-10-19):trust proxy 收窄 + 监听地址默认回环。
*
* 漏洞:`index.ts` 原先无条件 `app.set('trust proxy', 1)` ⇒ 直连客户端用
* `X-Forwarded-For` 就能伪造 `req.ip`,按 IP 的登录限流/审计随之绕过。
* 修复:`TRUST_PROXY` 支持 false/loopback/N/CIDR 列表,**默认 loopback**(绝不默认 true/1);
* `HOST` 默认 `127.0.0.1`(容器显式 0.0.0.0 覆盖,compose 由 T6 负责)。
*
* 注:`loginKeyGenerator` 未改动(已只读 req.ip,由 `loginAuditIpHardening.test.ts` 钉住)。
*/
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;
}
afterEach(() => {
delete process.env.TRUST_PROXY;
delete process.env.HOST;
vi.resetModules();
});
describe('SEC-3:TRUST_PROXY 解析(默认 loopback,绝不默认全信任)', () => {
it('未配置 ⇒ loopback(不是 true/1),HOST 默认 127.0.0.1', async () => {
const config = await loadConfig({});
expect(config.trustProxy).toBe('loopback');
expect(config.trustProxy).not.toBe(true);
expect(config.trustProxy).not.toBe(1);
expect(config.host).toBe('127.0.0.1');
});
it('TRUST_PROXY=false ⇒ false(不信任任何代理)', async () => {
expect((await loadConfig({ TRUST_PROXY: 'false' })).trustProxy).toBe(false);
});
it('TRUST_PROXY=1 ⇒ 数字跳数 1(显式配置才允许)', async () => {
expect((await loadConfig({ TRUST_PROXY: '1' })).trustProxy).toBe(1);
});
it('TRUST_PROXY=loopback / 显式 true 原样解析', async () => {
expect((await loadConfig({ TRUST_PROXY: 'loopback' })).trustProxy).toBe('loopback');
expect((await loadConfig({ TRUST_PROXY: 'true' })).trustProxy).toBe(true);
});
it('TRUST_PROXY=CIDR 列表 ⇒ 数组(外部 LB 场景显式配置)', async () => {
const config = await loadConfig({ TRUST_PROXY: '10.0.0.0/8, 192.168.0.11' });
expect(config.trustProxy).toEqual(['10.0.0.0/8', '192.168.0.11']);
});
it('单个 CIDR ⇒ 字符串', async () => {
expect((await loadConfig({ TRUST_PROXY: '10.0.0.0/8' })).trustProxy).toBe('10.0.0.0/8');
});
it('HOST=0.0.0.0 ⇒ 显式覆盖(只允许显式,不是默认值)', async () => {
expect((await loadConfig({ HOST: '0.0.0.0' })).host).toBe('0.0.0.0');
});
});
describe('SEC-3:index.ts 使用 config 值,不再硬编码 trust proxy / 0.0.0.0', () => {
const source = readFileSync(path.join(__dirname, '../src/index.ts'), 'utf8');
it('app.set trust proxy 取 config.trustProxy', () => {
expect(source).toContain("app.set('trust proxy', config.trustProxy)");
expect(source).not.toMatch(/app\.set\(\s*['"]trust proxy['"]\s*,\s*1\s*\)/);
expect(source).not.toMatch(/app\.set\(\s*['"]trust proxy['"]\s*,\s*true\s*\)/);
});
it('app.listen 用 config.host 且日志打印实际 host', () => {
expect(source).toMatch(/app\.listen\(config\.port,\s*config\.host/);
expect(source).toContain('API server running on ${config.host}:${config.port}');
expect(source).not.toContain("app.listen(config.port, '0.0.0.0'");
});
it('loginKeyGenerator 未被改动(仍只用 req.ip)', () => {
const start = source.indexOf('function loginKeyGenerator');
expect(start).toBeGreaterThan(-1);
const body = source.slice(start, start + 800);
expect(body).toContain('req.ip');
// 注释里可提到该头(说明为何禁用),但不得出现取值表达式
expect(body).not.toMatch(/req\.headers\s*\[\s*['"]x-forwarded-for/i);
});
});