Skip to content

Commit 15ba00f

Browse files
committed
fix(markdown): normalize app schemes before sanitization
1 parent 39b7752 commit 15ba00f

2 files changed

Lines changed: 38 additions & 10 deletions

File tree

‎apps/web/src/components/ChatMarkdown.test.tsx‎

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,23 +80,42 @@ describe.each([
8080
"obsidian://open?vault=Example&file=Notes",
8181
])("ChatMarkdown app deep link %s", (href) => {
8282
it.each([true, false])(
83-
"preserves app destinations through the markdown pipeline with parseRawHtml=%s",
83+
"preserves app destinations with lowercase and mixed-case schemes with parseRawHtml=%s",
8484
async (parseRawHtml) => {
8585
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
8686
let renderer: ReactTestRenderer | undefined;
87+
const uppercaseHref = href.replace(/^[^:]+/, (scheme) => scheme.toUpperCase());
88+
const mixedCaseHref = href.replace(/^./, (letter) => letter.toUpperCase());
89+
const text = [
90+
`[Open in app](${href})`,
91+
"[Open][app]",
92+
`[app]: ${uppercaseHref}`,
93+
`<${mixedCaseHref}>`,
94+
`[Uppercase](${uppercaseHref})`,
95+
"[Unsafe](JAVASCRIPT:alert)",
96+
"[Unknown](UNKNOWN://example.com)",
97+
...(parseRawHtml
98+
? [
99+
`<a href="${mixedCaseHref}">HTML app link</a>`,
100+
'<a href="JAVASCRIPT:alert">Unsafe HTML</a>',
101+
]
102+
: []),
103+
].join("\n\n");
87104
try {
88105
await act(async () => {
89106
renderer = create(
90-
<ChatMarkdown
91-
cwd="/workspace/project"
92-
text={`[Open in app](${href})\n\n[Open][app]\n\n[app]: ${href}\n\n<${href}>\n\n[Unsafe](javascript:alert)\n\n[Unknown](unknown://example.com)`}
93-
parseRawHtml={parseRawHtml}
94-
/>,
107+
<ChatMarkdown cwd="/workspace/project" text={text} parseRawHtml={parseRawHtml} />,
95108
);
96109
});
97110

98111
const destinations = renderer!.root.findAllByType("a").map((link) => link.props.href);
99-
expect(destinations.filter(Boolean)).toEqual([href, href, href]);
112+
expect(destinations.filter(Boolean)).toEqual([
113+
href,
114+
parseRawHtml ? href : uppercaseHref,
115+
parseRawHtml ? href : mixedCaseHref,
116+
parseRawHtml ? href : uppercaseHref,
117+
...(parseRawHtml ? [href] : []),
118+
]);
100119
} finally {
101120
await act(async () => {
102121
renderer?.unmount();

‎apps/web/src/components/ChatMarkdown.tsx‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,10 +449,19 @@ function markStandaloneImages(node: MarkdownImageHastNode) {
449449
});
450450
}
451451

452-
/** Carries authored image source metadata through the sanitizer to the image renderer. */
453-
function rehypePreserveImageSourceMeta() {
452+
/** Prepares app link schemes and authored image metadata for the sanitizer. */
453+
function rehypePrepareMarkdownSources() {
454454
return (tree: MarkdownImageHastNode) => {
455455
const visit = (node: MarkdownImageHastNode) => {
456+
const href = node.properties?.href;
457+
if (node.tagName === "a" && typeof href === "string" && isExternalAppLink(href)) {
458+
// The sanitizer matches protocols case-sensitively. Preserve the rest of the URL.
459+
const colon = href.indexOf(":");
460+
node.properties = {
461+
...node.properties,
462+
href: href.slice(0, colon).toLowerCase() + href.slice(colon),
463+
};
464+
}
456465
const src = node.properties?.src;
457466
const title = node.properties?.title;
458467
if (node.type === "element" && node.tagName === "img") {
@@ -522,7 +531,7 @@ const CHAT_MARKDOWN_REMARK_PLUGINS_WITH_BREAKS = [
522531

523532
const CHAT_MARKDOWN_REHYPE_PLUGINS = [
524533
rehypeRaw,
525-
rehypePreserveImageSourceMeta,
534+
rehypePrepareMarkdownSources,
526535
[rehypeSanitize, CHAT_MARKDOWN_SANITIZE_SCHEMA],
527536
] satisfies NonNullable<ReactMarkdownOptions["rehypePlugins"]>;
528537

0 commit comments

Comments
 (0)