Skip to content

Commit

Permalink
Update GrpcRemoteDownloader to only include relevant headers.
Browse files Browse the repository at this point in the history
Fixes GHSA-mxr8-q875-rhwq.

RELNOTES[INC]: GrpcRemoteDownloader only includes relevant headers instead of sending all credentials.

Closes bazelbuild#16439.

PiperOrigin-RevId: 480069164
Change-Id: I49950311c04d1997d26832431d531a9036efdb18
  • Loading branch information
coeuvre authored and aiuto committed Oct 12, 2022
1 parent 7b79c7d commit 63df177
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import build.bazel.remote.execution.v2.RequestMetadata;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.bazel.repository.downloader.Checksum;
import com.google.devtools.build.lib.bazel.repository.downloader.Downloader;
Expand Down Expand Up @@ -198,7 +199,7 @@ static FetchBlobRequest newFetchBlobRequest(
requestBuilder.addQualifiers(
Qualifier.newBuilder()
.setName(QUALIFIER_AUTH_HEADERS)
.setValue(authHeadersJson(authHeaders, includeAllHeaders))
.setValue(authHeadersJson(urls, authHeaders, includeAllHeaders))
.build());
}

Expand All @@ -225,9 +226,17 @@ private OutputStream newOutputStream(
}

private static String authHeadersJson(
Map<URI, Map<String, List<String>>> authHeaders, boolean includeAllHeaders) {
List<URL> urls, Map<URI, Map<String, List<String>>> authHeaders, boolean includeAllHeaders) {
ImmutableSet<String> hostSet =
urls.stream().map(URL::getHost).collect(ImmutableSet.toImmutableSet());
Map<String, JsonObject> subObjects = new TreeMap<>();
for (Map.Entry<URI, Map<String, List<String>>> entry : authHeaders.entrySet()) {
URI uri = entry.getKey();
// Only add headers that are relevant to the hosts.
if (!hostSet.contains(uri.getHost())) {
continue;
}

JsonObject subObject = new JsonObject();
Map<String, List<String>> orderedHeaders = new TreeMap<>(entry.getValue());
for (Map.Entry<String, List<String>> subEntry : orderedHeaders.entrySet()) {
Expand All @@ -244,7 +253,7 @@ private static String authHeadersJson(
}
}
}
subObjects.put(entry.getKey().toString(), subObject);
subObjects.put(uri.toString(), subObject);
}

JsonObject authHeadersJson = new JsonObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,9 +371,6 @@ public void testFetchBlobRequest() throws Exception {
+ "\"http://example.com\":{"
+ "\"Another-Header\":\"another header content\","
+ "\"Some-Header\":\"some header content\""
+ "},"
+ "\"http://example.org\":{"
+ "\"Org-Header\":\"org header content\""
+ "}"
+ "}";

Expand Down Expand Up @@ -427,9 +424,6 @@ public void testFetchBlobRequestWithAllHeaders() throws Exception {
+ "\"http://example.com\":{"
+ "\"Another-Header\":[\"another header content\",\"even more header content\"],"
+ "\"Some-Header\":[\"some header content\"]"
+ "},"
+ "\"http://example.org\":{"
+ "\"Org-Header\":[\"org header content\",\"and a second one\",\"and a third one\"]"
+ "}"
+ "}";

Expand Down

0 comments on commit 63df177

Please sign in to comment.