Commit 45a329ca by Ned Batchelder

Line length and indentation fixes.

parent 54d3f77e
...@@ -98,7 +98,9 @@ def safe_exec(code, globals_dict, files=None, python_path=None): ...@@ -98,7 +98,9 @@ def safe_exec(code, globals_dict, files=None, python_path=None):
""" """
# Clean the globals for sending back as JSON over stdout. # Clean the globals for sending back as JSON over stdout.
""" """
ok_types = (type(None), int, long, float, str, unicode, list, tuple, dict) ok_types = (
type(None), int, long, float, str, unicode, list, tuple, dict
)
bad_keys = ("__builtins__",) bad_keys = ("__builtins__",)
def jsonable(v): def jsonable(v):
if not isinstance(v, ok_types): if not isinstance(v, ok_types):
...@@ -108,7 +110,11 @@ def safe_exec(code, globals_dict, files=None, python_path=None): ...@@ -108,7 +110,11 @@ def safe_exec(code, globals_dict, files=None, python_path=None):
except Exception: except Exception:
return False return False
return True return True
g_dict = {k:v for k,v in g_dict.iteritems() if jsonable(v) and k not in bad_keys} g_dict = {
k:v
for k,v in g_dict.iteritems()
if jsonable(v) and k not in bad_keys
}
""" """
# Write the globals back to the calling process. # Write the globals back to the calling process.
""" """
...@@ -124,9 +130,13 @@ def safe_exec(code, globals_dict, files=None, python_path=None): ...@@ -124,9 +130,13 @@ def safe_exec(code, globals_dict, files=None, python_path=None):
log.debug("Exec: %s", code) log.debug("Exec: %s", code)
log.debug("Stdin: %s", stdin) log.debug("Stdin: %s", stdin)
res = jail_code.jail_code("python", code=jailed_code, stdin=stdin, files=files) res = jail_code.jail_code(
"python", code=jailed_code, stdin=stdin, files=files
)
if res.status != 0: if res.status != 0:
raise SafeExecException("Couldn't execute jailed code: %s" % res.stderr) raise SafeExecException(
"Couldn't execute jailed code: %s" % res.stderr
)
globals_dict.update(json.loads(res.stdout)) globals_dict.update(json.loads(res.stdout))
...@@ -195,7 +205,8 @@ def not_safe_exec(code, globals_dict, files=None, python_path=None): ...@@ -195,7 +205,8 @@ def not_safe_exec(code, globals_dict, files=None, python_path=None):
# Wrap the exception in a SafeExecException, but we don't # Wrap the exception in a SafeExecException, but we don't
# try here to include the traceback, since this is just a # try here to include the traceback, since this is just a
# substitute implementation. # substitute implementation.
raise SafeExecException("{0.__class__.__name__}: {0!s}".format(e)) msg = "{0.__class__.__name__}: {0!s}".format(e)
raise SafeExecException(msg)
finally: finally:
sys.path = original_path sys.path = original_path
......
...@@ -45,9 +45,9 @@ class TestFeatures(JailCodeHelpers, unittest.TestCase): ...@@ -45,9 +45,9 @@ class TestFeatures(JailCodeHelpers, unittest.TestCase):
def test_argv(self): def test_argv(self):
res = jailpy( res = jailpy(
code="import sys; print ':'.join(sys.argv[1:])", code="import sys; print ':'.join(sys.argv[1:])",
argv=["Hello", "world", "-x"] argv=["Hello", "world", "-x"]
) )
self.assertResultOk(res) self.assertResultOk(res)
self.assertEqual(res.stdout, "Hello:world:-x\n") self.assertEqual(res.stdout, "Hello:world:-x\n")
...@@ -99,7 +99,10 @@ class TestFeatures(JailCodeHelpers, unittest.TestCase): ...@@ -99,7 +99,10 @@ class TestFeatures(JailCodeHelpers, unittest.TestCase):
argv=["doit.py", "1", "2", "3"] argv=["doit.py", "1", "2", "3"]
) )
self.assertResultOk(res) self.assertResultOk(res)
self.assertEqual(res.stdout, "This is doit.py!\nMy args are ['doit.py', '1', '2', '3']\n") self.assertEqual(
res.stdout,
"This is doit.py!\nMy args are ['doit.py', '1', '2', '3']\n"
)
class TestLimits(JailCodeHelpers, unittest.TestCase): class TestLimits(JailCodeHelpers, unittest.TestCase):
...@@ -199,26 +202,26 @@ class TestSymlinks(JailCodeHelpers, unittest.TestCase): ...@@ -199,26 +202,26 @@ class TestSymlinks(JailCodeHelpers, unittest.TestCase):
# Run some code in the sandbox, with a copied directory containing # Run some code in the sandbox, with a copied directory containing
# the symlink. # the symlink.
res = jailpy( res = jailpy(
code=dedent("""\ code=dedent("""\
print open('copied/here.txt').read() # can read print open('copied/here.txt').read() # can read
print open('copied/herelink.txt').read() # can read print open('copied/herelink.txt').read() # can read
print open('copied/link.txt').read() # can't read print open('copied/link.txt').read() # can't read
"""), """),
files=[self.copied], files=[self.copied],
) )
self.assertEqual(res.stdout, "012345\n012345\n") self.assertEqual(res.stdout, "012345\n012345\n")
self.assertIn("ermission denied", res.stderr) self.assertIn("ermission denied", res.stderr)
def test_symlinks_wont_copy_data(self): def test_symlinks_wont_copy_data(self):
# Run some code in the sandbox, with a copied file which is a symlink. # Run some code in the sandbox, with a copied file which is a symlink.
res = jailpy( res = jailpy(
code=dedent("""\ code=dedent("""\
print open('here.txt').read() # can read print open('here.txt').read() # can read
print open('herelink.txt').read() # can read print open('herelink.txt').read() # can read
print open('link.txt').read() # can't read print open('link.txt').read() # can't read
"""), """),
files=[self.here_txt, self.herelink_txt, self.link_txt], files=[self.here_txt, self.herelink_txt, self.link_txt],
) )
self.assertEqual(res.stdout, "012345\n012345\n") self.assertEqual(res.stdout, "012345\n012345\n")
self.assertIn("ermission denied", res.stderr) self.assertIn("ermission denied", res.stderr)
...@@ -262,7 +265,8 @@ class TestMalware(JailCodeHelpers, unittest.TestCase): ...@@ -262,7 +265,8 @@ class TestMalware(JailCodeHelpers, unittest.TestCase):
# http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html # http://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html
res = jailpy(code=dedent("""\ res = jailpy(code=dedent("""\
import new, sys import new, sys
crash_me = new.function(new.code(0,0,0,0,"KABOOM",(),(),(),"","",0,""), {}) bad_code = new.code(0,0,0,0,"KABOOM",(),(),(),"","",0,"")
crash_me = new.function(bad_code, {})
print "Here we go..." print "Here we go..."
sys.stdout.flush() sys.stdout.flush()
crash_me() crash_me()
...@@ -285,8 +289,7 @@ class TestMalware(JailCodeHelpers, unittest.TestCase): ...@@ -285,8 +289,7 @@ class TestMalware(JailCodeHelpers, unittest.TestCase):
res = jailpy(code=dedent(""" res = jailpy(code=dedent("""
import os import os
places = [ places = [
"..", "/tmp", "/", "/home", "/etc", "..", "/tmp", "/", "/home", "/etc", "/var"
"/var"
] ]
for place in places: for place in places:
try: try:
......
...@@ -9,7 +9,7 @@ from codejail.safe_exec import safe_exec, not_safe_exec, SafeExecException ...@@ -9,7 +9,7 @@ from codejail.safe_exec import safe_exec, not_safe_exec, SafeExecException
class SafeExecTests(unittest.TestCase): class SafeExecTests(unittest.TestCase):
"""The tests for `safe_exec`, will be mixed into specific test classes below.""" """The tests for `safe_exec`, to be mixed into specific test classes."""
# SafeExecTests is a TestCase so pylint understands the methods it can # SafeExecTests is a TestCase so pylint understands the methods it can
# call, but it's abstract, so stop nose from running the tests. # call, but it's abstract, so stop nose from running the tests.
......
...@@ -9,7 +9,8 @@ import tempfile ...@@ -9,7 +9,8 @@ import tempfile
class TempDirectory(object): class TempDirectory(object):
def __init__(self): def __init__(self):
self.temp_dir = tempfile.mkdtemp(prefix="codejail-") self.temp_dir = tempfile.mkdtemp(prefix="codejail-")
# Make directory readable by other users ('sandbox' user needs to be able to read it) # Make directory readable by other users ('sandbox' user needs to be
# able to read it).
os.chmod(self.temp_dir, 0775) os.chmod(self.temp_dir, 0775)
def clean_up(self): def clean_up(self):
......
...@@ -35,7 +35,6 @@ load-plugins= ...@@ -35,7 +35,6 @@ load-plugins=
# it should appear only once). # it should appear only once).
disable= disable=
# Never going to use these # Never going to use these
# C0301: Line too long
# W0142: Used * or ** magic # W0142: Used * or ** magic
# W0141: Used builtin function 'map' # W0141: Used builtin function 'map'
...@@ -50,7 +49,7 @@ disable= ...@@ -50,7 +49,7 @@ disable=
# R0912: Too many branches # R0912: Too many branches
# R0913: Too many arguments # R0913: Too many arguments
# R0914: Too many local variables # R0914: Too many local variables
C0301,C0302,W0141,W0142,R0201,R0901,R0902,R0903,R0904,R0911,R0912,R0913,R0914 C0302,W0141,W0142,R0201,R0901,R0902,R0903,R0904,R0911,R0912,R0913,R0914
[REPORTS] [REPORTS]
...@@ -68,7 +67,7 @@ include-ids=yes ...@@ -68,7 +67,7 @@ include-ids=yes
files-output=no files-output=no
# Tells whether to display a full report or only the messages # Tells whether to display a full report or only the messages
reports=yes reports=no
# Python expression which should return a note less than 10 (10 is the highest # Python expression which should return a note less than 10 (10 is the highest
# note). You have access to the variables errors warning, statement which # note). You have access to the variables errors warning, statement which
...@@ -157,7 +156,7 @@ bad-names=foo,bar,baz,toto,tutu,tata ...@@ -157,7 +156,7 @@ bad-names=foo,bar,baz,toto,tutu,tata
# Regular expression which should only match functions or classes name which do # Regular expression which should only match functions or classes name which do
# not require a docstring # not require a docstring
no-docstring-rgx=(__.*__|test_.*) no-docstring-rgx=(__.*__|test_.*|setUp|tearDown)
[MISCELLANEOUS] [MISCELLANEOUS]
...@@ -169,7 +168,7 @@ notes=FIXME,XXX,TODO ...@@ -169,7 +168,7 @@ notes=FIXME,XXX,TODO
[FORMAT] [FORMAT]
# Maximum number of characters on a single line. # Maximum number of characters on a single line.
max-line-length=120 max-line-length=79
# Maximum number of lines in a module # Maximum number of lines in a module
max-module-lines=1000 max-module-lines=1000
......
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