Skip to content

Commit 8cefb8b

Browse files
Bencodescopybara-github
authored andcommitted
Avoid merging URLs in HttpUtils
`mergeUrls` does not need to rebuild the URL from scratch if user information exists on the original URL. This behavior can actually break the 302 redirect due to subtle changes in the URL/encoding and should be avoided when possible. This fixes #14866 by correcting the implementation of `mergeUrls` to match the documentation that was added instead of rebuilding the URL from scratch which breaks the encoding of signed URLs. Closes #14922. PiperOrigin-RevId: 431935885
1 parent 8769d5d commit 8cefb8b

File tree

2 files changed

+10
-1
lines changed
  • src
    • main/java/com/google/devtools/build/lib/bazel/repository/downloader
    • test/java/com/google/devtools/build/lib/bazel/repository/downloader

2 files changed

+10
-1
lines changed

src/main/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpUtils.java

-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ private static URL mergeUrls(URI preferred, URL original) throws IOException {
7777
// scheme of their redirect URLs.
7878
if (preferred.getHost() != null
7979
&& preferred.getScheme() != null
80-
&& (preferred.getUserInfo() != null || original.getUserInfo() == null)
8180
&& (preferred.getFragment() != null || original.getRef() == null)) {
8281
// In this case we obviously do not inherit anything from the original URL, as all inheritable
8382
// fields are either set explicitly or not present in the original either. Therefore, it is

src/test/java/com/google/devtools/build/lib/bazel/repository/downloader/HttpUtilsTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,14 @@ public void getLocation_preservesQuotingIfNotInheriting() throws Exception {
139139
when(connection.getHeaderField("Location")).thenReturn(redirect);
140140
assertThat(HttpUtils.getLocation(connection)).isEqualTo(URI.create(redirect).toURL());
141141
}
142+
143+
@Test
144+
public void getLocation_preservesQuotingWithUserIfNotInheriting() throws Exception {
145+
String redirect =
146+
"http://redirected.example.org/foo?"
147+
+ "response-content-disposition=attachment%3Bfilename%3D%22bar.tar.gz%22";
148+
when(connection.getURL()).thenReturn(new URL("http://a:[email protected]"));
149+
when(connection.getHeaderField("Location")).thenReturn(redirect);
150+
assertThat(HttpUtils.getLocation(connection)).isEqualTo(URI.create(redirect).toURL());
151+
}
142152
}

0 commit comments

Comments
 (0)