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.

refactor: consolidate escapeHtml implementations with fallback pattern (#2019)

LearningCircuit16c1eadcd479
diff --git a/src/local_deep_research/web/static/js/collection_details.js b/src/local_deep_research/web/static/js/collection_details.js
index a25135f77b..8f2b250808 100644
--- a/src/local_deep_research/web/static/js/collection_details.js
+++ b/src/local_deep_research/web/static/js/collection_details.js
@@ -506,14 +506,12 @@ function formatBytes(bytes) {
}
/**
- * Escape HTML to prevent XSS
+ * Fallback HTML escape function (used if xss-protection.js fails to load)
*/
-function escapeHtml(text) {
- if (!text) return '';
- const div = document.createElement('div');
- div.textContent = text;
- return div.innerHTML;
-}
+const escapeHtmlFallback = (str) => String(str || '').replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);
+
+// Use global escapeHtml from xss-protection.js with fallback
+const escapeHtml = window.escapeHtml || escapeHtmlFallback;
/**
* Show success message
diff --git a/src/local_deep_research/web/static/js/collection_upload.js b/src/local_deep_research/web/static/js/collection_upload.js
index 209d9df27a..89eb74dd3b 100644
--- a/src/local_deep_research/web/static/js/collection_upload.js
+++ b/src/local_deep_research/web/static/js/collection_upload.js
@@ -18,15 +18,14 @@ async function safeFetch(url, options = {}) {
}
/**
- * Escape HTML special characters to prevent XSS attacks
+ * Fallback HTML escape function (used if xss-protection.js fails to load)
* @param {string} text - Text to escape
* @returns {string} - Escaped text safe for HTML insertion
*/
-function escapeHtml(text) {
- const div = document.createElement('div');
- div.textContent = text;
- return div.innerHTML;
-}
+const escapeHtmlFallback = (str) => String(str).replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);
+
+// Use global escapeHtml from xss-protection.js with fallback
+const escapeHtml = window.escapeHtml || escapeHtmlFallback;
// Store selected files globally to avoid losing them
let selectedFiles = [];
diff --git a/src/local_deep_research/web/static/js/collections_manager.js b/src/local_deep_research/web/static/js/collections_manager.js
index 73020ddde1..5408c64a3f 100644
--- a/src/local_deep_research/web/static/js/collections_manager.js
+++ b/src/local_deep_research/web/static/js/collections_manager.js
@@ -242,14 +242,12 @@ function formatBytes(bytes) {
}
/**
- * Escape HTML to prevent XSS
+ * Fallback HTML escape function (used if xss-protection.js fails to load)
*/
-function escapeHtml(text) {
- if (!text) return '';
- const div = document.createElement('div');
- div.textContent = text;
- return div.innerHTML;
-}
+const escapeHtmlFallback = (str) => String(str || '').replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);
+
+// Use global escapeHtml from xss-protection.js with fallback
+const escapeHtml = window.escapeHtml || escapeHtmlFallback;
/**
* Show success message
diff --git a/src/local_deep_research/web/static/js/components/detail.js b/src/local_deep_research/web/static/js/components/detail.js
index 3a3d353d06..13264c382c 100644
--- a/src/local_deep_research/web/static/js/components/detail.js
+++ b/src/local_deep_research/web/static/js/components/detail.js
@@ -709,15 +709,12 @@
}
/**
- * Escape HTML to prevent XSS
- * @param {string} text - Text to escape
- * @returns {string} Escaped text
+ * Fallback HTML escape function (used if xss-protection.js fails to load)
*/
- function escapeHtml(text) {
- const div = document.createElement('div');
- div.textContent = text;
- return div.innerHTML;
- }
+ const escapeHtmlFallback = (str) => String(str || '').replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);
+
+ // Use global escapeHtml from xss-protection.js with fallback
+ const escapeHtml = window.escapeHtml || escapeHtmlFallback;
// Clean up on page unload
window.addEventListener('beforeunload', function() {
diff --git a/src/local_deep_research/web/static/js/components/settings.js b/src/local_deep_research/web/static/js/components/settings.js
index 126f81414c..41e5a83654 100644
--- a/src/local_deep_research/web/static/js/components/settings.js
+++ b/src/local_deep_research/web/static/js/components/settings.js
@@ -40,19 +40,15 @@
const CACHE_EXPIRATION = 24 * 60 * 60 * 1000;
/**
- * Escape HTML to prevent XSS attacks
- * @param {string} unsafe - Text to escape
- * @returns {string} Escaped text
+ * Fallback HTML escape function (used if xss-protection.js fails to load)
*/
- function escapeHtml(unsafe) {
- if (unsafe === null || unsafe === undefined) return '';
- return String(unsafe)
- .replace(/&/g, "&amp;")
- .replace(/</g, "&lt;")
- .replace(/>/g, "&gt;")
- .replace(/"/g, "&quot;")
- .replace(/'/g, "&#039;");
- }
+ const escapeHtmlFallback = (str) => {
+ if (str === null || str === undefined) return '';
+ return String(str).replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);
+ };
+
+ // Use global escapeHtml from xss-protection.js with fallback
+ const escapeHtml = window.escapeHtml || escapeHtmlFallback;
/**
* Helper function to generate custom dropdown HTML (similar to Jinja macro)
@@ -1546,9 +1542,8 @@
`;
} else {
// Sanitize data directory path to prevent XSS
- // Inline fallback ensures escaping even if xss-protection.js fails to load
- const escapeHtmlFallback = (str) => String(str).replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);
- const safeDataDirCompact = (window.escapeHtml || escapeHtmlFallback)(data.data_directory);
+ // Use escapeHtml (defined at top of IIFE with fallback)
+ const safeDataDirCompact = escapeHtml(data.data_directory);
html += `
<div class="data-location-compact">
diff --git a/src/local_deep_research/web/static/js/deletion/confirmation_modal.js b/src/local_deep_research/web/static/js/deletion/confirmation_modal.js
index 53ae024500..8ff37c7df8 100644
--- a/src/local_deep_research/web/static/js/deletion/confirmation_modal.js
+++ b/src/local_deep_research/web/static/js/deletion/confirmation_modal.js
@@ -200,13 +200,12 @@ function hideDeleteModal() {
}
/**
- * Escape HTML to prevent XSS
+ * Fallback HTML escape function (used if xss-protection.js fails to load)
*/
-function escapeHtml(text) {
- const div = document.createElement('div');
- div.textContent = text;
- return div.innerHTML;
-}
+const escapeHtmlFallback = (str) => String(str || '').replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);
+
+// Use global escapeHtml from xss-protection.js with fallback
+const escapeHtml = window.escapeHtml || escapeHtmlFallback;
/**
* Get tooltip text for an action
diff --git a/src/local_deep_research/web/static/js/embedding_settings.js b/src/local_deep_research/web/static/js/embedding_settings.js
index 3194f32222..cd020e123f 100644
--- a/src/local_deep_research/web/static/js/embedding_settings.js
+++ b/src/local_deep_research/web/static/js/embedding_settings.js
@@ -570,13 +570,12 @@ function showError(message) {
}
/**
- * Escape HTML to prevent XSS
+ * Fallback HTML escape function (used if xss-protection.js fails to load)
*/
-function escapeHtml(text) {
- const div = document.createElement('div');
- div.textContent = text;
- return div.innerHTML;
-}
+const escapeHtmlFallback = (str) => String(str || '').replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);
+
+// Use global escapeHtml from xss-protection.js with fallback
+const escapeHtml = window.escapeHtml || escapeHtmlFallback;
/**
* Toggle Ollama URL field visibility based on selected provider