Skip to content

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 8 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/openneuro-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@
"bytes": "^3.0.0",
"comlink": "^4.0.5",
"date-fns": "^2.16.1",
"dompurify": "^3.0.8",
"draft-js": "^0.11.7",
"email-validator": "^2.0.4",
"graphql": "16.8.1",
"jwt-decode": "^2.2.0",
"markdown-to-jsx": "^7.1.2",
"markdown-to-jsx": "^7.4.0",
"pluralize": "8.0.0",
"prop-types": "^15.6.0",
"react": "^17.0.1",
Expand All @@ -49,6 +50,7 @@
"devDependencies": {
"@testing-library/jest-dom": "6.1.3",
"@testing-library/react": "^11.1.0",
"@types/dompurify": "^3",
"@types/jsdom": "^16",
"@types/node": "18.11.9",
"@types/react": "^17.0.8",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Markdown from "markdown-to-jsx"
import { Markdown } from "../../utils/markdown"
import React from "react"

export interface MetaDataBlockProps {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react"
import Markdown from "markdown-to-jsx"
import { Markdown } from "../utils/markdown"
import Helmet from "react-helmet"
import { Navigate, useLocation } from "react-router-dom"
import pluralize from "pluralize"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState } from "react"
import UpdateDescription from "../mutations/description.jsx"
import { CancelButton } from "./cancel-button"
import { EditButton } from "./edit-button"
import Markdown from "markdown-to-jsx"
import { Markdown } from "../../utils/markdown"

import EditList from "./edit-list.jsx"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react"
import Markdown from "markdown-to-jsx"
import { Markdown } from "../../utils/markdown"
import { ReadMore } from "@openneuro/components/read-more"
import { MetaDataBlock } from "../components/MetaDataBlock"
import Files from "../files/files"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react"
import Markdown from "markdown-to-jsx"
import { Markdown } from "../../utils/markdown"
import { ReadMore } from "@openneuro/components/read-more"
import { MetaDataBlock } from "../components/MetaDataBlock"
import Files from "../files/files"
Expand Down
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>
`;
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()
})
})
161 changes: 161 additions & 0 deletions packages/openneuro-app/src/scripts/utils/markdown.tsx
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>
</>
)
}
Loading