Replies: 2 comments 3 replies
-
When allowing custom user input with calculations like you can do with mathjs, you can indeed easily crash a server. There is written something about security aspects here: https://mathjs.org/docs/expressions/security.html |
Beta Was this translation helpful? Give feedback.
-
Time for shameless self-promotion! I am the maintainter of filtrex, a minimalistic user expression parser with emphasis on security. It allows you to add whatever functions you like to the environment, therefore you could have something like: import { compileExpression } from 'filtrex'
import * as math from 'mathjs'
const options = { extraFunctions: math };
function executeUserExpression(expr) {
const fn = compileExpression(expr, options);
return fn();
}
executeUserExpression( `norm(subtract([1,2], [5,-8]))` )
// 10.77 In a real-life code, you'd want to cherry-pick the methods from Compared to mathjs's |
Beta Was this translation helpful? Give feedback.
-
I'm using mathjs for my discord bot, and one of the bot lists denied the bot due to mathjs not being secure for user input. They didn't provide any other information. I then moved to use the public API instead.
Now I'm thinking about moving back into mathjs, so I could make use of contexts and similar. What do I need to do to make sure the user input is safe to evaluate? The only thing I can think of is to prevent overloading. I did something similar before, so something like just creating a separate worker with limited access to system resources and with execution timeout should be enough.
I'm evaluating user input on server (node). If the input just crashes the process, it's fine, but it can't access anything outside of it's scope (so it can't crash the whole system, access files or similar).
Beta Was this translation helpful? Give feedback.
All reactions