Skip to content
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

fix: XSS vulnerability #137

Merged
merged 3 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function selectLanguage(options: Options, lang: string): [string, Grammar | unde
function highlight(markdownit: MarkdownIt, options: Options, text: string, lang: string): string {
const [langToUse, prismLang] = selectLanguage(options, lang)
const code = prismLang ? Prism.highlight(text, prismLang, langToUse) : markdownit.utils.escapeHtml(text)
const classAttribute = langToUse ? ` class="${markdownit.options.langPrefix}${langToUse}"` : ''
const classAttribute = langToUse ? ` class="${markdownit.options.langPrefix}${markdownit.utils.escapeHtml(langToUse)}"` : ''
return `<pre${classAttribute}><code${classAttribute}>${code}</code></pre>`
}

Expand Down
8 changes: 7 additions & 1 deletion test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ describe('markdown-it-prism', () => {
.use(markdownItPrism)
.render(await read('input/indented.md'))
).toEqual(await read('expected/indented.html'))

})

it('adds classes even if the language is unknown', async () => {
Expand All @@ -90,6 +89,13 @@ describe('markdown-it-prism', () => {
).toEqual(await read('expected/fenced-with-unknown-language.html'))
})

it('escapes HTML in the language name', async () => {
expect(markdownit()
.use(markdownItPrism)
.render(await read('input/fenced-with-html-in-language.md'))
).toEqual(await read('expected/fenced-with-html-in-language.html'))
})

it('falls back to defaultLanguageForUnknown if the specified language is unknown', async () => {
expect(markdownit()
.use(markdownItPrism, {
Expand Down
8 changes: 8 additions & 0 deletions testdata/expected/fenced-with-html-in-language.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<h1>Test</h1>
<p>This is a fenced code block:</p>
<pre class="language-&quot;&gt;&lt;img/onerror=&quot;alert('hacked')&quot;src=.&gt;&lt;span\class=&quot;"><code class="language-&quot;&gt;&lt;img/onerror=&quot;alert('hacked')&quot;src=.&gt;&lt;span\class=&quot;">public class Foo() {
public Foo(bar) {
System.out.println(bar);
}
}
</code></pre>
11 changes: 11 additions & 0 deletions testdata/input/fenced-with-html-in-language.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Test

This is a fenced code block:

```"><img/onerror="alert('hacked')"src=.><span\class="
public class Foo() {
public Foo(bar) {
System.out.println(bar);
}
}
```