runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Ignore invalid response header in HttpClient request

Open EricXiexm opened this issue 7 years ago • 39 comments

In .net standard 2.0 When i use System.Net.Http.HttpClient to request url data,throw an exception like: 图片1

The url run in web browser work well,but its response headers like: 图片2

The reponse headers is not standardized,in .net core it will throw an exception,but in framework4.5 work well.

Any solution for this in .net core.

EricXiexm avatar Jun 18 '19 05:06 EricXiexm

The reponse headers is not standardized,in .net core it will throw an exception,but in .NET Framework 4.5 works well.

cc: @dotnet/ncl @stephentoub

This looks like another case where we need to adjust SocketsHttpHandler behavior to ignore (with logging) malformed header lines and not reject the response entirely.

davidsh avatar Jun 21 '19 16:06 davidsh

I've just tested PlatformHandler/WinHTTP, it does not allow this. Will check Framework next.

scalablecory avatar Jun 22 '19 00:06 scalablecory

I can't replicate this in 4.7.2 either. It gives the following misleading error (CR is indeed followed by LF):

WebException: The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF

However, Chrome, Firefox, and Curl do allow it. I think it would be reasonable (and trivial) to allow it in SocketsHttpHandler. Thoughts, @davidsh @wfurt @stephentoub?

scalablecory avatar Jun 22 '19 08:06 scalablecory

WebException: The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF

I'm curious if the parsing will work if you add the 'UseUnsafeHeaderParsing' config item. It affects .NET Framework HttpWebRequest stack and in theory will affect HttpClient. You need to put this into the app.config for .NET Framework application.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
    </startup>
  <system.net>
    <settings>
      <httpWebRequest useUnsafeHeaderParsing ="true"/>
    </settings>
  </system.net>
</configuration>

https://docs.microsoft.com/en-us/dotnet/api/system.net.configuration.httpwebrequestelement.useunsafeheaderparsing?view=netframework-4.8

However, Chrome, Firefox, and Curl do allow it. I think it would be reasonable (and trivial) to allow it in SocketsHttpHandler. Thoughts, @davidsh @wfurt @stephentoub?

However, if this isn't a regression in .NET Core 3.0 compared with .NET Core 2.x and if this doesn't easily repro in .NET Framework without extra config settings, then this wouldn't meet the bar for a .NET Core 3.0 change.

Also, see related issue about malformed CR-LF sequences: dotnet/runtime#25319

davidsh avatar Jun 22 '19 14:06 davidsh

Works with useUnsafeHeaderParsing="true".

Does not work on 2.2. Moving to Future.

scalablecory avatar Jun 27 '19 21:06 scalablecory

Triage: We should create a new setting or something like that (likely not API). Bonus: Use same switch name as .NET Framework has. It will match .NET Framework and also browsers behavior (which @scalablecory tried).

In this specific case we should either ignore the line with date without header name (likely what .NET Framework does) or use the text prior to column ':' as key and the rest as value.

karelz avatar Oct 08 '19 20:10 karelz

Having a similar issue with a webservice where the response headers contain a header with a '=' instead of '-' in the header name.

Any possible workarounds in Core 2.X/3.0 until the useUnsafeHeaderParsing switch is implemented? Tried with both System.Net.WebRequest and System.Net.Http.HttpClient, and get the same error.

FullstackSensei avatar Oct 23 '19 09:10 FullstackSensei

I'm trying to connect to a State Server for storing data (yes, I know it's better to use SQL Server or Redis cache, but I can't install anything on the server or create tables on the SQL Server, and I need to survive application restarts), but the state server responds with an invalid header, it doesn't send the HTTP1.1 part, but just '200 OK'. Because of that, HttpClient throws an exception:

'Received an invalid status line: '200 OK'.'

So I need a way to ignore those errors. In the meantime I use TcpClient to send and receive the data, but I rather use HttpClient.

ChrisVanDijk avatar Mar 02 '20 10:03 ChrisVanDijk

@ChrisVanDijk that is not invalid header, that is invalid response format of the first line. This is not something covered by this issue.

Personally I don't think it is something we should be resilient to, unless we find out it is super-wide spread. Do you know what the server is? Is there a chance to fix it instead?

karelz avatar Mar 02 '20 17:03 karelz

@karelz it't the good old ASP.NET State Service, according to the documentation it should respond with a standard response format, but it doesn't. Maybe that's a bug, or they did it on purpose.

I don't think this happens a lot, and I hope nobody connects to the state service outside of the old ASP.NET world. I shouldn't use it either, but for this project I don't have another option. I've fixed it by using TcpClient, which works fine for cases like this.

ChrisVanDijk avatar Mar 03 '20 07:03 ChrisVanDijk

I have the same type of problem with a server which returns a field-name containing a separator charactor. The server is out of my control and it is a problem both in .NET 5 on Windows as well as using Xamarin on Android.

HermanEldering avatar Sep 24 '20 12:09 HermanEldering

Has anyone been able to resolve or workaround this problem?

I am having the same issue - connecting with a 3rd party API (which I have zero control over), and they are returning a custom header (in my case, a header called "Response Code"). This is causing the error "Received an invalid header line" error. In our older service using Framework we were able to get around this by using useUnsafeHeaderParsing, but this is not available in core/standard.

I've tried a number of things, including the below - but it seems they all use the same base validation logic, as I got the same error with all of the below:

  • HttpClient
  • WebClient
  • WebRequest
  • RestSharp

My next option, which I'm trying to avoid - is to create a proxy service written in .NET Framework, which will accept all response headers, remove invalid ones and then return the response to the main calling service.

jpseini avatar Jan 21 '21 14:01 jpseini

One possibility here is to use the new PlaintextStreamFilter callback to filter out/modify bad headers.

geoffkizer avatar Jan 21 '21 17:01 geoffkizer

One possibility here is to use the new PlaintextStreamFilter callback to filter out/modify bad headers.

Thanks for the suggestion. I'm still using 3.1 unfortunately, with little chance of being able to upgrade to 5.

jpseini avatar Jan 22 '21 06:01 jpseini

any update for this is very unlikely to make 3.x @jpseini. Servicing bar is pretty high.

wfurt avatar Jan 22 '21 07:01 wfurt

Any news on this? Is there at least a suggested workaround? My team is running into this as well with a server hosted by a public governmental agency so doubt we can request them to resolve the issue. A previous developer on the team wrote an entire proxy service using TCPClient to forward the requests back and forth, it works but only for a very specific scenario. Seems wise that you can better control the underlying SocketsHttpHandler

RiaanVR avatar Aug 04 '21 09:08 RiaanVR

@RiaanVR we do not have it too high on our list. Did you try the workaround mentioned above? https://github.com/dotnet/runtime/issues/29927#issuecomment-764820526

There were 6 reports in last 2 years, not super-common scenario ...

karelz avatar Aug 04 '21 10:08 karelz

@karelz does that depend on .NET 5 though? We currently have .NET 3.1 running

RiaanVR avatar Aug 04 '21 10:08 RiaanVR

Yes, it relies on 5.0 feature. Given the impact, it does not meet the bar for servicing 3.1 at this moment.

karelz avatar Aug 04 '21 11:08 karelz

I'm having the same problem trying to consume a public endpoint. I saw the suggestion of using PlaintextStreamFilter but i can't find any documentation or examples of how to use it 😞 Anyone has any pointers?

danielb7390 avatar Oct 09 '21 11:10 danielb7390