SELECT 1
Entry 031Tue 2026.11.10
One launch a day. Picked, run, explained.

Tue 2026.11.10

SafeScript: JavaScript for user code

Today's pick: SafeScript

SafeScript is a product that runs users’ code on a server or in a browser and limits what that code can reach. It is a language that accepts only part of JavaScript.

It is today’s pick because it rejects dangerous syntax in user code before the code runs.

exit 0Ran itIt works. It blocked every attempt I made to reach objects the host did not pass in.
Found on
Show HN
Built with
JavaScript

The problem

Services sometimes accept code that users wrote, such as pricing rules, validation rules, spreadsheet formulas and plugins. If you run that code with eval or new Function, the code can reach the server’s files and environment variables.

Node.js has the vm module, which runs code in a separate context. But the Node.js docs say the vm module is not a security mechanism and tell you not to use it to run untrusted code. So code like this usually runs in an isolated environment, such as a separate process or a container.

How it solves it

SafeScript was posted on Hacker News as a Show HN on November 9, 2026. In the README, the maker, Gregory Magarshak, compares it to JSON. JSON became a safe format for exchanging data, and SafeScript aims to be one for exchanging code to run.

SafeScript rejects eval, new, prototype access and global objects such as globalThis and process at compile time. Safe built-in objects such as Math and JSON are provided by default. The compiler turns the source into an ASTAbstract syntax tree. A tree that shows the structure of source code or text such as Markdown. Compilers and analysis tools build it to read code. and rebuilds the code from allowed nodes only. The output is plain JavaScript and PHP code. A script can use only the objects that the program calling run() passes in. I call that program the host. The compiler also inserts check code at the start of every function and loop. This code checks the running time and the number of operations.

What is different

The first difference is when it blocks code. Node.js’s vm runs JavaScript as it is and only gives it a separate set of global objects. SafeScript checks the syntax before anything runs. Names such as constructor and arguments can reach objects the host did not pass in. Code that uses them fails to compile. The tradeoff is less syntax. You cannot use new, class, template literals, or async and await.

The second difference is how it stops a script. According to the Node.js docs, vm’s timeout terminates execution once the time runs out. In SafeScript, the check code at the start of each function and loop reads the clock while the script runs. It throws an error when time is up. The compiler also makes every function async and adds await to every call. So a script reads like synchronous code even when a function from the host returns a Promise. The tradeoff is that functions passed to built-in methods such as sort and map do not work correctly. Those methods do not wait for the Promise that the function returns, so the results come out wrong. Errors that the check code throws inside those functions bypass the script and reach the host.

How it was built

  • The repository has 4 commits, made between October 23 and 25, 2026. No commit has a Co-authored-by trailer.
  • The compiler and runtime are 3,226 lines of JavaScript in the src folder.
  • The CHANGELOG for 1.1.0 says test-battle.js and test-compiler.js were merged into test.js and removed. Both files are still in the repository, and npm test runs them.

Running it

I cloned the SafeScript 1.2.0 repository and ran its tests on Node.js 22.22.0. Then I wrote 20 scripts and ran them through run().

  1. Install and tests. npm ci --ignore-scripts finished in 2.1 seconds. All 155 tests in test.js passed. npm test stopped at test-compiler.js, where 4 tests failed. Those 4 tests expect global objects such as Object to be undefined. According to the README, these objects are provided by default.
  2. Escape attempts. The compiler rejected code that tried to reach Function through constructor, through arguments, or through \u0063onstructor written with a Unicode escape. When JSON.parse read a JSON string with a proto key, the key was gone from the result. But run() threw these compile errors right away instead of returning them as a rejected Promise.
  3. Infinite loops and infinite recursion. With a 2-second timeout, while (true) stopped at 2,007 ms. Infinite recursion ended after 23 ms with a stack overflow error. A loop that pushed 20,000,000 items into an array also stopped at 2 seconds. By then, memory use had reached 323 MB. There is no option for a memory limit.
  4. Functions passed to built-in methods. Sorting [3, 1, 2] with a compare function left the order unchanged. [1, 2, 3].map(function (x) { return x * 2; }) returned 3 Promises instead of [2, 4, 6]. A reduce call that adds up [1, 2, 3] returned "[object Promise]3" instead of 6.
  5. An infinite loop inside a compare function. I put while (true) inside the sort compare function. The sort did not wait for the Promise from the compare function. It finished, and run() returned 'done' from the next line. Right after that, an uncaught timeout error ended the Node.js process with exit code 1. Then I put a loop that runs 50 times inside the compare function and sorted 2,000,000 items with a 2-second timeout. The sort did not stop at 2 seconds. It finished after 10.4 seconds. Memory reached 1,106 MB, and then the same timeout error ended the process.
  6. Speed. Over 1,000 runs of return x + y, run() averaged 0.10 ms, including compilation. The same 1,000 runs through new Function averaged 0.0013 ms.
$ node one.js app t19-crash.safe '{"timeout":1000}'
RESULT "done" ms 1006 maxRssMB 51
/tmp/run/app/src/runtime.js:649
        throw new Error('SafeScript: execution timed out after ' + timeout + 'ms');
              ^

Error: SafeScript: execution timed out after 1000ms

Verdict

It works. It blocked every attempt I made to reach objects the host did not pass in. But in 1.2.0, functions passed to sort or map return Promises, so the results are wrong. In my test, the host’s Node.js process exited when one of them hit the time limit. It is worth trying if you run short formulas or validation rules that users wrote.

What to take away

  • If you build a compiler that turns functions into async ones, also test the callbacks that built-in methods call. Today sort and map used the Promise from an async function as a value.
  • If you run untrusted code in Node.js, register an unhandledRejection handler in the host process. By default, Node.js ends the process when no code handles a rejected Promise. With the handler registered, it calls the handler instead.

Getting started

  1. git clone https://github.com/SafeScript/safescript
  2. npm ci --ignore-scripts
  3. node test.js
You need
Node.js. No account or key is needed.
Cost
Free under the MIT license.

Methods of built-in types such as RegExp work only after you register them with SafeScript.lib().

Steps select 1 ran.

Progress

  1. Day 3Active
  2. Day 7Quiet
  3. Day 15Active
  4. Day 30Upcoming
  5. Day 90Upcoming
  6. Day 180Upcoming
  7. Day 365Upcoming

Latest check Day 15, Tue 2026.11.24

Commits since launch
0 → 6
Stars
22 → 28
Latest release
v2.1.0
Last commit
2026.11.18
  • Added a new runtime (v2.0.0). It records a script’s changes separately and applies them only when you commit.
  • Added a debug mode, SafeScript.debug(), that lets you step through the run. An outside tool such as an IDE can control the run (v2.0.0).
  • The `new` operator is now allowed for whitelisted constructors such as Map, Set and Date (v2.0.0).
  • In debug mode, the session used to stop with a ReferenceError when the debugger listed a variable that was not initialized yet. That variable is now left out of the list (v2.1.0).

Full history →