Skip to content

Commit 3c7eec1

Browse files
coeuvreShreeM01
andcommitted
Update GrpcRemoteDownloader to only include relevant headers. (bazelbuild#16450)
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 Co-authored-by: kshyanashree <[email protected]>
1 parent 1537172 commit 3c7eec1

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

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

Lines changed: 13 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.devtools.build.lib.bazel.repository.downloader.Checksum;
2728
import com.google.devtools.build.lib.bazel.repository.downloader.Downloader;
2829
import com.google.devtools.build.lib.bazel.repository.downloader.HashOutputStream;
@@ -165,7 +166,7 @@ static FetchBlobRequest newFetchBlobRequest(
165166
requestBuilder.addQualifiers(
166167
Qualifier.newBuilder()
167168
.setName(QUALIFIER_AUTH_HEADERS)
168-
.setValue(authHeadersJson(authHeaders))
169+
.setValue(authHeadersJson(urls, authHeaders))
169170
.build());
170171
}
171172

@@ -190,15 +191,24 @@ private OutputStream newOutputStream(
190191
return out;
191192
}
192193

193-
private static String authHeadersJson(Map<URI, Map<String, String>> authHeaders) {
194+
private static String authHeadersJson(
195+
List<URL> urls, Map<URI, Map<String, String>> authHeaders) {
196+
ImmutableSet<String> hostSet =
197+
urls.stream().map(URL::getHost).collect(ImmutableSet.toImmutableSet());
194198
Map<String, JsonObject> subObjects = new TreeMap<>();
195199
for (Map.Entry<URI, Map<String, String>> entry : authHeaders.entrySet()) {
200+
URI uri = entry.getKey();
201+
// Only add headers that are relevant to the hosts.
202+
if (!hostSet.contains(uri.getHost())) {
203+
continue;
204+
}
205+
196206
JsonObject subObject = new JsonObject();
197207
Map<String, String> orderedHeaders = new TreeMap<>(entry.getValue());
198208
for (Map.Entry<String, String> subEntry : orderedHeaders.entrySet()) {
199209
subObject.addProperty(subEntry.getKey(), subEntry.getValue());
200210
}
201-
subObjects.put(entry.getKey().toString(), subObject);
211+
subObjects.put(uri.toString(), subObject);
202212
}
203213

204214
JsonObject authHeadersJson = new JsonObject();

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,6 @@ public void testFetchBlobRequest() throws Exception {
321321
+ "\"http://example.com\":{"
322322
+ "\"Another-Header\":\"another header content\","
323323
+ "\"Some-Header\":\"some header content\""
324-
+ "},"
325-
+ "\"http://example.org\":{"
326-
+ "\"Org-Header\":\"org header content\""
327324
+ "}"
328325
+ "}";
329326

0 commit comments

Comments
 (0)