Security hardening: prevent stored XSS and render crashes
- MarkdownView: add rehype-sanitize between rehype-raw and rehype-katex to strip scripts/event-handlers/javascript: URLs from user-authored markdown (was stored XSS, also affected the public /pg/* route); keep className on code/span/div so KaTeX and syntax highlighting still work. Add rehype-sanitize ^6.0.0 to deps and lockfile. - MarkdownContent / StandaloneMarkdownPage: parse markdown content via parseMarkdownContent() instead of an unguarded JSON.parse, so a single corrupt/legacy record no longer white-screens the whole page. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ import ReactMarkdown from "react-markdown";
|
||||
import remarkMath from "remark-math";
|
||||
import rehypeKatex from "rehype-katex";
|
||||
import rehypeRaw from "rehype-raw";
|
||||
import rehypeSanitize, { defaultSchema } from "rehype-sanitize";
|
||||
import remarkGfm from "remark-gfm";
|
||||
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
|
||||
import { okaidia } from "react-syntax-highlighter/dist/esm/styles/prism";
|
||||
@@ -46,6 +47,21 @@ const ParseTemplate = ({template, variables}) => {
|
||||
};
|
||||
|
||||
|
||||
// Markdown content is authored by users and rendered for everyone
|
||||
// (including the unauthenticated /pg/* route), so raw HTML must be
|
||||
// sanitized to prevent stored XSS. className is kept on code/span/div so
|
||||
// syntax highlighting and KaTeX (which runs after sanitize) still work;
|
||||
// scripts, event handlers and javascript: URLs are stripped.
|
||||
const sanitizeSchema = {
|
||||
...defaultSchema,
|
||||
attributes: {
|
||||
...defaultSchema.attributes,
|
||||
code: [...(defaultSchema.attributes?.code || []), ["className"]],
|
||||
span: [...(defaultSchema.attributes?.span || []), ["className"]],
|
||||
div: [...(defaultSchema.attributes?.div || []), ["className"]],
|
||||
},
|
||||
};
|
||||
|
||||
const MarkdownView = ({ content, template, height="auto" }) => {
|
||||
const {data: links, isLoading} = useLinks();
|
||||
|
||||
@@ -74,7 +90,7 @@ const MarkdownView = ({ content, template, height="auto" }) => {
|
||||
variables: content
|
||||
}) + "\n" + linkDefinitions}
|
||||
remarkPlugins={[remarkMath, remarkGfm]}
|
||||
rehypePlugins={[rehypeKatex, rehypeRaw]}
|
||||
rehypePlugins={[rehypeRaw, [rehypeSanitize, sanitizeSchema], rehypeKatex]}
|
||||
components={{
|
||||
code({ node, inline, className, children, ...props }) {
|
||||
const match = /language-(\w+)/.exec(className || "");
|
||||
|
||||
Reference in New Issue
Block a user