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
@@ -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.
/// <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
// reflection-gateway members are rejected regardless of receiver.
/// <summary>Verifies that the syntactic pass flags reflection-gateway members regardless of receiver.</summary>