{"id":3259,"library":"restrictedpython","title":"RestrictedPython","description":"RestrictedPython is a tool that defines a subset of the Python language, allowing program input to be executed within a trusted environment. It is not a full sandbox system but aids in establishing a controlled execution space for untrusted code. The current stable version is 8.1, released on 2025-10-19, and the project maintains an active release cadence.","status":"active","version":"8.1","language":"en","source_language":"en","source_url":"https://github.com/zopefoundation/RestrictedPython","tags":["security","sandbox","code execution","zope","ast","policy"],"install":[{"cmd":"pip install RestrictedPython","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"compile_restricted","correct":"from RestrictedPython import compile_restricted"},{"symbol":"safe_globals","correct":"from RestrictedPython import safe_globals"},{"note":"A predefined set of safe builtins; often used within safe_globals.","symbol":"safe_builtins","correct":"from RestrictedPython import safe_builtins"}],"quickstart":{"code":"from RestrictedPython import compile_restricted\nfrom RestrictedPython import safe_globals\n\nsource_code = \"\"\"\ndef greet(name):\n    return 'Hello, ' + str(name) + '!'\n\"\"\"\n\n# Prepare the global namespace for execution\n# safe_globals includes __builtins__ with restricted functions/modules\nrestricted_globals = safe_globals.copy()\n# Add any specific names or functions you want to allow in the restricted scope\nrestricted_globals['_getattr_'] = getattr # Example: allowing getattr in a restricted manner\n\nloc = {}\ntry:\n    # Compile the restricted code\n    byte_code = compile_restricted(\n        source_code,\n        filename='<restricted_code>',\n        mode='exec'\n    )\n    # Execute the compiled code within the restricted globals\n    exec(byte_code, restricted_globals, loc)\n\n    # Call the function from the restricted execution's local scope\n    result = loc['greet']('World')\n    print(result)\n\n    # Example of forbidden operation (will raise error if policy is strict)\n    # forbidden_code = \"import os; os.listdir('/')\"\n    # forbidden_byte_code = compile_restricted(forbidden_code, '<forbidden>', 'exec')\n    # exec(forbidden_byte_code, safe_globals, {})\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates compiling and executing a simple Python function within a restricted environment. It uses `compile_restricted` to process the source code and `exec` with a modified `safe_globals` dictionary to control available built-ins and attributes. You can extend `restricted_globals` to whitelist specific functions or modules as needed for your application."},"warnings":[{"fix":"Users must implement robust policies for operations like `_print_`, `_write_`, `_getattr_`, `_getitem_`, and strictly curate the `__import__` hook and `__builtins__` dictionary to prevent escapes. `safe_globals` is a starting point, not a complete solution.","message":"RestrictedPython is not a full security sandbox or a secured environment on its own. It provides mechanisms to define a subset of Python and helps in creating a trusted environment, but achieving true security requires careful policy implementation by the user.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your execution environment uses CPython.","message":"RestrictedPython officially supports only CPython. It does NOT support PyPy or other alternative Python implementations, as it cannot guarantee its restrictions in those environments.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade to RestrictedPython 8.0 or higher. If unable to upgrade, consider downgrading Python to 3.10 or lower, where `try/except*` is not available.","message":"Support for `try/except*` clauses was disallowed in version 8.0 due to a possible sandbox escape vulnerability (CVE-2025-22153).","severity":"breaking","affected_versions":"8.0 and higher"},{"fix":"Update code that unpacks the return value of `compile_restricted` calls. Instead of `code, errors = compile_restricted(...)`, use `result = compile_restricted(...)` and access `result.code`, `result.errors`, `result.warnings`, `result.used_names`.","message":"The `compile_restricted` functions (e.g., `compile_restricted_exec`, `compile_restricted_eval`, `compile_restricted_single`, `compile_restricted_function`) now return a `CompileResult` namedtuple instead of a simple tuple.","severity":"breaking","affected_versions":"4.0 and higher"},{"fix":"Remove usage of the `Ellipsis` statement in restricted code.","message":"The `Ellipsis` (`...`) statement was re-disallowed in version 5.0 due to unclear security implications, after being allowed in version 4.0.","severity":"breaking","affected_versions":"5.0 and higher"},{"fix":"Consult the `requires_python` metadata on PyPI or the official documentation's 'Supported Python versions' section to ensure compatibility with your Python interpreter.","message":"Support for older Python versions is progressively dropped with new major/minor releases. For example, Python 3.8 support was dropped in v8.0, and Python 3.7 support was dropped in v7.4.","severity":"breaking","affected_versions":"7.4 and higher"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}