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

invoke-webrequest and invoke-restmethod -headers parameter is more restrictive on PowerShell Core than on PowerShell Native #2895

Closed
nmbradford opened this Issue Dec 16, 2016 · 17 comments

Comments

Projects
None yet
9 participants
@nmbradford
Contributor

nmbradford commented Dec 16, 2016

Im working with an API that seems to not comply with the accepted standard for if-match header (value string must be enclosed in quotes), although this is apparently quite common and is accommodated in native PowerShell iwr/irm. PowerShell core behaves differently and rejects the header when building the request. Modifying the API (commercial product) to comply is not an option unfortunately.

Steps to reproduce

$headers = @{}
$headers.Add("if-match","12345")
Invoke-WebRequest -Uri "http://httpbin.org/headers" -Headers $headers

Expected behavior

Works on native PowerShell

Invoke-WebRequest -Uri "http://httpbin.org/headers" -Headers $headers
StatusCode        : 200                                                                                                                                                                                                                                                   
StatusDescription : OK                                                                                                                                                                                                                                                    
Content           : {                                                                                                                                                                                                                                                     
                      "headers": {                                                                                                                                                                                                                                        
                        "Host": "httpbin.org",                                                                                                                                                                                                                            
                        "If-Match": "12345",                                                                                                                                                                                                                          
                        "User-Agent": "Mozilla/5.0 (Windows NT; Windows NT 10.0; en-AU) WindowsPowerShell/5.1.14393.206"                                                                                                                                                  
                      }                                                                                                                                                                                                                                                   
                    }                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                          
RawContent        : HTTP/1.1 200 OK                                                                                                                                                                                                                                       
                    Connection: keep-alive                                                                                                                                                                                                                                
                    Access-Control-Allow-Origin: *                                                                                                                                                                                                                        
                    Access-Control-Allow-Credentials: true                                                                                                                                                                                                                
                    Content-Length: 182                                                                                                                                                                                                                                   
                    Content-Type: application/json                                                                                                                                                                                                                        
                    Date: Fri, 16 Dec 2016 05:06:38 GM...                                                                                                                                                                                                                 
Forms             : {}                                                                                                                                                                                                                                                    
Headers           : {[Connection, keep-alive], [Access-Control-Allow-Origin, *], [Access-Control-Allow-Credentials, true], [Content-Length, 182]...}                                                                                                                      
Images            : {}                                                                                                                                                                                                                                                    
InputFields       : {}                                                                                                                                                                                                                                                    
Links             : {}                                                                                                                                                                                                                                                    
ParsedHtml        : mshtml.HTMLDocumentClass                                                                                                                                                                                                                              
RawContentLength  : 182                                                                                                                                                                

Actual behavior

Fails on linux

Invoke-WebRequest -Uri "http://httpbin.org/headers" -Headers $headers
Invoke-WebRequest : The format of value '12345' is invalid.
At line:1 char:1
+ Invoke-WebRequest -Uri "http://httpbin.org/headers" -Headers $headers
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Invoke-WebRequest], FormatException
    + FullyQualifiedErrorId : System.FormatException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Note: enclosing the value in quotes allows the header to be accepted by iwr/irm, but the API I'm attempting to consume from PowerShell Core does not accept the value when enclosed in quotes.

Environment data

Name                           Value
----                           -----
PSVersion                      6.0.0-alpha
PSEdition                      Core
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   3.0.0.0
GitCommitId                    v6.0.0-alpha.14
CLRVersion
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

@joeyaiello joeyaiello referenced this issue Jan 25, 2017

Closed

PowerShell 6.0 Plan #3046

75 of 116 tasks complete

@SteveL-MSFT SteveL-MSFT added this to the 6.0.0-beta milestone Feb 28, 2017

@SteveL-MSFT SteveL-MSFT modified the milestones: 6.0.0-beta1, 6.0.0-beta2 Mar 16, 2017

@SteveL-MSFT SteveL-MSFT modified the milestones: 6.0.0-beta, 6.0.0, 6.0.0-HighPriority May 15, 2017

@BrucePay

This comment has been minimized.

Member

BrucePay commented May 24, 2017

This is an issue with the .NET core implementation of HTTP header parsing:

Message : The format of value '12345' is invalid.
Data : {}
InnerException :
TargetSite : System.Object ParseValue(System.String, System.Object, Int32 ByRef)
StackTrace : at System.Net.Http.Headers.HttpHeaderParser.ParseValue(String value, Object storeValue, Int32& index)
at System.Net.Http.Headers.HttpHeaders.ParseAndAddValue(String name, HeaderStoreItemInfo info, String value)
at System.Net.Http.Headers.HttpHeaders.Add(String name, String value)
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.GetRequest(Uri uri)
at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord()
at System.Management.Automation.Cmdlet.DoProcessRecord()
at System.Management.Automation.CommandProcessor.ProcessRecord()
341
HelpLink :
Source : System.Net.Http
HResult : -214623303

@dantraMSFT dantraMSFT self-assigned this Jun 8, 2017

