Skip to content

Commit cc39e0b

Browse files
committed
fix(auth): handle null APPDATA environment variable in GoogleAuthUtils.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>
1 parent 1431e11 commit cc39e0b

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/GoogleAuthUtils.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
package com.google.auth.oauth2;
3333

3434
import java.io.File;
35+
import java.io.FileNotFoundException;
36+
import java.io.UncheckedIOException;
3537

3638
/**
3739
* This public class provides shared utilities for common OAuth2 utils or ADC. It also exposes
@@ -70,7 +72,14 @@ static final File getWellKnownCredentialsFile(DefaultCredentialsProvider provide
7072
if (envPath != null) {
7173
cloudConfigPath = new File(envPath);
7274
} else if (provider.getOsName().indexOf("windows") >= 0) {
73-
File appDataPath = new File(provider.getEnv("APPDATA"));
75+
String appDataEnv = provider.getEnv("APPDATA");
76+
if (appDataEnv == null) {
77+
throw new UncheckedIOException(
78+
new FileNotFoundException(
79+
"APPDATA environment variable is not set; cannot locate the well-known"
80+
+ " credentials file on Windows."));
81+
}
82+
File appDataPath = new File(appDataEnv);
7483
cloudConfigPath = new File(appDataPath, provider.CLOUDSDK_CONFIG_DIRECTORY);
7584
} else {
7685
File configPath = new File(provider.getProperty("user.home", ""), ".config");

google-auth-library-java/oauth2_http/javatests/com/google/auth/oauth2/GoogleAuthUtilsTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,31 @@
3333

3434
import static org.junit.jupiter.api.Assertions.assertEquals;
3535
import static org.junit.jupiter.api.Assertions.assertNotNull;
36+
import static org.junit.jupiter.api.Assertions.assertThrows;
37+
import static org.junit.jupiter.api.Assertions.assertTrue;
3638

3739
import java.io.File;
40+
import java.io.FileNotFoundException;
41+
import java.io.UncheckedIOException;
3842
import org.junit.jupiter.api.Test;
3943

4044
class GoogleAuthUtilsTest {
4145

46+
@Test
47+
void getWellKnownCredentialsFile_windows_nullAppData_throwsUncheckedIOException() {
48+
DefaultCredentialsProviderTest.TestDefaultCredentialsProvider provider =
49+
new DefaultCredentialsProviderTest.TestDefaultCredentialsProvider();
50+
provider.setProperty("os.name", "windows");
51+
// APPDATA is intentionally not set, so getEnv("APPDATA") returns null
52+
53+
UncheckedIOException thrown =
54+
assertThrows(
55+
UncheckedIOException.class,
56+
() -> GoogleAuthUtils.getWellKnownCredentialsFile(provider));
57+
assertTrue(thrown.getCause() instanceof FileNotFoundException);
58+
assertTrue(thrown.getCause().getMessage().contains("APPDATA"));
59+
}
60+
4261
@Test
4362
void getWellKnownCredentialsPath_correct() {
4463
DefaultCredentialsProvider provider =

0 commit comments

Comments
 (0)