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
@@ -66,7 +66,9 @@ The scoping rationale: `System.Diagnostics.Process` is the dangerous type (spawn
The following member names are blocked regardless of the receiver type, to prevent reflection-based bypasses such as `typeof(x).Assembly.GetType("System.IO.File")`:
`GetType`, `GetTypeInfo`, `Assembly`, `Module`, `CreateInstance`, `InvokeMember`, `GetMethod`, `GetMethods`, `GetConstructor`, `GetConstructors`, `GetField`, `GetFields`, `GetProperty`, `GetProperties`, `GetMember`, `GetMembers`, `GetRuntimeMethod`, `GetRuntimeMethods`, `MethodHandle`, `TypeHandle`, `GetTypes`, `EntryPoint`, `DeclaredMethods`, `DeclaredMembers`, `DeclaredConstructors`, `DynamicInvoke`.
`GetType`, `GetTypeInfo`, `Assembly`, `Module`, `CreateInstance`, `InvokeMember`, `GetMethod`, `GetMethods`, `GetConstructor`, `GetConstructors`, `GetField`, `GetFields`, `GetProperty`, `GetProperties`, `GetMember`, `GetMembers`, `GetRuntimeMethod`, `GetRuntimeMethods`, `MethodHandle`, `TypeHandle`, `GetTypes`, `EntryPoint`, `DeclaredMethods`, `DeclaredMembers`, `DeclaredConstructors`, `DynamicInvoke`, `CreateDelegate`.
The check applies to **every spelling** that reaches a member: a member access (`recv.GetMethod(...)`), a conditional member binding (`recv?.GetMethod(...)` — code review 2026-12-02 CMN-1: before this the `?.` form was not inspected at all, so one character defeated the gate), and a bare identifier that resolves to a framework member of that name (e.g. `CreateDelegate(...)` after `using static System.Delegate;`). `CreateDelegate` joined the list with CMN-1: `Delegate.CreateDelegate` turns a `MethodInfo` — obtainable from `System.Type`, which is not a forbidden scope — into a directly callable delegate without naming any `System.Reflection` member.
`Invoke` is **deliberately excluded** from this list: the syntactic pass rejects a gateway member regardless of receiver, and `delegate.Invoke()` (`Func<>`/`Action<>` invocation) is a legitimate, common script pattern. The reflection late-bind `MethodInfo.Invoke` is already caught semantically via the `System.Reflection` scope, so excluding `Invoke` here avoids a false-positive blast radius without weakening the reflection deny-list.
@@ -105,8 +107,8 @@ The two reference sets are deliberately distinct and must not be conflated:
**Pass 2 — syntactic reflection-gateway and identifier hardening (adapted from Inbound API)**
- Walks the syntax tree for member-access expressions and simple name references.
- Flags any member name found in `ScriptTrustPolicy.ReflectionGatewayMembers`, regardless of receiver type.
- Walks the syntax tree for member-access expressions, conditional member bindings (`?.`) and simple name references.
- Flags any member name found in `ScriptTrustPolicy.ReflectionGatewayMembers`, regardless of receiver type, in both the `.` and the `?.` form. (Pass 1 additionally flags a gateway-named framework member reached as a bare identifier, e.g. through `using static`.)
- Flags any identifier token found in `ScriptTrustPolicy.ForbiddenIdentifiers` (`dynamic`, `Activator`).
Violations from both passes are merged and deduplicated before being returned.