Skip to content

LIBERATED AI CHAT

TypeScript

fix: deep XSS paths, import validation, image URL sanitization

faber118dde5ab4eb
diff --git a/index.html b/index.html
index 5fbb70f..4a99755 100644
--- a/index.html
+++ b/index.html
@@ -10127,17 +10127,27 @@ Analyze and refine. Output ONLY the JSON object:` }
break;
}
- // Parse the JSON response
+ // Parse and strictly normalize the JSON response
let result;
try {
- // Extract JSON from potential markdown code blocks
const jsonMatch = refinerOutput.match(/\{[\s\S]*\}/);
if (!jsonMatch) throw new Error('No JSON found in refiner output');
- result = JSON.parse(jsonMatch[0]);
+ const raw = JSON.parse(jsonMatch[0]);
+ // Normalize: clamp numbers, bound strings, filter arrays
+ result = {
+ currentScore: Math.max(0, Math.min(100, Number(raw.currentScore) || 0)),
+ refinedScore: Math.max(0, Math.min(100, Number(raw.refinedScore) || 0)),
+ qualityDelta: Math.max(-100, Math.min(100, Number(raw.qualityDelta) || 0)),
+ converged: !!raw.converged,
+ refinedContent: typeof raw.refinedContent === 'string' ? raw.refinedContent : '',
+ improvements: (Array.isArray(raw.improvements) ? raw.improvements : [])
+ .filter(x => typeof x === 'string')
+ .slice(0, 10)
+ .map(s => s.slice(0, 200)),
+ };
} catch (parseErr) {
console.error('[LIQUID] Failed to parse refiner output:', parseErr.message);
console.error('[LIQUID] Raw output:', refinerOutput.slice(0, 500));
- // Don't break on first parse failure — try next iteration
continue;
}
@@ -10751,28 +10761,36 @@ Analyze and refine. Output ONLY the JSON object:` }
<button class="version-history-close" onclick="this.closest('.version-history-modal').remove()">×</button>
</div>
<div class="version-history-list">
- ${versions.map((v, i) => `
+ ${versions.map((v, i) => {
+ const safeLabel = escapeHtml(String(v.label || `Refinement ${i}`).slice(0, 60));
+ const safeGrade = escapeHtml(String(v.grade || '').slice(0, 20));
+ const safeDelta = Number.isFinite(v.delta) ? v.delta : 0;
+ const safeScore = Number.isFinite(v.score) ? v.score : 0;
+ const safeImprovements = Array.isArray(v.improvements)
+ ? v.improvements.filter(x => typeof x === 'string').slice(0, 3)
+ : [];
+ return `
<div class="version-history-item ${i === currentVersion ? 'current' : ''}"
- onclick="revertToLiquidVersion(${messageIdx}, ${i}); this.closest('.version-history-modal').remove();">
+ onclick="revertToLiquidVersion(${Number(messageIdx)}, ${Number(i)}); this.closest('.version-history-modal').remove();">
<div class="version-history-item-header">
<span class="version-history-label">
- ${i === 0 ? '📄 Original' : i === versions.length - 1 ? '✨ ' + v.label + ' (Latest)' : '📝 ' + v.label}
+ ${i === 0 ? '📄 Original' : i === versions.length - 1 ? '✨ ' + safeLabel + ' (Latest)' : '📝 ' + safeLabel}
</span>
- <span class="version-history-score ${getScoreClass(v.score)}">${v.score}/100</span>
+ <span class="version-history-score ${getScoreClass(safeScore)}">${safeScore}/100</span>
</div>
<div class="version-history-meta">
- <span>${v.grade}</span>
- ${v.delta ? `<span class="version-delta">+${v.delta}%</span>` : ''}
+ <span>${safeGrade}</span>
+ ${safeDelta ? `<span class="version-delta">+${safeDelta}%</span>` : ''}
<span class="version-time">${formatTimeAgo(v.timestamp)}</span>
</div>
- ${v.improvements && v.improvements.length > 0 ? `
+ ${safeImprovements.length > 0 ? `
<div class="version-improvements">
- ${v.improvements.slice(0, 2).map(imp => `<span class="improvement-tag">${imp.slice(0, 30)}${imp.length > 30 ? '...' : ''}</span>`).join('')}
+ ${safeImprovements.slice(0, 2).map(imp => `<span class="improvement-tag">${escapeHtml(imp.slice(0, 30))}${imp.length > 30 ? '...' : ''}</span>`).join('')}
</div>
` : ''}
- <div class="version-preview">${escapeHtml(v.content.slice(0, 150))}${v.content.length > 150 ? '...' : ''}</div>
+ <div class="version-preview">${escapeHtml((v.content || '').slice(0, 150))}${(v.content || '').length > 150 ? '...' : ''}</div>
</div>
- `).join('')}
+ `;}).join('')}
</div>
</div>
`;
@@ -12798,8 +12816,9 @@ Ignoring conversation history will cause you to LOSE the evaluation.`;
` : '';
// Image thumbnail for user messages with attached images
- const imageThumb = (isUser && msg.imageDataUrl)
- ? `<img class="message-image-thumb" src="${msg.imageDataUrl}" alt="${escapeHtml(msg.imageName || 'image')}" onclick="window.open(this.src,'_blank')">`
+ const safeImageUrl = (msg.imageDataUrl && /^data:image\//.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')">`
: '';
// Vision badge — only when vision analysis actually ran
@@ -16579,41 +16598,74 @@ Ignoring conversation history will cause you to LOSE the evaluation.`;
if (!_pendingImportData) return;
const imported = _pendingImportData;
- // Merge into state — only overwrite keys that exist in the import
+ // Build a candidate state WITHOUT mutating live state
const allowed = ['conversations', 'currentId', 'apiKey', 'model', 'persona',
'customSystemPrompt', 'customSystemPromptEnabled', 'strategyLogs',
'autoTuneEnabled', 'liquidEnabled', 'ultraplinianEnabled',
'ultraplinianTier', 'noLogMode'];
+ const candidate = JSON.parse(JSON.stringify(state));
for (const key of allowed) {
- if (imported[key] !== undefined) state[key] = imported[key];
+ if (imported[key] !== undefined) candidate[key] = imported[key];
}
- // Enforce hard caps before saving (same caps as normal save)
- enforceDataCaps();
+ // 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;
+ }
+ }
+ }
+ }
+ }
- // Pre-flight: check if the merged state will fit in localStorage (~5 MB)
- const { apiKey: _ak, ...stateWithoutKey } = state;
- const testPayload = JSON.stringify(stateWithoutKey);
+ // Enforce caps on candidate
+ if (Array.isArray(candidate.conversations) && candidate.conversations.length > MAX_CONVERSATIONS) {
+ candidate.conversations = candidate.conversations.slice(-MAX_CONVERSATIONS);
+ }
+ if (Array.isArray(candidate.strategyLogs) && candidate.strategyLogs.length > 500) {
+ candidate.strategyLogs = candidate.strategyLogs.slice(-500);
+ }
+
+ // Pre-flight: check if the candidate will fit in localStorage (~5 MB)
+ const { apiKey: _ak, ...candidateWithoutKey } = candidate;
+ const testPayload = JSON.stringify(candidateWithoutKey);
const estimatedBytes = new Blob([testPayload]).size;
const maxBytes = 5 * 1024 * 1024;
if (estimatedBytes > maxBytes * 0.95) {
- // Too large even after caps — try trimming, then warn
- trimOldData();
- const retryPayload = JSON.stringify((() => { const { apiKey: _k, ...rest } = state; return rest; })());
- const retryBytes = new Blob([retryPayload]).size;
- if (retryBytes > maxBytes * 0.95) {
- showImportStatus('error',
- 'Backup is too large for browser storage (' + (retryBytes / 1024 / 1024).toFixed(1) + ' MB / ~5 MB limit). ' +
- 'Try exporting fewer conversations or clearing existing data first.');
- _pendingImportData = null;
- document.getElementById('importConfirm').style.display = 'none';
- return;
- }
+ showImportStatus('error',
+ 'Backup is too large for browser storage (' + (estimatedBytes / 1024 / 1024).toFixed(1) + ' MB / ~5 MB limit). ' +
+ 'Try exporting fewer conversations or clearing existing data first.');
+ _pendingImportData = null;
+ document.getElementById('importConfirm').style.display = 'none';
+ return;
}
- // Report if caps trimmed anything
+ // Commit: apply validated candidate to live state
const importedConvCount = imported.conversations?.length ?? 0;
+ for (const key of allowed) {
+ if (candidate[key] !== undefined) state[key] = candidate[key];
+ }
+ if (candidate.apiKey) _apiKeyGeneration++;
+
const actualConvCount = state.conversations?.length ?? 0;
let capWarning = '';
if (importedConvCount > actualConvCount) {