Path: redb-route/redb.Route/Expressions/ExpressionSandbox.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Expressions/ExpressionSandbox.cs
----
namespace redb.Route.Expressions;
///
/// Restricts what an expression may do while the scope is open: no method invocation through
/// reflection on the objects an expression reaches. Built-in functions and the string helpers
/// (contains, substring, length, ...) stay available. Opened by hosts that
/// evaluate expressions written as data — a payload template's expr() and its arguments —
/// so a template can read members but never run code. Flows with the async context and nests.
///
public static class ExpressionSandbox
{
private static readonly AsyncLocal Depth = new();
/// Whether a sandbox scope is open on the current flow.
public static bool IsActive => Depth.Value > 0;
/// Opens a scope; dispose the result to leave it.
public static IDisposable Enter()
{
Depth.Value++;
return new Scope();
}
private sealed class Scope : IDisposable
{
private bool _left;
public void Dispose()
{
if (_left) return;
_left = true;
Depth.Value--;
}
}
}