question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

In RestrictedLanguage mode, an instance of a given type cannot be created with a cast, but can be created with New-Object

See original GitHub issue

Note: I may have a misconception here, but the difference in behavior strikes me as unusual.

Steps to reproduce

try {

  $ps = [powershell]::Create()
  $ps.Runspace.SessionStateProxy.LanguageMode = 'Restricted'

  try {
    $ps.AddScript('[regex] "."').Invoke()
    $ps.Streams.Error | Write-Warning
  }
  catch { Write-Warning $_ }

  $ps.Commands.Clear(); $ps.Streams.ClearStreams()

  try {
    $ps.AddScript('New-Object regex "."').Invoke()
    $ps.Streams.Error | Write-Warning
  }
  catch { Write-Warning $_ }

}
finally { $ps.Dispose() }

Expected behavior

Both attempts to create a [regex] instance should fail, because it appears that [regex] is not on an apparent, but undocumented whitelist of permitted types (the whitelist is only documented for ConstrainedLanguage); by contrast, a type such as [int] can be constructed successfully with either method ([int] 0 and New-Object int).

Actual behavior

The cast method fails, whereas the New-Object method succeeds:

WARNING: Exception calling "Invoke" with "0" argument(s): "At line:1 char:1
+ [regex] "."
+ ~~~~~~
+ The type regex is not allowed in restricted language mode or a Data section."

Options RightToLeft MatchTimeout
------- ----------- ------------
   None       False -00:00:00.0010000

Environment data

PowerShell Core 7.1.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:23 (15 by maintainers)

github_iconTop GitHub Comments

4reactions
mklement0commented, Mar 8, 2021

@TravisEz13

This is not a security issue because restricted language mode does not make a security promise

Aren’t language modes all about security? If not, what purpose do they serve? Are only some modes security-relevant? If so, which? The linked about_Language_Modes conceptual help topic doesn’t provide any clues.

This is a bug because new-object will allow you to create a type but a cast will not allow the same time.

So what is the resolution here?

  • if you cannot cast to a type, don’t allow New-Object to instantiate it either?
  • if New-Object allows you to in instantiate a type, casts should allow it too?
  • What is the list of approved types in RestrictedLanguage mode, given that the error message The type regex is not allowed in restricted language mode or a Data section. implies that there is one?
4reactions
iSazonovcommented, Nov 13, 2020

I set Issue-Bug and WG-Security until team triages this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

about Language Modes - PowerShell
In RestrictedLanguage mode, users can run commands (cmdlets, functions, CIM commands, and workflows), but can't use script blocks.
Read more >
PowerShell New-Object blocked on some Constrained ...
Hi all, I have a PowerShell script that is being distributed by Intune to a number of PCs. The script basically creates a...
Read more >
Automatically retrieve Allowed Types for Constrained ...
Is there a way to do this? Or: How can I check (in full language mode) if a specific type is compliant with...
Read more >
A Deep Dive into PowerShell's Constrained Language Mode
Cmdlet parameters that convert string input to types work only when the resulting type is an allowed type. The ToString() method and the...
Read more >
"Only Core Types are supported in this language mode" error
The ConstrainedLanguage mode permits all cmdlets and all PowerShell language elements, but it limits permitted types.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found