diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e62e13b8..8fc60a5a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,9 +11,9 @@ You can also check the ## Unreleased - - Fixes - Correctly set CSP headers for index pages + - Harden markdown renderer sanitization - Maintenance - Add dev-container support diff --git a/app/components/markdown.tsx b/app/components/markdown.tsx index c99a39682..7dca5c1fe 100644 --- a/app/components/markdown.tsx +++ b/app/components/markdown.tsx @@ -1,13 +1,44 @@ import clsx from "clsx"; import { ComponentProps } from "react"; import ReactMarkdown from "react-markdown"; -import rehypeRaw from "rehype-raw"; -import rehypeSanitize from "rehype-sanitize"; +import rehypeSanitize, { defaultSchema } from "rehype-sanitize"; import remarkGfm from "remark-gfm"; import classes from "@/components/markdown.module.css"; import { palette } from "@/themes/palette"; +const sanitizeSchema = { + ...defaultSchema, + tagNames: [ + "h1", + "h2", + "h3", + "h4", + "h5", + "h6", + "p", + "strong", + "em", + "ins", + "del", + "s", + "ul", + "ol", + "li", + "blockquote", + "code", + "pre", + "br", + "hr", + "a", + ], + attributes: { + ...defaultSchema.attributes, + a: ["href", "title"], + "*": defaultSchema.attributes?.["*"] ?? [], + }, +}; + const components: ComponentProps["components"] = { h1: ({ children, className, ...props }) => (

@@ -46,10 +77,11 @@ const components: ComponentProps["components"] = { ), a: ({ children, className, style, ...props }) => ( {children} @@ -63,7 +95,7 @@ export const Markdown = ( ); @@ -81,7 +113,11 @@ export const InlineMarkdown = ({ p: ({ children }) => <>{children}, strong: ({ children }) => {children}, em: ({ children }) => {children}, - a: ({ children, href }) => {children}, + a: ({ children, href }) => { + const safeHref = + href && /^(https?|mailto|ircs?):/i.test(href) ? href : undefined; + return {children}; + }, h1: () => null, h2: () => null, h3: () => null, @@ -163,10 +199,11 @@ const componentsInheritFonts: ComponentProps< ), a: ({ children, className, style, ...props }) => ( {children} @@ -180,7 +217,7 @@ export const MarkdownInheritFonts = ( ); diff --git a/app/package.json b/app/package.json index 2aa8f0394..cf046c6d1 100644 --- a/app/package.json +++ b/app/package.json @@ -153,7 +153,6 @@ "react-transition-group": "^4.4.5", "react-virtualized-auto-sizer": "^1.0.25", "react-window": "^1.8.10", - "rehype-raw": "^7.0.0", "rehype-sanitize": "^6.0.0", "remark-gfm": "^4.0.0", "simple-statistics": "^7.6.0",