Open platform to collect and prioritize feedback
import * as markdown from "./markdown"
import { fiderAllowedSchemes } from "@fider/hooks"
fiderAllowedSchemes.get = () => "^monero:[48]\n^bitcoin:(1|3|bc1)"
const testCases = [
{
input: "Visit [GitHub](https://github.com) to learn more.",
expectedFull: '<p>Visit <a class="text-link" href="https://github.com" rel="noopener nofollow" target="_blank">GitHub</a> to learn more.</p>',
expectedPlainText: "Visit GitHub to learn more.",
},
{
input: "My Picture ",
expectedFull: '<p>My Picture <img src="http://demo.dev.fider.io:3000/images/100/28" alt=""></p>',
expectedPlainText: "My Picture",
},
{
input: "My Fider Picture ",
expectedFull:
'<p>My Fider Picture <img src="/static/images/attachments/zy0hBtqrjQki7M56p26AuAXljRoaNUSwZO6MOky5gnYm2nW1rsMmrp3dwhjGk7ok-aden.jpeg" alt="" class="fider-inline-image" data-bkey="attachments/zy0hBtqrjQki7M56p26AuAXljRoaNUSwZO6MOky5gnYm2nW1rsMmrp3dwhjGk7ok-aden.jpeg"></p>',
expectedPlainText: "My Fider Picture",
},
{
input: "# Hello World",
expectedFull: "<h1>Hello World</h1>",
expectedSimple: "<p>Hello World</p>",
expectedPlainText: "Hello World",
},
{
input: "Hello <b>Beautiful</b> World",
expectedFull: "<p>Hello <b>Beautiful</b> World</p>",
expectedPlainText: "Hello <b>Beautiful</b> World",
},
{
input: `[Uh oh...]("onerror="alert('XSS'))`,
// The quotes stay inside the (harmless, relative) href instead of breaking out of it
expectedFull: `<p><a class="text-link" href=""onerror="alert('XSS')" rel="noopener nofollow" target="_blank">Uh oh...</a></p>`,
expectedPlainText: "Uh oh...",
},
{
input: "~~Option 3~~",
expectedFull: "<p><del>Option 3</del></p>",
expectedPlainText: "Option 3",
},
{
input: "Check this out: `HEEEY`",
expectedFull: "<p>Check this out: <code>HEEEY</code></p>",
expectedPlainText: "Check this out: HEEEY",
},
{
input: `# Hello World
How are you?`,
expectedFull: `<h1>Hello World</h1>
<p>How are you?</p>`,
expectedPlainText: "Hello World How are you?",
},
{
input: `-123
-456
-789`,
expectedFull: "<p>-123<br>-456<br>-789</p>",
expectedPlainText: "-123 -456 -789",
},
{
input:
"[monero](monero:83zJ2jMbBoxJkhtpaRLk6fQVrvfmGbd8gYUL7FSdLxU91JSpiWXoLUtAMGqmfvfq3qRS5gJUvMY7oLFSx71wxhKRGG6ypMt) [bitcoin](bitcoin:1CgLs6CxXMAY4Pj4edQq5vyaFoP9NdqVKH) [litecoin](litecoin:ltc1qg0elpp0hxguwlsapl68gvklt5ngemj8k8lu0f5)",
expectedFull:
'<p><a class="text-link" href="monero:83zJ2jMbBoxJkhtpaRLk6fQVrvfmGbd8gYUL7FSdLxU91JSpiWXoLUtAMGqmfvfq3qRS5gJUvMY7oLFSx71wxhKRGG6ypMt" rel="noopener nofollow" target="_blank">monero</a> <a class="text-link" href="bitcoin:1CgLs6CxXMAY4Pj4edQq5vyaFoP9NdqVKH" rel="noopener nofollow" target="_blank">bitcoin</a> <a class="text-link" rel="noopener nofollow" target="_blank">litecoin</a></p>',
expectedPlainText: "monero bitcoin litecoin",
},
{
input: "Jane's & Jim's > [Matt](https://example.com)",
expectedFull: '<p>Jane\'s & Jim\'s > <a class="text-link" href="https://example.com" rel="noopener nofollow" target="_blank">Matt</a></p>',
expectedPlainText: "Jane's & Jim's > Matt",
},
]
testCases.forEach((x) => {
test(`Can parse markdown ${x.input} to ${x.expectedFull} (full mode)`, () => {
const result = markdown.full(x.input)
expect(result).toEqual(x.expectedFull)
})
})
testCases.forEach((x) => {
test(`Can convert markdown ${x.input} to ${x.expectedPlainText} (text)`, () => {
const result = markdown.toText(x.input)
expect(result).toEqual(x.expectedPlainText)
})
})
// Parse rendered HTML the way the browser would and list the live elements it creates.
const liveElements = (html: string): Element[] => {
const container = document.createElement("div")
container.innerHTML = html
return Array.from(container.querySelectorAll("*"))
}
describe("XSS prevention", () => {
const xssInputs = [
"<script>alert(1)</script>",
"<img src=x onerror=alert(1)>",
"<svg onload=alert(1)>",
'<a href="javascript:alert(1)">click</a>',
// Entity-encoded markup must stay text, not be decoded back into HTML
"<img src=x onerror=alert(1)>",
"<script>alert(1)</script>",
"`<img src=x onerror=alert(1)>`",
// Code spans/blocks are not escaped by marked 17's lexer; the renderers must escape them
"`<img src=x onerror=alert(1)>`",
"```\n<img src=x onerror=alert(1)>\n```",
]
xssInputs.forEach((input) => {
test(`full mode neutralizes: ${input}`, () => {
const result = markdown.full(input)
// No raw dangerous HTML elements (entity-encoded <script> is safe text)
expect(result).not.toMatch(/<script[\s>]/i)
expect(result).not.toMatch(/<svg[\s>]/i)
expect(result).not.toMatch(/<img[^>]*onerror/i)
expect(result).not.toMatch(/<a[^>]*javascript:/i)
})
test(`plainText mode neutralizes: ${input}`, () => {
const result = markdown.plainText(input)
expect(result).not.toMatch(/<script[\s>]/i)
expect(result).not.toMatch(/<svg[\s>]/i)
expect(result).not.toMatch(/<img[^>]*onerror/i)
expect(result).not.toMatch(/<a[^>]*javascript:/i)
})
})
})
describe("code is rendered as text", () => {
const inputs = ["`<b>`", "`<b>`", "`<img src=x onerror=alert(1)>`", "`<img src=x onerror=alert(1)>`", "```\n<b>bold</b>\n```"]
inputs.forEach((input) => {
test(`full mode creates no markup from code: ${input}`, () => {
const tags = liveElements(markdown.full(input)).map((e) => e.tagName.toLowerCase())
expect(tags.filter((t) => t !== "p" && t !== "code" && t !== "pre")).toEqual([])
})
test(`plainText mode creates no markup from code: ${input}`, () => {
expect(liveElements(markdown.plainText(input))).toEqual([])
})
})
})
describe("plainText returns HTML-safe text", () => {
test("does not decode entities back into markup", () => {
expect(liveElements(markdown.plainText("Hello <b>Beautiful</b> World"))).toEqual([])
expect(liveElements(markdown.plainText("<b>x</b>"))).toEqual([])
})
test("displays special characters as typed", () => {
const container = document.createElement("p")
container.innerHTML = markdown.plainText("Jane's & Jim's > [Matt](https://example.com) <b>hi</b>")
expect(container.textContent).toEqual("Jane's & Jim's > Matt <b>hi</b>")
})
})
describe("attribute values are escaped", () => {
test("fider-image alt text cannot inject attributes", () => {
const [p, img] = liveElements(markdown.full(''))
expect(p.tagName).toEqual("P")
expect(img.tagName).toEqual("IMG")
expect(img.getAttribute("class")).toEqual("fider-inline-image")
expect(img.getAttribute("alt")).toEqual('x" class="evil" data-x="1')
expect(img.hasAttribute("data-x")).toBe(false)
})
test("link title is quoted", () => {
const [, a] = liveElements(markdown.full('[a](http://x.com "hello world")'))
expect(a.getAttribute("title")).toEqual("hello world")
expect(a.hasAttribute("world")).toBe(false)
})
test("link title quotes cannot inject attributes", () => {
const [, a] = liveElements(markdown.full(`[a](http://x.com 'say "hi" style="color:red"')`))
expect(a.getAttribute("title")).toEqual('say "hi" style="color:red"')
expect(a.hasAttribute("style")).toBe(false)
})
test("link href quotes cannot inject attributes", () => {
const [, a] = liveElements(markdown.full('[a](http://x.com/"style="color:red)'))
expect(a.tagName).toEqual("A")
expect(a.hasAttribute("style")).toBe(false)
})
})
describe("fider-image references", () => {
const unsafe = [
"",
"",
"",
"",
]
unsafe.forEach((input) => {
test(`is not rendered as an inline image: ${input}`, () => {
const html = markdown.full(input)
expect(html).not.toContain("/static/images/")
expect(html).not.toContain("data-bkey")
})
})
})
describe("mentions", () => {
test("renders non-empty @[name] as a mention and leaves @[] as text", () => {
const spans = liveElements(markdown.full("@[] and @[Jane Doe]")).filter((e) => e.classList.contains("mention"))
expect(spans.map((e) => e.textContent)).toEqual(["@Jane Doe"])
})
})
describe("text is shown exactly as typed", () => {
// Raw HTML used to be escaped before parsing, so code then escaped it a second time and
// `List<String>` displayed as `List<String>`.
const shown = (html: string) => {
const container = document.createElement("div")
container.innerHTML = html
return container.textContent
}
const cases: [string, string][] = [
["```\n<b>x</b> List<String> a && b\n```", "<b>x</b> List<String> a && b\n"],
["`List<String>` and `a && b`", "List<String> and a && b"],
["`<b>` shows the entity itself", "<b> shows the entity itself"],
["Hello <b>Beautiful</b> World & more", "Hello <b>Beautiful</b> World & more"],
["x < y > z", "x < y > z"],
]
cases.forEach(([input, expected]) => {
test(`full: ${input}`, () => {
expect(shown(markdown.full(input))).toEqual(expected)
})
})
test("plainText and toText keep code as typed", () => {
expect(shown(markdown.plainText("`List<String>` and `a && b`"))).toEqual("List<String> and a && b")
expect(markdown.toText("`List<String>` and `a && b`")).toEqual("List<String> and a && b")
})
})
describe("text that starts with < keeps its block structure", () => {
// "<" must not reach marked's HTML-block rules, which would end paragraphs and list items early
const cases: [string, string][] = [
["- Use a map\n<String, Int> as the key", "<ul>\n<li>Use a map<br><String, Int> as the key</li>\n</ul>"],
["a\n<div>b</div>", "<p>a<br><div>b</div></p>"],
["Steps:\n<p>para</p>", "<p>Steps:<br><p>para</p></p>"],
["- item\n<!-- c -->", "<ul>\n<li>item<br><!-- c --></li>\n</ul>"],
]
cases.forEach(([input, expected]) => {
test(`full: ${JSON.stringify(input)}`, () => {
expect(markdown.full(input)).toEqual(expected)
})
})
test("angle brackets stay visible text, not <...> autolinks", () => {
// (the bare email is still linked by GFM's email autolinking, as before)
expect(markdown.full("<scheme:foo> and <[email protected]>")).toEqual(
'<p><scheme:foo> and <<a class="text-link" href="mailto:[email protected]" rel="noopener nofollow" target="_blank">[email protected]</a>></p>'
)
expect(markdown.toText("Contact <[email protected]> about X")).toEqual("Contact <[email protected]> about X")
})
})