-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
…ders Fix the signature of HttpConnector.connect. As the connect method handles redirects, we cannot have a fixed set of headers, once we support authentication, as the authentication headers depend on the host and that might change in a redirect. This change fixes the header generation in the HttpDownloader, but does not yet re-add the usage of the auth parmeter in the Starlark-provided download functions. Related #9327 Change-Id: Id12aa6a9a790fac6133a4da64baff58f02d36ef4 PiperOrigin-RevId: 269300472
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet; | ||
|
||
import com.google.common.base.Function; | ||
import com.google.common.base.Optional; | ||
import com.google.common.base.Predicates; | ||
import com.google.common.collect.ImmutableMap; | ||
|
@@ -313,21 +314,29 @@ private void tellParentThreadWeAreDone() { | |
private HttpStream establishConnection( | ||
final URL url, Optional<Checksum> checksum, Map<URI, Map<String, String>> additionalHeaders) | ||
throws IOException { | ||
ImmutableMap<String, String> headers = REQUEST_HEADERS; | ||
try { | ||
if (additionalHeaders.containsKey(url.toURI())) { | ||
headers = | ||
ImmutableMap.<String, String>builder() | ||
.putAll(headers) | ||
.putAll(additionalHeaders.get(url.toURI())) | ||
.build(); | ||
} | ||
} catch (URISyntaxException e) { | ||
// If we can't convert the URL to a URI (because it is syntactically malformed), still try to | ||
// do the connection, not adding authentication information as we cannot look it up. | ||
} | ||
final URLConnection connection = connector.connect(url, headers); | ||
final Map<String, String> allHeaders = headers; | ||
final Function<URL, ImmutableMap<String, String>> headerFunction = | ||
new Function<URL, ImmutableMap<String, String>>() { | ||
@Override | ||
public ImmutableMap<String, String> apply(URL url) { | ||
ImmutableMap<String, String> headers = REQUEST_HEADERS; | ||
try { | ||
if (additionalHeaders.containsKey(url.toURI())) { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
aehlig
Author
Contributor
|
||
headers = | ||
ImmutableMap.<String, String>builder() | ||
.putAll(headers) | ||
.putAll(additionalHeaders.get(url.toURI())) | ||
.build(); | ||
} | ||
} catch (URISyntaxException e) { | ||
// If we can't convert the URL to a URI (because it is syntactically malformed), still | ||
// try to | ||
// do the connection, not adding authentication information as we cannot look it up. | ||
} | ||
return headers; | ||
} | ||
}; | ||
|
||
final URLConnection connection = connector.connect(url, headerFunction); | ||
return httpStreamFactory.create( | ||
connection, | ||
url, | ||
|
@@ -340,10 +349,15 @@ public URLConnection connect(Throwable cause, ImmutableMap<String, String> extra | |
Event.progress(String.format("Lost connection for %s due to %s", url, cause))); | ||
return connector.connect( | ||
connection.getURL(), | ||
new ImmutableMap.Builder<String, String>() | ||
.putAll(allHeaders) | ||
.putAll(extraHeaders) | ||
.build()); | ||
new Function<URL, ImmutableMap<String, String>>() { | ||
@Override | ||
public ImmutableMap<String, String> apply(URL url) { | ||
return new ImmutableMap.Builder<String, String>() | ||
.putAll(headerFunction.apply(url)) | ||
.putAll(extraHeaders) | ||
.build(); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
I think this results in different behavior from before and from intended one.
I'd assume that we'd want to append auth headers to same "host" within a redirect, this commit however does matching by entire URI which will change and we won't get auth headers at all after a redirect.
Consider following examples:
In similar CVE in CURL, they implemented same host policy, this commit implements same URI policy which I think won't work in real life redirects 🤔
See https://curl.haxx.se/docs/CVE-2018-1000007.html
I might be missing something though, wdyt?