mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-05 18:20:31 +08:00
Merge pull request #608 from VectorPeak/codex/markdown-safe-hash-decode
fix(web): avoid crashes on malformed Markdown hash links
This commit is contained in:
@@ -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);
|
||||
}}
|
||||
|
||||
@@ -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" });
|
||||
}}
|
||||
|
||||
@@ -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, "")
|
||||
|
||||
@@ -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>"),
|
||||
|
||||
Reference in New Issue
Block a user