Skip to content

Commit

Permalink
fix(markdown): apply html sanitizer on markdownToHtml output (#2237)
Browse files Browse the repository at this point in the history
Co-authored-by: ocavue <ocavue@gmail.com>
  • Loading branch information
Pigment-RomainLoisel and ocavue committed Feb 21, 2024
1 parent 68773ba commit 00a41e3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 61 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-yaks-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@remirror/extension-markdown': minor
---

Fix the html sanitizing on HTML output from markdown. Removed the default html sanitizer because it doesn't provide any security guarantees and it's not been called due to a bug in the markdown extension.
12 changes: 3 additions & 9 deletions packages/remirror__extension-markdown/src/markdown-extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { DOMSerializer, Fragment } from '@remirror/pm/model';

import { htmlToMarkdown } from './html-to-markdown';
import { markdownToHtml } from './markdown-to-html';
import { htmlSanitizer } from './markdown-utils';

export interface MarkdownOptions {
/**
Expand All @@ -38,13 +37,8 @@ export interface MarkdownOptions {
markdownToHtml?: Static<(markdown: string, sanitizer?: (html: string) => string) => string>;

/**
* Provide a sanitizer to prevent XSS attacks.
*
* The default sanitizer has **zero** security guarantees so it's recommended
* that you provide your own html sanitizer here.
*
* If you want to sanitize on the backend as well you will need to override
* this method.
* Provide a sanitizer to prevent XSS attacks. Remirror does not provide any
* sanitization by default.
*/
htmlSanitizer?: Static<(html: string) => string>;

Expand Down Expand Up @@ -88,7 +82,7 @@ export interface MarkdownOptions {
defaultOptions: {
htmlToMarkdown,
markdownToHtml,
htmlSanitizer,
htmlSanitizer: undefined,
activeNodes: [ExtensionTag.Code],
copyAsMarkdown: false,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@ marked.use({
* Converts the provided markdown to HTML.
*/
export function markdownToHtml(markdown: string, sanitizer?: (html: string) => string): string {
return marked(markdown, { gfm: true, smartLists: true, xhtml: true, sanitizer });
const html = marked(markdown, { gfm: true, smartLists: true, xhtml: true });
if (sanitizer) {
return sanitizer(html);
}
return html;
}
51 changes: 0 additions & 51 deletions packages/remirror__extension-markdown/src/markdown-utils.ts

This file was deleted.

1 comment on commit 00a41e3

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 Published on https://remirror.io as production
🚀 Deployed on https://65d5cf572ba210856fa08dd6--remirror.netlify.app

Please sign in to comment.