Skip to content

armgit / memos

Public Imported from GitHub
0

Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.

fix(web): render tel: and sms: markdown links (#6341)

Johnny289ca1ec3d1d
diff --git a/web/src/components/MemoContent/MemoMarkdownRenderer.tsx b/web/src/components/MemoContent/MemoMarkdownRenderer.tsx
index 44486c742..9d386b525 100644
--- a/web/src/components/MemoContent/MemoMarkdownRenderer.tsx
+++ b/web/src/components/MemoContent/MemoMarkdownRenderer.tsx
@@ -8,6 +8,7 @@ import type { Attachment } from "@/types/proto/api/v1/attachment_service_pb";
import { lazyWithReload } from "@/utils/lazy";
import { resolveManagedAttachmentImageSource } from "@/utils/managed-attachment";
import { CodeBlock } from "./CodeBlock";
+import { memoUrlTransform } from "./constants";
import { MarkdownRenderContext, rootMarkdownRenderContext } from "./MarkdownRenderContext";
import { Mention } from "./Mention";
import { AnchorLink, Blockquote, Heading, HorizontalRule, Image, InlineCode, Link, List, ListItem, Paragraph } from "./markdown";
@@ -159,6 +160,7 @@ export const MemoMarkdownRendererCore = ({
<ReactMarkdown
remarkPlugins={buildRemarkPlugins(mathRemarkPlugins)}
rehypePlugins={buildRehypePlugins(mathRehypePlugins)}
+ urlTransform={memoUrlTransform}
components={markdownComponents}
>
{content}
diff --git a/web/src/components/MemoContent/constants.ts b/web/src/components/MemoContent/constants.ts
index faec6a24f..27620d0e3 100644
--- a/web/src/components/MemoContent/constants.ts
+++ b/web/src/components/MemoContent/constants.ts
@@ -1,3 +1,4 @@
+import { defaultUrlTransform } from "react-markdown";
import { defaultSchema } from "rehype-sanitize";
// Class names added by remark-gfm for task lists
@@ -21,6 +22,12 @@ const KATEX_BLOCK_CLASS_NAMES = ["language-math", "math-display"] as const;
const SPAN_CLASS_NAMES = ["mention", "tag"] as const;
const INPUT_ATTRIBUTES = [...(defaultSchema.attributes?.input || []), ["checked", true]] as const;
+/**
+ * Link schemes the browser hands off to another app (dialer, messaging) instead of navigating.
+ * They cannot run script, so they are as safe as the `mailto:` the default schema already allows.
+ */
+const HANDOFF_LINK_PROTOCOLS = ["tel", "sms"];
+
export const isTrustedIframeSrc = (src: string): boolean => TRUSTED_IFRAME_SRC_PATTERNS.some((pattern) => pattern.test(src));
/**
@@ -62,6 +69,21 @@ export const SANITIZE_SCHEMA = {
tagNames: [...(defaultSchema.tagNames || []).filter((tag) => tag !== "picture" && tag !== "source"), "iframe"],
protocols: {
...defaultSchema.protocols,
+ href: [...(defaultSchema.protocols?.href || []), ...HANDOFF_LINK_PROTOCOLS],
src: ["https"],
},
};
+
+/**
+ * react-markdown applies its own `href` allowlist (`defaultUrlTransform`) before the sanitizer
+ * runs, and it does not know about the handoff schemes above. Let those through unchanged and
+ * defer everything else to the default, so `javascript:` and friends are still dropped.
+ */
+export const memoUrlTransform = (url: string): string => {
+ // Exact-case match, mirroring how the sanitizer compares schemes.
+ const colon = url.indexOf(":");
+ if (colon > 0 && HANDOFF_LINK_PROTOCOLS.includes(url.slice(0, colon))) {
+ return url;
+ }
+ return defaultUrlTransform(url);
+};
diff --git a/web/src/components/MemoContent/markdown/Paragraph.tsx b/web/src/components/MemoContent/markdown/Paragraph.tsx
index f06432346..497fae645 100644
--- a/web/src/components/MemoContent/markdown/Paragraph.tsx
+++ b/web/src/components/MemoContent/markdown/Paragraph.tsx
@@ -35,7 +35,8 @@ export function getSingleLinkHref(node?: Element): string | undefined {
}
const href = onlyChild.properties?.href;
- if (typeof href !== "string") {
+ // Only web links have metadata to preview; `tel:`/`sms:`/`mailto:` hand off to another app.
+ if (typeof href !== "string" || !/^https?:\/\//i.test(href)) {
return undefined;
}
diff --git a/web/tests/memo-content-paragraph.test.tsx b/web/tests/memo-content-paragraph.test.tsx
index 52e4c6fff..b273eea6a 100644
--- a/web/tests/memo-content-paragraph.test.tsx
+++ b/web/tests/memo-content-paragraph.test.tsx
@@ -2,6 +2,7 @@ import { renderToStaticMarkup } from "react-dom/server";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import { describe, expect, it } from "vitest";
+import { memoUrlTransform } from "@/components/MemoContent/constants";
import { getSingleLinkHref } from "@/components/MemoContent/markdown/Paragraph";
const collectSingleLinkHrefs = (content: string): Array<string | undefined> => {
@@ -10,6 +11,8 @@ const collectSingleLinkHrefs = (content: string): Array<string | undefined> => {
renderToStaticMarkup(
<ReactMarkdown
remarkPlugins={[remarkGfm]}
+ // Match production so handoff schemes reach the helper instead of being stripped upstream.
+ urlTransform={memoUrlTransform}
components={{
p: ({ children, node }) => {
hrefs.push(getSingleLinkHref(node));
@@ -31,4 +34,10 @@ describe("memo content paragraph links", () => {
undefined,
]);
});
+
+ it("does not offer previews for handoff links", () => {
+ expect(
+ collectSingleLinkHrefs("<tel:+440000000000>\n\n<sms:+440000000000>\n\n<mailto:[email protected]>\n\n<https://example.com>"),
+ ).toEqual([undefined, undefined, undefined, "https://example.com"]);
+ });
});
diff --git a/web/tests/memo-content-security.test.tsx b/web/tests/memo-content-security.test.tsx
index cc108165f..4d970db92 100644
--- a/web/tests/memo-content-security.test.tsx
+++ b/web/tests/memo-content-security.test.tsx
@@ -6,7 +6,7 @@ import rehypeRaw from "rehype-raw";
import rehypeSanitize from "rehype-sanitize";
import remarkGfm from "remark-gfm";
import { describe, expect, it } from "vitest";
-import { isTrustedIframeSrc, SANITIZE_SCHEMA } from "@/components/MemoContent/constants";
+import { isTrustedIframeSrc, memoUrlTransform, SANITIZE_SCHEMA } from "@/components/MemoContent/constants";
import { remarkCurrencySafeMath } from "@/utils/remark-plugins/remark-currency-safe-math";
type IframeProps = React.ComponentProps<"iframe">;
@@ -31,7 +31,7 @@ const renderMemoContent = (content: string): string =>
const renderGfmContent = (content: string): string =>
renderToStaticMarkup(
- <ReactMarkdown remarkPlugins={[remarkGfm]} rehypePlugins={[[rehypeSanitize, SANITIZE_SCHEMA]]}>
+ <ReactMarkdown remarkPlugins={[remarkGfm]} rehypePlugins={[[rehypeSanitize, SANITIZE_SCHEMA]]} urlTransform={memoUrlTransform}>
{content}
</ReactMarkdown>,
);
@@ -52,6 +52,22 @@ describe("memo content sanitization", () => {
expect(html).toMatch(/class="katex-html"/);
});
+ it("keeps tel: and sms: link targets", () => {
+ const html = renderGfmContent("[phone me](tel:+440000000000) [text me](sms:+440000000000?body=hi)");
+
+ expect(html).toContain('href="tel:+440000000000"');
+ expect(html).toContain('href="sms:+440000000000?body=hi"');
+ });
+
+ it("still strips script-capable link targets", () => {
+ const html = renderGfmContent("[x](javascript:alert(1)) [y](data:text/html,hi) [z](vbscript:msgbox)");
+
+ expect(html).not.toMatch(/javascript:/);
+ expect(html).not.toMatch(/data:/);
+ expect(html).not.toMatch(/vbscript:/);
+ expect(html).toMatch(/<a>x<\/a>/);
+ });
+
it("preserves checked state for GFM task list items", () => {
const html = renderGfmContent("- [x] Done\n- [ ] Todo");
const inputs = html.match(/<input[^>]+\/>/g) ?? [];