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

HttpClient does not automatically follow https to http redirects #108588

Open
jordinl opened this issue Oct 7, 2024 · 4 comments · May be fixed by dotnet/dotnet-api-docs#11110
Open

HttpClient does not automatically follow https to http redirects #108588

jordinl opened this issue Oct 7, 2024 · 4 comments · May be fixed by dotnet/dotnet-api-docs#11110
Labels
area-System.Net.Http documentation Documentation bug or enhancement, does not impact product or test code help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@jordinl
Copy link

jordinl commented Oct 7, 2024

Description

https to http are not automatically followed even though the documentation states they should: https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclienthandler.allowautoredirect?view=net-8.0#remarks

Reproduction Steps

Clone this repo: https://github.com/jordinl/http-client-redirect-bug and execute it.

Expected behavior

Fetching https://verizon.com should return 200 status code and response URL https://www.verizon.com/

Actual behavior

Fetching https://verizon.com returns 301 status code and location header: http://verizon.com/

Regression?

No response

Known Workarounds

No response

Configuration

  • .NET 8 and 9
  • OS: Linux 6.10 / Fedora 39
  • Arch: x86_64

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Oct 7, 2024
@filipnavara
Copy link
Member

The documentation you linked has this big purple note box:

With AllowAutoRedirect set to true, the .NET Framework will follow redirections even when being redirected to an HTTP URI from an HTTPS URI. .NET Core versions 1.0, 1.1 and 2.0 will not follow a redirection from HTTPS to HTTP even if AllowAutoRedirect is set to true.

While it doesn't explicitly state anything about .NET Core > 2.0 I would assume the same restriction still applies and the documentation was just not updated.

@antonfirsov antonfirsov added the documentation Documentation bug or enhancement, does not impact product or test code label Oct 7, 2024
@antonfirsov antonfirsov added this to the Future milestone Oct 7, 2024
@antonfirsov antonfirsov removed the untriaged New issue has not been triaged by the area owner label Oct 7, 2024
@antonfirsov
Copy link
Member

Triage: this intended behavior, and won't be changed. We should the AllowAutoRedirect docs.

@jordinl
Copy link
Author

jordinl commented Oct 7, 2024

Coming from other programming languages, I find not allowing https to http redirects surprising. And if I wanted to not allow those I would choose to raise an exception when that happened.

Most languages handle those just fine:

Node.js

node -e '
const response = await fetch("https://verizon.com");
console.log(response.status, response.url);
'
#=> 200 https://www.verizon.com/

Python

python -c '
import requests
response = requests.get("https://verizon.com")
print(response.status_code, response.url)
'
#=> 200 https://www.verizon.com

Curl

curl -L https://verizon.com -o /dev/null -s -w '%{http_code} %{url_effective}\n'
#=> 200 https://www.verizon.com/

Java

jshell -s - <<EOF
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.URI;
var client = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.ALWAYS).build();
var request = HttpRequest.newBuilder().uri(new URI("https://verizon.com")).GET().build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("\n" +response.statusCode() + " " + response.uri());
EOF

#=> 200 https://www.verizon.com

Ruby

By default it raises an exception, which is how I would handle it if I didn't want to allow the redirects

ruby -e '
require "open-uri"
response = URI.open("https://verizon.com")
puts response.status, response.base_uri
'

/home/jordi/.rubies/ruby-3.1.3/lib/ruby/3.1.0/open-uri.rb:225:in `open_loop': redirection forbidden: https://verizon.com -> http://verizon.com/ (RuntimeError)
	from /home/jordi/.rubies/ruby-3.1.3/lib/ruby/3.1.0/open-uri.rb:151:in `open_uri'
	from /home/jordi/.rubies/ruby-3.1.3/lib/ruby/3.1.0/open-uri.rb:721:in `open'
	from /home/jordi/.rubies/ruby-3.1.3/lib/ruby/3.1.0/open-uri.rb:29:in `open'
	from -e:3:in `<main>'

But using a third-party gem allows the redirect:

gem install httparty
ruby -e '
require "httparty"
response = HTTParty.get("https://verizon.com")
puts "#{response.code} #{response.request.last_uri}"
'
#=> 200 https://www.verizon.com/

I understand not wanting to change the default for backwards compatibility, but I think it would be nice to add a flag so https to http redirects could be followed. Apparently this came up a couple times already:

#23801
#28039

@MihaZupan MihaZupan added the help wanted [up-for-grabs] Good issue for external contributors label Oct 23, 2024
@MihaZupan
Copy link
Member

Adding APIs to make opting-in to such redirects easier is tracked by #45364

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Net.Http documentation Documentation bug or enhancement, does not impact product or test code help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants