pax_global_header00006660000000000000000000000064136716460070014524gustar00rootroot0000000000000052 comment=1a4d734404aaac14d0611b624696b07dba65c7fd static-eval-2.1.0/000077500000000000000000000000001367164600700137405ustar00rootroot00000000000000static-eval-2.1.0/.npmrc000066400000000000000000000000231367164600700150530ustar00rootroot00000000000000package-lock=false static-eval-2.1.0/.travis.yml000066400000000000000000000003141367164600700160470ustar00rootroot00000000000000language: node_js os: linux dist: bionic before_install: - "nvm install-latest-npm" node_js: - "0.8" - "0.10" - "0.12" - "4" - "6" - "8" - "9" - "10" - "11" - "12" - "13" - "14" static-eval-2.1.0/CHANGELOG.md000066400000000000000000000020021367164600700155430ustar00rootroot00000000000000# static-eval Change Log All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). ## 2.1.0 * Add `allowAccessToMethodsOnFunctions` option to restore 1.x behaviour so that [cwise](https://github.com/scijs/cwise) can upgrade. ([@archmoj](https://github.com/archmoj) in [#31](https://github.com/browserify/static-eval/pull/31)) Do not use this option if you are not sure that you need it, as it had previously been removed for security reasons. There is a known exploit to execute arbitrary code. Only use it on trusted inputs, like the developer's JS files in a build system. ## 2.0.5 * Fix function bodies being invoked during declaration. ([@RoboPhred](https://github.com/RoboPhred) in [#30](https://github.com/browserify/static-eval/pull/30)) ## 2.0.4 * Short-circuit evaluation in `&&` and `||` expressions. ([@RoboPhred](https://github.com/RoboPhred) in [#28](https://github.com/browserify/static-eval/pull/28)) * Start tracking changes. static-eval-2.1.0/LICENSE000066400000000000000000000020571367164600700147510ustar00rootroot00000000000000MIT License Copyright (c) 2013 James Halliday Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. static-eval-2.1.0/example/000077500000000000000000000000001367164600700153735ustar00rootroot00000000000000static-eval-2.1.0/example/eval.js000066400000000000000000000002771367164600700166660ustar00rootroot00000000000000var evaluate = require('static-eval'); var parse = require('esprima').parse; var src = process.argv.slice(2).join(' '); var ast = parse(src).body[0].expression; console.log(evaluate(ast)); static-eval-2.1.0/example/vars.js000066400000000000000000000004231367164600700167030ustar00rootroot00000000000000var evaluate = require('../'); var parse = require('esprima').parse; var src = '[1,2,3+4*10+n,foo(3+5),obj[""+"x"].y]'; var ast = parse(src).body[0].expression; console.log(evaluate(ast, { n: 6, foo: function (x) { return x * 100 }, obj: { x: { y: 555 } } })); static-eval-2.1.0/index.js000066400000000000000000000165111367164600700154110ustar00rootroot00000000000000var unparse = require('escodegen').generate; module.exports = function (ast, vars, opts) { if(!opts) opts = {}; var rejectAccessToMethodsOnFunctions = !opts.allowAccessToMethodsOnFunctions; if (!vars) vars = {}; var FAIL = {}; var result = (function walk (node, noExecute) { if (node.type === 'Literal') { return node.value; } else if (node.type === 'UnaryExpression'){ var val = walk(node.argument, noExecute) if (node.operator === '+') return +val if (node.operator === '-') return -val if (node.operator === '~') return ~val if (node.operator === '!') return !val return FAIL } else if (node.type === 'ArrayExpression') { var xs = []; for (var i = 0, l = node.elements.length; i < l; i++) { var x = walk(node.elements[i], noExecute); if (x === FAIL) return FAIL; xs.push(x); } return xs; } else if (node.type === 'ObjectExpression') { var obj = {}; for (var i = 0; i < node.properties.length; i++) { var prop = node.properties[i]; var value = prop.value === null ? prop.value : walk(prop.value, noExecute) ; if (value === FAIL) return FAIL; obj[prop.key.value || prop.key.name] = value; } return obj; } else if (node.type === 'BinaryExpression' || node.type === 'LogicalExpression') { var op = node.operator; if (op === '&&') { var l = walk(node.left); if (l === FAIL) return FAIL; if (!l) return l; var r = walk(node.right); if (r === FAIL) return FAIL; return r; } else if (op === '||') { var l = walk(node.left); if (l === FAIL) return FAIL; if (l) return l; var r = walk(node.right); if (r === FAIL) return FAIL; return r; } var l = walk(node.left, noExecute); if (l === FAIL) return FAIL; var r = walk(node.right, noExecute); if (r === FAIL) return FAIL; if (op === '==') return l == r; if (op === '===') return l === r; if (op === '!=') return l != r; if (op === '!==') return l !== r; if (op === '+') return l + r; if (op === '-') return l - r; if (op === '*') return l * r; if (op === '/') return l / r; if (op === '%') return l % r; if (op === '<') return l < r; if (op === '<=') return l <= r; if (op === '>') return l > r; if (op === '>=') return l >= r; if (op === '|') return l | r; if (op === '&') return l & r; if (op === '^') return l ^ r; return FAIL; } else if (node.type === 'Identifier') { if ({}.hasOwnProperty.call(vars, node.name)) { return vars[node.name]; } else return FAIL; } else if (node.type === 'ThisExpression') { if ({}.hasOwnProperty.call(vars, 'this')) { return vars['this']; } else return FAIL; } else if (node.type === 'CallExpression') { var callee = walk(node.callee, noExecute); if (callee === FAIL) return FAIL; if (typeof callee !== 'function') return FAIL; var ctx = node.callee.object ? walk(node.callee.object, noExecute) : FAIL; if (ctx === FAIL) ctx = null; var args = []; for (var i = 0, l = node.arguments.length; i < l; i++) { var x = walk(node.arguments[i], noExecute); if (x === FAIL) return FAIL; args.push(x); } if (noExecute) { return undefined; } return callee.apply(ctx, args); } else if (node.type === 'MemberExpression') { var obj = walk(node.object, noExecute); if((obj === FAIL) || ( (typeof obj == 'function') && rejectAccessToMethodsOnFunctions )){ return FAIL; } if (node.property.type === 'Identifier' && !node.computed) { if (isUnsafeProperty(node.property.name)) return FAIL; return obj[node.property.name]; } var prop = walk(node.property, noExecute); if (prop === null || prop === FAIL) return FAIL; if (isUnsafeProperty(prop)) return FAIL; return obj[prop]; } else if (node.type === 'ConditionalExpression') { var val = walk(node.test, noExecute) if (val === FAIL) return FAIL; return val ? walk(node.consequent) : walk(node.alternate, noExecute) } else if (node.type === 'ExpressionStatement') { var val = walk(node.expression, noExecute) if (val === FAIL) return FAIL; return val; } else if (node.type === 'ReturnStatement') { return walk(node.argument, noExecute) } else if (node.type === 'FunctionExpression') { var bodies = node.body.body; // Create a "scope" for our arguments var oldVars = {}; Object.keys(vars).forEach(function(element){ oldVars[element] = vars[element]; }) for(var i=0; i