Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(web): avoid malformed markdown hash link crashes

VectorPeaka0098012b882
diff --git a/web/components/common/RichMarkdownRenderer.tsx b/web/components/common/RichMarkdownRenderer.tsx
index bf31721cd..57b92c483 100644
--- a/web/components/common/RichMarkdownRenderer.tsx
+++ b/web/components/common/RichMarkdownRenderer.tsx
@@ -16,6 +16,7 @@ import {
escapeUnknownHtmlTagsForDisplay,
markdownUrlTransform,
normalizeMarkdownForDisplay,
+ safeDecodeURIComponent,
} from "@/lib/markdown-display";
import {
InlineFileCard,
@@ -584,7 +585,7 @@ export default function RichMarkdownRenderer({
id && citationAnchorIdFor(id)
? citationAnchorIdFor(id)
: href?.startsWith("#")
- ? decodeURIComponent(href.slice(1))
+ ? safeDecodeURIComponent(href.slice(1))
: "references";
const target =
document.getElementById(hashTarget || "") ??
@@ -644,7 +645,7 @@ export default function RichMarkdownRenderer({
if (!isHashLink || !href) return;
event.preventDefault();
- const targetId = decodeURIComponent(href.slice(1));
+ const targetId = safeDecodeURIComponent(href.slice(1));
const target = document.getElementById(targetId);
if (target) scrollAnchorIntoView(target);
}}
diff --git a/web/components/common/SimpleMarkdownRenderer.tsx b/web/components/common/SimpleMarkdownRenderer.tsx
index 0366806f8..ed8bc963e 100644
--- a/web/components/common/SimpleMarkdownRenderer.tsx
+++ b/web/components/common/SimpleMarkdownRenderer.tsx
@@ -7,6 +7,7 @@ import {
citationAnchorIdFor,
markdownUrlTransform,
normalizeMarkdownForDisplay,
+ safeDecodeURIComponent,
} from "@/lib/markdown-display";
import {
InlineFileCard,
@@ -365,7 +366,7 @@ export default function SimpleMarkdownRenderer({
id && citationAnchorIdFor(id)
? citationAnchorIdFor(id)
: href?.startsWith("#")
- ? decodeURIComponent(href.slice(1))
+ ? safeDecodeURIComponent(href.slice(1))
: "references";
const target =
document.getElementById(hashTarget || "") ??
@@ -425,7 +426,7 @@ export default function SimpleMarkdownRenderer({
if (!isHashLink || !href) return;
event.preventDefault();
- const targetId = decodeURIComponent(href.slice(1));
+ const targetId = safeDecodeURIComponent(href.slice(1));
const target = document.getElementById(targetId);
target?.scrollIntoView({ block: "start", behavior: "smooth" });
}}
diff --git a/web/lib/markdown-display.ts b/web/lib/markdown-display.ts
index 28e0b2bc7..3d72ca627 100644
--- a/web/lib/markdown-display.ts
+++ b/web/lib/markdown-display.ts
@@ -213,6 +213,14 @@ export function markdownUrlTransform(
return "";
}
+export function safeDecodeURIComponent(value: string): string {
+ try {
+ return decodeURIComponent(value);
+ } catch {
+ return value;
+ }
+}
+
function sanitizeAllowedHtmlTag(tag: string): string {
return tag
.replace(HTML_EVENT_ATTR_REGEX, "")
diff --git a/web/tests/markdown-display.test.ts b/web/tests/markdown-display.test.ts
index f48bcecdd..d0161482b 100644
--- a/web/tests/markdown-display.test.ts
+++ b/web/tests/markdown-display.test.ts
@@ -5,6 +5,7 @@ import {
hasVisibleMarkdownContent,
markdownUrlTransform,
normalizeMarkdownForDisplay,
+ safeDecodeURIComponent,
} from "../lib/markdown-display";
test("normalizeMarkdownForDisplay removes empty details blocks", () => {
@@ -197,6 +198,15 @@ test("markdownUrlTransform only allows data images on img src", () => {
);
});
+test("safeDecodeURIComponent decodes valid hash components", () => {
+ assert.equal(safeDecodeURIComponent("section%201"), "section 1");
+});
+
+test("safeDecodeURIComponent keeps malformed hash components intact", () => {
+ assert.doesNotThrow(() => safeDecodeURIComponent("%E0%A4%A"));
+ assert.equal(safeDecodeURIComponent("%E0%A4%A"), "%E0%A4%A");
+});
+
test("hasVisibleMarkdownContent rejects empty raw-html placeholders", () => {
assert.equal(
hasVisibleMarkdownContent("<details><summary></summary></details>"),