Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Sanitize all marked-rendered HTML (chat, Inquire answers, admin banner/disclaimers) through DOMPurify before v-html, and vendor DOMPurify locally, closing the client-side XSS from LLM/admin content

Murtaza Nasirfce206f68b8f
diff --git a/scripts/download_offline_deps.py b/scripts/download_offline_deps.py
index 80f7f187..44b2aecc 100644
--- a/scripts/download_offline_deps.py
+++ b/scripts/download_offline_deps.py
@@ -23,6 +23,7 @@ DEPENDENCIES = {
"easymde.min.js": "https://cdn.jsdelivr.net/npm/easymde/dist/easymde.min.js",
"axios.min.js": "https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js",
"chart.umd.min.js": "https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.umd.min.js",
+ "purify.min.js": "https://cdn.jsdelivr.net/npm/[email protected]/dist/purify.min.js",
}
}
diff --git a/static/js/app.modular.js b/static/js/app.modular.js
index f76de772..1c1e27be 100644
--- a/static/js/app.modular.js
+++ b/static/js/app.modular.js
@@ -3285,7 +3285,7 @@ document.addEventListener('DOMContentLoaded', async () => {
if (!recordingDisclaimer.value || recordingDisclaimer.value.trim() === '') {
return '';
}
- return marked.parse(recordingDisclaimer.value);
+ return window.renderMarkdownSafe(recordingDisclaimer.value);
});
// Upload disclaimer parsed as markdown
@@ -3293,7 +3293,7 @@ document.addEventListener('DOMContentLoaded', async () => {
if (!uploadDisclaimer.value || uploadDisclaimer.value.trim() === '') {
return '';
}
- return marked.parse(uploadDisclaimer.value);
+ return window.renderMarkdownSafe(uploadDisclaimer.value);
});
// Custom banner parsed as markdown
@@ -3301,7 +3301,7 @@ document.addEventListener('DOMContentLoaded', async () => {
if (!customBanner.value || customBanner.value.trim() === '') {
return '';
}
- return marked.parse(customBanner.value);
+ return window.renderMarkdownSafe(customBanner.value);
});
// Get tag prompt preview
diff --git a/static/js/markdown-safe.js b/static/js/markdown-safe.js
new file mode 100644
index 00000000..bb16859c
--- /dev/null
+++ b/static/js/markdown-safe.js
@@ -0,0 +1,32 @@
+/**
+ * Safe markdown rendering.
+ *
+ * marked.parse() passes raw inline HTML through, and its output is assigned to
+ * v-html sinks (chat/Inquire answers, admin banner/disclaimers). That content
+ * originates from LLM output and admin settings, so an embedded
+ * `<img src=x onerror=...>` would execute. Every such path must go through
+ * this helper, which sanitizes the parsed HTML with DOMPurify.
+ *
+ * Fails closed: if DOMPurify is somehow unavailable, the text is emitted
+ * escaped rather than as raw HTML.
+ */
+(function () {
+ function renderMarkdownSafe(text) {
+ if (text === null || text === undefined) return '';
+ var html;
+ try {
+ html = (window.marked && window.marked.parse)
+ ? window.marked.parse(text)
+ : String(text);
+ } catch (e) {
+ html = String(text);
+ }
+ if (window.DOMPurify && typeof window.DOMPurify.sanitize === 'function') {
+ return window.DOMPurify.sanitize(html);
+ }
+ var div = document.createElement('div');
+ div.textContent = html;
+ return div.innerHTML;
+ }
+ window.renderMarkdownSafe = renderMarkdownSafe;
+})();
diff --git a/static/js/modules/composables/chat.js b/static/js/modules/composables/chat.js
index 5d540b38..47dbe87e 100644
--- a/static/js/modules/composables/chat.js
+++ b/static/js/modules/composables/chat.js
@@ -182,7 +182,7 @@ export function useChat(state, utils) {
}
assistantMessage.content += textContent;
- assistantMessage.html = marked.parse(assistantMessage.content);
+ assistantMessage.html = window.renderMarkdownSafe(assistantMessage.content);
if (shouldScroll) {
await Vue.nextTick();
@@ -221,7 +221,7 @@ export function useChat(state, utils) {
const errSuffix = `\n\n_⚠️ Connection ended before the response completed: ${error.message}_`;
if (partial) {
assistantMessage.content = partial + errSuffix;
- assistantMessage.html = marked.parse(assistantMessage.content);
+ assistantMessage.html = window.renderMarkdownSafe(assistantMessage.content);
} else {
assistantMessage.content = `Error: ${error.message}`;
assistantMessage.html = `<span class="text-red-500">Error: ${error.message}</span>`;
diff --git a/templates/index.html b/templates/index.html
index 011aaf77..cf51e6d2 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -63,6 +63,8 @@
<script src="{{ url_for('static', filename='vendor/js/tailwind.min.js') }}"></script>
<script src="{{ url_for('static', filename='vendor/js/vue.global.js') }}"></script>
<script src="{{ url_for('static', filename='vendor/js/marked.min.js') }}"></script>
+ <script src="{{ url_for('static', filename='vendor/js/purify.min.js') }}"></script>
+ <script src="{{ url_for('static', filename='js/markdown-safe.js') }}"></script>
<link rel="stylesheet" href="{{ url_for('static', filename='vendor/css/easymde.min.css') }}">
<script src="{{ url_for('static', filename='vendor/js/easymde.min.js') }}"></script>
<link rel="stylesheet" href="{{ url_for('static', filename='vendor/css/fontawesome.min.css') }}">
diff --git a/templates/inquire.html b/templates/inquire.html
index a3677cac..6d9c6a25 100644
--- a/templates/inquire.html
+++ b/templates/inquire.html
@@ -25,6 +25,8 @@
<script src="{{ url_for('static', filename='vendor/js/vue.global.js') }}"></script>
<!-- All dependencies bundled locally for offline support -->
<script src="{{ url_for('static', filename='vendor/js/marked.min.js') }}"></script>
+<script src="{{ url_for('static', filename='vendor/js/purify.min.js') }}"></script>
+<script src="{{ url_for('static', filename='js/markdown-safe.js') }}"></script>
<!-- All dependencies bundled locally for offline support -->
<link rel="stylesheet" href="{{ url_for('static', filename='vendor/css/fontawesome.min.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='css/styles.css') }}">
@@ -833,7 +835,7 @@
showProcessingStatus.value = false;
}
assistantMessage.content += data.delta;
- assistantMessage.html = marked.parse(assistantMessage.content);
+ assistantMessage.html = window.renderMarkdownSafe(assistantMessage.content);
// Smart scroll during streaming
await nextTick();
scrollToBottom();