-
Notifications
You must be signed in to change notification settings - Fork 14.2k
fix(cli): prompt for folder trust before auth #27845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Alvin0412
wants to merge
1
commit into
google-gemini:main
from
Alvin0412:fix/folder-trust-before-auth
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import prompts from 'prompts'; | ||
| import { coreEvents, ExitCodes } from '@google/gemini-cli-core'; | ||
| import { relaunchApp } from '../utils/processUtils.js'; | ||
| import { | ||
| isWorkspaceTrusted, | ||
| loadTrustedFolders, | ||
| TrustLevel, | ||
| } from './trustedFolders.js'; | ||
| import { maybePromptForFolderTrust } from './folderTrustPrompt.js'; | ||
| import type { MergedSettings } from './settings.js'; | ||
|
|
||
| vi.mock('prompts', () => ({ | ||
| default: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('../utils/processUtils.js', () => ({ | ||
| relaunchApp: vi.fn().mockResolvedValue(undefined), | ||
| })); | ||
|
|
||
| vi.mock('@google/gemini-cli-core', async (importOriginal) => { | ||
| const actual = | ||
| await importOriginal<typeof import('@google/gemini-cli-core')>(); | ||
| const mockCoreEvents = Object.create( | ||
| actual.coreEvents, | ||
| ) as typeof actual.coreEvents; | ||
| mockCoreEvents.emitFeedback = vi.fn(); | ||
| return { | ||
| ...actual, | ||
| coreEvents: mockCoreEvents, | ||
| }; | ||
| }); | ||
|
|
||
| vi.mock('./trustedFolders.js', async (importOriginal) => { | ||
| const actual = await importOriginal<typeof import('./trustedFolders.js')>(); | ||
| return { | ||
| ...actual, | ||
| isWorkspaceTrusted: vi.fn(), | ||
| loadTrustedFolders: vi.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| describe('maybePromptForFolderTrust', () => { | ||
| const settings = { | ||
| security: { | ||
| folderTrust: { | ||
| enabled: true, | ||
| }, | ||
| }, | ||
| } as MergedSettings; | ||
| const argv = { | ||
| prompt: undefined, | ||
| query: undefined, | ||
| isCommand: undefined, | ||
| }; | ||
| const setValue = vi.fn().mockResolvedValue(undefined); | ||
| const stdinIsTTY = Object.getOwnPropertyDescriptor(process.stdin, 'isTTY'); | ||
| const stdoutIsTTY = Object.getOwnPropertyDescriptor(process.stdout, 'isTTY'); | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| Object.defineProperty(process.stdin, 'isTTY', { | ||
| configurable: true, | ||
| value: true, | ||
| }); | ||
| Object.defineProperty(process.stdout, 'isTTY', { | ||
| configurable: true, | ||
| value: true, | ||
| }); | ||
| vi.mocked(isWorkspaceTrusted).mockReturnValue({ | ||
| isTrusted: undefined, | ||
| source: undefined, | ||
| }); | ||
| vi.mocked(loadTrustedFolders).mockReturnValue({ | ||
| setValue, | ||
| } as unknown as ReturnType<typeof loadTrustedFolders>); | ||
| vi.mocked(prompts).mockResolvedValue({ | ||
| trustLevel: TrustLevel.DO_NOT_TRUST, | ||
| }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| Object.defineProperty(process.stdin, 'isTTY', { | ||
| configurable: true, | ||
| value: stdinIsTTY?.value, | ||
| }); | ||
| Object.defineProperty(process.stdout, 'isTTY', { | ||
| configurable: true, | ||
| value: stdoutIsTTY?.value, | ||
| }); | ||
| }); | ||
|
|
||
| it('does nothing when the workspace trust state is already known', async () => { | ||
| vi.mocked(isWorkspaceTrusted).mockReturnValue({ | ||
| isTrusted: false, | ||
| source: 'file', | ||
| }); | ||
|
|
||
| await maybePromptForFolderTrust(settings, argv, '/repo'); | ||
|
|
||
| expect(prompts).not.toHaveBeenCalled(); | ||
| expect(setValue).not.toHaveBeenCalled(); | ||
| expect(relaunchApp).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('saves an explicit do-not-trust decision and continues without relaunching', async () => { | ||
| await maybePromptForFolderTrust(settings, argv, '/repo'); | ||
|
|
||
| expect(prompts).toHaveBeenCalledExactlyOnceWith( | ||
| expect.objectContaining({ | ||
| stdin: process.stdin, | ||
| stdout: expect.anything(), | ||
| }), | ||
| ); | ||
| expect(setValue).toHaveBeenCalledWith('/repo', TrustLevel.DO_NOT_TRUST); | ||
| expect(relaunchApp).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('saves a trust decision and relaunches before authentication continues', async () => { | ||
| vi.mocked(prompts).mockResolvedValue({ | ||
| trustLevel: TrustLevel.TRUST_FOLDER, | ||
| }); | ||
|
|
||
| await maybePromptForFolderTrust(settings, argv, '/repo'); | ||
|
|
||
| expect(setValue).toHaveBeenCalledWith('/repo', TrustLevel.TRUST_FOLDER); | ||
| expect(relaunchApp).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it('exits with a config error if the trust decision cannot be saved', async () => { | ||
| const processExitSpy = vi | ||
| .spyOn(process, 'exit') | ||
| .mockImplementation((code?: string | number | null) => { | ||
| throw new Error(`process.exit:${code}`); | ||
| }); | ||
| setValue.mockRejectedValueOnce(new Error('disk full')); | ||
|
|
||
| await expect( | ||
| maybePromptForFolderTrust(settings, argv, '/repo'), | ||
| ).rejects.toThrow(`process.exit:${ExitCodes.FATAL_CONFIG_ERROR}`); | ||
|
|
||
| expect(coreEvents.emitFeedback).toHaveBeenCalledWith( | ||
| 'error', | ||
| 'Failed to save trust settings. Exiting Gemini CLI.', | ||
| ); | ||
| processExitSpy.mockRestore(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2026 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import * as path from 'node:path'; | ||
| import prompts from 'prompts'; | ||
| import { | ||
| coreEvents, | ||
| createWorkingStdio, | ||
| ExitCodes, | ||
| isHeadlessMode, | ||
| } from '@google/gemini-cli-core'; | ||
| import { runExitCleanup } from '../utils/cleanup.js'; | ||
| import { relaunchApp } from '../utils/processUtils.js'; | ||
| import type { CliArgs } from './config.js'; | ||
| import type { MergedSettings } from './settings.js'; | ||
| import { | ||
| isFolderTrustEnabled, | ||
| isTrustLevel, | ||
| isWorkspaceTrusted, | ||
| loadTrustedFolders, | ||
| TrustLevel, | ||
| } from './trustedFolders.js'; | ||
|
|
||
| type FolderTrustPromptArgs = Pick<CliArgs, 'prompt' | 'query' | 'isCommand'>; | ||
|
|
||
| async function exitAfterFailedTrustSave(): Promise<never> { | ||
| coreEvents.emitFeedback( | ||
| 'error', | ||
| 'Failed to save trust settings. Exiting Gemini CLI.', | ||
| ); | ||
| await runExitCleanup(); | ||
| process.exit(ExitCodes.FATAL_CONFIG_ERROR); | ||
| } | ||
|
|
||
| async function exitAfterCancelledTrustPrompt(): Promise<never> { | ||
| await runExitCleanup(); | ||
| process.exit(ExitCodes.FATAL_CANCELLATION_ERROR); | ||
| } | ||
|
|
||
| /** | ||
| * Resolves an unknown workspace trust state before slow startup work like auth. | ||
| * | ||
| * Trusting a folder requires a relaunch so startup can reload workspace settings, | ||
| * hooks, MCP servers, and context files under the new trust decision. | ||
| */ | ||
| export async function maybePromptForFolderTrust( | ||
| settings: MergedSettings, | ||
| argv: FolderTrustPromptArgs, | ||
| workspaceDir: string = process.cwd(), | ||
| ): Promise<void> { | ||
| if (!isFolderTrustEnabled(settings)) { | ||
| return; | ||
| } | ||
|
|
||
| if ( | ||
| argv.isCommand || | ||
| isHeadlessMode({ prompt: argv.prompt, query: argv.query }) | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| const { isTrusted } = isWorkspaceTrusted(settings, workspaceDir, { | ||
| prompt: argv.prompt, | ||
| query: argv.query, | ||
| }); | ||
| if (isTrusted !== undefined) { | ||
| return; | ||
| } | ||
|
|
||
| const dirName = path.basename(workspaceDir); | ||
| const parentFolder = path.basename(path.dirname(workspaceDir)); | ||
| const { stdout } = createWorkingStdio(); | ||
| const response = await prompts({ | ||
| type: 'select', | ||
| name: 'trustLevel', | ||
| message: 'Do you want to trust this folder?', | ||
| choices: [ | ||
| { | ||
| title: `Trust folder (${dirName})`, | ||
| value: TrustLevel.TRUST_FOLDER, | ||
| }, | ||
| { | ||
| title: `Trust parent folder (${parentFolder})`, | ||
| value: TrustLevel.TRUST_PARENT, | ||
| }, | ||
| { | ||
| title: "Don't trust", | ||
| value: TrustLevel.DO_NOT_TRUST, | ||
| }, | ||
| ], | ||
| initial: 0, | ||
| stdin: process.stdin, | ||
| stdout, | ||
| }); | ||
|
|
||
| if (!isTrustLevel(response.trustLevel)) { | ||
| await exitAfterCancelledTrustPrompt(); | ||
| } | ||
|
|
||
| try { | ||
| await loadTrustedFolders().setValue(workspaceDir, response.trustLevel); | ||
| } catch { | ||
| await exitAfterFailedTrustSave(); | ||
| } | ||
|
|
||
| if ( | ||
| response.trustLevel === TrustLevel.TRUST_FOLDER || | ||
| response.trustLevel === TrustLevel.TRUST_PARENT | ||
| ) { | ||
| await relaunchApp(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
patchStdio()is active,process.stdinmight be patched or intercepted. To ensure the interactive prompt correctly receives user input, you should pass the workingstdinstream (obtained fromcreateWorkingStdio()) topromptsas part of its options (the second argument). Passingstdoutin the options object is also the standard and documented way to configure custom streams inprompts.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in 0273154. The prompt now explicitly passes
stdin: process.stdinand the real stdout fromcreateWorkingStdio(), with a test assertion covering both streams.I kept the streams on the prompt object rather than the second
promptsargument because this repository currently usesprompts@2.4.2; its implementation and TypeScript types passstdin/stdoutthrough the question object, while the second argument only handles submit/cancel callbacks. Also,createWorkingStdio()currently returns stdout/stderr proxies, not stdin, andpatchStdio()only patches stdout/stderr.