Commit 0a1b468d by Ned Batchelder

Change how we fallback to not_safe_exec.

Edx-platform will be using not_safe_exec in some cases, and we don't
want to log a warning in those cases, so only log when it's an aliased
fallback, not when the function is called directly.
parent 045e0916
......@@ -185,11 +185,6 @@ def not_safe_exec(code, globals_dict, files=None, python_path=None, slug=None):
and modifying sys.path.
"""
# Because it would be bad if this function were used in production, let's
# log a warning when it is used. Developers can can live with one more
# log line.
log.warning("Using codejail/safe_exec.py:not_safe_exec")
g_dict = json_safe(globals_dict)
with temp_directory() as tmpdir:
......@@ -216,7 +211,20 @@ def not_safe_exec(code, globals_dict, files=None, python_path=None, slug=None):
globals_dict.update(json_safe(g_dict))
# Running Python code in the sandbox makes it difficult to debug.
NO_SAFE_PYTHON = not jail_code.is_configured("python")
if ALWAYS_BE_UNSAFE or NO_SAFE_PYTHON: # pragma: no cover
safe_exec = not_safe_exec
# If the developer wants us to be unsafe (ALWAYS_BE_UNSAFE), or if there isn't
# a configured jail for Python, then we'll be UNSAFE.
UNSAFE = ALWAYS_BE_UNSAFE or not jail_code.is_configured("python")
if UNSAFE: # pragma: no cover
# Make safe_exec actually call not_safe_exec, but log that we're doing so.
def safe_exec(*args, **kwargs): # pylint: disable=E0102
"""An actually-unsafe safe_exec, that warns it's being used."""
# Because it would be bad if this function were used in production,
# let's log a warning when it is used. Developers can can live with
# one more log line.
slug = kwargs.get('slug', None)
log.warning("Using codejail/safe_exec.py:not_safe_exec for %s", slug)
return not_safe_exec(*args, **kwargs)
......@@ -96,7 +96,7 @@ class TestNotSafeExec(SafeExecTests, unittest.TestCase):
def setUp(self):
# If safe_exec is actually an alias to not_safe_exec, then there's no
# point running these tests.
if safe_exec is not_safe_exec: # pragma: no cover
if safe_exec.UNSAFE: # pragma: no cover
raise SkipTest
def safe_exec(self, *args, **kwargs):
......
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