code-execution-engine

A fast and secure Code-Execution-Engine in Javascript.

Usage no npm install needed!

<script type="module">
  import codeExecutionEngine from 'https://cdn.skypack.dev/code-execution-engine';
</script>

README

Maintenance GitHub license npm GitHub last commit npm

Code-Execution-Engine

A fast and secure Code-Execution-Engine in Javascript.

❗This package is not secure by default. Visit Security for production projects.❗

Table of Contents

Installation

Use the package manager npm to install Code-Execution-Engine.

$ npm install code-execution-engine

Or use the yarn package manager.

$ yarn add code-execution-engine

Usage

const cee = require("code-execution-engine");

cee
  .execute("print('Hello World')", cee.languages.PYTHON3, [], "", {
    timeout: 5,
  })
  .then((result) => {
    console.log(result);
  })
  .catch((error) => {
    console.error(error);
  });

execute(input, language, [args], [stdin], [options]) → Promise<String>

Returns the result (stdout) of the executed code. If stderr is not empty, an exception will be thrown with the content of stderr.

input: string – The source code that should be executed.

language: cee.Language – Pass the language the code is written in, for example, cee.languages.PYTHON3. Supported Lanuages

args: string[] - Command-Line arguments that are passed to the script

stdin: string - Set the stdin for the script

options: IExecuteOptions

      timeout: number - Max execution time of the script. This option doesn't work on windows. Defaults to 5

Supported Languages

Get the supported languages on your platform by calling

cee.getSupportedLanguages();
Linux / MacOS Windows
Python3 ✔️ ✔️
Python2 ✔️ ✔️
Javascript ✔️ ✔️
Ruby ✔️ ✔️
Go ✔️ ✔️
Batch ✔️
Bash ✔️
C ✔️
C++ ✔️
Java ✔️

More supported languages coming soon.

Security

IMPORTANT: There are no security modules available for windows!

const cee = require("code-execution-engine");

const executor = new cee.LXC("[NAME OF YOUR LXC-CONTAINER]");

// Run this function after the installation of the container
executor.init({
  runners: 150,
  // limitations per runner
  maxProcesses: 64,
  maxFiles: 2048,
});

executor
  .execute("echo 'Im in a secure environment!'", cee.languages.BASH)
  .then((result) => {
    console.log(result);
  })
  .catch((error) => {
    console.error(error);
  });

To use LXC, follow the instructions below to set up LXC.

LXC

LXC's are Linux containers, that run the code in a different and secure environment. To use them, you need to install them first. LXC's are only available on Linux-Systems.

To use this package with LXC, you need to install an unprivileged container.

Follow these instructions: linuxcontainers.org

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Credits

Inspired by: engineer-man/piston

License

MIT