PowerShell Constrained Language Mode: Unclear handling of dynamic .NET object properties
0 reputation · 05 Jun 2025, 09:26 UTC
0 reputation · 05 Jun 2025, 09:26 UTC
Enable scripts to access runtime‑generated properties on .NET objects while operating in Constrained Language Mode (CLM).
When a script attempts to read a property that is generated at runtime on a restricted object, the current CLM implementation raises a generic security error without specifying whether the failure is due to the property itself, the object type, or the AppLocker policy. This ambiguity hampers troubleshooting and limits the ability of administrators to fine‑tune permissions.
35275 reputation · 05 Jun 2025, 18:15 UTC
CLM does not support a per‑property allow‑list; its list is static per .NET type.
When a property is added at runtime (e.g., via Add-Member), CLM checks the property name against the static list for the object's base type. Because the name is not present, the check fails and the engine throws a generic security exception.
System.Management.Automation.MethodInvocationException (or AccessDenied) with a message that does not indicate whether the block came from the property, the type, or an AppLocker policy.LanguageSessionMode only selects the overall language mode (ConstrainedLanguage, FullLanguage, NoLanguage); it does not provide a per‑property allow‑list, so it cannot be used to enable only certain dynamic properties.$ExecutionContext.SessionState.LanguageMode$obj = [pscustomobject]@{}Add-Member -InputObject $obj -NotePropertyName DynamicProp -NotePropertyValue 42$obj.DynamicPropIf the script needs to access runtime‑generated properties while staying in CLM, expose those members through a wrapper object that has them defined at compile time, or relax the language mode to FullLanguage for the session (accepting the broader attack surface).
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.