fix(scripting): #971 the script sandbox is an allow-list — the K-1 bypasses (CreateDelegate, Expressions, XML/Tar I/O, X509Store, Timers/BackgroundWorker/AsParallel, Serilog.Log) are refused

ForbiddenTypeAnalyzer inverted from a BCL deny-list to an allow-list of types/members (ctx API, value/time/maths
types, collections, LINQ-to-objects, string/Regex/formatting; System.Type and CancellationToken with member
allow-lists). Every named type, expression type and used member (incl. return type and generic args) is checked;
__makeref/__arglist, lock and extern local functions refused syntactically. ScriptSandbox references only eight BCL
assemblies (defence in depth). The Monaco editor and the publish gate share both, so parity holds.

Tests: theory over the K-1 snippets (publish gate + analyzer alone against the full host), every-public-type
enumeration, shipped-sample + idiom regression, editor/publish parity. Differential over every script-shaped literal
in src/tests: all 511 previously-compiling scripts still compile (one expression-tree test intentionally refused).
PE cache signing by the host (option 4) deferred as a follow-up.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-12-03 10:09:39 -05:00
parent 65fd71de7a
commit c00ebf1320
13 changed files with 1149 additions and 477 deletions
+17 -15
View File
@@ -162,20 +162,21 @@ user's fault.
### 2. ForbiddenTypeAnalyzer
`ForbiddenTypeAnalyzer.Analyze(compilation)` walks the wrapped syntax tree and
resolves every referenced symbol against the sandbox deny-list (namespace
prefixes `System.IO`, `System.Net`, `System.Diagnostics`, `System.Reflection`,
`System.Threading` — the whole namespace, with only `CancellationToken`,
`Interlocked` and `Volatile` allowed back in — `System.Threading.Tasks`,
`System.Runtime.InteropServices`, `System.Runtime.Loader`, `Microsoft.Win32`;
type-granular denials `System.Environment`, `System.AppDomain`, `System.GC`,
`System.Activator`, `System.Runtime.CompilerServices.Unsafe` /
`RuntimeHelpers`; and member-granular denials on `System.Type`'s by-name
reflection entry points — `GetType(string)`, `GetMethod(s)`, `InvokeMember`,
and the `GetProperty`/`GetField`/`GetMember`/`GetConstructor`/`GetEvent`
families). The runtime publish gate uses the same analyzer, so a deny-list
change needs a `DiagnoseTests` case to keep editor ⇔ publish parity. Each violation is mapped
via `tree.GetMappedLineSpan(span)` to user-source coordinates and emitted as an
error marker with code `OTSCRIPT_FORBIDDEN`.
checks every referenced symbol, expression type and invoked member against the
sandbox **allow-list** (lmxopcua#971): the `ctx` API, primitive / time / maths
value types, the collection namespaces, LINQ-to-objects (`Enumerable`), string /
`StringBuilder` / `Regex`, the formatting types of `System.Globalization`, and
`System.Type` / `CancellationToken` as values with a member allow-list. Anything
else is refused — see `docs/VirtualTags.md` §"The sandbox is an allow-list" for
the full list and the rules. Because the reference set (`ScriptSandbox.BclAssemblyNames`)
is narrowed too, an API whose assembly is not referenced (XML, archives,
networking, expression trees, the console, processes) shows as an ordinary
**compiler** error rather than `OTSCRIPT_FORBIDDEN`, and is no longer offered by
completion. The runtime publish gate uses the same analyzer and the same
reference set, so an allow-list change needs a `DiagnoseTests` case to keep
editor ⇔ publish parity (`Editor_and_publish_gate_agree`). Each violation is
mapped via `tree.GetMappedLineSpan(span)` to user-source coordinates and emitted
as an error marker with code `OTSCRIPT_FORBIDDEN`.
### Runtime safety (what happens after publish)
@@ -208,7 +209,8 @@ scripted-alarm predicates) runs scripts through the shared `ScriptRunGuard`:
`ctx.CancellationToken.ThrowIfCancellationRequested()`) ends its own abandoned run
and frees the slot, instead of holding a pool thread until it returns. A script that
ignores the token is still an orphan. The editor completes the member, and
`CancellationToken` is on the sandbox allow-list.
`CancellationToken` is on the sandbox allow-list — for polling only: `Register`
and `WaitHandle` are refused (a callback or a wait outlives the evaluation).
- **Allocation budget.** A single run allocating more than
`Scripting:MaxAllocBytesPerRun` (64 MB) is quarantined. This is sampled after the
run returns, so a run that OOMs the process first is not stopped — that needs an