Menu

Tree [457483] master 0.0.27 /
 History

HTTPS access


File Date Author Commit
 .github 2024-06-29 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [457483] Remove reliance on globalThis for Worker
 @types 2023-10-05 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [227d60] Refactoring
 src 2024-06-29 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [457483] Remove reliance on globalThis for Worker
 test 2024-06-01 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [656960] Update dependencies
 .dockerignore 2023-10-10 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [3cd70a] Move examples to a different repository
 .eslintignore 2023-05-07 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [b2e73b] first commit
 .eslintrc.cjs 2023-10-10 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [19f6e8] UMD output improvements
 .gitattributes 2023-08-04 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [5d9aae] Add .gitattributes
 .gitignore 2023-05-07 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [b2e73b] first commit
 .gitmodules 2023-10-10 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [3cd70a] Move examples to a different repository
 .npmrc 2023-08-16 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [985dfe] Remove console destructuring and include explic...
 .nycrc 2023-08-13 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [cbc453] Add nyc for coverage, changed syntax checking s...
 .prettierrc.cjs 2023-10-10 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [19f6e8] UMD output improvements
 LICENSE 2023-05-09 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [eb87d6] Refactoring, new approach to Node.js sandboxing
 README.md 2023-09-27 Anders Bech Mellson Anders Bech Mellson [bb6184] Update import in example
 SECURITY 2023-05-07 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [b2e73b] first commit
 closure-externs.js 2023-10-28 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [14b433] Update dependencies; use new CC plugin
 defaultAllowedGlobalProps.config.mjs 2023-05-10 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [d30d93] Improvements
 esbuild.mjs 2024-06-06 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [d21639] Various improvements
 import_map.json 2023-10-05 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [227d60] Refactoring
 loader.mjs 2024-06-01 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [656960] Update dependencies
 package-lock.json 2024-06-29 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [457483] Remove reliance on globalThis for Worker
 package.json 2024-06-29 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [457483] Remove reliance on globalThis for Worker
 test.mjs 2023-10-28 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [b63a7f] Make loader URL
 tsconfig.json 2023-10-28 Ricardo Iván Vieitez Parra Ricardo Iván Vieitez Parra [14b433] Update dependencies; use new CC plugin

Read Me

🏜️ @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.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.