Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Safest for Untrusted Code: vm, Workers, or Processes?

· 6 min read · Home Tech

In this article
  1. Background: Why Isolation Matters
  2. How Node.js vm Works
  3. How Worker Threads Work
  4. Isolated Processes: The Real Sandbox
  5. Comparing the Three Options
  6. How to Choose the Right Approach
  7. Common Problems and Misconceptions
  8. Costs and Trade-offs
  9. Alternatives to Self-Hosting
  10. Frequently Asked Questions

The safest option for running untrusted code is an isolated process with a restrictive OS-level sandbox (containers, seccomp, or a dedicated user), because it provides the strongest isolation boundary. Node.js vm modules and worker_threads share memory and system resources with the main process, making them insufficient for security against malicious code. This article explains why, how each mechanism works, and how to choose the right level of isolation.

Background: Why Isolation Matters

When you run code you didn’t write—like a user-submitted script, a plugin, or a third-party snippet—you must assume it will try to break out of any sandbox. Even if the code is “just” a small calculation, a determined attacker can exploit bugs in the runtime, exhaust memory, or read sensitive data. The Node.js documentation itself warns that vm is not a security mechanism. The core issue is that all three options (vm, worker threads, processes) run on the same machine and share the same OS kernel. True security requires isolating at the OS level, not just at the JavaScript level.

How Node.js vm Works

The vm module creates a new V8 context—a separate global object and set of built-ins—so the script doesn’t see your application’s globals. You can run code with vm.runInNewContext() or create a vm.Script and run it in a context. This is useful for templates or simple expressions where you trust the code not to be malicious.

However, vm does not restrict access to the host’s resources. The code can:

  • Allocate unlimited memory (causing OOM crashes)
  • Create infinite loops (blocking the event loop)
  • Access the process object if you pass it in (don’t)
  • Exploit V8 or Node.js vulnerabilities to escape the context

In short, vm is a JavaScript sandbox, not a security sandbox. It prevents accidental variable collisions, but not deliberate attacks.

How Worker Threads Work

worker_threads run JavaScript in separate threads, each with its own event loop and V8 isolate. They communicate with the main thread via postMessage() and SharedArrayBuffer if needed. Workers are designed for parallelism, not isolation. They share the same process, so they can:

  • Access the same file system (unless you restrict it)
  • Spend CPU time and starve other threads
  • Use network sockets
  • Read environment variables

A worker can’t directly access the main thread’s variables, but it can still do anything the OS user can do. If your Node.js process runs as root, a malicious worker can read /etc/shadow. Worker threads are not a security boundary—they are a performance feature.

Isolated Processes: The Real Sandbox

Running untrusted code in a separate OS process gives you the strongest isolation available on a single machine. Each process has its own memory space, file descriptors, and environment. You can further restrict it with:

  • Containerization (Docker, Podman) with no network, read-only filesystem, limited CPU/memory
  • OS users (run as a non-privileged user with minimal permissions)
  • Seccomp or AppArmor to block syscalls
  • Resource limits via ulimit or cgroups

Even with these, a process is not completely secure—there are kernel exploits and side-channel attacks—but it is far safer than vm or workers. For truly hostile code, you would use a separate virtual machine or a serverless function service.

Comparing the Three Options

FeaturevmWorker ThreadsIsolated Process
Memory isolationNo (same process)Partial (separate heap, but same process)Yes (separate address space)
CPU isolationNo (event loop shared)Yes (separate thread)Yes (separate process)
Filesystem accessFull (unless restricted)FullCan restrict via OS
Network accessFullFullCan disable
OverheadLowMediumHigh
Security levelNoneLowHigh (with hardening)

How to Choose the Right Approach

Your choice depends on the level of trust you have in the code and the consequences of a breach.

  • Trusted code (your own app logic, a known library): vm is fine for simple expression evaluation, but even then, prefer new Function if you just need a math expression.
  • Semi-trusted code (user scripts that could be buggy but not malicious): worker threads can keep CPU-heavy tasks from blocking the main thread. But don’t give them access to sensitive data.
  • Untrusted code (any code you didn’t write and can’t verify): use an isolated process with minimal privileges. If the code must run many times, consider a pool of pre-forked workers that are recycled after each job.

Common Problems and Misconceptions

Misconception: “vm is safe because it’s a sandbox.” The Node.js docs explicitly say vm is not a security mechanism. Many developers have been burned by assuming it is.

Problem: Resource exhaustion. Even with vm, a script can run while(true){} and freeze your server. Worker threads can do the same, and also spawn thousands of threads. Isolated processes can be killed by the OS if they exceed limits, but you must set those limits.

Problem: Communication overhead. Isolated processes require serialization of data (JSON, etc.) which is slower than shared memory. For high-frequency communication, workers are better, but you trade security.

Problem: Zombie processes. If you spawn a child process and it doesn’t exit, you must handle cleanup. Use child.unref() or a timeout.

Costs and Trade-offs

Isolated processes are more expensive: each process has its own V8 instance, so memory usage can be 10–50 MB per process. Starting a process takes tens of milliseconds. Worker threads are lighter (a few MB) and start faster. vm is almost free. For a web service handling many untrusted requests, you might need a pool of processes, which can be heavy. But the cost of a security breach is usually far higher.

Alternatives to Self-Hosting

If you don’t want to manage process isolation yourself, consider:

  • Serverless functions (AWS Lambda, Cloudflare Workers) that run each invocation in a fresh sandbox.
  • WebAssembly with a strict runtime (like wasmtime) that provides memory isolation and capability-based security, but still not a full OS sandbox.
  • External sandboxing services that run untrusted code in isolated VMs.

Frequently Asked Questions

Is Node.js vm ever safe for untrusted code?

No. The official documentation states it is not intended as a security mechanism. Even with no access to process, there are known escapes and DoS vectors. Use it only for trusted code.

Can worker threads be made secure with workerData restrictions?

No. workerData only limits what data you pass initially. The worker can still require modules, access the filesystem, and use the network. It runs in the same process with the same privileges.

What is the difference between a worker thread and a child process?

A worker thread shares the same process memory space (but has its own heap) and can use SharedArrayBuffer for fast data sharing. A child process has its own memory and communicates via IPC (messages). Child processes are heavier but more isolated.

How do I limit CPU and memory for an isolated process?

Use OS-level controls: ulimit for CPU time and memory, or cgroups for finer control. In Docker, set --cpus and --memory. In Node.js, you can use child_process.spawn with resourceLimits (but that only applies to worker threads, not child processes).

Is running untrusted code in a Docker container completely safe?

No. Containers share the host kernel, so a kernel exploit can escape. For maximum security, use a VM or a serverless service. But containers are much safer than running in the main process.

What about using vm with a timeout?

A timeout can prevent infinite loops, but it doesn’t stop memory exhaustion or other attacks. You can use vm.Script with a timeout option, but it’s not a security control—it’s a convenience.