Skip to content

Commit

Permalink
fix: Populate HttpHeaders fields with List<String> instead of String.
Browse files Browse the repository at this point in the history
IdentityPoolCredentials#getSubjectTokenFromMetadataServer calls
HttpHeaders.putAll to set request headers. The latter sets its fields through
reflective access: well-known headers such as Content-Type or Authentication
have dedicated fields of type List<String>, while remaining headers go into a
Map<String, Object> grab bag. However, we attempt to set every header to a
String, which causes a crash for well-known headers.

See bazelbuild/bazel#15639 for where this issue
was first noticed.
  • Loading branch information
tjgq committed Aug 25, 2022
1 parent ead58b2 commit d070494
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -230,7 +232,10 @@ private String getSubjectTokenFromMetadataServer() throws IOException {

if (identityPoolCredentialSource.hasHeaders()) {
HttpHeaders headers = new HttpHeaders();
headers.putAll(identityPoolCredentialSource.headers);
for (Map.Entry<String, String> entry : identityPoolCredentialSource.headers.entrySet()) {
// HttpHeaders expects List<String> instead of String.
headers.put(entry.getKey(), Collections.singletonList(entry.getValue()));
}
request.setHeaders(headers);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ static InputStream writeIdentityPoolCredentialsStream(
GenericJson credentialSource = new GenericJson();
GenericJson headers = new GenericJson();
headers.put("Metadata-Flavor", "Google");
headers.put("Authorization", "Bearer token");
credentialSource.put("url", url);
credentialSource.put("headers", headers);

Expand All @@ -695,6 +696,7 @@ private static IdentityPoolCredentialSource buildUrlBasedCredentialSource(
Map<String, Object> credentialSourceMap = new HashMap<>();
Map<String, String> headers = new HashMap<>();
headers.put("Metadata-Flavor", "Google");
headers.put("Authorization", "Bearer token");
credentialSourceMap.put("url", url);
credentialSourceMap.put("headers", headers);
credentialSourceMap.put("format", formatMap);
Expand Down

0 comments on commit d070494

Please sign in to comment.