fix: close all remaining XSS sinks, harden import + load schemas

Show Magic version tooltip: escapeAttr() on v.label and improvements
in title attribute — prevents quote-breakout to onmouseover.

Message role: hardcoded to 'user'|'assistant' in class attribute —
arbitrary role strings can no longer inject HTML via class="message X".

Strategy logs: all fields (model, strategy, error, score, duration)
now escaped/coerced before innerHTML insertion.

Image rendering: strict base64 data:image/ regex rejects embedded
quotes; both src and alt use escapeAttr(); onclick removed (was an
XSS amplifier for injected src values).

Import schema hardened:
- Non-object conversations/messages filtered out (null, string, array)
- msg.role restricted to user/assistant/system
- imageDataUrl strict base64 regex; imageName stripped of HTML chars
- liquidVersions: all fields typed/bounded
- strategyLogs: all fields typed/bounded
- Runs BEFORE size check (validate-then-commit preserved)

Load-time sanitization: sanitizeLoadedState() runs on every page load
with the same rules — existing stored XSS payloads are neutralized
even without a fresh import.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
faber
2026-07-12 11:09:58 -04:00
parent 118dde5ab4
commit c13246ab13

View File

@@ -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)
? `<img class="message-image-thumb" src="${safeImageUrl}" alt="${escapeHtml(msg.imageName || 'image')}" onclick="window.open(this.src,'_blank')">`
? `<img class="message-image-thumb" src="${escapeAttr(safeImageUrl)}" alt="${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 `
<div class="message ${msg.role}" data-idx="${idx}">
<div class="message ${msg.role === 'user' ? 'user' : 'assistant'}" data-idx="${idx}">
<div class="message-avatar">${isUser ? '_&lt;' : '&gt;_'}</div>
<div class="message-wrapper">
${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 `
<div class="liquid-version-item ${isActive ? 'active' : ''}"
onclick="event.stopPropagation(); revertToLiquidVersion(${idx}, ${i})"
title="${v.label}: Score ${v.score}${v.improvements?.length ? ' - ' + v.improvements.join(', ') : ''}">
onclick="event.stopPropagation(); revertToLiquidVersion(${Number(idx)}, ${Number(i)})"
title="${safeVLabel}: Score ${safeVScore}${safeVImps ? ' - ' + escapeAttr(safeVImps) : ''}">
<span class="liquid-version-num">v${i}</span>
<span class="liquid-version-score">${v.score}</span>
<span class="liquid-version-score">${safeVScore}</span>
${delta ? `<span class="liquid-version-delta">${delta}</span>` : ''}
</div>
`;
@@ -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 => `
<div class="log-entry">
<div class="log-header">
<span>${new Date(log.timestamp).toLocaleString()}</span>
<span>${log.model}</span>
<span>${escapeHtml(new Date(log.timestamp).toLocaleString())}</span>
<span>${escapeHtml(String(log.model || ''))}</span>
</div>
<div class="log-query">${escapeHtml(log.query)}</div>
<div class="log-query">${escapeHtml(log.query || '')}</div>
<div class="log-results">
${log.results.map((r, i) => `
${(Array.isArray(log.results) ? log.results : []).map((r, i) => `
<div class="log-result ${i === 0 ? 'winner' : ''}">
<span class="log-strategy">${i === 0 ? '🏆 ' : ''}${r.strategy}</span>
<span class="log-score ${r.score < 0 ? 'negative' : ''}">${r.score.toFixed(0)} pts</span>
<span class="log-duration">${r.duration}ms</span>
<span class="log-preview">${r.error ? '❌ ' + r.error : escapeHtml(r.contentPreview || '')}</span>
<span class="log-strategy">${i === 0 ? '🏆 ' : ''}${escapeHtml(String(r.strategy || ''))}</span>
<span class="log-score ${(Number(r.score) || 0) < 0 ? 'negative' : ''}">${(Number(r.score) || 0).toFixed(0)} pts</span>
<span class="log-duration">${Number(r.duration) || 0}ms</span>
<span class="log-preview">${r.error ? '❌ ' + escapeHtml(String(r.error)) : escapeHtml(r.contentPreview || '')}</span>
</div>
`).join('')}
</div>