-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
086f4c8
commit b150a37
Showing
12 changed files
with
219 additions
and
202 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
FROM python:3.10-slim | ||
|
||
RUN adduser --disabled-password --gecos '' appuser | ||
|
||
WORKDIR /app | ||
|
||
COPY app.py . | ||
|
||
RUN pip install flask gunicorn RestrictedPython | ||
|
||
RUN chown -R appuser:appuser /app | ||
|
||
USER appuser | ||
|
||
EXPOSE 5000 | ||
|
||
CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:5000", "app:app"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from flask import Flask, request, jsonify | ||
import tempfile | ||
import shutil | ||
import re | ||
import sys | ||
import io | ||
|
||
app = Flask(__name__) | ||
|
||
DANGEROUS_KEYWORDS = [ | ||
r'\bos\b', | ||
r'\bsys\b', | ||
r'\bimport\b', | ||
r'\beval\b', | ||
r'\bexec\b', | ||
r'\bopen\b', | ||
r'\bsubprocess\b' | ||
] | ||
|
||
def is_code_safe(code): | ||
|
||
for pattern in DANGEROUS_KEYWORDS: | ||
if re.search(pattern, code): | ||
return False | ||
return True | ||
|
||
@app.route('/execute', methods=['POST']) | ||
def execute_python_safely(): | ||
code = request.json.get('code') | ||
inputs = request.json.get('inputs', []) | ||
|
||
if not code: | ||
return jsonify({"error": "No code provided."}), 400 | ||
|
||
if not is_code_safe(code): | ||
return jsonify({"error": "Code contains unsafe operations."}), 400 | ||
|
||
temp_dir = tempfile.mkdtemp(prefix="user_code_") | ||
|
||
try: | ||
input_data = iter(inputs) | ||
|
||
output_capture = io.StringIO() | ||
sys.stdout = output_capture # | ||
|
||
exec_env = { | ||
'input': lambda prompt: next(input_data, ''), | ||
} | ||
|
||
exec(code, exec_env) | ||
|
||
output = output_capture.getvalue() | ||
|
||
return jsonify({ | ||
"output": output, | ||
"status": 0 | ||
}) | ||
except Exception as e: | ||
return jsonify({"error": str(e)}), 500 | ||
finally: | ||
shutil.rmtree(temp_dir) | ||
sys.stdout = sys.__stdout__ | ||
|
||
if __name__ == '__main__': | ||
app.run(host='0.0.0.0', port=5000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { NextResponse } from "next/server"; | ||
|
||
export async function POST(req) { | ||
try { | ||
const { code, inputs } = await req.json(); | ||
const response = await fetch(process.env.COMPILER_URL, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ code, inputs }), | ||
}); | ||
const data = await response.json(); | ||
if (data.error) { | ||
throw new Error(data.error); | ||
} | ||
if (!response.ok) { | ||
throw new Error("Failed to compile code"); | ||
} | ||
return NextResponse.json(data, { status: 200 }); | ||
} catch (error) { | ||
return NextResponse.json({ error: error.message }, { status: 500 }); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.