Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix: harden XSS protection with inline fallbacks and simplified markdown rendering

LearningCircuitbfaac83594ef
diff --git a/src/local_deep_research/web/static/js/components/fallback/ui.js b/src/local_deep_research/web/static/js/components/fallback/ui.js
index 97ad478972..69e1ecf167 100644
--- a/src/local_deep_research/web/static/js/components/fallback/ui.js
+++ b/src/local_deep_research/web/static/js/components/fallback/ui.js
@@ -11,6 +11,14 @@
console.log('Initializing fallback UI utilities');
+ /**
+ * Inline fallback for HTML escaping - provides XSS protection even if
+ * xss-protection.js fails to load. This duplication is intentional for
+ * defense-in-depth: each file has its own fallback to prevent a single
+ * point of failure from compromising security.
+ */
+ const escapeHtmlFallback = (str) => String(str).replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);
+
/**
* Show a loading spinner
* @param {HTMLElement} container - Container element for spinner
@@ -18,10 +26,12 @@
*/
function showSpinner(container, message) {
if (!container) container = document.body;
+ // Escape message to prevent XSS
+ const escapedMessage = message ? (window.escapeHtml || escapeHtmlFallback)(message) : '';
const spinnerHtml = `
<div class="loading-spinner centered">
<div class="spinner"></div>
- ${message ? `<div class="spinner-message">${message}</div>` : ''}
+ ${escapedMessage ? `<div class="spinner-message">${escapedMessage}</div>` : ''}
</div>
`;
container.innerHTML = spinnerHtml;
@@ -46,12 +56,15 @@
function showError(message) {
console.error(message);
+ // Escape message to prevent XSS
+ const escapedMessage = (window.escapeHtml || escapeHtmlFallback)(message);
+
// Create a notification element
const notification = document.createElement('div');
notification.className = 'ldr-notification ldr-error';
notification.innerHTML = `
<i class="fas fa-exclamation-circle"></i>
- <span>${message}</span>
+ <span>${escapedMessage}</span>
<button class="ldr-close-notification"><i class="fas fa-times"></i></button>
`;
@@ -87,12 +100,15 @@
function showMessage(message) {
console.log(message);
+ // Escape message to prevent XSS
+ const escapedMessage = (window.escapeHtml || escapeHtmlFallback)(message);
+
// Create a notification element
const notification = document.createElement('div');
notification.className = 'ldr-notification ldr-success';
notification.innerHTML = `
<i class="fas fa-check-circle"></i>
- <span>${message}</span>
+ <span>${escapedMessage}</span>
<button class="ldr-close-notification"><i class="fas fa-times"></i></button>
`;
@@ -122,48 +138,25 @@
}
/**
- * Simple markdown renderer
+ * Simple markdown renderer fallback
* @param {string} markdown - Markdown content
- * @returns {string} HTML content
+ * @returns {string} HTML content (escaped for security)
*/
function renderMarkdown(markdown) {
if (!markdown) return '';
- // This is a very basic markdown renderer for fallback purposes
- let html = markdown;
-
- // Convert headers
- html = html.replace(/^# (.*$)/gm, '<h1>$1</h1>');
- html = html.replace(/^## (.*$)/gm, '<h2>$1</h2>');
- html = html.replace(/^### (.*$)/gm, '<h3>$1</h3>');
- html = html.replace(/^#### (.*$)/gm, '<h4>$1</h4>');
- html = html.replace(/^##### (.*$)/gm, '<h5>$1</h5>');
-
- // Convert code blocks
- html = html.replace(/```([\s\S]*?)```/g, '<pre><code>$1</code></pre>');
+ // Fallback: escape all HTML and display as preformatted text for security
+ // Using regex-based partial markdown is fragile and a security risk,
+ // so we escape all HTML and display as preformatted text with a warning
+ console.warn('Fallback UI: Marked library not available. Displaying as plaintext for security.');
+ const escaped = (window.escapeHtml || escapeHtmlFallback)(markdown);
- // Convert inline code
- html = html.replace(/`([^`]+)`/g, '<code>$1</code>');
-
- // Convert bold
- html = html.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
-
- // Convert italic
- html = html.replace(/\*(.*?)\*/g, '<em>$1</em>');
-
- // Convert links
- html = html.replace(/\[(.*?)\]\((.*?)\)/g, '<a href="$2">$1</a>');
-
- // Convert paragraphs - this is simplistic
- html = html.replace(/\n\s*\n/g, '</p><p>');
- html = '<p>' + html + '</p>';
-
- // Fix potentially broken paragraph tags
- html = html.replace(/<\/p><p><\/p><p>/g, '</p><p>');
- html = html.replace(/<\/p><p><(h[1-5])/g, '</p><$1');
- html = html.replace(/<\/(h[1-5])><p>/g, '</$1>');
-
- return html;
+ return `<div class="markdown-content">
+ <div class="alert alert-warning" style="margin-bottom: 1rem;">
+ <i class="fas fa-exclamation-triangle"></i> Markdown rendering unavailable. Displaying as plaintext.
+ </div>
+ <pre style="white-space: pre-wrap; word-wrap: break-word; font-family: inherit;">${escaped}</pre>
+ </div>`;
}
/**
diff --git a/src/local_deep_research/web/static/js/components/results.js b/src/local_deep_research/web/static/js/components/results.js
index 1bf613491c..fa3319fd60 100644
--- a/src/local_deep_research/web/static/js/components/results.js
+++ b/src/local_deep_research/web/static/js/components/results.js
@@ -10,6 +10,14 @@
let researchId = null;
let researchData = null;
+ /**
+ * Inline fallback for HTML escaping - provides XSS protection even if
+ * xss-protection.js fails to load. This duplication is intentional for
+ * defense-in-depth: each file has its own fallback to prevent a single
+ * point of failure from compromising security.
+ */
+ const escapeHtmlFallback = (str) => String(str).replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);
+
/**
* Initialize the results component
*/
@@ -587,9 +595,16 @@
const renderedHtml = window.ui.renderMarkdown(content);
resultsContainer.innerHTML = renderedHtml;
} else {
- // Fall back to basic formatting
- content = content.replace(/\n/g, '<br>');
- resultsContainer.innerHTML = `<div class="markdown-content">${content}</div>`;
+ // Fallback: escape content and display as preformatted text for security
+ // Using regex-based partial markdown is fragile and a security risk
+ console.warn('Markdown rendering unavailable. Displaying as plaintext.');
+ const escaped = (window.escapeHtml || escapeHtmlFallback)(content);
+ resultsContainer.innerHTML = `<div class="markdown-content">
+ <div class="alert alert-warning" style="margin-bottom: 1rem;">
+ <i class="fas fa-exclamation-triangle"></i> Markdown rendering unavailable. Displaying as plaintext.
+ </div>
+ <pre style="white-space: pre-wrap; word-wrap: break-word; font-family: inherit;">${escaped}</pre>
+ </div>`;
}
// Add syntax highlighting if Prism is available
@@ -608,9 +623,10 @@
* @param {string} message - Error message
*/
function showError(message) {
+ const escapedMessage = (window.escapeHtml || escapeHtmlFallback)(message);
resultsContainer.innerHTML = `
<div class="alert alert-danger" role="alert">
- <i class="fas fa-exclamation-triangle"></i> ${message}
+ <i class="fas fa-exclamation-triangle"></i> ${escapedMessage}
</div>
<p class="text-center mt-3">
<a href="/research" class="btn btn-primary">
diff --git a/src/local_deep_research/web/static/js/security/xss-protection.js b/src/local_deep_research/web/static/js/security/xss-protection.js
index 4034e89e09..60232739da 100644
--- a/src/local_deep_research/web/static/js/security/xss-protection.js
+++ b/src/local_deep_research/web/static/js/security/xss-protection.js
@@ -4,6 +4,31 @@
* This module provides secure HTML sanitization functions to prevent
* cross-site scripting (XSS) attacks when rendering dynamic content.
* Uses DOMPurify for proven, security-reviewed HTML sanitization.
+ *
+ * ARCHITECTURE NOTE: Inline Fallback Pattern
+ * ------------------------------------------
+ * Several files in this codebase define their own inline `escapeHtmlFallback`
+ * functions. This duplication is INTENTIONAL and serves as defense-in-depth:
+ *
+ * 1. If this xss-protection.js file fails to load (network error, CDN issue,
+ * script parsing error), call sites still have XSS protection via their
+ * inline fallback.
+ *
+ * 2. The inline fallback is a one-liner that's easy to audit and verify:
+ * `(str) => String(str).replace(/[&<>"']/g, (m) => ({'&':'&amp;',...})[m])`
+ *
+ * 3. The usage pattern is: `(window.escapeHtml || escapeHtmlFallback)(text)`
+ * This ensures the global function is preferred when available, with the
+ * inline fallback as a safety net.
+ *
+ * Trade-offs:
+ * - Pros: Each call site independently protected; no single point of failure
+ * - Cons: Maintenance burden; must keep all fallbacks consistent
+ *
+ * Files using this pattern:
+ * - components/results.js
+ * - components/fallback/ui.js
+ * - services/ui.js
*/
(function() {
diff --git a/src/local_deep_research/web/static/js/services/ui.js b/src/local_deep_research/web/static/js/services/ui.js
index c4a1d66527..615ab0559b 100644
--- a/src/local_deep_research/web/static/js/services/ui.js
+++ b/src/local_deep_research/web/static/js/services/ui.js
@@ -55,6 +55,14 @@ function updateProgressBar(fillElementId, percentageElementId, percentage) {
}
}
+/**
+ * Inline fallback for HTML escaping - provides XSS protection even if
+ * xss-protection.js fails to load. This duplication is intentional for
+ * defense-in-depth: each file has its own fallback to prevent a single
+ * point of failure from compromising security.
+ */
+const escapeHtmlFallback = (str) => String(str).replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);
+
/**
* Show a loading spinner
* @param {string|Element} container - The container ID or element to add the spinner to
@@ -66,14 +74,16 @@ function showSpinner(container, message = 'Loading...') {
if (containerEl) {
containerEl.innerHTML = '';
+ // Escape message before including in HTML template
+ const escapedMessage = message ? (window.escapeHtml || escapeHtmlFallback)(message) : '';
const spinnerHTML = `
<div class="loading-spinner centered">
<div class="spinner"></div>
- ${message ? `<div class="spinner-message">${message}</div>` : ''}
+ ${escapedMessage ? `<div class="spinner-message">${escapedMessage}</div>` : ''}
</div>
`;
- // Safe: spinner HTML is controlled/static, but sanitize message for safety
+ // Safe: spinner HTML is controlled/static, message is escaped above
safeSetHTML(containerEl, spinnerHTML, 'ui');
}
}
@@ -102,14 +112,16 @@ function showError(container, message) {
const containerEl = typeof container === 'string' ? document.getElementById(container) : container;
if (containerEl) {
+ // Escape message before including in HTML template
+ const escapedMessage = (window.escapeHtml || escapeHtmlFallback)(message);
const errorHTML = `
<div class="error-message">
<i class="fas fa-exclamation-circle"></i>
- <span>${message}</span>
+ <span>${escapedMessage}</span>
</div>
`;
- // Sanitize message to prevent XSS
+ // Safe: error HTML is controlled/static, message is escaped above
safeSetHTML(containerEl, errorHTML, 'ui');
}
}
@@ -166,10 +178,13 @@ function showMessage(message, type = 'success', duration = UI_CONFIG.NOTIFICATIO
// Apply dynamic accent color for border (static styles handled by CSS class)
banner.style.borderBottom = `3px solid ${accentColor}`;
- // Set content with colored icon (sanitize message to prevent XSS)
+ // Escape message before including in HTML template
+ const escapedMessage = (window.escapeHtml || escapeHtmlFallback)(message);
+
+ // Set content with colored icon
safeSetHTML(banner, `
<i class="fas ${iconClass}" style="color: ${accentColor}; font-size: 1.1rem;"></i>
- <span>${message}</span>
+ <span>${escapedMessage}</span>
`, 'ui');
// Add ARIA attributes for accessibility
@@ -224,19 +239,27 @@ function renderMarkdown(markdown) {
return `<div class="markdown-content">${processedHtml}</div>`;
} else {
- // Basic fallback if marked is not available
- console.warn('Marked library not available. Using basic formatting.');
- const basic = markdown
- .replace(/\n\n/g, '<br><br>')
- .replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
- .replace(/\*(.*?)\*/g, '<em>$1</em>')
- .replace(/\[(.*?)\]\((.*?)\)/g, '<a href="$2" target="_blank">$1</a>');
-
- return `<div class="markdown-content">${basic}</div>`;
+ // Fallback if marked is not available - display as plaintext for security
+ // Using regex-based partial markdown is fragile and a security risk,
+ // so we escape all HTML and display as preformatted text with a warning
+ console.warn('Marked library not available. Displaying as plaintext for security.');
+ const escaped = typeof window.escapeHtml === 'function'
+ ? window.escapeHtml(markdown)
+ : markdown.replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);
+
+ return `<div class="markdown-content">
+ <div class="alert alert-warning" style="margin-bottom: 1rem;">
+ <i class="fas fa-exclamation-triangle"></i> Markdown rendering unavailable. Displaying as plaintext.
+ </div>
+ <pre style="white-space: pre-wrap; word-wrap: break-word; font-family: inherit;">${escaped}</pre>
+ </div>`;
}
} catch (error) {
console.error('Error rendering markdown:', error);
- return `<div class="alert alert-danger">Error rendering content: ${error.message}</div>`;
+ const escapedMessage = typeof window.escapeHtml === 'function'
+ ? window.escapeHtml(error.message)
+ : String(error.message).replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);
+ return `<div class="alert alert-danger">Error rendering content: ${escapedMessage}</div>`;
}
}
@@ -406,10 +429,13 @@ function showAlert(message, type = 'info', skipIfToastShown = true) {
// Clear any existing alerts
alertContainer.innerHTML = '';
+ // Escape message before including in HTML template
+ const escapedMessage = (window.escapeHtml || escapeHtmlFallback)(message);
+
// Create alert element
const alert = document.createElement('div');
alert.className = `alert alert-${type}`;
- safeSetHTML(alert, `<i class="fas ${type === 'success' ? 'fa-check-circle' : 'fa-exclamation-circle'}"></i> ${message}`, 'ui');
+ safeSetHTML(alert, `<i class="fas ${type === 'success' ? 'fa-check-circle' : 'fa-exclamation-circle'}"></i> ${escapedMessage}`, 'ui');
// Add a close button
const closeBtn = document.createElement('span');