Skip to content

Fix XSS in equation numbers #29

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

Merged
merged 2 commits into from
Jul 2, 2021
Merged
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
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