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

HttpRequestMessage does not respect TryAddWithoutValidation when it deals with User Agent. #51583

Closed
emorell96 opened this issue Apr 20, 2021 · 7 comments

Comments

@emorell96
Copy link

emorell96 commented Apr 20, 2021

Description

I am using .Net 5 right now, and I am trying to send a request to an API.

I am setting the User Agent using the TryAddWithoutValidation method since I need to use a non compliant User Agent like "My App / 1.0.5" (with the spaces).

Yet when I check the request on Fiddler I see that .Net removes my spaces between App and / and / and 1.0.5.

var requestMessage = new HttpRequestMessage(HttpMethod.Get, "https://test.com");
requestMessage.Headers.TryAddWithoutValidation("User-Agent", "My Android App / 1.0.5");

And you will see in Fiddler it appears as User-Agent: "My Android App/1.0.5" (without the spaces) causing a 500 error in my case.

I thought the whole idea of TryAddWithoutValidation was that it would leave my string values alone and just send what I told it to send.

Configuration

.net 5.0 in Windows 10 x64

Regression?

As far as I know this seems to be a recurrent issue in .net core: #35335
#21131

Other information

I have tried using ParseAdd of the UserAgent, and using the default headers of the HttpClient object, all the same result. My user agent gets parsed and transformed. Any help would be appreciated since I cannot communicate with the API unless this gets fixed.

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Net.Http untriaged New issue has not been triaged by the area owner labels Apr 20, 2021
@ghost
Copy link

ghost commented Apr 20, 2021

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

I am using .Net 5 right now, and I am trying to send a request to an API.

I am setting the User Agent using the TryAddWithoutValidation method since I need to use a non compliant User Agent like "My App / 1.0.5" (with the spaces).

Yet when I check the request on Fiddler I see that .Net removes my spaces between App and / and / and 1.0.5.

requestMessage.Headers.TryAddWithoutValidation("User-Agent", "My Android App / 1.0.5");

And you will see in Fiddler it appears as User-Agent: "My Android App/1.0.5" (without the spaces) causing a 500 error in my case.

I thought the whole idea of TryAddWithoutValidation was that it would leave my string values alone and just send what I told it to send.

Configuration

.net 5.0 in Windows 10 x64

Regression?

As far as I know this seems to be a recurrent issue in .net core: #35335
#21131

Other information

I have tried using ParseAdd of the UserAgent, and using the default headers of the HttpClient object, all the same result. My user agent gets parsed and transformed. Any help would be appreciated since I cannot communicate with the API unless this gets fixed.

Author: emorell96
Assignees: -
Labels:

area-System.Net.Http, untriaged

Milestone: -

@ManickaP ManickaP removed the untriaged New issue has not been triaged by the area owner label Apr 22, 2021
@ManickaP ManickaP added this to the 6.0.0 milestone Apr 22, 2021
@ManickaP
Copy link
Member

Triage: we should at least try to investigate for 6.0.

@emorell96
Copy link
Author

I posted this in StackOverflow and someone else experienced it as well: SO
I investigated a little and it seems like the .net still parses the user agent and when it sees "App / 1.0.22" it translates that into a ProductHeaderValue object with Name = "App" and Version ="1.022" without spaces.

@geoffkizer
Copy link
Contributor

Fiddler is acting as a proxy here, right?

There's some code to propagate the User-Agent header on proxy tunnel creation here: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/HttpConnectionPool.cs#L1460

That code is causing the User-Agent to get parsed, which means we will reformat it when we send it. We should change the code to not force parsing of User-Agent here.

That said, if you are not using a proxy (or not using HTTPS) then this shouldn't happen.

@emorell96
Copy link
Author

Fiddler is a proxy to check the requests but it is not set on code but on Windows. I don't know if that would change anything. All I can say is that I know the user agent gets parsed because the API blocks my calls, and I viewed the requests through Fiddler to confirm this. You are welcome to investigate this:

var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("user-agent",    "my android app / 5.23.0");

var url = "https://google.com";
var requestMessage = new HttpRequestMessage(HttpMethod.Get, url);
var response = await httpClient.SendAsync(requestMessage);

and see what happens.

@geoffkizer
Copy link
Contributor

If you are using DefaultRequestHeaders to set the header value then you'll also hit this issue: #49463

@karelz karelz added the bug label May 4, 2021
@karelz
Copy link
Member

karelz commented May 14, 2021

Fixed by #49673

I verified that it works fine on latest 6.0 daily build.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants