Skip to content

fix(auth): don't throw NullPointerException on IAM error responses without a body - #15015

Open
macastelaz wants to merge 6 commits into
googleapis:mainfrom
macastelaz:fix-iamutils-empty-error-body
Open

macastelaz wants to merge 6 commits into
googleapis:mainfrom
macastelaz:fix-iamutils-empty-error-body

Conversation

@macastelaz

@macastelaz macastelaz commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Fixes part of #15014.

Summary

IamUtils.sign() and IamUtils.getIdToken() lose the HTTP status code—or throw an unchecked exception—when an IAM 4xx response doesn't carry a standard {"error": {"message": "..."}} JSON body:

  • a NullPointerException when the response has no content;
  • an IllegalArgumentException ("no JSON input found") when the body is empty;
  • a JSON parse or field-validation IOException without the HTTP status code when the body isn't the expected IAM JSON (for example, an HTML error page from a proxy, {}, or JSON null).

With this change they always fail with an IOException that includes the HTTP status code (wrapped in SigningException for sign()), falling back to the HTTP status message whenever error.message isn't present. Responses that carry a valid IAM JSON error body behave as before.

Root cause

  • getSignature() and getIdToken() call response.parseAs(GenericData.class) and OAuth2Utils.validateMap / validateString directly on every 4xx response.
  • NetHttpTransport sends every POST or PUT that has content in streaming mode (NetHttpRequest: setFixedLengthStreamingMode / setChunkedStreamingMode).
  • When a streaming-mode request gets a 401 (or 407), JDK HttpURLConnection calls disconnectInternal() and throws HttpRetryException("cannot retry due to server authentication, in streaming mode"). After that, getErrorStream() returns null because the connection is no longer connected. Responses with Content-Length: 0 end up the same way.
  • NetHttpResponse.getContent() and HttpResponse.getContent() then return null, and parsing throws an NPE. Non-JSON 4xx bodies (such as proxy HTML pages) or JSON objects without error.message similarly throw before the caller formats the status code.

As a result, every 401 from signBlob or generateIdToken surfaces as an NPE:

  • ImpersonatedCredentials.sign() and idTokenWithAudience() throw a bare NPE;
  • ComputeEngineCredentials.sign() throws a SigningException that wraps the NPE and has no status code.

I found this on GKE agent-identity workloads, where certificate-bound tokens sent without mTLS get a 401 (#15014; the mTLS fix is a separate PR). But it affects any credential type that gets a 401 or non-JSON 4xx from these endpoints.

Change

  • IamUtils.parseErrorMessage(HttpResponse) returns the JSON error.message when present and non-empty. When the response has no content, an empty or non-JSON body, or JSON without error.message, it falls back to the HTTP status message (or a fixed fallback if the reason phrase is missing or empty).
  • getSignature() and getIdToken() use it on their 4xx branch.

Testing

New IamUtilsTest cases:

  • empty body for sign() and getIdToken();
  • empty-string body;
  • missing or empty reason phrase;
  • HTML body (e.g. proxy error page) for sign() and getIdToken();
  • {} JSON object, JSON missing or empty error.message, and JSON null body;
  • standard IAM JSON error body for sign() and getIdToken();
  • getIdToken() 401 with IAM's Bearer challenge, which doesn't trigger a credential refresh.

Full oauth2_http suite: 1337 tests, 0 failures, 2 skipped (JDK 21).

Live on a GKE agent-identity pod with released 1.57.0, ImpersonatedCredentials.sign() and idTokenWithAudience() throw the NPE at IamUtils.java:171 / :260, and ComputeEngineCredentials.sign() throws a SigningException wrapping it.

Not changed

On a 401 without a Bearer challenge, getIdToken() keeps HttpCredentialsAdapter as its unsuccessful-response handler, which refreshes and retries (up to 10 times by default). That behavior predates this change, and I'll look at it separately.

…thout a body

IamUtils.sign() and IamUtils.getIdToken() parse the JSON body of every 4xx
response. HttpURLConnection discards the body of a 401 response to a request
sent in streaming mode (NetHttpTransport sends every POST with content in
streaming mode), so the response has no content and parseAs() throws a
NullPointerException. Callers such as ImpersonatedCredentials.sign(),
ImpersonatedCredentials.idTokenWithAudience() and ComputeEngineCredentials.sign()
then surface a bare NPE instead of the 401.

Use the HTTP status message when the error response has no content, so these
calls fail with an IOException (or SigningException) that includes the status
code.
- Fall back to the status message when the 4xx body is empty but not null
  (the JSON parser throws IllegalArgumentException: no JSON input found).
- Treat an empty reason phrase like a missing one.
- Tests: empty-string body, missing reason phrase, sign() with a JSON body,
  and an IAM-shaped 401 (Bearer challenge) for getIdToken that asserts no
  credential refresh.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors error message parsing in IamUtils to gracefully handle empty or null response bodies from IAM Credentials 4xx responses, preventing potential NullPointerException or IllegalArgumentException failures. It introduces a helper method parseErrorMessage that falls back to the HTTP status message when the response body is empty, and adds comprehensive unit tests to verify these scenarios. A review comment correctly identifies a potential NullPointerException if the parsed response error is null and suggests adding a null check before validation.

@macastelaz macastelaz added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 16, 2026
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 16, 2026
@macastelaz
macastelaz marked this pull request as ready for review November 16, 2026 18:57
@macastelaz
macastelaz requested review from a team as code owners November 16, 2026 18:57
@macastelaz
macastelaz requested review from lqiu96 and nbayati November 16, 2026 19:19
@macastelaz
macastelaz enabled auto-merge (squash) November 20, 2026 04:24

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants