Skip to content

fix(auth): handle null APPDATA environment variable in GoogleAuthUtils.getWellKnownCredentialsFile#12913

Open
anishesg wants to merge 2 commits intogoogleapis:mainfrom
anishesg:fix/ph-issue-12565
Open

fix(auth): handle null APPDATA environment variable in GoogleAuthUtils.getWellKnownCredentialsFile#12913
anishesg wants to merge 2 commits intogoogleapis:mainfrom
anishesg:fix/ph-issue-12565

Conversation

@anishesg
Copy link
Copy Markdown

In GoogleAuthUtils.getWellKnownCredentialsFile(), on Windows systems where CLOUDSDK_CONFIG is unset, the code calls provider.getEnv("APPDATA") and passes the result directly to new File(String). When APPDATA is not present in the environment, getEnv() returns null, and new File(null) immediately throws a raw NullPointerException with no useful context.

The fix adds an explicit null check after retrieving the APPDATA value. When it is null, the method now throws an UncheckedIOException wrapping a FileNotFoundException with a descriptive message indicating that APPDATA is not set, rather than propagating an opaque NullPointerException. This is consistent with the library's overall approach of surfacing meaningful errors when credential paths cannot be resolved.

A regression test in GoogleAuthUtilsTest verifies that the new exception type and message are produced when APPDATA is absent on a simulated Windows environment.

Fixes #12565

…s.getWellKnownCredentialsFile

In `GoogleAuthUtils.getWellKnownCredentialsFile()`, on Windows systems where `CLOUDSDK_CONFIG` is unset, the code calls `provider.getEnv("APPDATA")` and passes the result directly to `new File(String)`. When `APPDATA` is not present in the environment, `getEnv()` returns `null`, and `new File(null)` immediately throws a raw `NullPointerException` with no useful context.

Signed-off-by: anish k <ak8686@princeton.edu>
Copy link
Copy Markdown
Contributor

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

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 adds validation for the APPDATA environment variable on Windows in GoogleAuthUtils.java, ensuring an UncheckedIOException is thrown if it is missing, and includes a supporting unit test. Feedback suggests further improving this check by also handling empty or blank strings to prevent invalid path construction.

…trings, add tests

Signed-off-by: anish k <ak8686@princeton.edu>
@anishesg
Copy link
Copy Markdown
Author

Friendly ping — all tests pass, just waiting on the multi-approver requirement. Let me know if there's anything else needed on this one.

@lqiu96 lqiu96 added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Apr 28, 2026
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Apr 28, 2026
} else if (provider.getOsName().indexOf("windows") >= 0) {
File appDataPath = new File(provider.getEnv("APPDATA"));
String appDataEnv = provider.getEnv("APPDATA");
if (appDataEnv == null || appDataEnv.trim().isEmpty()) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: Guava has Strings.isNullOrEmpty for a convenient API

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.

I think this check would reject if the env var was set to a string like " ", which is probably more robust than just checking for Null or Empty. If we decide to keep it this way, we might want to update it in here too, so that our checks for APPDATA are consistent.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

From a quick search, I don't think " " is a valid in Windows and probably not realistic (IIUC APPDATA is used throughout Windows and should be valid). Since the other location already uses Guava, let's just make it consistent and use Guava as well (both impls resolve the main NPE possibility and are fine with me).

if (appDataEnv == null || appDataEnv.trim().isEmpty()) {
throw new UncheckedIOException(
new FileNotFoundException(
"APPDATA environment variable is not set or empty; cannot locate the well-known"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you update the message to be something like ... is not set or is empty for clarity?

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.

[google-auth-library-java] NullPointerException when APPDATA environment variable is unset on Windows in GoogleAuthUtils.getWellKnownCredentialsFile

4 participants