Commit 182a1a18 by Ned Batchelder

Cleanups

parent 0021b0ac
......@@ -5,6 +5,7 @@ from django.conf import settings
import codejail.jailpy
class ConfigureCodeJailMiddleware(object):
"""Middleware to configure codejail on startup."""
......
......@@ -4,7 +4,8 @@
# - AppArmor.md from xserver
import logging
import os, os.path
import os
import os.path
import resource
import shutil
import subprocess
......@@ -22,6 +23,7 @@ log = logging.getLogger(__name__)
PYTHON_CMD = None
def configure(python_bin, user=None):
"""Configure the jailpy module."""
global PYTHON_CMD
......@@ -30,6 +32,7 @@ def configure(python_bin, user=None):
PYTHON_CMD.extend(['sudo', '-u', 'sandbox'])
PYTHON_CMD.extend([python_bin, '-E'])
def is_configured():
return bool(PYTHON_CMD)
......@@ -42,7 +45,9 @@ if hasattr(sys, 'real_prefix'):
class JailResult(object):
"""A passive object for us to return from jailpy."""
pass
def __init__(self):
self.stdout = self.stderr = self.status = None
def jailpy(code, files=None, argv=None, stdin=None):
"""
......@@ -104,7 +109,7 @@ def set_process_limits():
resource.setrlimit(resource.RLIMIT_NPROC, (0, 0)) # no subprocesses
resource.setrlimit(resource.RLIMIT_FSIZE, (0, 0)) # no files
mem = 32 * 2**20 # 32 MB should be enough for anyone, right? :)
mem = 32 * (2 ** 20) # 32 MB should be enough for anyone, right? :)
resource.setrlimit(resource.RLIMIT_STACK, (mem, mem))
resource.setrlimit(resource.RLIMIT_RSS, (mem, mem))
resource.setrlimit(resource.RLIMIT_DATA, (mem, mem))
......
......@@ -6,9 +6,9 @@ import shutil
import sys
import textwrap
import jailpy
from codejail import jailpy
from codejail.util import temp_directory, change_directory
from util import temp_directory, change_directory
def safe_exec(code, globals_dict, files=None, python_path=None):
"""Execute code as "exec" does, but safely.
......@@ -98,7 +98,7 @@ def json_safe(d):
ok_types = (type(None), int, long, float, str, unicode, list, tuple, dict)
bad_keys = ("__builtins__",)
jd = {}
for k,v in d.iteritems():
for k, v in d.iteritems():
if not isinstance(v, ok_types):
continue
if k in bad_keys:
......
......@@ -9,6 +9,7 @@ from codejail.jailpy import jailpy, is_configured
dedent = textwrap.dedent
class JailPyHelpers(object):
"""Assert helpers for jailpy tests."""
def setUp(self):
......@@ -112,6 +113,7 @@ class TestLimits(JailPyHelpers, unittest.TestCase):
# TODO: read network
# TODO: fork
class TestMalware(JailPyHelpers, unittest.TestCase):
def test_crash_cpython(self):
# http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
......
......@@ -7,6 +7,7 @@ from nose.plugins.skip import SkipTest
from codejail.safe_exec import safe_exec, not_safe_exec
class SafeExecTests(object):
"""The tests for `safe_exec`, will be mixed into specific test classes below."""
def test_set_values(self):
......@@ -60,6 +61,7 @@ class TestSafeExec(SafeExecTests, unittest.TestCase):
def safe_exec(self, *args, **kwargs):
safe_exec(*args, **kwargs)
class TestNotSafeExec(SafeExecTests, unittest.TestCase):
"""Run SafeExecTests, with not_safe_exec."""
def setUp(self):
......
......@@ -3,7 +3,6 @@
import contextlib
import os
import shutil
import sys
import tempfile
......@@ -19,6 +18,7 @@ class TempDirectory(object):
# if this errors, something is genuinely wrong, so don't ignore errors.
shutil.rmtree(self.temp_dir)
@contextlib.contextmanager
def temp_directory(delete_when_done=True):
"""
......@@ -40,6 +40,7 @@ class ChangeDirectory(object):
def clean_up(self):
os.chdir(self.old_dir)
@contextlib.contextmanager
def change_directory(new_dir):
"""
......
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