silentmatt / expr-eval

Mathematical expression evaluator in JavaScript
http://silentmatt.com/javascript-expression-evaluator/
MIT License
1.21k stars 239 forks source link

fix: allow restricting certain member access #270

Closed gka closed 2 years ago

gka commented 2 years ago

This adds a new Parser option restrictMemberAccess which can be used to prevent certain member properties to be accessed throw the . operator.

it('should restrict certain member access', function () {
    var parser = new Parser({ allowMemberAccess: true, restrictMemberAccess: ['b', 'constructor', '__proto__'] });
    assert.throws(function () { parser.evaluate('a.b', { a: { b: 2, c: 3 } }); }, /access to member "b" is not permitted/);
    assert.throws(function () { parser.evaluate('min.__proto__'); }, /access to member "__proto__" is not permitted/);
    assert.throws(function () { parser.evaluate('min.constructor'); }, /access to member "constructor" is not permitted/);
    assert.doesNotThrow(function () { parser.evaluate('a.c', { a: { b: 2, c: 3 } }); });
  });