/** * 在**设备**上验证设置守卫:按"旧版前端那种全量下发空值"的打法打一次 PUT, * 配置必须一个字都不能掉。 * * 为什么要单独做设备版:手机跑的是 Python 内核(Chaquopy), * 电脑跑的是 Node 内核,两份实现是分别写的,只在电脑上验过不算数。 * * 用法: node scripts/device-settings-attack.mjs [serial] [port] */ import { spawnSync } from 'node:child_process'; const SERIAL = process.argv[2] || 'emulator-5556'; const PORT = process.argv[3] || '5199'; const ADB = 'C:\\Android\\Sdk\\platform-tools\\adb.exe'; function sh(cmd, timeout = 60000) { const res = spawnSync(ADB, ['-s', SERIAL, 'shell', cmd], { encoding: 'utf8', timeout }); return `${res.stdout || ''}${res.stderr || ''}`.trim(); } function getState() { const out = sh(`curl -s --max-time 20 http://127.0.0.1:${PORT}/api/state`); try { return JSON.parse(out); } catch { console.error('拿不到 state,原始返回:', out.slice(0, 300)); process.exit(1); } } function put(patch) { const b64 = Buffer.from(JSON.stringify(patch), 'utf8').toString('base64'); sh(`echo '${b64}' | base64 -d > /data/local/tmp/patch.json`); const out = sh( `curl -s --max-time 20 -X PUT -H 'Content-Type: application/json' -d @/data/local/tmp/patch.json http://127.0.0.1:${PORT}/api/settings`, ); return out.slice(0, 80); } const KEYS = ['provider', 'model', 'baseUrl', 'thinkingLevel', 'nickname']; console.log(`=== 设备 ${SERIAL} 上的 Python 内核 ===`); const before = getState().settings; console.log(' 保存前:', KEYS.map((k) => `${k}=${JSON.stringify(before[k])}`).join(' ')); console.log(` hasApiKey=${before.hasApiKey} systemPrompt=${(before.systemPrompt || '').length}字`); console.log('\n--- 攻击:全量下发空值(就是"一保存就清空"那个 bug 的形状)---'); console.log(' PUT →', put({ provider: '', baseUrl: '', model: '', apiKey: '', nickname: '', thinkingLevel: '', systemPrompt: '' })); const after = getState().settings; console.log(' 保存后:', KEYS.map((k) => `${k}=${JSON.stringify(after[k])}`).join(' ')); console.log(` hasApiKey=${after.hasApiKey} systemPrompt=${(after.systemPrompt || '').length}字`); const hurt = []; for (const key of KEYS) { if (String(before[key] ?? '') !== String(after[key] ?? '')) { hurt.push(`${key}: ${JSON.stringify(before[key])} → ${JSON.stringify(after[key])}`); } } if (before.hasApiKey && !after.hasApiKey) hurt.push('API Key 被清掉了'); console.log('\n=== 结论 ==='); if (hurt.length) { console.log(' ❌ 还是被改动:\n ' + hurt.join('\n ')); process.exit(1); } console.log(' ✅ 空值被忽略,配置一个字都没掉');