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: remove duplicate escapeHtmlFallback declarations that crash settings page (#2091)

LearningCircuit4f1b6c5a92c8
diff --git a/src/local_deep_research/web/static/js/collection_create.js b/src/local_deep_research/web/static/js/collection_create.js
index d8acbcd723..ddaaa16e3a 100644
--- a/src/local_deep_research/web/static/js/collection_create.js
+++ b/src/local_deep_research/web/static/js/collection_create.js
@@ -3,9 +3,7 @@
* Handles creation of new collections
*/
-// Inline fallback for XSS protection if xss-protection.js fails to load
-// Using 'var' allows redeclaration if this script is loaded with other scripts that define the same fallback
-var escapeHtmlFallback = escapeHtmlFallback || ((str) => String(str).replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]));
+// escapeHtmlFallback is provided globally by services/ui.js (loaded via base.html)
// Use existing URLS configuration from config/urls.js
// Collection create endpoint is now available as URLS.LIBRARY_API.COLLECTION_CREATE
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 8f2b250808..0d21f51bc6 100644
--- a/src/local_deep_research/web/static/js/collection_details.js
+++ b/src/local_deep_research/web/static/js/collection_details.js
@@ -505,13 +505,9 @@ function formatBytes(bytes) {
return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
}
-/**
- * Fallback HTML escape function (used if xss-protection.js fails to load)
- */
-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;
+// escapeHtmlFallback is provided globally by services/ui.js (loaded via base.html)
+// escapeHtml is provided globally by security/xss-protection.js with escapeHtmlFallback as safety net
+var 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 89eb74dd3b..ca6543a1ef 100644
--- a/src/local_deep_research/web/static/js/collection_upload.js
+++ b/src/local_deep_research/web/static/js/collection_upload.js
@@ -22,10 +22,9 @@ async function safeFetch(url, options = {}) {
* @param {string} text - Text to escape
* @returns {string} - Escaped text safe for HTML insertion
*/
-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;
+// escapeHtmlFallback is provided globally by services/ui.js (loaded via base.html)
+// escapeHtml is provided globally by security/xss-protection.js with escapeHtmlFallback as safety net
+var 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 5408c64a3f..183a6e02e3 100644
--- a/src/local_deep_research/web/static/js/collections_manager.js
+++ b/src/local_deep_research/web/static/js/collections_manager.js
@@ -241,13 +241,9 @@ function formatBytes(bytes) {
return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
}
-/**
- * Fallback HTML escape function (used if xss-protection.js fails to load)
- */
-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;
+// escapeHtmlFallback is provided globally by services/ui.js (loaded via base.html)
+// escapeHtml is provided globally by security/xss-protection.js with escapeHtmlFallback as safety net
+var 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 13264c382c..d7c055ed42 100644
--- a/src/local_deep_research/web/static/js/components/detail.js
+++ b/src/local_deep_research/web/static/js/components/detail.js
@@ -709,7 +709,10 @@
}
/**
- * Fallback HTML escape function (used if xss-protection.js fails to load)
+ * Fallback HTML escape function (used if xss-protection.js fails to load).
+ * NOTE: This declaration is safe because it is INSIDE an IIFE (function scope).
+ * Do NOT move it to top-level scope — it would conflict with the global
+ * escapeHtmlFallback in services/ui.js and crash the page.
*/
const escapeHtmlFallback = (str) => String(str || '').replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);
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 c5d9188308..9c9fe9bd54 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
@@ -13,9 +13,10 @@
/**
* 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.
+ * xss-protection.js fails to load.
+ * NOTE: This declaration is safe because it is INSIDE an IIFE (function scope).
+ * Do NOT move it to top-level scope — it would conflict with the global
+ * escapeHtmlFallback in services/ui.js and crash the page.
*/
const escapeHtmlFallback = (str) => String(str).replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);
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 30245fa236..4733b71a54 100644
--- a/src/local_deep_research/web/static/js/components/results.js
+++ b/src/local_deep_research/web/static/js/components/results.js
@@ -12,9 +12,10 @@
/**
* 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.
+ * xss-protection.js fails to load.
+ * NOTE: This declaration is safe because it is INSIDE an IIFE (function scope).
+ * Do NOT move it to top-level scope — it would conflict with the global
+ * escapeHtmlFallback in services/ui.js and crash the page.
*/
const escapeHtmlFallback = (str) => String(str).replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);
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 41e5a83654..a4da61860f 100644
--- a/src/local_deep_research/web/static/js/components/settings.js
+++ b/src/local_deep_research/web/static/js/components/settings.js
@@ -40,7 +40,10 @@
const CACHE_EXPIRATION = 24 * 60 * 60 * 1000;
/**
- * Fallback HTML escape function (used if xss-protection.js fails to load)
+ * Fallback HTML escape function (used if xss-protection.js fails to load).
+ * NOTE: This declaration is safe because it is INSIDE an IIFE (function scope).
+ * Do NOT move it to top-level scope — it would conflict with the global
+ * escapeHtmlFallback in services/ui.js and crash the page.
*/
const escapeHtmlFallback = (str) => {
if (str === null || str === undefined) return '';
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 8ff37c7df8..aa7b13431d 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
@@ -199,13 +199,9 @@ function hideDeleteModal() {
}
}
-/**
- * Fallback HTML escape function (used if xss-protection.js fails to load)
- */
-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;
+// escapeHtmlFallback is provided globally by services/ui.js (loaded via base.html)
+// escapeHtml is provided globally by security/xss-protection.js with escapeHtmlFallback as safety net
+var escapeHtml = window.escapeHtml || escapeHtmlFallback;
/**
* Get tooltip text for an action
diff --git a/src/local_deep_research/web/static/js/deletion/delete_manager.js b/src/local_deep_research/web/static/js/deletion/delete_manager.js
index 7dc9904732..06d12efb69 100644
--- a/src/local_deep_research/web/static/js/deletion/delete_manager.js
+++ b/src/local_deep_research/web/static/js/deletion/delete_manager.js
@@ -12,9 +12,7 @@
* URLValidator is not needed here as all fetch() calls are to internal paths.
*/
-// Inline fallback for XSS protection if xss-protection.js fails to load
-// Using 'var' allows redeclaration if this script is loaded with other scripts that define the same fallback
-var escapeHtmlFallback = escapeHtmlFallback || ((str) => String(str).replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]));
+// escapeHtmlFallback is provided globally by services/ui.js (loaded via base.html)
// API Endpoints
const DELETE_API = {
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 cd020e123f..f3889d7517 100644
--- a/src/local_deep_research/web/static/js/embedding_settings.js
+++ b/src/local_deep_research/web/static/js/embedding_settings.js
@@ -569,13 +569,9 @@ function showError(message) {
}, 5000);
}
-/**
- * Fallback HTML escape function (used if xss-protection.js fails to load)
- */
-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;
+// escapeHtmlFallback is provided globally by services/ui.js (loaded via base.html)
+// escapeHtml is provided globally by security/xss-protection.js with escapeHtmlFallback as safety net
+var escapeHtml = window.escapeHtml || escapeHtmlFallback;
/**
* Toggle Ollama URL field visibility based on selected provider
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 cb5ec7f630..b8b729fd23 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
@@ -11,33 +11,22 @@
*
* 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:
+ * A single global `escapeHtmlFallback` is defined in services/ui.js (loaded
+ * via base.html on all pages). This provides defense-in-depth XSS protection
+ * if this xss-protection.js file fails to load.
*
- * 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.
+ * IMPORTANT: Only services/ui.js should define escapeHtmlFallback at top-level
+ * scope. Other files must NOT redeclare it — doing so causes SyntaxError
+ * ("Identifier has already been declared") since const/var cannot coexist
+ * in the same global scope across <script> tags.
*
- * 2. The inline fallback is a one-liner that's easy to audit and verify:
- * `(str) => String(str).replace(/[&<>"']/g, (m) => ({'&':'&amp;',...})[m])`
+ * Files wrapped in IIFEs (components/results.js, components/settings.js,
+ * components/detail.js, components/fallback/ui.js) may safely define their
+ * own scoped fallback.
*
- * 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
- * - components/settings.js
- * - services/ui.js
- *
- * Files depending on window.escapeHtml (no inline fallback):
- * - pages/subscriptions.js
- * - pages/news.js
+ * 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.
*/
(function() {
diff --git a/src/local_deep_research/web/static/js/services/socket.js b/src/local_deep_research/web/static/js/services/socket.js
index b1af246d37..28f9190b0e 100644
--- a/src/local_deep_research/web/static/js/services/socket.js
+++ b/src/local_deep_research/web/static/js/services/socket.js
@@ -3,9 +3,7 @@
* Manages WebSocket communication using Socket.IO
*/
-// Inline fallback for XSS protection if xss-protection.js fails to load
-// Using 'var' allows redeclaration if this script is loaded with other scripts that define the same fallback
-var escapeHtmlFallback = escapeHtmlFallback || ((str) => String(str).replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]));
+// escapeHtmlFallback is provided globally by services/ui.js (loaded via base.html)
window.socket = (function() {
let socket = null;
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 0c6617dde5..144ec8ad29 100644
--- a/src/local_deep_research/web/static/js/services/ui.js
+++ b/src/local_deep_research/web/static/js/services/ui.js
@@ -56,10 +56,18 @@ 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.
+ * GLOBAL fallback for HTML escaping - provides XSS protection even if
+ * xss-protection.js fails to load. Loaded via base.html on ALL pages.
+ *
+ * WARNING: DO NOT redeclare this variable in other top-level scripts.
+ * Declaring `const`, `let`, or `var` escapeHtmlFallback at top-level
+ * in another <script> tag will cause: SyntaxError: "Identifier
+ * 'escapeHtmlFallback' has already been declared" — crashing that
+ * script entirely.
+ *
+ * If you need escapeHtmlFallback in another file, either:
+ * 1. Reference this global directly (it's available everywhere)
+ * 2. Wrap your code in an IIFE and declare a local copy inside it
*/
const escapeHtmlFallback = (str) => String(str).replace(/[&<>"']/g, (m) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'})[m]);
diff --git a/src/local_deep_research/web/templates/base.html b/src/local_deep_research/web/templates/base.html
index 48227748ad..bfebd7cede 100644
--- a/src/local_deep_research/web/templates/base.html
+++ b/src/local_deep_research/web/templates/base.html
@@ -147,7 +147,11 @@
<!-- All vendor libraries are now loaded through Vite in the <head> section -->
- <!-- Core JS -->
+ <!-- Core JS
+ IMPORTANT: ui.js defines the global `escapeHtmlFallback` constant.
+ All subsequent scripts can reference it directly. Do NOT redeclare
+ escapeHtmlFallback at top-level in any other script — it will cause
+ a SyntaxError and crash the page. See the comment in ui.js. -->
<script src="/static/js/services/theme.js"></script>
<script src="/static/js/services/formatting.js"></script>
<script src="/static/js/services/ui.js"></script>