Modal logo

Run untrusted code with Restricted Functions

This guide page documents Restricted Functions, which can be used to safely execute untrusted code in Modal Functions.

Create a Restricted Function 

To create a Restricted Function, set restrict_modal_access=True in the Function definition:

@app.function(restrict_modal_access=True)
def run_untrusted_code(code_input: str):
    # This code cannot access Modal resources
    return eval(code_input)

When restrict_modal_access is enabled, the Function cannot

  • access Modal resources (Queues, Dicts, etc.)
  • call other Functions
  • access Modal’s internal APIs

Block pickled payloads 

By default, Modal serializes Function inputs and outputs with pickle, and unpickling the output of untrusted code can run that code in the caller’s process. Set allow_pickle=False so that no pickled data goes into or comes out of the Function:

@app.function(restrict_modal_access=True, allow_pickle=False)
def run_untrusted_code(code_input: str) -> str:
    return str(eval(code_input))

Arguments and return values must then be basic types (None, bool, int, float, str, bytes, lists and dicts), and exceptions are raised in the caller as modal.exception.FunctionCallError (see Error Handling). @app.cls also accepts allow_pickle=False, with constructor arguments defined by modal.parameter(). Web endpoints, generators and batched functions aren’t supported.

To make sure the calling process never unpickles anything, also run it with MODAL_ALLOW_PICKLE=0 (or allow_pickle = false in .modal.toml):

MODAL_ALLOW_PICKLE=0 modal run restricted_app.py

Sandboxes offer an alternative interface for untrusted code 

Modal provides two primitives for running untrusted code: Restricted Functions and Sandboxes. While both can be used for running untrusted code, they provide different interfaces: Sandboxes provide a process interface, while Restricted Functions provide a function-calling interface. Process interfaces are especially useful for stateful, multi-stage communication, while function-calling interfaces are especially useful for stateless, input/output communication.

These differences are summarized in the table below.

FeatureRestricted FunctionSandbox
StateStatelessStateful
InterfaceFunction-likeContainer-like
SetupSimple decoratorRequires explicit creation/termination
Use caseQuick, isolated code executionInteractive development, long-running sessions

Best Practices 

When running untrusted code, consider these additional security measures:

  1. Use single_use_containers=True to ensure each container only handles one request. Containers that get reused could cause information leakage between users.
@app.function(restrict_modal_access=True, single_use_containers=True)
def isolated_function(input_data):
    # Each input gets a fresh container
    return process(input_data)

Note: Prior to v1.3.0, single-use containers were configured by setting max_inputs=1.

  1. Set appropriate timeouts to prevent long-running operations:
@app.function(
    restrict_modal_access=True,
    timeout=30,  # 30 second timeout
    single_use_containers=True
)
def time_limited_function(input_data):
    return process(input_data)
  1. Consider using block_network=True to prevent the container from making outbound network requests:
@app.function(
    restrict_modal_access=True,
    block_network=True,
    single_use_containers=True
)
def network_isolated_function(input_data):
    return process(input_data)
  1. Use allow_pickle=False so the caller never unpickles data from the Function. See Block pickled payloads.

  2. Minimize the App source that’s included in the container

A restricted Modal Function will have read access to its source files in the container, so you’ll want to avoid including anything that would be harmful if exfiltrated by the untrusted process.

If deploying an App from within a larger package, the entire package source may be automatically included by default. A best practice would be to make the untrusted Function part of a standalone App that includes the minimum necessary files to run:

restricted_app = modal.App("restricted-app", include_source=False)

image = (
    modal.Image.debian_slim()
    .add_local_file("restricted_executor.py", "/root/restricted_executor.py")
)

@restricted_app.function(
    restrict_modal_access=True,
    block_network=True,
    single_use_containers=True
)
def isolated_function(input_data):
    return process(input_data)

Example: Running LLM-generated Code 

Below is a complete example of running code generated by a language model:

import modal

app = modal.App("restricted-access-example")


@app.function(
    restrict_modal_access=True,
    allow_pickle=False,
    single_use_containers=True,
    timeout=30,
    block_network=True,
)
def run_llm_code(generated_code: str):
    try:
        # Create a restricted environment
        execution_scope = {}

        # Execute the generated code
        exec(generated_code, execution_scope)

        # Return the result if it exists
        return execution_scope.get("result", None)
    except Exception as e:
        return f"Error executing code: {str(e)}"


@app.local_entrypoint()
def main():
    # Example LLM-generated code
    code = """
def calculate_fibonacci(n):
    if n <= 1:
        return n
    return calculate_fibonacci(n-1) + calculate_fibonacci(n-2)

result = calculate_fibonacci(10)
    """

    result = run_llm_code.remote(code)
    print(f"Result: {result}")

This example locks down the container to ensure that the code is safe to execute by:

  • Restricting Modal access
  • Blocking pickled payloads
  • Using a fresh container for each execution
  • Setting a timeout
  • Blocking network access
  • Catching and handling potential errors

Error Handling 

When a Restricted Function attempts to access Modal resources, it will raise an AuthError:

@app.function(restrict_modal_access=True)
def restricted_function(q: modal.Queue):
    try:
        # This will fail because the Function is restricted
        return q.get()
    except modal.exception.AuthError as e:
        return f"Access denied: {e}"

The error message will indicate that the operation is not permitted due to restricted Modal access.

With allow_pickle=False, remote exceptions are raised as modal.exception.FunctionCallError, with exception_repr and remote_traceback attributes:

@app.local_entrypoint()
def main():
    try:
        run_untrusted_code.remote("1 / 0")
    except modal.exception.FunctionCallError as e:
        print(f"Remote exception: {e.exception_repr}")
        print(e.remote_traceback)