Code Monkey home page Code Monkey logo

run-user-code-saas's Introduction

API For Code Execution

MIT License GitHub Workflow Status

This is an API for running code in different languages, such as Python, Javascript and Ruby. It uses Docker to run the code in a safe environment, avoiding vulnerabilities and security breaches.

Table of Contents

Getting Started

Pre requisites

Make sure you have Docker e Node.js Installed.

Installation

In the project folder, install all dependencies:

  npm install

Pull the docker images, make sure your docker cli works without sudo:

  npm run pull-images

Start the server in development mode, every change in the code will restart the server:

  npm run dev

Run with docker

In the project folder, install all dependencies:

  npm install

Pull the docker images, make sure your docker CLI works without sudo:

  npm run pull-images

Create a docker image:

  docker build -t code-exec .

Run the docker image:

  docker run -p 3000:3000 -v /var/run/docker.sock:/var/run/docker.sock --name code-exec code-exec

Run in production

Follow the installation steps, then run the following command instead of npm run dev:

  npm start

Testing

To run the tests, run the following command:

  npm test

Make sure you have installed all dependencies before running the tests.

API Reference

Returns python code output

  POST /run/python
Paramter Type Description
code string Mandatory.
input string Optional. stdin separated by \n

Returns javacript code output

  POST /run/javascript
Paramter Type Description
code string Mandatory.

Returns ruby code output

  POST /run/ruby
Paramter Type Description
code string Mandatory.
input string Optional. stdin separated by \n

Why Docker? What could go wrong?

With the intention of running the user's code safely, it is understood that the user will not be able to have any interaction with the data and files on the server. In this sense, to avoid a vulnerability such as:

import os
os.system('shutdown -f') # shutdown the server

or

while (true) {} // infinite loop

or

File.delete('important_file.txt') # delete a file

With each user request, a container is created (similar to a virtual machine) which closes at the end of the program execution or after 3 seconds, avoiding infinite loops.

Other possible vulnerabilities such as file installation or container exits are escaped by limiting RAM memory and processing. Also, It removes privileges, network and disk writes (even within the container). In this way, a large part of the weaknesses are removed, especially in conjunction with a rate limiter (by IP, by Path, etc), load balancer, a queue system such that the server can run more than one container at a time and other security measures.

Inspirations and References

Technologies

Feedback

If you have any feedback, please reach out at [email protected]

run-user-code-saas's People

Contributors

marcelo-schreiber avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.