Secure Exec

repository·main·Indexed 21 days ago

https://github.com/rivet-dev/secure-exec

A lightweight, high-performance code execution engine using V8 isolates to run untrusted code in a secure, deny-by-default environment. It provides Node.js compatibility and supports core modules (fs, child_process, http) and frameworks like Express, Hono, and Next.js without the overhead of containers or VMs. Features include a pluggable filesystem architecture with ObjectFs and ChunkedFs engines, and compatibility shims for AgentOS runtime, sandbox, sidecar, and TypeScript environments.

Tokens
32.6K
Snippets
62
Records
116
Agent score
73%

What's inside secure-exec

  1. Common Use Cases for Secure Exec

    main

    Secure Exec is designed for high-performance, isolated code execution in several scenarios:

    • AI Agent Tool Use: Executing code generated by LLMs safely.
    • User-facing Dev Servers: Running lightweight web frameworks like Express or Hono.
    • MCP Tool-code Execution: Implementing Model Context Protocol tools.
    • Plugin Systems: Creating sandboxed extension points for your applications.
    • Coding Playgrounds: Providing interactive, isolated environments for users to write and run code.
  2. Compare Secure Exec vs Cloudflare Workers

    main

    Secure Exec and Cloudflare Workers both run untrusted JavaScript in V8, but they serve different architectural needs:

    FeatureSecure ExecCloudflare Workers
    Form factorLibrary you embed (secure-exec), runs where your app runsManaged edge platform you deploy to
    Isolation unitPer runtime: each NodeRuntime.create() is its own VM and OS processPer Worker isolate, scheduled by Cloudflare
    Guest runtimeV8 isolate inside a virtualized POSIX kernel (filesystem, processes, sockets, PTYs)V8 isolate with the workerd runtime
    PermissionsDeny-by-default capability policy you configure per runtimePlatform-managed; no per-call capability policy
    SubprocessesReal node:child_process against kernel-managed processesNot available
    FilesystemFull virtualized filesystem per runtimeLimited in-memory node:fs surface, ephemeral
    OperationYou operate it (your process, your machine)Cloudflare operates it

    Choose Cloudflare Workers for managed, globally distributed deployment of your own code. Choose Secure Exec to run untrusted or AI-generated code inside your own application with a hard, self-placed isolation boundary, a real virtualized filesystem, and a controllable capability policy.

  3. What is @secure-exec/sidecar?

    main
    The @secure-exec/sidecar package serves as a compatibility shim for @rivet-dev/agentos-sidecar. It is designed to provide a consistent interface or bridge for sidecar-related functionality within the Secure Exec ecosystem, ensuring compatibility with the AgentOS sidecar implementation.
  4. What is Code Mode (MCP)

    main

    Code Mode is a pattern that allows an AI agent to use a single code-execution tool instead of many individual Model Context Protocol (MCP) tools. Instead of making multiple round-trips to call isolated tools, the LLM writes JavaScript that orchestrates multiple operations (loops, branching, Promise.all) in a single execution. This code is run safely within a Secure Exec V8 sandbox.

    Key Benefits:

    • Reduced Token Overhead: Replacing dozens of tool descriptions with one code-execution tool can reduce tool description tokens by up to 81%.
    • Fewer Round-trips: Multiple tool calls and data transformations are chained in one execution.
    • Real Control Flow: The agent can use standard programming constructs like loops and conditionals.
    • Structured Output: The agent returns a single JSON value via globalThis.__return(), which the host decodes as result.value.
  5. Node.js and Framework Compatibility

    main

    Secure Exec provides a bridged environment that supports most Node.js core modules and web frameworks.

    Supported Node.js Modules

    Most core modules are bridged to real host capabilities rather than being stubbed, including:

    • fs (Filesystem)
    • child_process (Subprocesses)
    • http / net (Networking)
    • dns
    • process
    • os

    Framework Support

    You can run web servers and frameworks out of the box, such as:

    • Express
    • Hono
    • Next.js

    Advanced Orchestration

    • Long-running tasks: For stateful or durable tasks, it is recommended to pair Secure Exec with Rivet Actors to handle persistence and fault tolerance.
    • Production Servers: For production deployments of web servers, pair Secure Exec with Rivet Actors to leverage built-in routing, scaling, and lifecycle management.
  6. Understand Secure Exec networking isolation

    main

    Secure Exec virtualizes all VM networking to ensure guest code cannot access the real host network. The networking model is built on several isolation principles:

    • Kernel Socket Table: All guest networking (including fetch(), node:http, and raw sockets) is routed through a virtualized kernel socket table rather than the host network.
    • Hermetic Loopback: Guests can access loopback services within their own VM, but the socket table is isolated; a guest cannot reach services running on the real host's loopback interface.
    • Default Deny Egress: Outbound networking is denied by default. You must explicitly opt-in using the network permission.
    • Host-to-Guest Proxying: Host loopback ports are invisible to the guest unless you explicitly expose them using loopbackExemptPorts.
  7. Handle guest failures and long-running processes

    main

    Detecting Failures

    A common pattern is to check the exitCode and use stderr to provide diagnostic information when a guest fails:

    const { stderr, exitCode } = await rt.exec(code);
    if (exitCode !== 0) throw new Error(`guest exited ${exitCode}: ${stderr}`);

    Long-running Guests

    For processes that are intended to stay alive (like development servers), use spawn() instead of exec() or run(). spawn() returns a live NodeRuntimeProcess handle which provides:

    • onStdout / onStderr streaming hooks.
    • writeStdin(data) to send data to the guest.
    • kill() to terminate the process.
    • wait() to await the process exit.
  8. Understand Audited Resource Limit Classes

    main

    Secure Exec classifies its resource limit constants into three distinct categories. Understanding these classes helps you distinguish between hard system constraints and configurable security policies:

    • invariant: Hard limits that are fundamental to the system's stability or correctness. These cannot be changed by users or policies (e.g., MAX_SYMLINK_DEPTH, MAX_PATH_LENGTH).
    • policy: Configurable limits that define the security boundary of an execution environment. These are typically used to restrict resource consumption (e.g., DEFAULT_PYTHON_MAX_OLD_SPACE_MB, MAX_WASM_MODULE_FILE_BYTES).
    • policy-deferred: Limits that are part of the policy system but may be evaluated or applied at a different stage of the execution lifecycle (e.g., DEFAULT_PROCESS_TIMEOUT_MS).
  9. Compare ObjectFs and ChunkedFs engines

    main

    The architecture provides two primary filesystem engines depending on your use case:

    ObjectFs (Direct Mapping)

    Maps paths directly to object keys. Best when external tools need to interact with the same bucket layout.

    • Semantics: Lossy POSIX. rename is a copy-and-delete; hard links are unsupported; symlinks are marker objects; partial writes rewrite the entire object.
    • Storage: One object per file under a configured prefix.

    ChunkedFs (Managed POSIX)

    A managed filesystem designed for performance and deduplication.

    • Semantics: Full POSIX support. Small files are stored inline in metadata; larger files are split into content-addressed chunks.
    • Storage: Metadata is authoritative. Blocks are addressed by hash (blake3(content)). Identical chunks are deduplicated automatically.
    • Defaults: Inline threshold is 64 KiB; default chunk size is 4 MiB.