feat(deploy): #971 follow-up deploy-time script compile gate
The deploy gate never compiled scripts, so a stored script the #971 allow-list now refuses only failed after the deploy (VirtualTag/Calculation Bad "sandbox violation", scripted-alarm load failing for every alarm on the node). - Core.Scripting.Abstractions: IDeployScriptChecker (Roslyn-free) + request/result records; ScriptContextKind moved here (same namespace). - Core.Scripting: ScriptEvaluator.Verify shares a private BuildCheckedCompilation factory with CompileToImage (wrap, injection guard, sandbox compile, ForbiddenTypeAnalyzer; no stack-guard rewrite, no emit). RoslynDeployScriptChecker checks per seam (VT/Calc as VirtualTagContext->object?, passthrough/blank skipped; predicates DependencyExtractor first then AlarmPredicateContext->bool), caches by (compile identity, SHA-256), parallel, bounded. - ControlPlane: DeployScriptGate collects enabled VTs / enabled alarm predicates / Calculation tags, resolves {{equip}} through EquipmentReferenceMap exactly as the compose seams do (template compiled once when SubstitutionPreservesCompilation, else the resolved source), groups findings per script naming its users. AdminOperationsActor warms the cache before the snapshot transaction and runs the authoritative pass on the same snapshot; Deployment:ScriptCompileGateMode = Error (default) | Warn | Off; an absent checker or an internal failure never blocks. - Host binds RoslynDeployScriptChecker on admin nodes. - Docs: VirtualTags.md (gate + upgrade note), ScriptEditor.md, security.md, CLAUDE.md. Found while testing: {{equip}} substitution pastes RawPaths into C# literals unescaped, so a segment containing \ or " breaks (or silently alters) the script; the gate now reports it at deploy.
This commit is contained in:
@@ -82,6 +82,49 @@ public sealed class ScriptEvaluator<TContext, TResult> : IDisposable
|
||||
/// <exception cref="CompilationErrorException">The source does not compile.</exception>
|
||||
/// <exception cref="ScriptSandboxViolationException">The source uses a forbidden API.</exception>
|
||||
public static byte[] CompileToImage(string scriptSource)
|
||||
{
|
||||
var compilation = BuildCheckedCompilation(scriptSource, out var syntaxTree);
|
||||
|
||||
// Step 3a — stack-depth guard (lmxopcua#779). Insert ScriptStackGuard.Check() at the entry of the script
|
||||
// body and of every lambda / anonymous method / local function, so unbounded recursion fails the script with
|
||||
// a catchable exception instead of a StackOverflowException that terminates the server. After the analyzer,
|
||||
// so the analyzer and its diagnostics only ever see the user's code. The rewrite is designed to bind exactly
|
||||
// as the original did; an emit failure here is an internal defect, reported like any other emit failure.
|
||||
var guardedTree = ScriptStackGuardInjector.Inject(compilation, syntaxTree);
|
||||
compilation = compilation.ReplaceSyntaxTree(syntaxTree, guardedTree);
|
||||
|
||||
// Step 4 — emit to an in-memory PE stream and load into a collectible ALC.
|
||||
using var peStream = new MemoryStream();
|
||||
var emitResult = compilation.Emit(peStream);
|
||||
if (!emitResult.Success)
|
||||
{
|
||||
var emitErrors = emitResult.Diagnostics
|
||||
.Where(d => d.Severity == DiagnosticSeverity.Error)
|
||||
.ToArray();
|
||||
throw new CompilationErrorException(emitErrors);
|
||||
}
|
||||
|
||||
return peStream.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Every step of <see cref="CompileToImage"/> that can reject a USER script — wrap + parse, the
|
||||
/// wrapper-injection guard, the sandbox compile and <see cref="ForbiddenTypeAnalyzer"/> — with no stack-guard
|
||||
/// rewrite and no emit (those fail only on internal defects). The deploy-time script compile gate
|
||||
/// (lmxopcua#971 follow-up, <c>RoslynDeployScriptChecker</c>) calls this, and it builds its compilation
|
||||
/// through the SAME <see cref="BuildCheckedCompilation"/> the runtime compile does, so the gate accepts a
|
||||
/// source exactly when the runtime compile would.
|
||||
/// </summary>
|
||||
/// <param name="scriptSource">The user script source code to check.</param>
|
||||
/// <exception cref="CompilationErrorException">The source does not compile.</exception>
|
||||
/// <exception cref="ScriptSandboxViolationException">The source uses a forbidden API.</exception>
|
||||
public static void Verify(string scriptSource) => _ = BuildCheckedCompilation(scriptSource, out _);
|
||||
|
||||
/// <summary>
|
||||
/// Steps 1–3 of the compile pipeline, shared by <see cref="CompileToImage"/> (runtime) and
|
||||
/// <see cref="Verify"/> (deploy gate): returns the checked compilation and its wrapper tree, or throws.
|
||||
/// </summary>
|
||||
private static CSharpCompilation BuildCheckedCompilation(string scriptSource, out SyntaxTree syntaxTree)
|
||||
{
|
||||
if (scriptSource is null) throw new ArgumentNullException(nameof(scriptSource));
|
||||
|
||||
@@ -91,7 +134,7 @@ public sealed class ScriptEvaluator<TContext, TResult> : IDisposable
|
||||
// wrapper's `Run` method is what we invoke at runtime; the user's source is
|
||||
// pasted in as its body so explicit `return` semantics apply.
|
||||
var wrapperSource = BuildWrapperSource(scriptSource, sandbox.Imports);
|
||||
var syntaxTree = CSharpSyntaxTree.ParseText(wrapperSource);
|
||||
syntaxTree = CSharpSyntaxTree.ParseText(wrapperSource);
|
||||
|
||||
// Step 1a — defend against wrapper-source injection.
|
||||
// A script body of `return 0; } public static int Evil() { return 0;` would
|
||||
@@ -134,26 +177,7 @@ public sealed class ScriptEvaluator<TContext, TResult> : IDisposable
|
||||
if (rejections.Count > 0)
|
||||
throw new ScriptSandboxViolationException(rejections);
|
||||
|
||||
// Step 3a — stack-depth guard (lmxopcua#779). Insert ScriptStackGuard.Check() at the entry of the script
|
||||
// body and of every lambda / anonymous method / local function, so unbounded recursion fails the script with
|
||||
// a catchable exception instead of a StackOverflowException that terminates the server. After the analyzer,
|
||||
// so the analyzer and its diagnostics only ever see the user's code. The rewrite is designed to bind exactly
|
||||
// as the original did; an emit failure here is an internal defect, reported like any other emit failure.
|
||||
var guardedTree = ScriptStackGuardInjector.Inject(compilation, syntaxTree);
|
||||
compilation = compilation.ReplaceSyntaxTree(syntaxTree, guardedTree);
|
||||
|
||||
// Step 4 — emit to an in-memory PE stream and load into a collectible ALC.
|
||||
using var peStream = new MemoryStream();
|
||||
var emitResult = compilation.Emit(peStream);
|
||||
if (!emitResult.Success)
|
||||
{
|
||||
var emitErrors = emitResult.Diagnostics
|
||||
.Where(d => d.Severity == DiagnosticSeverity.Error)
|
||||
.ToArray();
|
||||
throw new CompilationErrorException(emitErrors);
|
||||
}
|
||||
|
||||
return peStream.ToArray();
|
||||
return compilation;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user