Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ConstrainedLanguage mode: [pscustomobject] literals cannot be used, even though the type is white-listed, and an equivalent New-Object call succeeds #20767

Open
5 tasks done
mklement0 opened this issue Nov 23, 2023 · 4 comments
Labels
Needs-Triage The issue is new and needs to be triaged by a work group.

Comments

@mklement0
Copy link
Contributor

mklement0 commented Nov 23, 2023

Prerequisites

Steps to reproduce

# From a regular session (with language mode 'FullLanguage')
# CAVEAT: You won't be able to undo this in the same session.
$ExecutionContext.SessionState.LanguageMode = 'ConstrainedLanguage'
[pscustomobject] @{ foo = 1 }

Perhaps I'm missing something, but the fact that hashtable literals alone (without the [pscustomobject] "cast") work fine, and that [pscustomobject] is among the whitelisted types, as well as the fact that the equivalent New-Object pscustomobject -Property @{ foo = 1 } does work, leads me to believe that this is a bug (it isn't specific to 7.4.0 - I see it in 7.3.8 too, and even in Windows PowerShell).

Expected behavior

A [pscustomobject] instance should be constructed and output.

Actual behavior

An error occurs (see below).

Error details

Curiously, the error message depends on whether at least one such literal was defined earlier, while the session was still in FullLanguage mode:

  • In the latter case - note the confusing reference to restricted mode:
InvalidArgument: Cannot convert value "System.Collections.Specialized.OrderedDictionary" to type "System.Management.Automation.LanguagePrimitives+InternalPSCustomObject". 
Error: "Cannot convert hashtable to an object of the following type: System.Management.Automation.LanguagePrimitives+InternalPSCustomObject. 
Hashtable-to-Object conversion is not supported in restricted language mode or a Data section."
  • If no [pscustomobject] literal was defined before entering constrained mode:
InvalidArgument: Cannot convert value to type "System.Management.Automation.LanguagePrimitives+InternalPSCustomObject". 
Only core types are supported in this language mode.

Environment data

PowerShell 7.4.0

Visuals

No response

@nightroman
Copy link

@mklement0 Thank you for digging into this. One (side) question, please

leads me to believe that this is a bug (it isn't specific to 7.4.0 - I see it in 7.3.8 too, and even in_Windows PowerShell).

What exactly changed in 7.4.0 so that the problem became visible?

@mklement0
Copy link
Contributor Author

@nightroman, the only thing I'm personally aware of is the presumptive bug in the newly introduced audit mode for the system lockdown feature:

@no-identd
Copy link

no-identd commented Jan 13, 2024

See also MicrosoftDocs/PowerShell-Docs#6884 & #14071, found via https://stackoverflow.com/questions/64805592/automatically-retrieve-allowed-types-for-constrained-language-mode/64806919#comment114584908_64805592, itself found via https://www.reddit.com/r/PowerShell/comments/10zb95d/pscustomobject_doesnt_work_with_task_scheduler/

@nightroman @mklement0 I think the answers given in the two above issues should address some of the confusion about this voiced here, albeit I haven't had time to read them in detail yet

Stack Overflow
For my hobby project ConvertTo-Expression, I would like the output expression of my cmdlet (by default) compliant with the Constrained Language mode. For this, I might include a hardcoded list with

@mklement0
Copy link
Contributor Author

Thank you, @no-identd.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs-Triage The issue is new and needs to be triaged by a work group.
Projects
None yet
Development

No branches or pull requests

3 participants