From acf10ba8bbc1bb3d5dcad3c0ff7b3e384b11c9ca Mon Sep 17 00:00:00 2001 From: Karim Hamidou Date: Sat, 9 Dec 2017 19:19:27 -0800 Subject: [PATCH] More fun with pipes --- .gitignore | 1 + requirements.txt | 3 ++- templates/index.html | 38 +++++++++++++++++++++++--------------- web.py | 43 ++++++++++++++++++++++++++++++++++++++----- 4 files changed, 64 insertions(+), 21 deletions(-) diff --git a/.gitignore b/.gitignore index b948985..3cbfb39 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.swp *.pyc +venv/ diff --git a/requirements.txt b/requirements.txt index d1c1e9f..86d3b3e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ flask==0.12.2 gunicorn==19.7.1 -python-prctl==1.6.1 WTForms==2.1 Flask-WTF==0.14.2 + +#python-prctl==1.6.1 diff --git a/templates/index.html b/templates/index.html index e0108b3..f706830 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,19 +1,27 @@ - -

-

- {{ form.hidden_tag() }} - {% for error in form.code.errors %} -

{{ error }}

- {% endfor %} -

- {{ form.code.label }} {{ form.code() }} -

-

- -

-
- + +

+

+ {{ form.hidden_tag() }} + {% for error in form.code.errors %} +

{{ error }}

+ {% endfor %} +

+ {{ form.code.label }}
{{ form.code(cols="80", rows="25") }} +

+

+ +

+
+

+ Result: +

+            
+                {{ result }}
+            
+        
+

+ diff --git a/web.py b/web.py index 6024df0..600b775 100644 --- a/web.py +++ b/web.py @@ -1,22 +1,55 @@ -import prctl +#import prctl +import io +import os +import sys from flask import Flask, render_template, request from flask_wtf import FlaskForm -from wtforms import TextField +from wtforms import TextAreaField from wtforms.validators import DataRequired app = Flask(__name__) +app.secret_key = os.environ['SECRET_KEY'] class EvalForm(FlaskForm): - code = TextField('code', validators=[DataRequired()]) + code = TextAreaField('code', validators=[DataRequired()]) -@app.route("/") -def hello(): +@app.route("/", methods=("GET", "POST",)) +def main(): form = EvalForm() + if form.validate_on_submit(): + res = execute_in_sandbox(form.code.data) + return render_template("index.html", form=form, result=res) + return render_template("index.html", form=form) +def execute_in_sandbox(code): + read_pipe, write_pipe = os.pipe() + pid = os.fork() + + if pid == 0: + # if fork() -> share environment & everything + # execve while limiting reads to anything not in /etc, blocking + # anything else. + cmd = "/usr/bin/emacs" + + os.dup2(write_pipe, sys.stdout.fileno()) + os.execv(cmd, [cmd, "--version"]) + else: + os.close(write_pipe) + os.waitpid(pid, 0) + ret = [] + while True: + result = os.read(read_pipe, 32) + if result == "" or result is None: + break + else: + ret.append(result) + + os.close(read_pipe) + return "".join(ret) if __name__ == "__main__": app.run() -- 2.43.0