Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
A JavaScript interpreter written in JavaScript.
You might be working in a JavaScript environment where eval()
isn't
allowed (and you have a genuinely good reason why you want to use it).
Maybe this'll slip under the radar. You could also extend this to make
it execute ES6 code in an ES5 environment. PRs welcome!
Most of the heavy lifting is done by acorn, a JavaScript parser written in JavaScript. eval.js converts the AST it generates into JavaScript function closures, which when run execute the whole program.
It's also possible to use eval.js with esprima.
This npm package comes with a REPL which allows you to experiment with it. It's easy to install and use:
marten@procyon:~/git/evaljs$ npm install -g evaljs
marten@procyon:~/git/evaljs$ evaljs
> 1 + 1
2
> new Error('Hello World!')
[Error: Hello World!]
> throw new Error('Hello World!')
Error: Hello World!
at newWithArgs (/home/marten/git/evaljs/index.js:255:10)
at /home/marten/git/evaljs/index.js:249:12
at Array.0 (/home/marten/git/evaljs/index.js:581:11)
at /home/marten/git/evaljs/index.js:466:31
at REPLServer.repl.start.eval (/home/marten/git/evaljs/bin/evaljs:12:34)
at repl.js:249:20
at REPLServer.repl.start.eval (/home/marten/git/evaljs/bin/evaljs:14:7)
at Interface.<anonymous> (repl.js:239:12)
at Interface.EventEmitter.emit (events.js:95:17)
at Interface._onLine (readline.js:202:10)
> marten@procyon:~/git/evaljs$
evaljs.evaluate(code)
A drop in alternative for window.eval()
.
new evaljs.Environment([scopesOrGlobalObject])
Generates a new JS Environment to 'run' code in. The argument can be
one of the following:
require()
,
exports
, and module
.)A JS Environment has the following properties:
env.gen(node)
: Takes either the result of acorn's parse()
method (an AST), or a JS string containing source code. This
AST/code will be converted into a function that, when run, executes
the AST/code passed in and returns the result.env.DEBUG
: When set to true
, evaljs will write debug
information to stdout.15.4kB min+gzip
ISC
No labeled statements; no nice error handling (although there is a
DEBUG
option). There are probably bugs. That said, it can run itself
including acorn without modifications, so its supported subset of JS is
usable. PRs containing improvements welcome!
Not sure. I only tested with small snippets so far in Node.js, for which the speed difference isn't notable. But it's probably slow.
eval.js is written by Marten de Vries. Credits for the original idea go to closure-interpreter.
FAQs
A JavaScript interpreter written in JavaScript
The npm package evaljs receives a total of 15 weekly downloads. As such, evaljs popularity was classified as not popular.
We found that evaljs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.