Script: add optional bounded Lua runtime adapter (#39)
This commit is contained in:
1 parent
b3c566ed4d
commit
ae7bedb755
7 files changed
+418
-58
No files matched your search
+66
-11
@@ -11,6 +11,11 @@
|
||||
namespace Void::Script
|
||||
{
|
||||
|
||||
// The adapter's public interface is always declared so consumers can compile
|
||||
// against it regardless of the build configuration. When Void is built without
|
||||
// Lua (`VOID_SCRIPT_HAS_LUA` undefined), `LuaEvaluator::evaluate` returns a
|
||||
// structured `Error` instead of running the chunk.
|
||||
|
||||
/// Host function: receives integer arguments, returns an integer or an error
|
||||
/// message. It must not commit externally visible state itself; the evaluator
|
||||
/// records each call in `Result::host_calls` so the caller can commit after a
|
||||
@@ -23,27 +28,78 @@ struct Host {
|
||||
HostFunction function;
|
||||
};
|
||||
|
||||
/// Standard Lua libraries the evaluator may open. Every library is opt-in
|
||||
/// through this explicit allow-list; `os`, `io`, `package` and `debug` are
|
||||
/// never opened, and even the base library has `load`, `loadfile`, `dofile`,
|
||||
/// `collectgarbage` and `print` removed so a script cannot reach the ambient
|
||||
/// filesystem, process, dynamic loader or host output.
|
||||
struct Bindings {
|
||||
bool base{true};
|
||||
bool table{true};
|
||||
bool string{true};
|
||||
bool math{true};
|
||||
bool coroutine{true};
|
||||
|
||||
[[nodiscard]] bool operator==(const Bindings&) const = default;
|
||||
};
|
||||
|
||||
/// Returns true when evaluation must stop. It is polled from the instruction
|
||||
/// hook and before every host call, so a long-running or host-heavy script is
|
||||
/// interrupted cooperatively. An exception thrown by the callback is treated
|
||||
/// as a cancellation request.
|
||||
using CancelFunction = std::function<bool()>;
|
||||
|
||||
/// Complete evaluator policy. The defaults are a small, safe allow-list: no
|
||||
/// host functions, no cancellation source, and only the hardened standard
|
||||
/// libraries selected by `Bindings`.
|
||||
struct Configuration {
|
||||
Limits limits{};
|
||||
Bindings bindings{};
|
||||
std::vector<Host> hosts{};
|
||||
CancelFunction cancelled{};
|
||||
};
|
||||
|
||||
/// Bounded Lua text evaluator.
|
||||
///
|
||||
/// Each `evaluate` call creates a fresh, isolated Lua state, applies the
|
||||
/// allocator, instruction and host-call limits, exposes the integer-keyed
|
||||
/// inputs as `input[key]`, registers the host functions as globals, and runs
|
||||
/// the chunk with a protected call. Only integer return values are accepted
|
||||
/// and collected into an owned result. On failure the state is discarded, so
|
||||
/// inputs and the caller's host state are untouched; a later valid evaluation
|
||||
/// starts clean.
|
||||
/// allocator, instruction, host-call and cancellation limits, exposes the
|
||||
/// integer-keyed inputs as `input[key]`, registers the host functions as
|
||||
/// globals, and runs the chunk with a protected call. Only integer return
|
||||
/// values are accepted and collected into an owned result. On failure the
|
||||
/// state is discarded, so inputs and the caller's host state are untouched; a
|
||||
/// later valid evaluation starts clean.
|
||||
///
|
||||
/// The Lua state alone is not a security boundary: run untrusted scripts in a
|
||||
/// separately isolated process. This evaluator only removes the obvious
|
||||
/// ambient capabilities and enforces resource budgets.
|
||||
class LuaEvaluator
|
||||
{
|
||||
public:
|
||||
|
||||
explicit LuaEvaluator(Limits limits = Limits{},
|
||||
std::vector<Host> hosts = {})
|
||||
: limits_(limits)
|
||||
, hosts_(std::move(hosts))
|
||||
: configuration_{
|
||||
.limits = limits,
|
||||
.bindings = Bindings{},
|
||||
.hosts = std::move(hosts),
|
||||
.cancelled = CancelFunction{}}
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] const Limits& limits() const noexcept { return limits_; }
|
||||
explicit LuaEvaluator(Configuration configuration)
|
||||
: configuration_(std::move(configuration))
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] const Limits& limits() const noexcept
|
||||
{
|
||||
return configuration_.limits;
|
||||
}
|
||||
|
||||
[[nodiscard]] const Bindings& bindings() const noexcept
|
||||
{
|
||||
return configuration_.bindings;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::expected<Result, Error>
|
||||
evaluate(std::string_view source,
|
||||
@@ -51,8 +107,7 @@ class LuaEvaluator
|
||||
|
||||
private:
|
||||
|
||||
Limits limits_;
|
||||
std::vector<Host> hosts_;
|
||||
Configuration configuration_;
|
||||
};
|
||||
|
||||
} // namespace Void::Script
|
||||
@@ -20,6 +20,7 @@ enum class ErrorKind : std::uint8_t {
|
||||
result_limit,
|
||||
host_error,
|
||||
invalid_input,
|
||||
cancelled,
|
||||
};
|
||||
|
||||
struct Error {
|
||||
|
||||
Reference in new issue
Block a user