Show HN: SafeScript, a safe version of JavaScript for executing untrusted code g...
Show HN @show
Show HN: SafeScript, a safe version of JavaScript for executing untrusted code github.com · 4 points · 0 comments · HN thread
TL;DR SafeScript is a Show HN project: a strict subset of JavaScript meant for running untrusted code, which compiles to plain JS and PHP. It restricts the language (no eval, new, globals or prototype access), injects curated frozen safe globals, adds runtime property guards, timeouts and operation limits, and uses a file header to declare capabilities. The project is MIT-licensed and the story has 4 points and no comments yet.
- Strict JavaScript subset with no eval, new, delete, class, import, async/await, template literals, or access to globalThis, window or document; the output AST is rebuilt from scratch, so the original source text never appears in the output.
- Bracket access is rewritten to runtime guards that block constructor, proto, prototype, caller and callee, even for computed keys.
- Compile-time __check() calls enforce a timeout and an operation limit, with hardcoded ceilings of 60 seconds and 100 million ops that cannot be raised.
- Host hooks (beforeCall/afterCall) can block, transform, rate-limit or short-circuit calls to injected capabilities.
- A .safe file declares its capabilities in a header (@needs, @uses, @type, @env) and never imports or requires anything; the host supplies the implementations.
- Math.random is deterministic by default (mulberry32 with seed 0), so runs are reproducible; true randomness requires explicit host approval. The compiler is about 48 KB minified (about 12 KB gzipped).
Discussion The story has 4 points and no comments, so there is no discussion to summarize yet.