Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 31 additions & 0 deletions packages/core/src/ide/ide-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe('IdeClient', () => {
delete process.env['GEMINI_CLI_IDE_SERVER_PORT'];
delete process.env['GEMINI_CLI_IDE_SERVER_STDIO_COMMAND'];
delete process.env['GEMINI_CLI_IDE_SERVER_STDIO_ARGS'];
delete process.env['GEMINI_CLI_IDE_AUTH_TOKEN'];

// Mock dependencies
vi.spyOn(process, 'cwd').mockReturnValue('/test/workspace/sub-dir');
Expand Down Expand Up @@ -923,5 +924,35 @@ describe('IdeClient', () => {
IDEConnectionStatus.Connected,
);
});

it('should connect with an auth token from environment variable if config file is missing', async () => {
vi.mocked(fs.promises.readFile).mockRejectedValue(
new Error('File not found'),
);
(
vi.mocked(fs.promises.readdir) as Mock<
(path: fs.PathLike) => Promise<string[]>
>
).mockResolvedValue([]);
process.env['GEMINI_CLI_IDE_SERVER_PORT'] = '9090';
process.env['GEMINI_CLI_IDE_AUTH_TOKEN'] = 'env-auth-token';

const ideClient = await IdeClient.getInstance();
await ideClient.connect();

expect(StreamableHTTPClientTransport).toHaveBeenCalledWith(
new URL('http://127.0.0.1:9090/mcp'),
expect.objectContaining({
requestInit: {
headers: {
Authorization: 'Bearer env-auth-token',
},
},
}),
);
expect(ideClient.getConnectionStatus().status).toBe(
IDEConnectionStatus.Connected,
);
});
});
});
7 changes: 4 additions & 3 deletions packages/core/src/ide/ide-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,10 @@ export class IdeClient {
this.setState(IDEConnectionStatus.Connecting);

this.connectionConfig = await this.getConnectionConfigFromFile();
if (this.connectionConfig?.authToken) {
this.authToken = this.connectionConfig.authToken;
}
this.authToken =
this.connectionConfig?.authToken ??
process.env['GEMINI_CLI_IDE_AUTH_TOKEN'];
Comment on lines +154 to +156
Copy link
Contributor

Choose a reason for hiding this comment

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

security-high high

The introduction of reading the authentication token from the GEMINI_CLI_IDE_AUTH_TOKEN environment variable creates a security risk. Storing secrets in environment variables can expose them to other processes, potentially leading to token theft. A compromised token would allow an attacker to modify files in the workspace, creating a path to arbitrary code execution. It is strongly recommended to avoid using environment variables for sensitive data.


const workspacePath =
this.connectionConfig?.workspacePath ??
process.env['GEMINI_CLI_IDE_WORKSPACE_PATH'];
Expand Down