Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
package com.google.auth.oauth2;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.UncheckedIOException;

/**
* This public class provides shared utilities for common OAuth2 utils or ADC. It also exposes
Expand Down Expand Up @@ -70,7 +72,14 @@ static final File getWellKnownCredentialsFile(DefaultCredentialsProvider provide
if (envPath != null) {
cloudConfigPath = new File(envPath);
} else if (provider.getOsName().indexOf("windows") >= 0) {
File appDataPath = new File(provider.getEnv("APPDATA"));
String appDataEnv = provider.getEnv("APPDATA");
if (appDataEnv == null) {
throw new UncheckedIOException(
new FileNotFoundException(
"APPDATA environment variable is not set; cannot locate the well-known"
+ " credentials file on Windows."));
}
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.

medium

While checking for null prevents a NullPointerException, an empty string for the APPDATA environment variable would also result in an invalid path. It is recommended to handle empty or blank strings as well to ensure the well-known credentials path is correctly resolved or fails with a descriptive error.

Suggested change
if (appDataEnv == null) {
throw new UncheckedIOException(
new FileNotFoundException(
"APPDATA environment variable is not set; cannot locate the well-known"
+ " credentials file on Windows."));
}
if (appDataEnv == null || appDataEnv.trim().isEmpty()) {
throw new UncheckedIOException(
new FileNotFoundException(
"APPDATA environment variable is not set or empty; cannot locate the well-known"
+ " credentials file on Windows."));
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch — updated the check to appDataEnv == null || appDataEnv.trim().isEmpty() and adjusted the error message accordingly. Also added tests for the empty-string and blank-string cases.

File appDataPath = new File(appDataEnv);
cloudConfigPath = new File(appDataPath, provider.CLOUDSDK_CONFIG_DIRECTORY);
} else {
File configPath = new File(provider.getProperty("user.home", ""), ".config");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,31 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.UncheckedIOException;
import org.junit.jupiter.api.Test;

class GoogleAuthUtilsTest {

@Test
void getWellKnownCredentialsFile_windows_nullAppData_throwsUncheckedIOException() {
DefaultCredentialsProviderTest.TestDefaultCredentialsProvider provider =
new DefaultCredentialsProviderTest.TestDefaultCredentialsProvider();
provider.setProperty("os.name", "windows");
// APPDATA is intentionally not set, so getEnv("APPDATA") returns null

UncheckedIOException thrown =
assertThrows(
UncheckedIOException.class,
() -> GoogleAuthUtils.getWellKnownCredentialsFile(provider));
assertTrue(thrown.getCause() instanceof FileNotFoundException);
assertTrue(thrown.getCause().getMessage().contains("APPDATA"));
}

@Test
void getWellKnownCredentialsPath_correct() {
DefaultCredentialsProvider provider =
Expand Down
Loading