npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@exact-realty/lot

v0.0.19

Published

Sandbox for isolating ECMAScript code

Downloads

8

Readme

🏜️ @exact-realty/lot 🏖️

Reliability Rating Vulnerabilities Bugs Security Rating Maintainability Rating NPM Downloads

Welcome to @exact-realty/lot — the versatile ECMAScript sandbox you've been looking for!

Our sandbox supports multiple runtimes and allows for bidirectional communication, ensuring you have the flexibility and security to run your code in various environments.

🚀 Features

  • Support for multiple runtimes:
    • Browser (using an iframe with a worker inside or just an iframe)
    • Dedicated worker (can run in the browser or with Deno)
    • Node.js
  • Browser isolation using Content Security Policy (CSP)
  • Message passing using the MessageEvent class and event listeners for secure communication using the structured clone algorithm
  • Hardening of global variables, including Function and eval, to prevent direct code execution
  • Bidirectional communication, enabling the parent to call into the sandbox and vice versa

💻 Installation

To install, run:

npm install "@exact-realty/lot"
yarn add "@exact-realty/lot"

📚 Usage

Using our sandbox is easy! First, import the desired sandbox function, then call it with your code and any additional parameters. Here's an example using browserSandbox:

import { browserSandbox } from '@exact-realty/lot';

const sandbox = await browserSandbox(`
  /* sandboxed code*/;
  module.exports={hello:(name)=>\`Hello, ${name}!\`}; 
`);
const result = await sandbox('hello', 'World');
console.log(result); // Output: "Hello, World!"

Our sandbox provides two interfaces:

export interface IPerformTask {
  (op: string, ...args: unknown[]): Promise<unknown>;
}

export interface ISandbox {
  (
    script: string,
    allowedGlobals?: string[] | undefined | null,
    externalMethods?: Record<string, unknown> | null,
    abort?: AbortSignal,
    options?: TSandboxOptions,
  ): Promise<IPerformTask>;
}

export type TSandboxOptions = {
	browserRequireWorker?: boolean;
	workerType?: WorkerOptions['type'];
}

ISandbox is an interface for the browserSandbox, nodejsSandbox and workerSandbox functions. It takes a string script representing the code to be sandboxed, an optional array of allowed global variables allowedGlobals, an optional object of external methods externalMethods, and an optional AbortSignal abort. It returns a promise that resolves to an implementation of IPerformTask.

IPerformTask is an interface for the result of the various sandbox function. It takes a string op representing the function name and a list of arguments, and it returns a promise that resolves to the result of the task.

The script to be sandboxed, script, must expose an object in module.exports with a dictionary of the different functions that can be called from outside. The type of module.exports is Record<string, typeof Function.prototype>.

🤝 Contributing

We welcome any contributions and feedback! Please feel free to submit pull requests, bug reports or feature requests to our GitHub repository.

❗️ Disclaimer

⚠️ Please note that even though we have implemented several security measures, it's important to understand that sandbox escapes are always a possibility. Running untrusted code in Node.js is especially risky due to its inherent platform limitations. Our sandbox relies on node:vm, which was not designed for running untrusted code.

To mitigate these risks, we strongly recommend taking a security-in-depth approach and relying on additional security mechanisms such as process isolation, seccomp(2), pledge(2), ProcessSystemCallDisablePolicy and SELinux, to name a few. Where feasible, we also recommend static code analysis and code reviews, as well as adequate auditing and logging.

Note that the sandbox does not prevent denial-of-service attacks such as infinite loops or memory exhaustion. It's important to take appropriate measures to prevent these types of attacks, such as setting resource limits or using timeouts.

📜 License

This project is released under the ISC license. Check out the LICENSE file for more information.