@dantraMSFT

This comment has been minimized.

Contributor

dantraMSFT commented Jun 12, 2017

@nmbradford have you opened an issue with corefx?
Also, I'm a little confused by your statement Note: enclosing the value in quotes allows the header to be accepted by iwr/irm
Perhaps I'm misreading your statement but the syntax you illustrated does not result in a quoted string being passed to the underlying API.

@nmbradford

This comment has been minimized.

Contributor

nmbradford commented Jun 12, 2017

Correct - the syntax in my example does not contain quoted string as the header value, and demonstrates the different behaviour between the two platforms.

My comment about quoting the value is intended to provide more information, but is not represented in the example.

To be clear, if you do enclose the value in quotes on Core, an exception is not thrown (although that was not a useful workaround for me in this scenario as the API I was interacting with did not accept the value when quoted.)

I have not raised an issue with corefx.

@dantraMSFT

This comment has been minimized.

Contributor

dantraMSFT commented Jun 12, 2017

@nmbradford At this point, I think that's the only viable path. The code path I'm seeing goes through ParseMultipleEntityTags in GenericHeaderParser.cs. Either that code path would need to be more forgiving or a separate if-match parser would be needed. I don't see how we can work around it in PowerShell.

@nmbradford

This comment has been minimized.

Contributor

nmbradford commented Jun 13, 2017

yes - that's my point, on Windows it is more forgiving :). I realise the issue is in corefx (and Ive already worked around it by building the webrequest myself rather than using iwr), but to users of Powershell Core, it still represents a problem.

@SteveL-MSFT

This comment has been minimized.

Member

SteveL-MSFT commented Jun 13, 2017

@dantraMSFT can you open an issue in corefx and link it to this?

@dantraMSFT

This comment has been minimized.

Contributor

dantraMSFT commented Jun 13, 2017

Will do, Steve.
@nmbradford Would you mind providing a couple of example header values and the details of what you are doing to work around it. I'd like to be specific when I create the issue.
Thanks.

@nmbradford

This comment has been minimized.

Contributor

nmbradford commented Jun 13, 2017

no prob. My initial post has steps to repro for the failure scenario. For the header to be accepted on Core, try this (with quoted string):

$headers["if-match"] = "`"12345`""
Invoke-WebRequest -Uri "http://httpbin.org/headers" -Headers $headers

In order to work around it, I'm not using IWR at all any more, instead constructing the httpclient request object directly and using the DefaultRequestHeaders.TryAddWithoutValidation() method rather than the DefaultRequestHeaders.add() method that IWR is using.

@dantraMSFT

This comment has been minimized.

Contributor

dantraMSFT commented Jun 13, 2017

I've created an issue in the CoreFX repo: dotnet/corefx#20994

@SteveL-MSFT

This comment has been minimized.

Member

SteveL-MSFT commented Jun 13, 2017

Perhaps another way to solve this would be to expose a -SkipHeaderValidation switch? cc @joeyaiello

@dantraMSFT

This comment has been minimized.

Contributor

dantraMSFT commented Jun 21, 2017

@joeyaiello are you fine with Steve's suggestion or do you have other ideas?

@joeyaiello

This comment has been minimized.

Member

joeyaiello commented Jun 21, 2017

I would go with -SkipHeaderValidation, yeah. ( @SteveL-MSFT changed it from -NoHeaderValidation 😉 )

@nmbradford

This comment has been minimized.

Contributor

nmbradford commented Jun 22, 2017

Agree - this would be an easier workaround, but then iwr and irm would behave differently on Windows/non windows platforms (meaning scripts would not be portable without platform detection)?

@SteveL-MSFT

This comment has been minimized.

Member

SteveL-MSFT commented Jun 22, 2017

@nmbradford it would behave the same on Windows and Linux as long as you're using PSCore6. We have already added new capabilities to the webcmdlets that won't be in Windows PowerShell 5.1

@jimwAtMs

This comment has been minimized.

jimwAtMs commented Jan 24, 2018

I assume running 6.0 release has this fix, but passing in empty header gets a format exception, but on PS 5 it does not.

This works on 5
Invoke-WebRequest -Uri $uri -Method Post -Headers @{Authorization=""} @params

On 6 it gets this error. Removing the Authorization header gets around it.
Invoke-WebRequest : The format of value '' is invalid.

@markekraus

This comment has been minimized.

Collaborator

markekraus commented Jan 24, 2018

@jimwAtMs PowerShell Core now has strict header parsing on by default. Any headers passed to -Headers that are standards defined (such as Authorization) will result in a format error if the value is not standards compliant. You can bypass header validation with -SkipHeaderValidation

https://github.com/PowerShell/PowerShell/blob/master/docs/BREAKINGCHANGES.md#changes-to-web-cmdlets

More info: https://get-powershellblog.blogspot.com/2017/11/powershell-core-web-cmdlets-in-depth.html#L08

@jimwAtMs

This comment has been minimized.

jimwAtMs commented Jan 24, 2018

Thanks @markekraus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment