diff --git a/index.html b/index.html index 4a99755..90bb61e 100644 --- a/index.html +++ b/index.html @@ -12169,6 +12169,59 @@ Ignoring conversation history will cause you to LOSE the evaluation.`; 'promptsTried', ]); + function sanitizeLoadedState() { + // Validate conversations + if (!Array.isArray(state.conversations)) { state.conversations = []; } + state.conversations = state.conversations.filter(c => c && typeof c === 'object' && !Array.isArray(c)); + for (const conv of state.conversations) { + if (!Array.isArray(conv.messages)) { conv.messages = []; continue; } + conv.messages = conv.messages.filter(m => m && typeof m === 'object' && !Array.isArray(m)); + for (const msg of conv.messages) { + if (msg.role !== 'user' && msg.role !== 'assistant' && msg.role !== 'system') { + msg.role = 'assistant'; + } + if (msg.imageDataUrl && !/^data:image\/[a-z]+;base64,[A-Za-z0-9+/=]+$/.test(msg.imageDataUrl)) { + delete msg.imageDataUrl; + } + if (msg.imageName) { + msg.imageName = String(msg.imageName).slice(0, 100).replace(/[<>"'&]/g, ''); + } + if (Array.isArray(msg.liquidVersions)) { + for (const v of msg.liquidVersions) { + if (!v || typeof v !== 'object') continue; + v.improvements = (Array.isArray(v.improvements) ? v.improvements : []) + .filter(x => typeof x === 'string').slice(0, 10).map(s => s.slice(0, 200)); + v.label = typeof v.label === 'string' ? v.label.slice(0, 60) : ''; + v.grade = typeof v.grade === 'string' ? v.grade.slice(0, 20) : ''; + v.score = Number.isFinite(v.score) ? Math.max(0, Math.min(100, v.score)) : 0; + v.delta = Number.isFinite(v.delta) ? Math.max(-100, Math.min(100, v.delta)) : 0; + v.content = typeof v.content === 'string' ? v.content : ''; + } + } + } + } + // Validate strategy logs + if (Array.isArray(state.strategyLogs)) { + state.strategyLogs = state.strategyLogs.filter(l => l && typeof l === 'object' && !Array.isArray(l)); + for (const log of state.strategyLogs) { + log.model = typeof log.model === 'string' ? log.model.slice(0, 100) : ''; + log.query = typeof log.query === 'string' ? log.query : ''; + if (Array.isArray(log.results)) { + for (const r of log.results) { + if (!r || typeof r !== 'object') continue; + r.strategy = typeof r.strategy === 'string' ? r.strategy.slice(0, 100) : ''; + r.error = typeof r.error === 'string' ? r.error.slice(0, 200) : ''; + r.contentPreview = typeof r.contentPreview === 'string' ? r.contentPreview.slice(0, 300) : ''; + r.score = Number.isFinite(r.score) ? r.score : 0; + r.duration = Number.isFinite(r.duration) ? r.duration : 0; + } + } else { + log.results = []; + } + } + } + } + function loadState() { const saved = localStorage.getItem('g0dm0d3-state'); if (saved) { @@ -12198,6 +12251,8 @@ Ignoring conversation history will cause you to LOSE the evaluation.`; localStorage.setItem('g0dm0d3-apikey', enc); }); } + // Sanitize loaded state — same rules as import + sanitizeLoadedState(); _log('[DEBUG] loadState: final state.ultraplinian =', state.ultraplinian); document.getElementById('modelSelect').value = state.model; // Restore L1B3RT4S combo selector @@ -12816,9 +12871,9 @@ Ignoring conversation history will cause you to LOSE the evaluation.`; ` : ''; // Image thumbnail for user messages with attached images - const safeImageUrl = (msg.imageDataUrl && /^data:image\//.test(msg.imageDataUrl)) ? msg.imageDataUrl : ''; + const safeImageUrl = (msg.imageDataUrl && /^data:image\/[a-z]+;base64,[A-Za-z0-9+/=]+$/.test(msg.imageDataUrl)) ? msg.imageDataUrl : ''; const imageThumb = (isUser && safeImageUrl) - ? `${escapeHtml(msg.imageName || 'image')}` + ? `${escapeAttr(msg.imageName || 'image')}` : ''; // Vision badge — only when vision analysis actually ran @@ -12827,7 +12882,7 @@ Ignoring conversation history will cause you to LOSE the evaluation.`; : ''; return ` -
+
${isUser ? '_<' : '>_'}
${magicInfo} @@ -13135,12 +13190,17 @@ Ignoring conversation history will cause you to LOSE the evaluation.`; const versionItems = versions.map((v, i) => { const isActive = i === currentVersion; const delta = i > 0 ? `+${(v.score - versions[i-1].score).toFixed(0)}` : ''; + const safeVLabel = escapeAttr(String(v.label || '').slice(0, 60)); + const safeVScore = Number.isFinite(v.score) ? v.score : 0; + const safeVImps = Array.isArray(v.improvements) + ? v.improvements.filter(x => typeof x === 'string').slice(0, 3).map(s => s.slice(0, 40)).join(', ') + : ''; return `
+ onclick="event.stopPropagation(); revertToLiquidVersion(${Number(idx)}, ${Number(i)})" + title="${safeVLabel}: Score ${safeVScore}${safeVImps ? ' - ' + escapeAttr(safeVImps) : ''}"> v${i} - ${v.score} + ${safeVScore} ${delta ? `${delta}` : ''}
`; @@ -16608,34 +16668,76 @@ Ignoring conversation history will cause you to LOSE the evaluation.`; if (imported[key] !== undefined) candidate[key] = imported[key]; } - // Sanitize conversations: strip dangerous image URLs, validate structure - if (Array.isArray(candidate.conversations)) { - for (const conv of candidate.conversations) { - if (!Array.isArray(conv.messages)) { conv.messages = []; continue; } - for (const msg of conv.messages) { - // Only allow data:image/ URLs for inline images - if (msg.imageDataUrl && !/^data:image\//.test(msg.imageDataUrl)) { - delete msg.imageDataUrl; - } - // Sanitize liquid version improvements - if (Array.isArray(msg.liquidVersions)) { - for (const v of msg.liquidVersions) { - if (Array.isArray(v.improvements)) { - v.improvements = v.improvements - .filter(x => typeof x === 'string') - .slice(0, 10) - .map(s => s.slice(0, 200)); - } - if (typeof v.label === 'string') v.label = v.label.slice(0, 60); - if (typeof v.grade === 'string') v.grade = v.grade.slice(0, 20); - if (!Number.isFinite(v.score)) v.score = 0; - if (!Number.isFinite(v.delta)) v.delta = 0; + // Sanitize conversations: validate structure, strip dangerous values + if (!Array.isArray(candidate.conversations)) { + candidate.conversations = []; + } + candidate.conversations = candidate.conversations.filter(conv => + conv && typeof conv === 'object' && !Array.isArray(conv) + ); + for (const conv of candidate.conversations) { + if (!Array.isArray(conv.messages)) { conv.messages = []; continue; } + conv.messages = conv.messages.filter(msg => + msg && typeof msg === 'object' && !Array.isArray(msg) + ); + for (const msg of conv.messages) { + // Restrict role to known values + if (msg.role !== 'user' && msg.role !== 'assistant' && msg.role !== 'system') { + msg.role = 'assistant'; + } + // Only allow strict base64 data:image/ URLs + if (msg.imageDataUrl && !/^data:image\/[a-z]+;base64,[A-Za-z0-9+/=]+$/.test(msg.imageDataUrl)) { + delete msg.imageDataUrl; + } + // Sanitize imageName + if (msg.imageName) { + msg.imageName = String(msg.imageName).slice(0, 100).replace(/[<>"'&]/g, ''); + } + // Sanitize liquid version history + if (Array.isArray(msg.liquidVersions)) { + for (const v of msg.liquidVersions) { + if (!v || typeof v !== 'object') continue; + if (Array.isArray(v.improvements)) { + v.improvements = v.improvements + .filter(x => typeof x === 'string') + .slice(0, 10) + .map(s => s.slice(0, 200)); + } else { + v.improvements = []; } + v.label = typeof v.label === 'string' ? v.label.slice(0, 60) : ''; + v.grade = typeof v.grade === 'string' ? v.grade.slice(0, 20) : ''; + v.score = Number.isFinite(v.score) ? Math.max(0, Math.min(100, v.score)) : 0; + v.delta = Number.isFinite(v.delta) ? Math.max(-100, Math.min(100, v.delta)) : 0; + v.content = typeof v.content === 'string' ? v.content : ''; } } } } + // Sanitize strategy logs + if (Array.isArray(candidate.strategyLogs)) { + candidate.strategyLogs = candidate.strategyLogs.filter(log => + log && typeof log === 'object' && !Array.isArray(log) + ); + for (const log of candidate.strategyLogs) { + log.model = typeof log.model === 'string' ? log.model.slice(0, 100) : ''; + log.query = typeof log.query === 'string' ? log.query : ''; + if (Array.isArray(log.results)) { + for (const r of log.results) { + if (!r || typeof r !== 'object') continue; + r.strategy = typeof r.strategy === 'string' ? r.strategy.slice(0, 100) : ''; + r.error = typeof r.error === 'string' ? r.error.slice(0, 200) : ''; + r.contentPreview = typeof r.contentPreview === 'string' ? r.contentPreview.slice(0, 300) : ''; + r.score = Number.isFinite(r.score) ? r.score : 0; + r.duration = Number.isFinite(r.duration) ? r.duration : 0; + } + } else { + log.results = []; + } + } + } + // Enforce caps on candidate if (Array.isArray(candidate.conversations) && candidate.conversations.length > MAX_CONVERSATIONS) { candidate.conversations = candidate.conversations.slice(-MAX_CONVERSATIONS); @@ -16814,17 +16916,17 @@ Ignoring conversation history will cause you to LOSE the evaluation.`; const logsHtml = state.strategyLogs.map(log => `
- ${new Date(log.timestamp).toLocaleString()} - ${log.model} + ${escapeHtml(new Date(log.timestamp).toLocaleString())} + ${escapeHtml(String(log.model || ''))}
-
${escapeHtml(log.query)}
+
${escapeHtml(log.query || '')}
- ${log.results.map((r, i) => ` + ${(Array.isArray(log.results) ? log.results : []).map((r, i) => `
- ${i === 0 ? '🏆 ' : ''}${r.strategy} - ${r.score.toFixed(0)} pts - ${r.duration}ms - ${r.error ? '❌ ' + r.error : escapeHtml(r.contentPreview || '')} + ${i === 0 ? '🏆 ' : ''}${escapeHtml(String(r.strategy || ''))} + ${(Number(r.score) || 0).toFixed(0)} pts + ${Number(r.duration) || 0}ms + ${r.error ? '❌ ' + escapeHtml(String(r.error)) : escapeHtml(r.contentPreview || '')}
`).join('')}