#!/usr/bin/env node import { spawnSync } from 'node:child_process'; const CANARY_LIVE_SCRIPT = 'scripts/check-pingora-canary-live.mjs'; const failures = []; main(); if (failures.length > 0) { console.error('[check:pingora-canary-live-guard] FAILED'); for (const failure of failures) { console.error(`- ${failure}`); } process.exit(1); } console.log('[check:pingora-canary-live-guard] OK'); function main() { assertRejectsControlCharacter('--base-url', [ '--base-url', 'http://127.0.0.1\n--fake-flag', ]); assertRejectsControlCharacter('--prefix', [ '--base-url', 'http://127.0.0.1', '--prefix', '/__genarrative_pingora_canary\nX-Injected: yes', ]); assertRejectsControlCharacter('--host', [ '--base-url', 'http://127.0.0.1', '--host', 'example.com\nX-Injected: yes', ]); assertRejectsControlCharacter('--path', [ '--base-url', 'http://127.0.0.1', '--path', '/api/assets/history\nX-Injected: yes', ]); assertRejectsControlCharacter('--timeout-ms', [ '--base-url', 'http://127.0.0.1', '--timeout-ms', '5000\n1', ]); } function assertRejectsControlCharacter(label, args) { const result = spawnSync('node', [CANARY_LIVE_SCRIPT, ...args], { cwd: process.cwd(), encoding: 'utf8', env: process.env, }); if ((result.status ?? 0) === 0) { failures.push(`${label} 带控制字符时必须在发起 canary 请求前失败。`); return; } assertIncludes( `${result.stdout}\n${result.stderr}`, `${label} 不能包含换行或 NUL 字符`, `${label} 带控制字符时必须给出明确错误。`, ); } function assertIncludes(value, expected, reason) { if (!String(value).includes(expected)) { failures.push(`${reason} 缺少: ${expected}`); } }