Files
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

305 lines
12 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, vi } from 'vitest';
import { emitConfigWarnings, validateConfig } from '../src/config';
/**
* OPS-3(2026-10-19):启动配置 fail-fast。
*
* 缺陷:NODE_ENV 缺失/非法时静默按"非生产"跑;production 缺 DATABASE_URL/CORS/STORAGE_DIR
* 或 s3 缺 S3_KEY_PREFIX 时仍可启动(可能连错库、反射任意源、写进主系统目录/桶根)。
* 本测试只测**纯函数**(传入 env),不连接 DB、不读本地 .env。
*/
describe('OPS-3:validateConfig 纯函数', () => {
it('NODE_ENV 缺失 ⇒ 抛错并点名', () => {
expect(() => validateConfig({} as NodeJS.ProcessEnv)).toThrow(/NODE_ENV 缺失/);
});
it('NODE_ENV 非法 ⇒ 抛错', () => {
expect(() => validateConfig({ NODE_ENV: 'prod', DATABASE_URL: 'postgresql://x' } as never)).toThrow(
/NODE_ENV 非法/
);
});
it('production 缺 DATABASE_URL/CORS_ORIGINS/STORAGE_DIR ⇒ 一次性列出全部缺失项', () => {
expect(() => validateConfig({ NODE_ENV: 'production' } as never)).toThrow(
/DATABASE_URL[\s\S]*CORS_ORIGINS[\s\S]*STORAGE_DIR/
);
});
it('production 的 DATABASE_URL 不得使用开发默认值', () => {
expect(() =>
validateConfig({
NODE_ENV: 'production',
DATABASE_URL: 'postgresql://supplier:change-me@localhost:5432/supplier_delivery',
CORS_ORIGINS: 'https://supplier.example.com',
STORAGE_DIR: '/app/uploads',
} as never)
).toThrow(/开发默认值/);
});
it('production 的 STORAGE_DIR 不得指向主系统 server/uploads', () => {
expect(() =>
validateConfig({
NODE_ENV: 'production',
DATABASE_URL: 'postgresql://supplier:secret@db:5432/supplier_delivery',
CORS_ORIGINS: 'https://supplier.example.com',
STORAGE_DIR: '/srv/server/uploads',
} as never)
).toThrow(/server\/uploads/);
});
it('production 且 STORAGE_PROVIDER=s3 时 S3_KEY_PREFIX 必需', () => {
expect(() =>
validateConfig({
NODE_ENV: 'production',
DATABASE_URL: 'postgresql://supplier:secret@db:5432/supplier_delivery',
CORS_ORIGINS: 'https://supplier.example.com',
STORAGE_DIR: '/app/uploads',
STORAGE_PROVIDER: 's3',
} as never)
).toThrow(/S3_KEY_PREFIX/);
});
it('production 合法配置 ⇒ 通过且无警告', () => {
const result = validateConfig({
NODE_ENV: 'production',
DATABASE_URL: 'postgresql://supplier:secret@db:5432/supplier_delivery',
CORS_ORIGINS: 'https://supplier.example.com',
STORAGE_DIR: '/app/uploads',
STORAGE_PROVIDER: 's3',
S3_KEY_PREFIX: 'supplier-system',
} as never);
expect(result).toMatchObject({ nodeEnv: 'production', production: true, warnings: [] });
});
it('非 production 允许开发默认,但必须逐条 warn(不静默)', () => {
const result = validateConfig({ NODE_ENV: 'development' } as never);
expect(result.production).toBe(false);
const joined = result.warnings.join('\n');
expect(joined).toMatch(/DATABASE_URL.*默认值/);
expect(joined).toMatch(/STORAGE_DIR.*默认值/);
expect(joined).toMatch(/CORS_ORIGINS.*任意来源/);
});
it('NODE_ENV=test 下不打印告警(避免污染其它用例的 console.warn 断言)', () => {
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
try {
expect(emitConfigWarnings({ nodeEnv: 'test', production: false, warnings: ['x'] })).toEqual([]);
expect(warn).not.toHaveBeenCalled();
// 非 test 仍打印
expect(emitConfigWarnings({ nodeEnv: 'development', production: false, warnings: ['y'] })).toEqual(['y']);
expect(warn).toHaveBeenCalledWith('[config] y');
} finally {
warn.mockRestore();
}
});
it('非 production 显式配置 ⇒ 无"使用默认值"警告', () => {
const result = validateConfig({
NODE_ENV: 'test',
DATABASE_URL: 'postgresql://supplier:secret@127.0.0.1:5432/supplier_delivery',
CORS_ORIGINS: 'http://127.0.0.1:3021',
STORAGE_DIR: './uploads-supplier',
} as never);
expect(result.warnings).toEqual([]);
});
});
describe('B-12.1:S3_KEY_PREFIX 归一化绕过(红队复攻)', () => {
for (const prefix of ['/', '//', ' / ']) {
it(`production 下 S3_KEY_PREFIX=${JSON.stringify(prefix)} ⇒ 拒绝启动(运行时会归一成空)`, () => {
expect(() =>
validateConfig({
NODE_ENV: 'production',
DATABASE_URL: 'postgresql://supplier:secret@db:5432/supplier_delivery',
CORS_ORIGINS: 'https://supplier.example.com',
STORAGE_DIR: '/app/uploads',
STORAGE_PROVIDER: 's3',
S3_KEY_PREFIX: prefix,
} as never)
).toThrow(/S3_KEY_PREFIX/);
});
}
it('S3_KEY_PREFIX 带空白/斜杠的合法值 ⇒ 通过,且运行时前缀与校验同一归一化', async () => {
const result = validateConfig({
NODE_ENV: 'production',
DATABASE_URL: 'postgresql://supplier:secret@db:5432/supplier_delivery',
CORS_ORIGINS: 'https://supplier.example.com',
STORAGE_DIR: '/app/uploads',
STORAGE_PROVIDER: ' s3 ',
S3_KEY_PREFIX: ' supplier-system ',
} as never);
expect(result.production).toBe(true);
const saved = { ...process.env };
try {
process.env.NODE_ENV = 'development';
process.env.JWT_SECRET = 'test-secret';
process.env.DATABASE_URL = 'postgresql://supplier:secret@127.0.0.1:5432/supplier_delivery';
process.env.STORAGE_PROVIDER = ' S3 ';
process.env.S3_KEY_PREFIX = ' /supplier-system/ ';
vi.resetModules();
const mod = await import('../src/config');
expect(mod.config.storage.provider, 'STORAGE_PROVIDER 必须 trim+lower').toBe('s3');
expect(mod.config.storage.s3.keyPrefix, '运行时前缀必须与校验同归一化').toBe('supplier-system');
} finally {
for (const key of Object.keys(process.env)) {
if (!(key in saved)) delete process.env[key];
}
Object.assign(process.env, saved);
vi.resetModules();
}
});
it('STORAGE_PROVIDER=\' S3 \' 按 s3 校验(production 缺 S3_KEY_PREFIX 必须拒绝)', () => {
expect(() =>
validateConfig({
NODE_ENV: 'production',
DATABASE_URL: 'postgresql://supplier:secret@db:5432/supplier_delivery',
CORS_ORIGINS: 'https://supplier.example.com',
STORAGE_DIR: '/app/uploads',
STORAGE_PROVIDER: ' S3 ',
} as never)
).toThrow(/S3_KEY_PREFIX/);
});
});
describe('B-12.2:NODE_ENV 带空白必须归一后回写 process.env', () => {
it("NODE_ENV=' production' ⇒ 校验按 production 且 process.env.NODE_ENV 归一为 production", () => {
const saved = {
NODE_ENV: process.env.NODE_ENV,
DATABASE_URL: process.env.DATABASE_URL,
CORS_ORIGINS: process.env.CORS_ORIGINS,
STORAGE_DIR: process.env.STORAGE_DIR,
};
try {
process.env.NODE_ENV = ' production';
process.env.DATABASE_URL = 'postgresql://supplier:secret@db:5432/supplier_delivery';
process.env.CORS_ORIGINS = 'https://supplier.example.com';
process.env.STORAGE_DIR = '/app/uploads';
const result = validateConfig();
expect(result).toMatchObject({ nodeEnv: 'production', production: true });
expect(process.env.NODE_ENV, '运行时 index.ts/RLS 读 process.env,必须已被归一').toBe('production');
} finally {
for (const [key, value] of Object.entries(saved)) {
if (value === undefined) delete process.env[key];
else process.env[key] = value;
}
}
});
it("非默认 env 对象(纯函数)不污染 process.env", () => {
const saved = process.env.NODE_ENV;
try {
process.env.NODE_ENV = 'test';
validateConfig({ NODE_ENV: 'production', DATABASE_URL: 'postgresql://a', CORS_ORIGINS: 'https://x', STORAGE_DIR: '/app/uploads' } as never);
expect(process.env.NODE_ENV).toBe('test');
} finally {
if (saved === undefined) delete process.env.NODE_ENV;
else process.env.NODE_ENV = saved;
}
});
});
describe('O-12.2:production DATABASE_URL 禁止主系统库/角色(隔离红线)', () => {
const baseEnv = {
NODE_ENV: 'production',
CORS_ORIGINS: 'https://supplier.example.com',
STORAGE_DIR: '/app/uploads',
};
for (const [label, databaseUrl, pattern] of [
['主系统库 canteen_delivery', 'postgresql://supplier:secret@db:5432/canteen_delivery', /canteen_delivery/],
[
'主系统库 + sslmode 变体',
'postgresql://supplier:secret@db:5432/canteen_delivery?sslmode=require',
/canteen_delivery/,
],
['主系统角色 canteen', 'postgresql://canteen:secret@db:5432/supplier_delivery', /角色 canteen/],
] as const) {
it(`${label} ⇒ 拒绝启动`, () => {
expect(() => validateConfig({ ...baseEnv, DATABASE_URL: databaseUrl } as never)).toThrow(pattern);
});
}
for (const [label, databaseUrl, pattern] of [
[
'编码库名 canteen%5Fdelivery',
'postgresql://supplier:secret@db:5432/canteen%5Fdelivery?sslmode=require',
/canteen_delivery/,
],
[
'小写编码库名 canteen%5fdelivery',
'postgresql://supplier:secret@db:5432/canteen%5fdelivery',
/canteen_delivery/,
],
[
'编码路径分隔符 canteen%2Fdelivery',
'postgresql://supplier:secret@db:5432/canteen%2Fdelivery',
/路径分隔符/,
],
[
'编码用户名 %63anteen',
'postgresql://%63anteen:secret@db:5432/supplier_delivery',
/角色 canteen/,
],
[
'大小写 Canteen_Delivery',
'postgresql://supplier:secret@db:5432/Canteen_Delivery?sslmode=require',
/canteen_delivery/,
],
[
'非法百分号编码(解码失败)',
'postgresql://supplier:secret@db:5432/%E0%A4%A',
/无法解析\/解码失败|不是合法的连接串/,
],
] as const) {
it(`production ${label} ⇒ 拒绝启动(fail-closed)`, () => {
expect(() => validateConfig({ ...baseEnv, DATABASE_URL: databaseUrl } as never)).toThrow(pattern);
});
}
it('尾部斜杠的本系统合法库名 ⇒ 通过(归一后比较)', () => {
expect(() =>
validateConfig({
...baseEnv,
DATABASE_URL: 'postgresql://supplier:secret@db:5432/supplier_delivery/?sslmode=require',
} as never)
).not.toThrow();
});
it('本系统 supplier 角色 + supplier_delivery 库 ⇒ 通过', () => {
expect(() =>
validateConfig({
...baseEnv,
DATABASE_URL: 'postgresql://supplier:secret@db:5432/supplier_delivery?sslmode=require',
} as never)
).not.toThrow();
});
});
describe('O-12.15:NODE_ENV 归一后 isProduction/serveStatic 必须同源', () => {
it("NODE_ENV=' production' ⇒ config.serveStatic=true(all-in-one 前端仍被服务)", async () => {
const keys = ['NODE_ENV', 'JWT_SECRET', 'DATABASE_URL', 'CORS_ORIGINS', 'STORAGE_DIR', 'SERVE_STATIC'];
const saved = Object.fromEntries(keys.map((key) => [key, process.env[key]]));
try {
process.env.NODE_ENV = ' production';
process.env.JWT_SECRET = 'test-secret';
process.env.DATABASE_URL = 'postgresql://supplier:secret@db:5432/supplier_delivery';
process.env.CORS_ORIGINS = 'https://supplier.example.com';
process.env.STORAGE_DIR = '/app/uploads';
delete process.env.SERVE_STATIC;
vi.resetModules();
const mod = await import('../src/config');
expect(process.env.NODE_ENV).toBe('production');
expect(mod.config.serveStatic, 'production 下必须服务静态前端').toBe(true);
} finally {
for (const [key, value] of Object.entries(saved)) {
if (value === undefined) delete process.env[key];
else process.env[key] = value;
}
vi.resetModules();
}
});
});