-
Notifications
You must be signed in to change notification settings - Fork 40
fix(app): Use our own Markdown component that sanitizes HTML following GitHub-like rules #2982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4c189c8
deps(app): Update markdown-to-jsx to latest
nellh bb875a5
fix(app): Display raw HTML in markdown instead of parsing to JSX
nellh e9edd64
Revert "fix(app): Display raw HTML in markdown instead of parsing to …
nellh 0b0ee6a
deps(app): Add DOMPurify for Markdown HTML sanitization.
nellh 7d253bd
fix(app): Use our own Markdown component that sanitizes HTML followin…
nellh 69b87d2
fix(app): Add comments to Markdown component.
nellh e0fa636
tests(app): Add example to Markdown tests
nellh 00e4566
tests(app): Update Markdown test snapshots
nellh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/openneuro-app/src/scripts/dataset/components/MetaDataBlock.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/openneuro-app/src/scripts/dataset/routes/dataset-default.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/openneuro-app/src/scripts/dataset/routes/snapshot-default.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/openneuro-app/src/scripts/utils/__tests__/__snapshots__/markdown.spec.tsx.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
||
exports[`Test <Markdown> component > allows a href with certain protocols 1`] = ` | ||
<DocumentFragment> | ||
<a | ||
href="https://example.com" | ||
> | ||
Example link that should work. | ||
</a> | ||
</DocumentFragment> | ||
`; | ||
|
||
exports[`Test <Markdown> component > does not allow href with unknown protocols 1`] = ` | ||
<DocumentFragment> | ||
<a> | ||
Example link that should work. | ||
</a> | ||
</DocumentFragment> | ||
`; | ||
|
||
exports[`Test <Markdown> component > filters out disallowed tags 1`] = ` | ||
<DocumentFragment> | ||
<ul> | ||
<li> | ||
Markdown document | ||
</li> | ||
</ul> | ||
</DocumentFragment> | ||
`; | ||
|
||
exports[`Test <Markdown> component > safely handles broken HTML tags 1`] = ` | ||
<DocumentFragment> | ||
<ul> | ||
<li> | ||
Markdown document | ||
|
||
<br /> | ||
<br /> | ||
<ul> | ||
<li> | ||
test content | ||
</li> | ||
</ul> | ||
</li> | ||
</ul> | ||
</DocumentFragment> | ||
`; | ||
29 changes: 29 additions & 0 deletions
29
packages/openneuro-app/src/scripts/utils/__tests__/markdown.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from "react" | ||
import { render } from "@testing-library/react" | ||
import { Markdown } from "../markdown" | ||
|
||
describe("Test <Markdown> component", () => { | ||
it("safely handles broken HTML tags", () => { | ||
const brokenTagInput = "* Markdown document\n<br><br>\n * test content" | ||
const { asFragment } = render(<Markdown>{brokenTagInput}</Markdown>) | ||
expect(asFragment()).toMatchSnapshot() | ||
}) | ||
it("filters out disallowed tags", () => { | ||
const badTags = | ||
'* Markdown document\n<script type="text/javascript">alert("this should not happen")</script>\n' | ||
const { asFragment } = render(<Markdown>{badTags}</Markdown>) | ||
expect(asFragment()).toMatchSnapshot() | ||
}) | ||
it("allows a href with certain protocols", () => { | ||
const hrefExample = | ||
'<a href="https://example.com">Example link that should work.</a>' | ||
const { asFragment } = render(<Markdown>{hrefExample}</Markdown>) | ||
expect(asFragment()).toMatchSnapshot() | ||
}) | ||
it("does not allow href with unknown protocols", () => { | ||
const hrefExample = | ||
'<a href="about:memory">Example link that should not work.</a>' | ||
const { asFragment } = render(<Markdown>{hrefExample}</Markdown>) | ||
expect(asFragment()).toMatchSnapshot() | ||
}) | ||
}) | ||
nellh marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
import React from "react" | ||
import MarkdownToJsx from "markdown-to-jsx" | ||
import DOMPurify from "dompurify" | ||
|
||
interface MarkdownProps { | ||
children: string | ||
} | ||
|
||
const ALLOWED_TAGS = [ | ||
"h1", | ||
"h2", | ||
"h3", | ||
"h4", | ||
"h5", | ||
"h6", | ||
"br", | ||
"b", | ||
"i", | ||
"strong", | ||
"em", | ||
"a", | ||
"pre", | ||
"code", | ||
"img", | ||
"tt", | ||
"div", | ||
"ins", | ||
"del", | ||
"sup", | ||
"sub", | ||
"p", | ||
"picture", | ||
"ol", | ||
"ul", | ||
"table", | ||
"thead", | ||
"tbody", | ||
"tfoot", | ||
"blockquote", | ||
"dl", | ||
"dt", | ||
"dd", | ||
"kbd", | ||
"q", | ||
"samp", | ||
"var", | ||
"hr", | ||
"ruby", | ||
"rt", | ||
"rp", | ||
"li", | ||
"tr", | ||
"td", | ||
"th", | ||
"s", | ||
"strike", | ||
"summary", | ||
"details", | ||
"caption", | ||
"figure", | ||
"figcaption", | ||
"abbr", | ||
"bdo", | ||
"cite", | ||
"dfn", | ||
"mark", | ||
"small", | ||
"source", | ||
"span", | ||
"time", | ||
"wbr", | ||
] | ||
|
||
const ALLOWED_ATTR = [ | ||
"abbr", | ||
"accept", | ||
"accept-charset", | ||
"accesskey", | ||
"action", | ||
"align", | ||
"alt", | ||
"aria-describedby", | ||
"aria-hidden", | ||
"aria-label", | ||
"aria-labelledby", | ||
"axis", | ||
"border", | ||
"char", | ||
"charoff", | ||
"charset", | ||
"checked", | ||
"clear", | ||
"cols", | ||
"colspan", | ||
"compact", | ||
"coords", | ||
"datetime", | ||
"dir", | ||
"disabled", | ||
"enctype", | ||
"for", | ||
"frame", | ||
"headers", | ||
"height", | ||
"hreflang", | ||
"hspace", | ||
"id", | ||
"ismap", | ||
"label", | ||
"lang", | ||
"maxlength", | ||
"media", | ||
"method", | ||
"multiple", | ||
"name", | ||
"nohref", | ||
"noshade", | ||
"nowrap", | ||
"open", | ||
"progress", | ||
"prompt", | ||
"readonly", | ||
"rel", | ||
"rev", | ||
"role", | ||
"rows", | ||
"rowspan", | ||
"rules", | ||
"scope", | ||
"selected", | ||
"shape", | ||
"size", | ||
"span", | ||
"start", | ||
"summary", | ||
"tabindex", | ||
"title", | ||
"type", | ||
"usemap", | ||
"valign", | ||
"value", | ||
"width", | ||
"itemprop", | ||
"href", | ||
"cite", | ||
"src", | ||
"longdesc", | ||
] | ||
|
||
export function Markdown({ children }: MarkdownProps) { | ||
const sanitizedMarkdown = DOMPurify.sanitize(children, { | ||
ALLOWED_TAGS, | ||
ALLOWED_ATTR, | ||
ALLOW_ARIA_ATTR: false, | ||
}) | ||
return ( | ||
<> | ||
<MarkdownToJsx>{sanitizedMarkdown}</MarkdownToJsx> | ||
</> | ||
) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.