From a9549fbc6e463892028a6db7d77b15358aef2904 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Thu, 3 Dec 2026 09:44:42 -0500 Subject: [PATCH] fix(script-analysis): forbid System.AppDomain in scripts (CMN-2, WP1 task 1.2) AppDomain's assembly-loading members (ExecuteAssembly[ByName], Load, CreateInstanceFrom[AndUnwrap], CreateInstanceAndUnwrap) were neither a forbidden scope nor exact gateway names, so a script could load and run an external assembly (even from a UNC path) in the host process. The whole type is now a forbidden scope, like System.Environment and System.GC. AppContext/Debugger were considered and left alone (no assembly-load surface). Tests: every AppDomain loading spelling (member access, ?., using static, alias) is flagged; the ban is exact-type. --- docs/requirements/Component-ScriptAnalysis.md | 3 ++- .../ScriptTrustPolicy.cs | 6 +++++ .../ScriptTrustValidatorTests.cs | 26 +++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/docs/requirements/Component-ScriptAnalysis.md b/docs/requirements/Component-ScriptAnalysis.md index 41e6c046..941bf8dd 100644 --- a/docs/requirements/Component-ScriptAnalysis.md +++ b/docs/requirements/Component-ScriptAnalysis.md @@ -43,6 +43,7 @@ The following namespace/type prefixes are forbidden in all scripts: | `Microsoft.Win32` | Win32 API access — forbidden entirely | | `System.Environment` | Whole type — forbidden entirely; exposes process control (`Exit`/`FailFast`), the host environment, and secrets via environment variables (e.g. `SCADABRIDGE_API_KEY`). Scripts that need a line break use `"\n"` instead of `Environment.NewLine`. | | `System.GC` | Whole type — forbidden entirely; GC control (`Collect`, `KeepAlive`, memory-pressure knobs) has no legitimate script use. | +| `System.AppDomain` | Whole type — forbidden entirely (code review 2026-12-02 CMN-2); its assembly-loading members (`ExecuteAssembly`, `ExecuteAssemblyByName`, `Load`, `CreateInstanceFrom[AndUnwrap]`, `CreateInstanceAndUnwrap`) load and run external code — a UNC path needs no local write — inside the host process. | | `Microsoft.Data` | Concrete ADO.NET provider namespace (`Microsoft.Data.SqlClient`, …) — forbidden; closes the arbitrary-host `new SqlConnection("Server=attacker")` channel | | `System.Data.SqlClient` | Concrete ADO.NET SQL Server provider — forbidden (see provider-namespace posture below) | | `System.Data.Odbc` | Concrete ADO.NET ODBC provider — forbidden | @@ -60,7 +61,7 @@ The following types are explicitly allowed despite falling within a forbidden na - `System.Threading.CancellationToken` — cooperative cancellation - `System.Threading.CancellationTokenSource` — cooperative cancellation -The scoping rationale: `System.Diagnostics.Process` is the dangerous type (spawns processes); `Stopwatch`, `Debug`, and `Activity` are harmless diagnostic utilities. Forbidding the whole `System.Diagnostics` namespace, as some earlier call sites did, was overly broad. In contrast, `System.Environment` and `System.GC` are forbidden as **whole types** because — unlike `System.Diagnostics` — they have no harmless members a script legitimately needs. +The scoping rationale: `System.Diagnostics.Process` is the dangerous type (spawns processes); `Stopwatch`, `Debug`, and `Activity` are harmless diagnostic utilities. Forbidding the whole `System.Diagnostics` namespace, as some earlier call sites did, was overly broad. In contrast, `System.Environment`, `System.GC` and `System.AppDomain` are forbidden as **whole types** because — unlike `System.Diagnostics` — they have no harmless members a script legitimately needs. #### Reflection-gateway members diff --git a/src/ZB.MOM.WW.ScadaBridge.ScriptAnalysis/ScriptTrustPolicy.cs b/src/ZB.MOM.WW.ScadaBridge.ScriptAnalysis/ScriptTrustPolicy.cs index 2c86a486..8ccb76f8 100644 --- a/src/ZB.MOM.WW.ScadaBridge.ScriptAnalysis/ScriptTrustPolicy.cs +++ b/src/ZB.MOM.WW.ScadaBridge.ScriptAnalysis/ScriptTrustPolicy.cs @@ -52,6 +52,12 @@ public static class ScriptTrustPolicy // Whole type — GC control (Collect, KeepAlive, memory pressure knobs) has // no legitimate script use. "System.GC", + // Whole type (code review 2026-12-02 CMN-2) — AppDomain's assembly-loading members + // (ExecuteAssembly[ByName], Load, CreateInstanceFrom[AndUnwrap], + // CreateInstanceAndUnwrap) pull external code — even a UNC path — into the host + // process. The exact-match gateway list cannot cover every overload name, and the + // type has no legitimate script use, so it is banned like Environment and GC. + "System.AppDomain", // Concrete ADO.NET provider namespaces. Scripts reach a DbConnection ONLY // via the Database helper, whose abstract System.Data.Common types stay // allowed; forbidding the concrete providers closes the arbitrary-host diff --git a/tests/ZB.MOM.WW.ScadaBridge.ScriptAnalysis.Tests/ScriptTrustValidatorTests.cs b/tests/ZB.MOM.WW.ScadaBridge.ScriptAnalysis.Tests/ScriptTrustValidatorTests.cs index fb18e08e..5fdab538 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.ScriptAnalysis.Tests/ScriptTrustValidatorTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.ScriptAnalysis.Tests/ScriptTrustValidatorTests.cs @@ -443,6 +443,32 @@ public class ScriptTrustValidatorTests [InlineData("var c = new Microsoft.Data.SqlClient.SqlConnection(\"Server=attacker\");")] public void FindViolations_Flags(string code) => Assert.NotEmpty(ScriptTrustValidator.FindViolations(code)); + // Code review 2026-12-02 CMN-2: System.AppDomain's assembly-loading members + // (ExecuteAssembly, Load, CreateInstanceFrom[AndUnwrap], ...) pulled external code + // into the host process with no validator objection. The whole type is now forbidden. + /// Verifies that every System.AppDomain assembly-loading spelling is flagged. + /// The script source expected to be flagged. + [Theory] + [InlineData("AppDomain.CurrentDomain.ExecuteAssembly(@\"\\\\host\\share\\x.dll\");")] + [InlineData("AppDomain.CurrentDomain.ExecuteAssemblyByName(\"x\");")] + [InlineData("var a = AppDomain.CurrentDomain.Load(\"x\");")] + [InlineData("var o = AppDomain.CurrentDomain.CreateInstanceFromAndUnwrap(\"x.dll\", \"T\");")] + [InlineData("var o = AppDomain.CurrentDomain.CreateInstanceAndUnwrap(\"x\", \"T\");")] + [InlineData("var o = System.AppDomain.CurrentDomain?.CreateInstanceFrom(\"x.dll\", \"T\");")] + [InlineData("var d = AppDomain.CurrentDomain; d.ExecuteAssembly(\"x.dll\");")] + [InlineData("using static System.AppDomain; var d = CurrentDomain;")] + [InlineData("using D = System.AppDomain; var d = D.CurrentDomain;")] + public void AppDomain_AssemblyLoading_IsFlagged(string code) => + Assert.NotEmpty(ScriptTrustValidator.FindViolations(code)); + + /// The AppDomain ban is exact-type: a lookalike name in the System namespace is not swept up. + [Fact] + public void AppDomainScope_IsExactType() + { + Assert.Contains("System.AppDomain", ScriptTrustPolicy.ForbiddenScopes); + Assert.Empty(ScriptTrustValidator.FindViolations("var s = \"AppDomain\".Length;")); + } + // Syntactic gateway closure (matters in TPA-degraded fallback mode): these // reflection-gateway members are rejected regardless of receiver. /// Verifies that the syntactic pass flags reflection-gateway members regardless of receiver.