Commit ec7a04fd by Ned Batchelder

A /debug/run_python endpoint for staff to test the sandboxing of Python code.

parent df17c0c7
from django.db import models
# Create your models here.
"""Views for debugging and diagnostics"""
import pprint
from django.http import Http404
from django.contrib.auth.decorators import login_required
from django_future.csrf import ensure_csrf_cookie, csrf_exempt
from mitxmako.shortcuts import render_to_response
from codejail.safe_exec import safe_exec
@login_required
@ensure_csrf_cookie
def run_python(request):
if not request.user.is_staff:
raise Http404
c = {}
c['code'] = ''
c['results'] = None
if request.method == 'POST':
py_code = c['code'] = request.POST.get('code')
g, l = {}, {}
try:
safe_exec(py_code, g, l)
except Exception as e:
c['results'] = str(e)
else:
c['results'] = pprint.pformat(l)
return render_to_response("debug/run_python_form.html", c)
......@@ -589,6 +589,7 @@ INSTALLED_APPS = (
# For testing
'django.contrib.admin', # only used in DEBUG mode
'debug',
# Discussion forums
'django_comment_client',
......
<html><body>
<div>
<p>Python:</p>
<form method='post'>
<input type="hidden" name="csrfmiddlewaretoken" value="${ csrf_token }">
<div>
<textarea name='code' rows='20' cols='80'>${code|h}</textarea>
</div>
<input type='submit' value='Run it!'/>
</form>
</div>
%if results:
<div>
<p>Results:</p>
<pre>
${results|h}
</pre>
</div>
%endif
......@@ -358,6 +358,10 @@ urlpatterns += (
url(r'^comm/foldit_ops', 'foldit.views.foldit_ops', name="foldit_ops"),
)
urlpatterns += (
url(r'^debug/run_python', 'debug.views.run_python'),
)
urlpatterns = patterns(*urlpatterns)
if settings.DEBUG:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment