/** * 复现"保存设置后配置被清空"。 * 直接按 SettingsScreen 发的那个 patch 形状打 PUT /api/settings,再看 GET /api/state。 * * 用法: node scripts/probe-settings-wipe.mjs [base] */ const BASE = process.argv[2] || 'http://127.0.0.1:5199'; const get = async () => (await fetch(`${BASE}/api/state`)).json(); async function put(patch) { const res = await fetch(`${BASE}/api/settings`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(patch), }); const text = await res.text(); let json; try { json = JSON.parse(text); } catch { json = { __raw: text.slice(0, 200) }; } return { status: res.status, json }; } const show = (label, s) => { console.log(` ${label}`); console.log(` provider=${JSON.stringify(s.provider)} model=${JSON.stringify(s.model)}`); console.log(` baseUrl=${JSON.stringify(s.baseUrl)} thinking=${JSON.stringify(s.thinkingLevel)}`); console.log(` nickname=${JSON.stringify(s.nickname)} toolsDir=${JSON.stringify(s.toolsDir)}`); console.log(` hasApiKey=${s.hasApiKey} python=${s.tools?.python} mcp=${s.tools?.mcp}`); console.log(` systemPrompt 长度=${(s.systemPrompt || '').length}`); }; console.log('=== 保存前 ==='); const before = await get(); show('当前', before.settings); console.log('\n=== 模拟界面发的那份 patch ==='); // 关键:SettingsScreen 用 json.put("tools", mapOf(...)),看内核怎么处理 const patch = { provider: before.settings.provider, baseUrl: before.settings.baseUrl, model: before.settings.model, temperature: before.settings.temperature, maxRounds: before.settings.maxRounds, thinkingLevel: before.settings.thinkingLevel, toolsDir: before.settings.toolsDir, systemPrompt: before.settings.systemPrompt, tools: { python: true, mcp: true }, nickname: before.settings.nickname || '我', }; console.log(' ', JSON.stringify(patch).slice(0, 220)); const res = await put(patch); console.log('\n=== PUT 响应 ==='); console.log(' HTTP', res.status, JSON.stringify(res.json).slice(0, 300)); console.log('\n=== 保存后 ==='); const after = await get(); show('当前', after.settings); const lost = []; for (const key of ['provider', 'model', 'baseUrl', 'thinkingLevel', 'nickname', 'toolsDir']) { if (String(before.settings[key] ?? '') !== String(after.settings[key] ?? '')) { lost.push(`${key}: ${JSON.stringify(before.settings[key])} → ${JSON.stringify(after.settings[key])}`); } } if ((before.settings.systemPrompt || '').length !== (after.settings.systemPrompt || '').length) { lost.push(`systemPrompt 长度: ${(before.settings.systemPrompt || '').length} → ${(after.settings.systemPrompt || '').length}`); } if (before.settings.hasApiKey && !after.settings.hasApiKey) lost.push('API Key 丢了'); console.log('\n=== 结论 ==='); console.log(lost.length ? '❌ 有字段被改动/清空:\n ' + lost.join('\n ') : '✅ 没有字段丢失'); /* ------------------------------------------------------------------ * * 第二关:模拟"旧版前端那个 bug"——全量下发空值。 * 内核现在必须把这些空值当"没填"丢掉,一个字段都不许被清掉。 * ------------------------------------------------------------------ */ console.log('\n=== 攻击测试:下发一堆空值 ==='); const attack = { provider: '', baseUrl: '', model: '', apiKey: '', nickname: '', systemPrompt: '', thinkingLevel: '', }; const attackRes = await put(attack); console.log(' HTTP', attackRes.status); const afterAttack = await get(); const hurt = []; for (const key of ['provider', 'model', 'baseUrl', 'thinkingLevel', 'nickname']) { if (String(after.settings[key] ?? '') !== String(afterAttack.settings[key] ?? '')) { hurt.push(`${key}: ${JSON.stringify(after.settings[key])} → ${JSON.stringify(afterAttack.settings[key])}`); } } if (after.settings.hasApiKey && !afterAttack.settings.hasApiKey) hurt.push('API Key 被清'); if ((after.settings.systemPrompt || '').length !== (afterAttack.settings.systemPrompt || '').length) { hurt.push(`systemPrompt: ${(after.settings.systemPrompt || '').length} → ${(afterAttack.settings.systemPrompt || '').length}`); } console.log(hurt.length ? ' ❌ 还是被清掉了:\n ' + hurt.join('\n ') : ' ✅ 空值被正确忽略,配置没被动');