Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Harden Ensu markdown links

Manav Rathieb4112632b9f
diff --git a/web/apps/ensu/src/components/MarkdownRenderer.tsx b/web/apps/ensu/src/components/MarkdownRenderer.tsx
index 29551ca380..3591ef32b3 100644
--- a/web/apps/ensu/src/components/MarkdownRenderer.tsx
+++ b/web/apps/ensu/src/components/MarkdownRenderer.tsx
@@ -115,6 +115,18 @@ const openExternalUrl = async (url: string) => {
}
};
+const safeExternalUrl = (href: string | undefined) => {
+ if (!href) return undefined;
+ try {
+ const url = new URL(href);
+ return ["http:", "https:", "mailto:"].includes(url.protocol)
+ ? url.toString()
+ : undefined;
+ } catch {
+ return undefined;
+ }
+};
+
type AnchorProps = React.ComponentPropsWithoutRef<"a"> & { node?: unknown };
const ExternalLink = ({
@@ -122,18 +134,23 @@ const ExternalLink = ({
href,
children,
...rest
-}: AnchorProps) => (
- <a
- {...rest}
- href={href}
- onClick={(e) => {
- e.preventDefault();
- if (href) void openExternalUrl(href);
- }}
- >
- {children}
- </a>
-);
+}: AnchorProps) => {
+ const safeHref = safeExternalUrl(href);
+ if (!safeHref) return <>{children}</>;
+
+ return (
+ <a
+ {...rest}
+ href={safeHref}
+ onClick={(e) => {
+ e.preventDefault();
+ if (safeHref) void openExternalUrl(safeHref);
+ }}
+ >
+ {children}
+ </a>
+ );
+};
export const MarkdownRenderer = ({
content,
@@ -144,10 +161,7 @@ export const MarkdownRenderer = ({
<ReactMarkdown
remarkPlugins={[remarkGfm, remarkMath]}
rehypePlugins={[
- [
- rehypeKatex,
- { strict: false, throwOnError: false, trust: true },
- ],
+ [rehypeKatex, { strict: false, throwOnError: false }],
]}
components={{ pre: CodeBlock, a: ExternalLink }}
>