Skip to content

Commit 1ccc0a3

Browse files
coeuvrecopybara-github
authored andcommitted
Update GrpcRemoteDownloader to only include relevant headers.
Fixes GHSA-mxr8-q875-rhwq. RELNOTES[INC]: GrpcRemoteDownloader only includes relevant headers instead of sending all credentials. Closes #16439. PiperOrigin-RevId: 480069164 Change-Id: I49950311c04d1997d26832431d531a9036efdb18
1 parent a4200bc commit 1ccc0a3

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/main/java/com/google/devtools/build/lib/remote/downloader/GrpcRemoteDownloader.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import build.bazel.remote.execution.v2.RequestMetadata;
2424
import com.google.common.annotations.VisibleForTesting;
2525
import com.google.common.base.Strings;
26+
import com.google.common.collect.ImmutableSet;
2627
import com.google.common.collect.Iterables;
2728
import com.google.devtools.build.lib.bazel.repository.downloader.Checksum;
2829
import com.google.devtools.build.lib.bazel.repository.downloader.Downloader;
@@ -198,7 +199,7 @@ static FetchBlobRequest newFetchBlobRequest(
198199
requestBuilder.addQualifiers(
199200
Qualifier.newBuilder()
200201
.setName(QUALIFIER_AUTH_HEADERS)
201-
.setValue(authHeadersJson(authHeaders, includeAllHeaders))
202+
.setValue(authHeadersJson(urls, authHeaders, includeAllHeaders))
202203
.build());
203204
}
204205

@@ -225,9 +226,17 @@ private OutputStream newOutputStream(
225226
}
226227

227228
private static String authHeadersJson(
228-
Map<URI, Map<String, List<String>>> authHeaders, boolean includeAllHeaders) {
229+
List<URL> urls, Map<URI, Map<String, List<String>>> authHeaders, boolean includeAllHeaders) {
230+
ImmutableSet<String> hostSet =
231+
urls.stream().map(URL::getHost).collect(ImmutableSet.toImmutableSet());
229232
Map<String, JsonObject> subObjects = new TreeMap<>();
230233
for (Map.Entry<URI, Map<String, List<String>>> entry : authHeaders.entrySet()) {
234+
URI uri = entry.getKey();
235+
// Only add headers that are relevant to the hosts.
236+
if (!hostSet.contains(uri.getHost())) {
237+
continue;
238+
}
239+
231240
JsonObject subObject = new JsonObject();
232241
Map<String, List<String>> orderedHeaders = new TreeMap<>(entry.getValue());
233242
for (Map.Entry<String, List<String>> subEntry : orderedHeaders.entrySet()) {
@@ -244,7 +253,7 @@ private static String authHeadersJson(
244253
}
245254
}
246255
}
247-
subObjects.put(entry.getKey().toString(), subObject);
256+
subObjects.put(uri.toString(), subObject);
248257
}
249258

250259
JsonObject authHeadersJson = new JsonObject();

src/test/java/com/google/devtools/build/lib/remote/downloader/GrpcRemoteDownloaderTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,6 @@ public void testFetchBlobRequest() throws Exception {
371371
+ "\"http://example.com\":{"
372372
+ "\"Another-Header\":\"another header content\","
373373
+ "\"Some-Header\":\"some header content\""
374-
+ "},"
375-
+ "\"http://example.org\":{"
376-
+ "\"Org-Header\":\"org header content\""
377374
+ "}"
378375
+ "}";
379376

@@ -427,9 +424,6 @@ public void testFetchBlobRequestWithAllHeaders() throws Exception {
427424
+ "\"http://example.com\":{"
428425
+ "\"Another-Header\":[\"another header content\",\"even more header content\"],"
429426
+ "\"Some-Header\":[\"some header content\"]"
430-
+ "},"
431-
+ "\"http://example.org\":{"
432-
+ "\"Org-Header\":[\"org header content\",\"and a second one\",\"and a third one\"]"
433427
+ "}"
434428
+ "}";
435429

0 commit comments

Comments
 (0)