Skip to content

Commit

Permalink
fix: code blocks of unknown languages cause HTML injection (#3711)
Browse files Browse the repository at this point in the history
* fix: code blocks of unknown languages cause HTML injection

A code block of unknown language (that is, a language not treated as special by Memos and not handled by highlight.js) should fall back on rendering its plaintext content. However, the content is never properly escaped before it is appended to the DOM, and thus any string that happens to contain HTML is unsafely rendered. This commit fixes the issue by ensuring that, when none of the previous cases handle the text, any HTML entities are escaped first.

* Update CodeBlock.tsx to conform to eslint
  • Loading branch information
andrigamerita authored Jul 19, 2024
1 parent af95280 commit d264f45
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion web/src/components/MemoContent/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ const CodeBlock: React.FC<Props> = ({ language, content }: Props) => {
// Skip error and use default highlighted code.
}

return content;
// escape any HTML entities when rendering original content
return Object.assign(document.createElement("span"), {
textContent: content,
}).innerHTML;
}, [formatedLanguage, content]);

const handleCopyButtonClick = useCallback(() => {
Expand Down

0 comments on commit d264f45

Please sign in to comment.