Powershell: 馃 Exception swallowed when string is longer than 241 characters

Created on 4 May 2020  路  14Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

$bearer = "." * 242
Invoke-RestMethod -Uri "https://httpbin.org/bearer" -Authentication Bearer -Token $bearer

Expected behavior

It throws an exception when string is 241 characters or less

Cannot bind parameter 'Token'. Cannot convert the value of type "System.String" to type "System.Security.SecureString".

Actual behavior

No exception thrown

Process finished with exit code 0.

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.0
PSEdition                      Core
GitCommitId                    7.0.0
OS                             Microsoft Windows 10.0.18362
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Issue-Bug Resolution-Duplicate WG-Engine

All 14 comments

@prcdpr I don't understand your issue. Could you explain more ?

Token parameter take a SecureString and not String :

$bearer = "." * 242
Invoke-RestMethod -Uri "https://httpbin.org/bearer" -Authentication Bearer -Token ( ConvertTo-SecureString -String $bearer -AsPlainText  )

Then this code work too :

$bearer = "." * 5
Invoke-RestMethod -Uri "https://httpbin.org/bearer" -Authentication Bearer -Token ( ConvertTo-SecureString -String $bearer -AsPlainText  )

I know this.

The issue I'm pointing out is that PowerShell behaves differently and doesn't throw an exception when you supply the long string by mistake instead of SecureString. Today I spent 15 minutes investigating why is my PowerShell code simply terminating without exception.

With trial and error I found out that it's possible to get an exception using try catch block, but when the supplied string is shorter it actually throws a meaningful error instead of silently terminating.

Following example

$bearer = "." * 241
Invoke-RestMethod -Uri "https://httpbin.org/bearer" -Authentication Bearer -Token $bearer

throws an exception

Cannot bind parameter 'Token'. Cannot convert the value of type "System.String" to type "System.Security.SecureString".

However this code

$bearer = "." * 242
Invoke-RestMethod -Uri "https://httpbin.org/bearer" -Authentication Bearer -Token $bearer

terminates silently without any error and with 0 exit code

@prcdpr on my machine, this code throw an error :

$bearer = "." * 242
Invoke-RestMethod -Uri "https://httpbin.org/bearer" -Authentication Bearer -Token $bearer

Invoke-RestMethod: Cannot bind parameter 'Token'. Cannot convert the ".................................................................................................................................................................................................................................................." value of type "System.String" to type "System.Security.SecureString".

I can't reproduce

Name                           Value
----                           -----
PSVersion                      7.0.0
PSEdition                      Core
GitCommitId                    7.0.0
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Could you try without loading your profile ?
pwsh -NoProfile

@fMichaleczek can you try putting repro code inside file repro.ps1 and running

pwsh.exe .\repro.ps1

Please try different string lengths from 1-1000 (for example every hundred)

@prcdpr Understood. Nothing relate your code, more how pwsh.exe works.

You should read this :
https://stackoverflow.com/questions/36943318/how-to-get-the-error-code-errorlevel-from-powershell-into-windows-command-prom

try { 
    $bearer = "." * 242
    Invoke-RestMethod -Uri "https://httpbin.org/bearer" -Authentication Bearer -Token $bearer
    ...
} 
catch {
    exit 22
}

Nope, it's not that 馃槃

Sorry I was not clear enough. Please have look at this recording.

whatisgoingonfdgfg

It's a bug relative to the new concise ErrorView

I can't reproduce if I add $ErrorView = 'NormalView' on top of the script.

/cc @SteveL-MSFT

@prcdpr It's an bug of display, note that in all cases, ERRORLEVEL is always 0. You should handle it, see the stackoverflow link.

Duplicate of #12191?

Yeah, it is. I'll close this one in favor of that since that one's older. Feel free to continue the discussion over in #12191 folks, and don't hesitate to add any missing information from here into that issue thread. Thanks! 馃槉

@vexx32 Maybe it's just semantics but the other issue only has the _Issue-Question_ label while this had _Area-Engine_ and _Issue-Bug_. Do they get different priority or something?

Was this page helpful?
0 / 5 - 0 ratings