~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
feat: Add DOMPurify XSS prevention for UI components (#1141)
LearningCircuit083b215154a0
diff --git a/package.json b/package.jsonindex 6bebdf2894..38951667bf 100644--- a/package.json+++ b/package.json@@ -20,6 +20,7 @@"chartjs-adapter-date-fns": "^3.0.0","chartjs-plugin-annotation": "^3.0.0","date-fns": "^4.1.0",+ "dompurify": "^3.2.2","highlight.js": "^11.9.0","html2canvas": "^1.4.1","jspdf": "^3.0.2",diff --git a/src/local_deep_research/__version__.py b/src/local_deep_research/__version__.pyindex 70f1ec7a57..3feb6d069d 100644--- a/src/local_deep_research/__version__.py+++ b/src/local_deep_research/__version__.py@@ -1 +1 @@-__version__ = "1.2.18"+__version__ = "1.2.19"diff --git a/src/local_deep_research/web/static/js/services/ui.js b/src/local_deep_research/web/static/js/services/ui.jsindex 254cd6f325..0a8d3b7565 100644--- a/src/local_deep_research/web/static/js/services/ui.js+++ b/src/local_deep_research/web/static/js/services/ui.js@@ -2,6 +2,8 @@* UI utility functions*/+import { safeSetHTML, safeSetText, sanitizeHTML } from '../utils/sanitizer.js';+/*** Update a progress bar UI element* @param {string|Element} fillElementId - The ID or element to fill@@ -49,7 +51,8 @@ function showSpinner(container, message = 'Loading...') {</div>`;- containerEl.innerHTML = spinnerHTML;+ // Safe: spinner HTML is controlled/static, but sanitize message for safety+ safeSetHTML(containerEl, spinnerHTML, 'ui');}}@@ -84,7 +87,8 @@ function showError(container, message) {</div>`;- containerEl.innerHTML = errorHTML;+ // Sanitize message to prevent XSS+ safeSetHTML(containerEl, errorHTML, 'ui');}}@@ -126,11 +130,11 @@ function showMessage(message, type = 'success', duration = 3000) {break;}- // Set the content- toast.innerHTML = `+ // Set the content (sanitize message to prevent XSS)+ safeSetHTML(toast, `${icon}<div class="toast-message">${message}</div>- `;+ `, 'ui');// Add to containertoastContainer.appendChild(toast);@@ -360,12 +364,12 @@ function showAlert(message, type = 'info', skipIfToastShown = true) {// Create alert elementconst alert = document.createElement('div');alert.className = `alert alert-${type}`;- alert.innerHTML = `<i class="fas ${type === 'success' ? 'fa-check-circle' : 'fa-exclamation-circle'}"></i> ${message}`;+ safeSetHTML(alert, `<i class="fas ${type === 'success' ? 'fa-check-circle' : 'fa-exclamation-circle'}"></i> ${message}`, 'ui');// Add a close buttonconst closeBtn = document.createElement('span');closeBtn.className = 'alert-close';- closeBtn.innerHTML = '×';+ closeBtn.textContent = '×'; // Use textContent instead of innerHTMLcloseBtn.addEventListener('click', () => {alert.remove();alertContainer.style.display = 'none';diff --git a/src/local_deep_research/web/static/js/utils/sanitizer.js b/src/local_deep_research/web/static/js/utils/sanitizer.jsnew file mode 100644index 0000000000..faeafb3d36--- /dev/null+++ b/src/local_deep_research/web/static/js/utils/sanitizer.js@@ -0,0 +1,231 @@+/**+ * HTML Sanitization Utility using DOMPurify+ *+ * Provides XSS protection for dynamic content by sanitizing HTML before insertion.+ * Use these functions instead of direct innerHTML assignments to prevent XSS attacks.+ *+ * @module sanitizer+ */++import DOMPurify from 'dompurify';++/**+ * Configuration for different sanitization contexts+ */+const SANITIZE_CONFIG = {+ // Strict: For user-generated content, only allow safe formatting+ strict: {+ ALLOWED_TAGS: ['b', 'i', 'em', 'strong', 'a', 'p', 'br', 'span', 'div'],+ ALLOWED_ATTR: ['href', 'title', 'class'],+ ALLOW_DATA_ATTR: false,+ ALLOW_UNKNOWN_PROTOCOLS: false,+ SAFE_FOR_TEMPLATES: true+ },++ // UI: For UI components, allow common elements+ ui: {+ ALLOWED_TAGS: ['div', 'span', 'i', 'strong', 'em', 'b', 'p', 'br', 'small', 'code', 'pre'],+ ALLOWED_ATTR: ['class', 'id', 'title', 'aria-label', 'role', 'data-*'],+ ALLOW_DATA_ATTR: true,+ ALLOW_UNKNOWN_PROTOCOLS: false,+ SAFE_FOR_TEMPLATES: true+ },++ // Rich: For research content, allow more formatting+ rich: {+ ALLOWED_TAGS: [+ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',+ 'p', 'br', 'hr', 'div', 'span',+ 'strong', 'em', 'b', 'i', 'u', 's', 'strike',+ 'ul', 'ol', 'li',+ 'a', 'code', 'pre',+ 'blockquote', 'q', 'cite',+ 'table', 'thead', 'tbody', 'tr', 'th', 'td',+ 'img'+ ],+ ALLOWED_ATTR: [+ 'href', 'title', 'class', 'id',+ 'alt', 'src', // For images+ 'cite', // For citations+ 'target', 'rel' // For links+ ],+ ALLOW_DATA_ATTR: false,+ ALLOW_UNKNOWN_PROTOCOLS: false,+ SAFE_FOR_TEMPLATES: true,+ // Enforce safe link targets+ ADD_ATTR: ['target'],+ FORBID_ATTR: ['onerror', 'onload']+ }+};++/**+ * Sanitize HTML string using DOMPurify with given configuration+ *+ * @param {string} dirty - Potentially unsafe HTML string+ * @param {string} level - Sanitization level: 'strict', 'ui', or 'rich'+ * @returns {string} Sanitized HTML string safe for insertion+ *+ * @example+ * const clean = sanitizeHTML('<script>alert("xss")</script><p>Safe content</p>', 'strict');+ * // Returns: '<p>Safe content</p>'+ */+export function sanitizeHTML(dirty, level = 'ui') {+ if (!dirty || typeof dirty !== 'string') {+ return '';+ }++ const config = SANITIZE_CONFIG[level] || SANITIZE_CONFIG.ui;+ return DOMPurify.sanitize(dirty, config);+}++/**+ * Safely set innerHTML of an element with sanitized HTML+ *+ * @param {HTMLElement} element - Target element+ * @param {string} htmlString - HTML string to sanitize and insert+ * @param {string} level - Sanitization level: 'strict', 'ui', or 'rich'+ *+ * @example+ * safeSetHTML(element, '<b>Bold text</b><script>alert("xss")</script>', 'ui');+ * // Sets element.innerHTML to: '<b>Bold text</b>'+ */+export function safeSetHTML(element, htmlString, level = 'ui') {+ if (!element || !(element instanceof HTMLElement)) {+ console.error('safeSetHTML: Invalid element provided');+ return;+ }++ element.innerHTML = sanitizeHTML(htmlString, level);+}++/**+ * Escape HTML special characters for plain text display+ * Use this when you want to display user input as-is without any HTML parsing+ *+ * @param {string} text - Text to escape+ * @returns {string} Escaped text safe for HTML context+ *+ * @example+ * const safe = escapeHTML('<script>alert("xss")</script>');+ * // Returns: '<script>alert("xss")</script>'+ */+export function escapeHTML(text) {+ if (!text || typeof text !== 'string') {+ return '';+ }++ const div = document.createElement('div');+ div.textContent = text;+ return div.innerHTML;+}++/**+ * Safely set text content of an element (no HTML parsing)+ * Use this for plain text that should never be interpreted as HTML+ *+ * @param {HTMLElement} element - Target element+ * @param {string} text - Plain text to set+ *+ * @example+ * safeSetText(element, '<b>This will be displayed as literal text</b>');+ */+export function safeSetText(element, text) {+ if (!element || !(element instanceof HTMLElement)) {+ console.error('safeSetText: Invalid element provided');+ return;+ }++ element.textContent = text || '';+}++/**+ * Create a safe DOM element from HTML string+ * Returns a DocumentFragment containing sanitized elements+ *+ * @param {string} htmlString - HTML string to parse+ * @param {string} level - Sanitization level+ * @returns {DocumentFragment} Document fragment with sanitized content+ *+ * @example+ * const fragment = createSafeElement('<div>Safe content</div>');+ * parentElement.appendChild(fragment);+ */+export function createSafeElement(htmlString, level = 'ui') {+ const template = document.createElement('template');+ template.innerHTML = sanitizeHTML(htmlString, level);+ return template.content;+}++/**+ * Sanitize URL to prevent javascript: and data: URI attacks+ *+ * @param {string} url - URL to sanitize+ * @returns {string} Safe URL or empty string if unsafe+ *+ * @example+ * const safe = sanitizeURL('javascript:alert("xss")');+ * // Returns: ''+ *+ * const safe2 = sanitizeURL('https://example.com');+ * // Returns: 'https://example.com'+ */+export function sanitizeURL(url) {+ if (!url || typeof url !== 'string') {+ return '';+ }++ const trimmed = url.trim().toLowerCase();++ // Block dangerous protocols+ const dangerousProtocols = ['javascript:', 'data:', 'vbscript:', 'file:'];+ for (const protocol of dangerousProtocols) {+ if (trimmed.startsWith(protocol)) {+ console.warn(`Blocked dangerous URL protocol: ${protocol}`);+ return '';+ }+ }++ // Allow safe protocols+ const safeProtocols = ['http:', 'https:', 'mailto:', 'tel:', 'ftp:', '//', '/'];+ const isSafe = safeProtocols.some(proto => trimmed.startsWith(proto));++ if (!isSafe && trimmed.includes(':')) {+ console.warn('Blocked URL with unknown protocol:', url);+ return '';+ }++ return url;+}++/**+ * Configure DOMPurify hooks for additional security+ */+function configureDOMPurify() {+ // Hook to enforce safe link targets+ DOMPurify.addHook('afterSanitizeAttributes', (node) => {+ // Set all links to open in new tab with noopener noreferrer+ if (node.tagName === 'A') {+ node.setAttribute('target', '_blank');+ node.setAttribute('rel', 'noopener noreferrer');+ }++ // Remove any remaining inline event handlers+ for (const attr of node.attributes) {+ if (attr.name.startsWith('on')) {+ node.removeAttribute(attr.name);+ }+ }+ });+}++// Initialize DOMPurify configuration on module load+configureDOMPurify();++export default {+ sanitizeHTML,+ safeSetHTML,+ escapeHTML,+ safeSetText,+ createSafeElement,+ sanitizeURL+};