fix(script-analysis): inspect ?. member bindings and CreateDelegate in the trust gate (CMN-1, WP1 task 1.1)

The reflection-gateway check ran only in VisitMemberAccessExpression, so
recv?.GetMethod(...) (a MemberBindingExpression) was never inspected and
Delegate.CreateDelegate turned the MethodInfo into a callable delegate:
one character defeated the trust gate on all four call sites.

- HardeningWalker gains VisitMemberBindingExpression with the same check.
- CreateDelegate joins ReflectionGatewayMembers.
- Pass 1 also flags a gateway-named framework member reached as a bare
  identifier (using static System.Delegate / System.Type).

Tests: the review's probe script (compiles clean, now rejected, also on
the minimal fallback set), the ?. variant of every .-form rejection test,
every gateway member via ?., and legitimate ?. use stays clean.
This commit is contained in:
Joseph Doherty
2026-12-03 09:43:55 -05:00
parent f7e4f5b4ed
commit 29192eeb44
4 changed files with 228 additions and 3 deletions
@@ -111,6 +111,11 @@ public static class ScriptTrustPolicy
"DeclaredMembers",
"DeclaredConstructors",
"DynamicInvoke",
// Code review 2026-12-02 CMN-1: Delegate.CreateDelegate / MethodInfo.CreateDelegate
// turn a MethodInfo (obtainable from System.Type, which is not a forbidden scope)
// into a directly-callable delegate, so a forbidden method can be invoked without
// ever naming a System.Reflection member. No legitimate script use.
"CreateDelegate",
// DELIBERATELY EXCLUDED: "Invoke". The syntactic pass rejects a gateway
// member regardless of receiver, and delegate.Invoke() is a legitimate,
// common script pattern (Func<>/Action<> invocation). MethodInfo.Invoke
@@ -163,6 +163,20 @@ public static class ScriptTrustValidator
var info = model.GetSymbolInfo(node);
var symbol = info.Symbol ?? info.CandidateSymbols.FirstOrDefault();
// Code review 2026-12-02 CMN-1: a reflection-gateway member reached WITHOUT a
// member-access spelling — a bare identifier brought in by `using static
// System.Delegate;` / `using static System.Type;` — never reaches the syntactic
// walker's member-access/member-binding checks. Judge it by the resolved
// symbol's name instead. Only framework (metadata) members count, so a
// script-declared helper that happens to share a gateway name is not flagged.
// The message matches the syntactic pass's so a `.`-spelled use dedupes.
if (symbol is IMethodSymbol or IPropertySymbol or IFieldSymbol or IEventSymbol
&& ScriptTrustPolicy.ReflectionGatewayMembers.Contains(symbol.Name)
&& symbol.Locations.All(l => l.IsInMetadata))
{
violations.Add($"forbidden reflection member access '.{symbol.Name}'");
}
// The set of fully-qualified scopes this reference touches: the
// resolved symbol's containing namespace and type, or — when the
// symbol could not be resolved (a type from an unreferenced
@@ -423,6 +437,24 @@ public static class ScriptTrustValidator
base.VisitMemberAccessExpression(node);
}
/// <summary>
/// Code review 2026-12-02 CMN-1: the member on the right of a conditional access
/// (<c>recv?.GetMethod(...)</c>) is a <see cref="MemberBindingExpressionSyntax"/>, not
/// a <see cref="MemberAccessExpressionSyntax"/>, so it never reached
/// <see cref="VisitMemberAccessExpression"/>. Apply the same receiver-agnostic
/// gateway check to it. (A <c>?.</c> chain is a conditional access whose
/// <c>WhenNotNull</c> part holds the bindings, so every link is visited.)
/// </summary>
/// <param name="node">The member-binding node.</param>
public override void VisitMemberBindingExpression(MemberBindingExpressionSyntax node)
{
var memberName = node.Name.Identifier.ValueText;
if (ScriptTrustPolicy.ReflectionGatewayMembers.Contains(memberName))
_violations.Add($"forbidden reflection member access '.{memberName}'");
base.VisitMemberBindingExpression(node);
}
/// <inheritdoc />
public override void VisitIdentifierName(IdentifierNameSyntax node)
{