forked from waylonflinn/markdown-it-katex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: escape HTML when katex fails to render (waylonflinn#26)
fix XSS vulnerability when katex fails to render
- Loading branch information
1 parent
e027160
commit 182bf27
Showing
4 changed files
with
54 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,24 @@ | ||
|
||
// copy from Markdown-it | ||
const HTML_ESCAPE_TEST_RE = /[&<>"]/ | ||
const HTML_ESCAPE_REPLACE_RE = /[&<>"]/g | ||
|
||
type UnsafeChar = '&' | '<' | '>' | '"' | ||
|
||
const HTML_REPLACEMENTS = { | ||
'&': '&', | ||
'<': '<', | ||
'>': '>', | ||
'"': '"', | ||
} | ||
|
||
function replaceUnsafeChar(ch: string) { | ||
return HTML_REPLACEMENTS[ch as UnsafeChar] | ||
} | ||
|
||
export function escapeHtml(str: string) { | ||
if (HTML_ESCAPE_TEST_RE.test(str)) { | ||
return str.replace(HTML_ESCAPE_REPLACE_RE, replaceUnsafeChar) | ||
} | ||
return str | ||
} |