|
23 | 23 |
|
24 | 24 | # Set this to True to log all the code and globals being executed.
|
25 | 25 | LOG_ALL_CODE = False
|
26 |
| -# Set this to True to use the unsafe code, so that you can debug it. |
| 26 | + |
| 27 | +# Set this to True to run submitted code with no confinement and no sandbox. |
| 28 | +# |
| 29 | +# WARNING: This is deeply dangerous; anyone who can submit code can take |
| 30 | +# over the computer immediately and entirely. |
| 31 | +# |
| 32 | +# The only purpose of this setting is for local debugging. |
27 | 33 | ALWAYS_BE_UNSAFE = False
|
28 | 34 |
|
29 | 35 |
|
@@ -80,8 +86,22 @@ def safe_exec(
|
80 | 86 | the code raises an exception, this function will raise `SafeExecException`
|
81 | 87 | with the stderr of the sandbox process, which usually includes the original
|
82 | 88 | exception message and traceback.
|
83 |
| -
|
84 | 89 | """
|
| 90 | + if ALWAYS_BE_UNSAFE: |
| 91 | + not_safe_exec( |
| 92 | + code, |
| 93 | + globals_dict, |
| 94 | + files=files, |
| 95 | + python_path=python_path, |
| 96 | + limit_overrides_context=limit_overrides_context, |
| 97 | + slug=slug, |
| 98 | + extra_files=extra_files, |
| 99 | + ) |
| 100 | + return |
| 101 | + |
| 102 | + if not jail_code.is_configured('python'): |
| 103 | + raise RuntimeError("safe_exec has not been configured for Python") |
| 104 | + |
85 | 105 | the_code = []
|
86 | 106 |
|
87 | 107 | files = list(files or ())
|
@@ -257,6 +277,11 @@ def not_safe_exec(
|
257 | 277 | Note that `limit_overrides_context` is ignored here, because resource limits
|
258 | 278 | are not applied.
|
259 | 279 | """
|
| 280 | + # Because it would be bad if this function were used in production, |
| 281 | + # let's log a warning when it is used. Developers can live with |
| 282 | + # one more log line. |
| 283 | + log.warning("Using codejail/safe_exec.py:not_safe_exec for %s", slug) |
| 284 | + |
260 | 285 | g_dict = json_safe(globals_dict)
|
261 | 286 |
|
262 | 287 | with temp_directory() as tmpdir:
|
@@ -286,22 +311,3 @@ def not_safe_exec(
|
286 | 311 | sys.path = original_path
|
287 | 312 |
|
288 | 313 | globals_dict.update(json_safe(g_dict))
|
289 |
| - |
290 |
| - |
291 |
| -# If the developer wants us to be unsafe (ALWAYS_BE_UNSAFE), or if there isn't |
292 |
| -# a configured jail for Python, then we'll be UNSAFE. |
293 |
| -UNSAFE = ALWAYS_BE_UNSAFE or not jail_code.is_configured("python") |
294 |
| - |
295 |
| -if UNSAFE: # pragma: no cover |
296 |
| - # Make safe_exec actually call not_safe_exec, but log that we're doing so. |
297 |
| - |
298 |
| - def safe_exec(*args, **kwargs): # pylint: disable=E0102 |
299 |
| - """An actually-unsafe safe_exec, that warns it's being used.""" |
300 |
| - |
301 |
| - # Because it would be bad if this function were used in production, |
302 |
| - # let's log a warning when it is used. Developers can live with |
303 |
| - # one more log line. |
304 |
| - slug = kwargs.get('slug', None) |
305 |
| - log.warning("Using codejail/safe_exec.py:not_safe_exec for %s", slug) |
306 |
| - |
307 |
| - return not_safe_exec(*args, **kwargs) |
|
0 commit comments