fix(core): reject numeric project IDs in GOOGLE_CLOUD_PROJECT (#24695) (#26532)

This commit is contained in:
Abhijit Balaji
2026-05-05 12:50:36 -07:00
committed by GitHub
parent f17cfb2a71
commit f29eb9a569
2 changed files with 28 additions and 0 deletions
@@ -8,6 +8,7 @@ import {
ProjectIdRequiredError,
setupUser,
ValidationCancelledError,
InvalidNumericProjectIdError,
resetUserDataCacheForTesting,
} from './setup.js';
import { ValidationRequiredError } from '../utils/googleQuotaErrors.js';
@@ -218,6 +219,20 @@ describe('setupUser', () => {
ProjectIdRequiredError,
);
});
it('should throw InvalidNumericProjectIdError when GOOGLE_CLOUD_PROJECT is numeric', async () => {
vi.stubEnv('GOOGLE_CLOUD_PROJECT', '1234567890');
await expect(setupUser({} as OAuth2Client, mockConfig)).rejects.toThrow(
InvalidNumericProjectIdError,
);
});
it('should throw InvalidNumericProjectIdError when GOOGLE_CLOUD_PROJECT_ID is numeric', async () => {
vi.stubEnv('GOOGLE_CLOUD_PROJECT_ID', '1234567890');
await expect(setupUser({} as OAuth2Client, mockConfig)).rejects.toThrow(
InvalidNumericProjectIdError,
);
});
});
describe('new user', () => {
+13
View File
@@ -36,6 +36,15 @@ export class ProjectIdRequiredError extends Error {
}
}
export class InvalidNumericProjectIdError extends Error {
constructor(projectId: string) {
super(
`Invalid Google Cloud Project ID: "${projectId}". The GOOGLE_CLOUD_PROJECT (or GOOGLE_CLOUD_PROJECT_ID) environment variable must be set to your string-based Project ID (e.g., "my-project-123"), not your numeric Project Number. Please update your environment variables.`,
);
this.name = 'InvalidNumericProjectIdError';
}
}
/**
* Error thrown when user cancels the validation process.
* This is a non-recoverable error that should result in auth failure.
@@ -122,6 +131,10 @@ export async function setupUser(
process.env['GOOGLE_CLOUD_PROJECT_ID'] ||
undefined;
if (projectId && /^\d+$/.test(projectId)) {
throw new InvalidNumericProjectIdError(projectId);
}
const projectCache = userDataCache.getOrCreate(client, () =>
createCache<string | undefined, Promise<UserData>>({
storage: 'map',