mathjax / MathJax-node

MathJax for Node
Apache License 2.0
615 stars 97 forks source link

sanitize user configuration [was: Cannot read property 'config'] #309

Closed vsevolod-ch closed 7 years ago

vsevolod-ch commented 7 years ago

Hello! I am trying to use this module, with this settings:

mathR.config({
        displayMessages: true,
        displayErrors: true,
        undefinedCharError: true,
        MathJax: {
                jax: ["input/MathML","output/SVG"],
                extensions: ["mml2jax.js"]
        }
});
mathR.start();

and run with:

mathR.typeset({
      math: post.math,
      format: 'MathML',
                svg: true
},function (data) {if (!data.errors) console.log(data.svg);});

but it returns error:

/math-render/node_modules/mathjax-node/lib/main.js:682
  TEX.config.equationNumbers.autoNumber = data.equationNumbers;
     ^

TypeError: Cannot read property 'config' of undefined

What i am doing wrong?

pkra commented 7 years ago

Try removing the MathJax block from the configuration -- the information there is not needed. If that doesn't help, try posting a full example (e.g., on runkit.com).

vsevolod-ch commented 7 years ago

Thanks!! This problem is solved!

pkra commented 7 years ago

Glad to hear it solved your issue.

Re-labeling as bug.

pkra commented 7 years ago

The problem underlying this error is that mathjax-node, by default, loads all input and output components and thus the code base assumes them to be defined -- but the jax part of this user configuration overwrites the defaults, thus disabling the TeX input and causing the error.

One way to fix this would be to delete the jax array from the user input before replacing the default configuration with it, i.e., around https://github.com/mathjax/MathJax-node/blob/master/lib/main.js#L475. Alternatives might be to merge the arrays (to enable custom input/output jaxes -- which do not exist and would need to be integrated into the API more deeply) or to add a lot of catching code throughout.

@dpvc @zorkow any thoughts?

dpvc commented 7 years ago

One way to fix this would be to delete the jax array from the user input

That would resolve the immediate problem, but there are a number of other arrays that could be damaged by improper configuration as well. For example, allowing the extensions array would cause the toMathML.js extension to be lost (affecting MathML output), and allowing the extensions in the TeX block would mean the loss of the AMS math and symbols extensions. Allowing inlineMath and displayMath in the TeX block makes it possible to disable mathjax-node entirely, specifying AuthorInit would override all of the modifications mathjax-node is making, and so on.

So if you want to prevent the user from causing problems, I suspect that the real solution is to sanitize the user's configuration. One possible way would be merge the MathJax configuration into the users configuration rather than the other way, e.g., something like

window.MathJax = Insert(Insert({},MathJaxConfig),window.MathJax);

though in that case, I'd move the current window.MathJax into a local variable, since this sets window.MathJax at this point.

I don't think anything in the current window.MathJax will need to be overridden by the user, with the possible exception of the CommonHTML's undefined font, but since mathjax-node expects that to be monospaced, I'm not sure how effective changing that would be.

So I think that might be a more global solution.