Skip to content

Commit

Permalink
Merge pull request #29 from yy0931/master
Browse files Browse the repository at this point in the history
Fix XSS in equation numbers
  • Loading branch information
goessner authored Jul 2, 2021
2 parents 61e637f + 4480d65 commit 035ff22
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions texmath.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
*--------------------------------------------------------------------------------------------*/
'use strict';

function escapeHTML(text) {
return text
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
}

function texmath(md, options) {
const delimiters = options && options.delimiters || 'dollars';
const outerSpace = options && options.outerSpace || false; // inline rules, effectively `dollars` require surrounding spaces, i.e ` $\psi$ `, to be accepted as inline formulas. This is primarily a guard against misinterpreting single `$`'s in normal markdown text (relevant for inline math only. Default: `false`, for backwards compatibility).
Expand All @@ -30,7 +39,7 @@ function texmath(md, options) {

for (const rule of texmath.rules[delimiters].block) {
md.block.ruler.before('fence', rule.name, texmath.block(rule)); // ! important for ```math delimiters
md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$2/,tokens[idx].info) // equation number .. ?
md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$2/,escapeHTML(tokens[idx].info)) // equation number .. ?
.replace(/\$1/,texmath.render(tokens[idx].content,true,katexOptions));
}
}
Expand Down Expand Up @@ -110,11 +119,7 @@ texmath.render = function(tex,displayMode,options) {
res = texmath.katex.renderToString(tex, options);
}
catch(err) {
res = `${tex}:${err.message}`
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
res = escapeHTML(`${tex}:${err.message}`)
}
return res;
}
Expand Down

0 comments on commit 035ff22

Please sign in to comment.