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

http: on redirect, headers are copied to new request #45410

Closed
fdietze opened this issue Mar 22, 2021 · 8 comments
Closed

http: on redirect, headers are copied to new request #45410

fdietze opened this issue Mar 22, 2021 · 8 comments
Assignees
Labels
area-vm Use area-vm for VM related issues, including code coverage, FFI, and the AOT and JIT backends. library-_http

Comments

@fdietze
Copy link

fdietze commented Mar 22, 2021

On a HTTP 302 redirect, the headers of the original request are copied over to the new request.
AFAIK, copying headers in this case is wrong.

https://github.com/dart-lang/sdk/blob/master/sdk/lib/_http/http_impl.dart#L2493-L2498

This is causing problems in my application. My url works well with curl -L but does not with dart http.

@vsmenon vsmenon added library-_http area-vm Use area-vm for VM related issues, including code coverage, FFI, and the AOT and JIT backends. labels Mar 22, 2021
@aam
Copy link
Contributor

aam commented Mar 22, 2021

Thanks for the report.
What kind of request is being redirected (GET/HEAD/...)?
My understanding is that new request should point to new URL but otherwise be identical to the original one.
Can you provide a little more details as to why you think copying headers is wrong?

@fdietze
Copy link
Author

fdietze commented Mar 22, 2021

In my case, it's a GET request.

The first http server checks my Authorization header and generates a pre-signed url to download a file from another host. When the Authorization header is also attached to the second request, the second request fails, because two auth mechanisms are provided at the same time.

It works fine with curl and http, but not with the dart http client. I tested it with 303 and 302 HTTP status codes.

$ http -v GET https://myfirsthost/file.mp3 "Authorization:Bearer $TOKEN" --follow
GET /file.mp3 HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Authorization: Bearer TOKEN
Connection: keep-alive
Host: myfirsthost
User-Agent: HTTPie/2.4.0



HTTP/1.1 303 See Other
Apigw-Requestid: xxxxxxxxxxx
Connection: keep-alive
Content-Length: 0
Date: Mon, 22 Mar 2021 15:43:30 GMT
location: https://mysecondhost/file.mp3?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=[.......]&X-Amz-SignedHeaders=host



GET /file.mp3?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=[......]&X-Amz-SignedHeaders=host HTTP/1.1
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: mysecondhost
User-Agent: HTTPie/2.4.0



HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 9088353
Content-Type: audio/mp3
Date: Mon, 22 Mar 2021 15:43:31 GMT
ETag: "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
Last-Modified: Fri, 19 Mar 2021 12:25:30 GMT
Server: AmazonS3
x-amz-id-2: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x-amz-request-id: xxxxxxxxxxxxxxxx
x-amz-server-side-encryption: aws:kms
x-amz-server-side-encryption-aws-kms-key-id: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx



+-----------------------------------------+
| NOTE: binary data not shown in terminal |
+-----------------------------------------+

@aam
Copy link
Contributor

aam commented Mar 22, 2021

Aha, thanks for the details. It looks like Authorization header is a special case that is handled differently by different runtimes/languages(for example see https://developers.nest.com/guides/api/how-to-handle-redirects).
Perhaps we should consider stripping it in Dart as well.

Meanwhile, Dart developers have a control over whether redirects are automatically followed via https://api.flutter.dev/flutter/dart-io/HttpClientRequest/followRedirects.html, if redirects are followed manually developer can strip out headers as needed.

@fdietze
Copy link
Author

fdietze commented Mar 23, 2021

Alright, the special case for 'Authorization' explains it.
Yes, following the redirect manually works if you have control over the http requests. But with libraries that use http internally, like flutter_cache_manager, it becomes a bit more tricky.

@RCSandberg
Copy link

RCSandberg commented May 21, 2021

Hi, I'd like to follow up on this as well. I seems to have run into the same(?) issue, but with a 307.

Can you confirm whether or not this seems to be the case?

I'm making a GET request to an endpoint that requires Authentication header to be set.
From that endpoint, I get a 307 which should redirect me to a new uri , which should give me a 200.
This has been verified with Postman and works as intended.

The uri I'm getting redirected to does not require an authentication header to be set and is on a new hostname. I have been able to figure out that the issue seems to have to do with that.

There is a setting in Postman (that is found under the Settings tab) that is disabled by default:

Follow Authorization header
Retain authorization header when a redirect happens to a different hostname.

When I enable that feature in Postman, I get the same problem as I do with HttpClient when running httpClient.send()

@brianquinlan brianquinlan self-assigned this Jan 25, 2022
copybara-service bot pushed a commit that referenced this issue Jan 27, 2022
TESTED=updated unit tests
Bug: #45410
Change-Id: I7c555a4818fd719d42748b6a18780e3d9b3ee147
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/229947
Reviewed-by: Alexander Aprelev <aam@google.com>
Commit-Queue: Brian Quinlan <bquinlan@google.com>
@brianquinlan
Copy link
Contributor

@Hixie
Copy link
Contributor

Hixie commented Jan 27, 2022

#47246 also seems to be a duplicate of this?

@kevmoo
Copy link
Member

kevmoo commented Jan 28, 2022

Fixed in 57db739

copybara-service bot pushed a commit that referenced this issue Jan 28, 2022
TESTED=updated unit tests
Bug: #45410
Change-Id: I7c555a4818fd719d42748b6a18780e3d9b3ee147
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/229947
Reviewed-by: Alexander Aprelev <aam@google.com>
Commit-Queue: Brian Quinlan <bquinlan@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, FFI, and the AOT and JIT backends. library-_http
Projects
None yet
Development

No branches or pull requests

7 participants