Skip to content

Commit 77f0233

Browse files
coeuvreShreeM01
andauthored
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 59b8b8f commit 77f0233

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;
@@ -171,7 +172,7 @@ static FetchBlobRequest newFetchBlobRequest(
171172
requestBuilder.addQualifiers(
172173
Qualifier.newBuilder()
173174
.setName(QUALIFIER_AUTH_HEADERS)
174-
.setValue(authHeadersJson(authHeaders))
175+
.setValue(authHeadersJson(urls, authHeaders))
175176
.build());
176177
}
177178

@@ -197,15 +198,24 @@ private OutputStream newOutputStream(
197198
return out;
198199
}
199200

200-
private static String authHeadersJson(Map<URI, Map<String, String>> authHeaders) {
201+
private static String authHeadersJson(
202+
List<URL> urls, Map<URI, Map<String, String>> authHeaders) {
203+
ImmutableSet<String> hostSet =
204+
urls.stream().map(URL::getHost).collect(ImmutableSet.toImmutableSet());
201205
Map<String, JsonObject> subObjects = new TreeMap<>();
202206
for (Map.Entry<URI, Map<String, String>> entry : authHeaders.entrySet()) {
207+
URI uri = entry.getKey();
208+
// Only add headers that are relevant to the hosts.
209+
if (!hostSet.contains(uri.getHost())) {
210+
continue;
211+
}
212+
203213
JsonObject subObject = new JsonObject();
204214
Map<String, String> orderedHeaders = new TreeMap<>(entry.getValue());
205215
for (Map.Entry<String, String> subEntry : orderedHeaders.entrySet()) {
206216
subObject.addProperty(subEntry.getKey(), subEntry.getValue());
207217
}
208-
subObjects.put(entry.getKey().toString(), subObject);
218+
subObjects.put(uri.toString(), subObject);
209219
}
210220

211221
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
@@ -320,9 +320,6 @@ public void testFetchBlobRequest() throws Exception {
320320
+ "\"http://example.com\":{"
321321
+ "\"Another-Header\":\"another header content\","
322322
+ "\"Some-Header\":\"some header content\""
323-
+ "},"
324-
+ "\"http://example.org\":{"
325-
+ "\"Org-Header\":\"org header content\""
326323
+ "}"
327324
+ "}";
328325

0 commit comments

Comments
 (0)