luerl_sandbox (luerl v1.5.1)

View Source

Sandboxed execution environment for Luerl with reduction counting. This module provides a way to run Lua code with controlled execution, limiting the number of reductions (computational steps) to prevent excessive resource consumption.

The ControlFlags is a map or keyword list which can contain the following fields

    #{max_time => MaxTime,              % 100 msecs
      max_reductions => MaxReds,        % none
      spawn_opts => SpawnOpts}          % []

MaxReds limits the number of reductions (default no max) and MaxTime (default 100 msecs) limits the maximum time to run the string. SpawnOpts are spawn options to the process running the evaluation. The default values are shown as comments. Any other fields are ignored.

Summary

Functions

Create a new Luerl state with the standard sandboxing.

Take an existing Luerl state and run the default sandboxing on it. Or create a new Luerl state and run a sandboxing table on it.

Take an existing Luerl state and run the TablePaths on it to control the sandboxing.

Run the Lua expression controlled by the default ControlFlags in a new LuaState with the default sandboxing.

Run the Lua expression controlled by the default ControlFlags in the LuaState.

Run the Lua expression controlled by the ControlFlags in the LuaState.

Types

controlflags()

-type controlflags() :: #{atom() := term()} | [{atom(), term()}].

luerldata()

-type luerldata() ::
          nil |
          boolean() |
          binary() |
          number() |
          #tref{i :: term()} |
          #usdref{i :: term()} |
          #eref{i :: term()} |
          #funref{i :: term(), env :: term()} |
          #erl_func{code :: term()} |
          #erl_mfa{m :: term(), f :: term(), a :: term()}.

luerlstate()

-type luerlstate() ::
          #luerl{tabs :: term(),
                 envs :: term(),
                 usds :: term(),
                 fncs :: term(),
                 g :: term(),
                 stk :: term(),
                 cs :: term(),
                 meta :: term(),
                 rand :: term(),
                 tag :: term(),
                 trace_func :: term(),
                 trace_data :: term(),
                 private :: term()}.

sandboxtable()

-type sandboxtable() :: [[atom()]].

Functions

init()

-spec init() -> LuaState when LuaState :: luerlstate().

Create a new Luerl state with the standard sandboxing.

init/1

-spec init(LuaState) -> LuaState when LuaState :: luerlstate();
          (TablePaths) -> LuaState when LuaState :: luerlstate(), TablePaths :: sandboxtable().

Take an existing Luerl state and run the default sandboxing on it. Or create a new Luerl state and run a sandboxing table on it.

init(LuaState, TablePaths)

-spec init(LuaState, TablePaths) -> LuaState when LuaState :: luerlstate(), TablePaths :: [[atom()]].

Take an existing Luerl state and run the TablePaths on it to control the sandboxing.

run(Expression)

-spec run(Expression) -> {Reply, LuaState}
             when
                 Expression :: string(),
                 LuaState :: luerlstate(),
                 Reply :: {ok, Result, LuaState} | Error,
                 Result :: luerldata(),
                 Error :: term().

Run the Lua expression controlled by the default ControlFlags in a new LuaState with the default sandboxing.

run(Expression, LuaState)

-spec run(Expression, LuaState) -> {Reply, LuaState}
             when
                 Expression :: string(),
                 LuaState :: luerlstate(),
                 Reply :: {ok, Result, LuaState} | Error,
                 Result :: luerldata(),
                 Error :: term().

Run the Lua expression controlled by the default ControlFlags in the LuaState.

run(Expression, ControlFlags, LuaState)

-spec run(Expression, ControlFlags, LuaState) -> {Reply, LuaState}
             when
                 Expression :: string(),
                 ControlFlags :: controlflags(),
                 LuaState :: luerlstate(),
                 Reply :: {ok, Result, LuaState} | Error,
                 Result :: luerldata(),
                 Error :: term().

Run the Lua expression controlled by the ControlFlags in the LuaState.

run(String, LuaState, MaxReds, SpawnOpts)

This function is deprecated. Use `run/3`.

run(String, LuaState, MaxReds, SpawnOpts, Timeout)

This function is deprecated. Use `run/3`.