Skip to content

Commit

Permalink
Merge pull request #1633 from hackmdio/bugfix/fix-mermaid-render-xss
Browse files Browse the repository at this point in the history
fix: avoid eval string when putting back parsed string of mermaid
  • Loading branch information
Yukaii authored Dec 25, 2020
2 parents 8e3432a + 568355a commit 25119ad
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions public/js/extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,12 @@ function replaceExtraTags (html) {
return html
}

if (typeof window.mermaid !== 'undefined' && window.mermaid) window.mermaid.startOnLoad = false
if (typeof window.mermaid !== 'undefined' && window.mermaid) {
window.mermaid.startOnLoad = false
window.mermaid.parseError = function (err, hash) {
console.warn(err)
}
}

// dynamic event or object binding here
export function finishView (view) {
Expand Down Expand Up @@ -397,10 +402,14 @@ export function finishView (view) {
var $value = $(value)
const $ele = $(value).closest('pre')

window.mermaid.parse($value.text())
$ele.addClass('mermaid')
$ele.html($value.text())
window.mermaid.init(undefined, $ele)
const text = $value.text()
// validate the syntax first
if (window.mermaid.parse(text)) {
$ele.addClass('mermaid')
$ele.text(text)
// render the diagram
window.mermaid.init(undefined, $ele)
}
} catch (err) {
$value.unwrap()
$value.parent().append(`<div class="alert alert-warning">${escapeHTML(err.str)}</div>`)
Expand Down

0 comments on commit 25119ad

Please sign in to comment.