import { spawnSync } from 'node:child_process'; import { readFileSync } from 'node:fs'; import path from 'node:path'; import { describe, expect, it } from 'vitest'; /** * O-12.8 / O-12.10(2026-10-19 T6 红队复攻): * · webpack devServer 不得再用 `allowedHosts:'all'`(DNS rebinding 可借 devServer 代理打内网); * · 测试守卫不得把本机 3001 当"安全本地"——3001 是**主系统** API,默认拒绝,需 ALLOW_MAIN_SYSTEM=1。 */ const serverDir = path.resolve(__dirname, '..'); function runGuard(base: string, extraEnv: Record = {}) { const script = `require('./scripts/test-target-guard.js').guardTestTarget({ base: ${JSON.stringify(base)}, explicit: true })`; return spawnSync(process.execPath, ['-e', script], { cwd: serverDir, env: { ...process.env, ...extraEnv }, encoding: 'utf8', }); } describe('O-12.10:本机 3001(主系统)默认拒绝', () => { it('http://127.0.0.1:3001/api 无 ALLOW_MAIN_SYSTEM ⇒ exit 1 且提示主系统', () => { const result = runGuard('http://127.0.0.1:3001/api', { ALLOW_MAIN_SYSTEM: '' }); expect(result.status).toBe(1); expect(`${result.stderr}${result.stdout}`).toMatch(/主系统/); }); it('本系统 3022 ⇒ 放行(exit 0)', () => { const result = runGuard('http://127.0.0.1:3022/api'); expect(result.status).toBe(0); expect(result.stdout).toContain('[test] target: http://127.0.0.1:3022/api'); }); it('显式 ALLOW_MAIN_SYSTEM=1 才放行 3001', () => { const result = runGuard('http://127.0.0.1:3001/api', { ALLOW_MAIN_SYSTEM: '1' }); expect(result.status).toBe(0); }); it('远程 + ALLOW_REMOTE=1 但非 TTY ⇒ 仍 exit 1', () => { const result = runGuard('https://www.xdjchuanhui.com/api', { ALLOW_REMOTE: '1' }); expect(result.status).toBe(1); expect(`${result.stderr}${result.stdout}`).toMatch(/非交互环境/); }); }); describe('O-12.8:webpack devServer allowedHosts 收紧', () => { it("allowedHosts 为 'auto',不再是 'all'", () => { const source = readFileSync(path.join(serverDir, '../webpack.config.js'), 'utf8'); expect(source).toContain("allowedHosts: 'auto'"); expect(source).not.toContain("allowedHosts: 'all'"); // 回环默认与 WEB_HOST 覆盖保持不变 expect(source).toContain("host: process.env.WEB_HOST || '127.0.0.1'"); }); });