What's the best way to run user-submitted code with a Node.js Express backend?
Asked by
seismic__station
I'm trying to create a platform similar to those live coding sites where users can submit code in multiple languages like Bash, Python, Rust, Ruby, etc., and see the output in real-time. I'm using Node.js with an Express backend and I'm exploring options for safely running user-submitted code. Here are a few options I'm considering:
1. Running the code directly on the EC2 instance โ this seems really risky.
2. Using Docker containers โ I'm curious about pros/cons and how long each container should run.
3. Running within an AWS Elastic Container Service Task โ what do I need to know here?
I'd love your suggestions on these options and any other methods you might recommend. Also, how should I handle queuing for submissions, returning data to users, and showing real-time streams of terminal commands like package downloads?
Answers
Have you thought about using AWS Lambda? Itโs a great option because it's already sandboxed and can handle multiple languages. Instead of overcomplicating with a queue system, just set up a simple HTTP endpoint to execute code submissions and return results to users directly. As for the output stream, Lambda supports streaming, so you can easily send real-time updates back to users.
Reply to this answer
Definitely don't run user-submitted code directly on your EC2 instance. That's a recipe for disaster! Containerization is a good approach, but you need to ensure high securityโconsider using separate containers for each language to avoid cross-contamination of commands. It's crucial to prioritize safety above all else.
Reply to this answer
Cloudflare Workers could be another option, but it sounds like you need to stick with AWS. Using Docker containers would work really wellโjust create a fresh container for each code submission and tear it down afterward. A timeout of around 5-10 seconds should help manage costs on AWS. Using something like BullMQ for queuing and streaming results via WebSocket will give you real-time feedback for users.
Reply to this answer
When users submit code, you should spawn a new Docker container each time with a set timeout. If they come back later and hit 'run' again, it should indeed start another fresh container for that session. If users need to store files, think about how you'll handle that, too, during multiple submissions.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions