{"id":21198,"library":"edx-codejail","title":"edx-codejail","description":"CodeJail manages execution of untrusted code in secure sandboxes. It is designed primarily for Python execution, but can be used for other languages as well. The current version is 4.1.0, released May 2025. Release cadence is irregular, approximately 2-3 minor versions per year.","status":"active","version":"4.1.0","language":"python","source_language":"en","source_url":"https://github.com/openedx/codejail","tags":["sandbox","security","untrusted-code","execution","openedx"],"install":[{"cmd":"pip install edx-codejail","lang":"bash","label":"PyPI"}],"dependencies":[{"reason":"Used for sandboxing on Linux (AppArmor profile required for secure execution)","package":"apparmor","optional":true},{"reason":"Required at runtime for safe exec compilation","package":"setuptools","optional":false}],"imports":[{"note":"importing from top-level breaks because CodeJail is defined in codejail.jail","wrong":"from codejail import CodeJail","symbol":"CodeJail","correct":"from codejail.jail import CodeJail"},{"note":"is_safe is in codejail.safe_exec, not top-level","wrong":"from codejail import is_safe","symbol":"is_safe","correct":"from codejail.safe_exec import is_safe"},{"note":"safe_exec function is in codejail.safe_exec module","wrong":"from codejail.jail import safe_exec","symbol":"safe_exec","correct":"from codejail.safe_exec import safe_exec"}],"quickstart":{"code":"from codejail.jail import CodeJail\nfrom codejail.safe_exec import safe_exec\n\n# Configure a Python jail (requires proper system setup)\njail = CodeJail('python', user='sandbox', sys_packages=['/usr/bin/python3'])\n\n# Use safe_exec for quick sandboxed execution\nresult = safe_exec(\"print(1+1)\", globals=globals(), python_path='/usr/bin/python3')\nprint(result)\n","lang":"python","description":"Basic usage: configure a jail and execute untrusted code safely."},"warnings":[{"fix":"Update code to handle cases where execution is blocked, or explicitly configure the jail to allow unsafe operations if needed.","message":"In v4.0.0, 'unsafe execution' now requires explicit opt-in via calling code rather than being allowed by default. Code that relied on the old behavior will raise an error.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Ensure AppArmor is installed and the codejail profile is loaded. Run as root or use appropriate sudo.","message":"CodeJail requires root or specific user privileges to set up AppArmor profiles. Running without proper system configuration will cause silent failures.","severity":"gotcha","affected_versions":">=3.0.0"},{"fix":"Use jail.run() for more control or parse the returned dict.","message":"The safe_exec function does not capture stdout/stderr; it returns a dictionary with 'status', 'stderr', etc. New users often expect print() output to appear.","severity":"gotcha","affected_versions":"all"},{"fix":"Replace sys_packages with python_path in CodeJail constructor.","message":"Using 'sys_packages' argument to CodeJail is deprecated. Use 'python_path' instead.","severity":"deprecated","affected_versions":">=4.0.0"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Use: from codejail.jail import CodeJail","cause":"CodeJail is in codejail.jail, not top-level codejail.","error":"ImportError: cannot import name 'CodeJail' from 'codejail'"},{"fix":"Either use jail.run() with appropriate configuration or set the environment variable CODEJAIL_UNSAFE=1 (not recommended for production).","cause":"Since v4.0.0, unsafe execution is opt-in. The safe_exec function requires explicit permission.","error":"codejail.safe_exec: unsafe execution is not allowed"},{"fix":"Verify the Python interpreter path on your system (which python3) and pass the correct path.","cause":"The python_path specified does not exist or is not executable.","error":"FileNotFoundError: /usr/bin/python3 not found"},{"fix":"Run the application as a user with appropriate permissions, or adjust file permissions.","cause":"The user running the jail does not have execute permission on the Python interpreter.","error":"OSError: [Errno 13] Permission denied: '/usr/bin/python3'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}