mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-05-12 12:54:07 -07:00
feat(cli): Add permissions command to modify trust settings (#8792)
This commit is contained in:
@@ -22,7 +22,9 @@ import * as ServerConfig from '@google/gemini-cli-core';
|
|||||||
import { isWorkspaceTrusted } from './trustedFolders.js';
|
import { isWorkspaceTrusted } from './trustedFolders.js';
|
||||||
|
|
||||||
vi.mock('./trustedFolders.js', () => ({
|
vi.mock('./trustedFolders.js', () => ({
|
||||||
isWorkspaceTrusted: vi.fn().mockReturnValue(true), // Default to trusted
|
isWorkspaceTrusted: vi
|
||||||
|
.fn()
|
||||||
|
.mockReturnValue({ isTrusted: true, source: 'file' }), // Default to trusted
|
||||||
}));
|
}));
|
||||||
|
|
||||||
vi.mock('fs', async (importOriginal) => {
|
vi.mock('fs', async (importOriginal) => {
|
||||||
@@ -2098,7 +2100,10 @@ describe('loadCliConfig approval mode', () => {
|
|||||||
// --- Untrusted Folder Scenarios ---
|
// --- Untrusted Folder Scenarios ---
|
||||||
describe('when folder is NOT trusted', () => {
|
describe('when folder is NOT trusted', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.mocked(isWorkspaceTrusted).mockReturnValue(false);
|
vi.mocked(isWorkspaceTrusted).mockReturnValue({
|
||||||
|
isTrusted: false,
|
||||||
|
source: 'file',
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should override --approval-mode=yolo to DEFAULT', async () => {
|
it('should override --approval-mode=yolo to DEFAULT', async () => {
|
||||||
|
|||||||
@@ -414,7 +414,7 @@ export async function loadCliConfig(
|
|||||||
const ideMode = settings.ide?.enabled ?? false;
|
const ideMode = settings.ide?.enabled ?? false;
|
||||||
|
|
||||||
const folderTrust = settings.security?.folderTrust?.enabled ?? false;
|
const folderTrust = settings.security?.folderTrust?.enabled ?? false;
|
||||||
const trustedFolder = isWorkspaceTrusted(settings) ?? true;
|
const trustedFolder = isWorkspaceTrusted(settings)?.isTrusted ?? true;
|
||||||
|
|
||||||
const allExtensions = annotateActiveExtensions(
|
const allExtensions = annotateActiveExtensions(
|
||||||
extensions,
|
extensions,
|
||||||
|
|||||||
@@ -87,7 +87,10 @@ describe('update tests', () => {
|
|||||||
// Clean up before each test
|
// Clean up before each test
|
||||||
fs.rmSync(userExtensionsDir, { recursive: true, force: true });
|
fs.rmSync(userExtensionsDir, { recursive: true, force: true });
|
||||||
fs.mkdirSync(userExtensionsDir, { recursive: true });
|
fs.mkdirSync(userExtensionsDir, { recursive: true });
|
||||||
vi.mocked(isWorkspaceTrusted).mockReturnValue(true);
|
vi.mocked(isWorkspaceTrusted).mockReturnValue({
|
||||||
|
isTrusted: true,
|
||||||
|
source: 'file',
|
||||||
|
});
|
||||||
vi.spyOn(process, 'cwd').mockReturnValue(tempWorkspaceDir);
|
vi.spyOn(process, 'cwd').mockReturnValue(tempWorkspaceDir);
|
||||||
Object.values(mockGit).forEach((fn) => fn.mockReset());
|
Object.values(mockGit).forEach((fn) => fn.mockReset());
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -30,7 +30,9 @@ vi.mock('./settings.js', async (importActual) => {
|
|||||||
|
|
||||||
// Mock trustedFolders
|
// Mock trustedFolders
|
||||||
vi.mock('./trustedFolders.js', () => ({
|
vi.mock('./trustedFolders.js', () => ({
|
||||||
isWorkspaceTrusted: vi.fn(),
|
isWorkspaceTrusted: vi
|
||||||
|
.fn()
|
||||||
|
.mockReturnValue({ isTrusted: true, source: 'file' }),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// NOW import everything else, including the (now effectively re-exported) settings.js
|
// NOW import everything else, including the (now effectively re-exported) settings.js
|
||||||
@@ -120,7 +122,10 @@ describe('Settings Loading and Merging', () => {
|
|||||||
(mockFsExistsSync as Mock).mockReturnValue(false);
|
(mockFsExistsSync as Mock).mockReturnValue(false);
|
||||||
(fs.readFileSync as Mock).mockReturnValue('{}'); // Return valid empty JSON
|
(fs.readFileSync as Mock).mockReturnValue('{}'); // Return valid empty JSON
|
||||||
(mockFsMkdirSync as Mock).mockImplementation(() => undefined);
|
(mockFsMkdirSync as Mock).mockImplementation(() => undefined);
|
||||||
vi.mocked(isWorkspaceTrusted).mockReturnValue(true);
|
vi.mocked(isWorkspaceTrusted).mockReturnValue({
|
||||||
|
isTrusted: true,
|
||||||
|
source: 'file',
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@@ -1843,7 +1848,10 @@ describe('Settings Loading and Merging', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should NOT merge workspace settings when workspace is not trusted', () => {
|
it('should NOT merge workspace settings when workspace is not trusted', () => {
|
||||||
vi.mocked(isWorkspaceTrusted).mockReturnValue(false);
|
vi.mocked(isWorkspaceTrusted).mockReturnValue({
|
||||||
|
isTrusted: false,
|
||||||
|
source: 'file',
|
||||||
|
});
|
||||||
(mockFsExistsSync as Mock).mockReturnValue(true);
|
(mockFsExistsSync as Mock).mockReturnValue(true);
|
||||||
const userSettingsContent = {
|
const userSettingsContent = {
|
||||||
ui: { theme: 'dark' },
|
ui: { theme: 'dark' },
|
||||||
@@ -2222,7 +2230,10 @@ describe('Settings Loading and Merging', () => {
|
|||||||
delete process.env['TESTTEST']; // reset
|
delete process.env['TESTTEST']; // reset
|
||||||
const geminiEnvPath = path.resolve(path.join(GEMINI_DIR, '.env'));
|
const geminiEnvPath = path.resolve(path.join(GEMINI_DIR, '.env'));
|
||||||
|
|
||||||
vi.mocked(isWorkspaceTrusted).mockReturnValue(isWorkspaceTrustedValue);
|
vi.mocked(isWorkspaceTrusted).mockReturnValue({
|
||||||
|
isTrusted: isWorkspaceTrustedValue,
|
||||||
|
source: 'file',
|
||||||
|
});
|
||||||
(mockFsExistsSync as Mock).mockImplementation((p: fs.PathLike) =>
|
(mockFsExistsSync as Mock).mockImplementation((p: fs.PathLike) =>
|
||||||
[USER_SETTINGS_PATH, geminiEnvPath].includes(p.toString()),
|
[USER_SETTINGS_PATH, geminiEnvPath].includes(p.toString()),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -492,7 +492,7 @@ export function setUpCloudShellEnvironment(envFilePath: string | null): void {
|
|||||||
export function loadEnvironment(settings: Settings): void {
|
export function loadEnvironment(settings: Settings): void {
|
||||||
const envFilePath = findEnvFile(process.cwd());
|
const envFilePath = findEnvFile(process.cwd());
|
||||||
|
|
||||||
if (!isWorkspaceTrusted(settings)) {
|
if (!isWorkspaceTrusted(settings).isTrusted) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -674,7 +674,7 @@ export function loadSettings(
|
|||||||
userSettings,
|
userSettings,
|
||||||
);
|
);
|
||||||
const isTrusted =
|
const isTrusted =
|
||||||
isWorkspaceTrusted(initialTrustCheckSettings as Settings) ?? true;
|
isWorkspaceTrusted(initialTrustCheckSettings as Settings).isTrusted ?? true;
|
||||||
|
|
||||||
// Create a temporary merged settings object to pass to loadEnvironment.
|
// Create a temporary merged settings object to pass to loadEnvironment.
|
||||||
const tempMergedSettings = mergeSettings(
|
const tempMergedSettings = mergeSettings(
|
||||||
|
|||||||
@@ -229,52 +229,70 @@ describe('isWorkspaceTrusted', () => {
|
|||||||
it('should return true for a directly trusted folder', () => {
|
it('should return true for a directly trusted folder', () => {
|
||||||
mockCwd = '/home/user/projectA';
|
mockCwd = '/home/user/projectA';
|
||||||
mockRules['/home/user/projectA'] = TrustLevel.TRUST_FOLDER;
|
mockRules['/home/user/projectA'] = TrustLevel.TRUST_FOLDER;
|
||||||
expect(isWorkspaceTrusted(mockSettings)).toBe(true);
|
expect(isWorkspaceTrusted(mockSettings)).toEqual({
|
||||||
|
isTrusted: true,
|
||||||
|
source: 'file',
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return true for a child of a trusted folder', () => {
|
it('should return true for a child of a trusted folder', () => {
|
||||||
mockCwd = '/home/user/projectA/src';
|
mockCwd = '/home/user/projectA/src';
|
||||||
mockRules['/home/user/projectA'] = TrustLevel.TRUST_FOLDER;
|
mockRules['/home/user/projectA'] = TrustLevel.TRUST_FOLDER;
|
||||||
expect(isWorkspaceTrusted(mockSettings)).toBe(true);
|
expect(isWorkspaceTrusted(mockSettings)).toEqual({
|
||||||
|
isTrusted: true,
|
||||||
|
source: 'file',
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return true for a child of a trusted parent folder', () => {
|
it('should return true for a child of a trusted parent folder', () => {
|
||||||
mockCwd = '/home/user/projectB';
|
mockCwd = '/home/user/projectB';
|
||||||
mockRules['/home/user/projectB/somefile.txt'] = TrustLevel.TRUST_PARENT;
|
mockRules['/home/user/projectB/somefile.txt'] = TrustLevel.TRUST_PARENT;
|
||||||
expect(isWorkspaceTrusted(mockSettings)).toBe(true);
|
expect(isWorkspaceTrusted(mockSettings)).toEqual({
|
||||||
|
isTrusted: true,
|
||||||
|
source: 'file',
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return false for a directly untrusted folder', () => {
|
it('should return false for a directly untrusted folder', () => {
|
||||||
mockCwd = '/home/user/untrusted';
|
mockCwd = '/home/user/untrusted';
|
||||||
mockRules['/home/user/untrusted'] = TrustLevel.DO_NOT_TRUST;
|
mockRules['/home/user/untrusted'] = TrustLevel.DO_NOT_TRUST;
|
||||||
expect(isWorkspaceTrusted(mockSettings)).toBe(false);
|
expect(isWorkspaceTrusted(mockSettings)).toEqual({
|
||||||
|
isTrusted: false,
|
||||||
|
source: 'file',
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return undefined for a child of an untrusted folder', () => {
|
it('should return undefined for a child of an untrusted folder', () => {
|
||||||
mockCwd = '/home/user/untrusted/src';
|
mockCwd = '/home/user/untrusted/src';
|
||||||
mockRules['/home/user/untrusted'] = TrustLevel.DO_NOT_TRUST;
|
mockRules['/home/user/untrusted'] = TrustLevel.DO_NOT_TRUST;
|
||||||
expect(isWorkspaceTrusted(mockSettings)).toBeUndefined();
|
expect(isWorkspaceTrusted(mockSettings).isTrusted).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return undefined when no rules match', () => {
|
it('should return undefined when no rules match', () => {
|
||||||
mockCwd = '/home/user/other';
|
mockCwd = '/home/user/other';
|
||||||
mockRules['/home/user/projectA'] = TrustLevel.TRUST_FOLDER;
|
mockRules['/home/user/projectA'] = TrustLevel.TRUST_FOLDER;
|
||||||
mockRules['/home/user/untrusted'] = TrustLevel.DO_NOT_TRUST;
|
mockRules['/home/user/untrusted'] = TrustLevel.DO_NOT_TRUST;
|
||||||
expect(isWorkspaceTrusted(mockSettings)).toBeUndefined();
|
expect(isWorkspaceTrusted(mockSettings).isTrusted).toBeUndefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should prioritize trust over distrust', () => {
|
it('should prioritize trust over distrust', () => {
|
||||||
mockCwd = '/home/user/projectA/untrusted';
|
mockCwd = '/home/user/projectA/untrusted';
|
||||||
mockRules['/home/user/projectA'] = TrustLevel.TRUST_FOLDER;
|
mockRules['/home/user/projectA'] = TrustLevel.TRUST_FOLDER;
|
||||||
mockRules['/home/user/projectA/untrusted'] = TrustLevel.DO_NOT_TRUST;
|
mockRules['/home/user/projectA/untrusted'] = TrustLevel.DO_NOT_TRUST;
|
||||||
expect(isWorkspaceTrusted(mockSettings)).toBe(true);
|
expect(isWorkspaceTrusted(mockSettings)).toEqual({
|
||||||
|
isTrusted: true,
|
||||||
|
source: 'file',
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle path normalization', () => {
|
it('should handle path normalization', () => {
|
||||||
mockCwd = '/home/user/projectA';
|
mockCwd = '/home/user/projectA';
|
||||||
mockRules[`/home/user/../user/${path.basename('/home/user/projectA')}`] =
|
mockRules[`/home/user/../user/${path.basename('/home/user/projectA')}`] =
|
||||||
TrustLevel.TRUST_FOLDER;
|
TrustLevel.TRUST_FOLDER;
|
||||||
expect(isWorkspaceTrusted(mockSettings)).toBe(true);
|
expect(isWorkspaceTrusted(mockSettings)).toEqual({
|
||||||
|
isTrusted: true,
|
||||||
|
source: 'file',
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -297,7 +315,10 @@ describe('isWorkspaceTrusted with IDE override', () => {
|
|||||||
vi.spyOn(fs, 'readFileSync').mockReturnValue(
|
vi.spyOn(fs, 'readFileSync').mockReturnValue(
|
||||||
JSON.stringify({ [process.cwd()]: TrustLevel.DO_NOT_TRUST }),
|
JSON.stringify({ [process.cwd()]: TrustLevel.DO_NOT_TRUST }),
|
||||||
);
|
);
|
||||||
expect(isWorkspaceTrusted(mockSettings)).toBe(true);
|
expect(isWorkspaceTrusted(mockSettings)).toEqual({
|
||||||
|
isTrusted: true,
|
||||||
|
source: 'ide',
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return false when ideTrust is false, ignoring config', () => {
|
it('should return false when ideTrust is false, ignoring config', () => {
|
||||||
@@ -306,7 +327,10 @@ describe('isWorkspaceTrusted with IDE override', () => {
|
|||||||
vi.spyOn(fs, 'readFileSync').mockReturnValue(
|
vi.spyOn(fs, 'readFileSync').mockReturnValue(
|
||||||
JSON.stringify({ [process.cwd()]: TrustLevel.TRUST_FOLDER }),
|
JSON.stringify({ [process.cwd()]: TrustLevel.TRUST_FOLDER }),
|
||||||
);
|
);
|
||||||
expect(isWorkspaceTrusted(mockSettings)).toBe(false);
|
expect(isWorkspaceTrusted(mockSettings)).toEqual({
|
||||||
|
isTrusted: false,
|
||||||
|
source: 'ide',
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fall back to config when ideTrust is undefined', () => {
|
it('should fall back to config when ideTrust is undefined', () => {
|
||||||
@@ -314,7 +338,10 @@ describe('isWorkspaceTrusted with IDE override', () => {
|
|||||||
vi.spyOn(fs, 'readFileSync').mockReturnValue(
|
vi.spyOn(fs, 'readFileSync').mockReturnValue(
|
||||||
JSON.stringify({ [process.cwd()]: TrustLevel.TRUST_FOLDER }),
|
JSON.stringify({ [process.cwd()]: TrustLevel.TRUST_FOLDER }),
|
||||||
);
|
);
|
||||||
expect(isWorkspaceTrusted(mockSettings)).toBe(true);
|
expect(isWorkspaceTrusted(mockSettings)).toEqual({
|
||||||
|
isTrusted: true,
|
||||||
|
source: 'file',
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should always return true if folderTrust setting is disabled', () => {
|
it('should always return true if folderTrust setting is disabled', () => {
|
||||||
@@ -326,6 +353,9 @@ describe('isWorkspaceTrusted with IDE override', () => {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
ideContextStore.set({ workspaceState: { isTrusted: false } });
|
ideContextStore.set({ workspaceState: { isTrusted: false } });
|
||||||
expect(isWorkspaceTrusted(settings)).toBe(true);
|
expect(isWorkspaceTrusted(settings)).toEqual({
|
||||||
|
isTrusted: true,
|
||||||
|
source: undefined,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -47,6 +47,11 @@ export interface TrustedFoldersFile {
|
|||||||
path: string;
|
path: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface TrustResult {
|
||||||
|
isTrusted: boolean | undefined;
|
||||||
|
source: 'ide' | 'file' | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
export class LoadedTrustedFolders {
|
export class LoadedTrustedFolders {
|
||||||
constructor(
|
constructor(
|
||||||
readonly user: TrustedFoldersFile,
|
readonly user: TrustedFoldersFile,
|
||||||
@@ -166,9 +171,15 @@ export function isFolderTrustEnabled(settings: Settings): boolean {
|
|||||||
return folderTrustSetting;
|
return folderTrustSetting;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getWorkspaceTrustFromLocalConfig(): boolean | undefined {
|
function getWorkspaceTrustFromLocalConfig(
|
||||||
|
trustConfig?: Record<string, TrustLevel>,
|
||||||
|
): TrustResult {
|
||||||
const folders = loadTrustedFolders();
|
const folders = loadTrustedFolders();
|
||||||
|
|
||||||
|
if (trustConfig) {
|
||||||
|
folders.user.config = trustConfig;
|
||||||
|
}
|
||||||
|
|
||||||
if (folders.errors.length > 0) {
|
if (folders.errors.length > 0) {
|
||||||
for (const error of folders.errors) {
|
for (const error of folders.errors) {
|
||||||
console.error(
|
console.error(
|
||||||
@@ -177,19 +188,26 @@ function getWorkspaceTrustFromLocalConfig(): boolean | undefined {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return folders.isPathTrusted(process.cwd());
|
const isTrusted = folders.isPathTrusted(process.cwd());
|
||||||
|
return {
|
||||||
|
isTrusted,
|
||||||
|
source: isTrusted !== undefined ? 'file' : undefined,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isWorkspaceTrusted(settings: Settings): boolean | undefined {
|
export function isWorkspaceTrusted(
|
||||||
|
settings: Settings,
|
||||||
|
trustConfig?: Record<string, TrustLevel>,
|
||||||
|
): TrustResult {
|
||||||
if (!isFolderTrustEnabled(settings)) {
|
if (!isFolderTrustEnabled(settings)) {
|
||||||
return true;
|
return { isTrusted: true, source: undefined };
|
||||||
}
|
}
|
||||||
|
|
||||||
const ideTrust = ideContextStore.get()?.workspaceState?.isTrusted;
|
const ideTrust = ideContextStore.get()?.workspaceState?.isTrusted;
|
||||||
if (ideTrust !== undefined) {
|
if (ideTrust !== undefined) {
|
||||||
return ideTrust;
|
return { isTrusted: ideTrust, source: 'ide' };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fall back to the local user configuration
|
// Fall back to the local user configuration
|
||||||
return getWorkspaceTrustFromLocalConfig();
|
return getWorkspaceTrustFromLocalConfig(trustConfig);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,6 +28,16 @@ vi.mock('../ui/commands/ideCommand.js', async () => {
|
|||||||
vi.mock('../ui/commands/restoreCommand.js', () => ({
|
vi.mock('../ui/commands/restoreCommand.js', () => ({
|
||||||
restoreCommand: vi.fn(),
|
restoreCommand: vi.fn(),
|
||||||
}));
|
}));
|
||||||
|
vi.mock('../ui/commands/permissionsCommand.js', async () => {
|
||||||
|
const { CommandKind } = await import('../ui/commands/types.js');
|
||||||
|
return {
|
||||||
|
permissionsCommand: {
|
||||||
|
name: 'permissions',
|
||||||
|
description: 'Permissions command',
|
||||||
|
kind: CommandKind.BUILT_IN,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest';
|
import { describe, it, expect, vi, beforeEach, type Mock } from 'vitest';
|
||||||
import { BuiltinCommandLoader } from './BuiltinCommandLoader.js';
|
import { BuiltinCommandLoader } from './BuiltinCommandLoader.js';
|
||||||
@@ -69,7 +79,9 @@ describe('BuiltinCommandLoader', () => {
|
|||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.clearAllMocks();
|
vi.clearAllMocks();
|
||||||
mockConfig = { some: 'config' } as unknown as Config;
|
mockConfig = {
|
||||||
|
getFolderTrust: vi.fn().mockReturnValue(true),
|
||||||
|
} as unknown as Config;
|
||||||
|
|
||||||
restoreCommandMock.mockReturnValue({
|
restoreCommandMock.mockReturnValue({
|
||||||
name: 'restore',
|
name: 'restore',
|
||||||
@@ -123,4 +135,19 @@ describe('BuiltinCommandLoader', () => {
|
|||||||
const mcpCmd = commands.find((c) => c.name === 'mcp');
|
const mcpCmd = commands.find((c) => c.name === 'mcp');
|
||||||
expect(mcpCmd).toBeDefined();
|
expect(mcpCmd).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should include permissions command when folder trust is enabled', async () => {
|
||||||
|
const loader = new BuiltinCommandLoader(mockConfig);
|
||||||
|
const commands = await loader.loadCommands(new AbortController().signal);
|
||||||
|
const permissionsCmd = commands.find((c) => c.name === 'permissions');
|
||||||
|
expect(permissionsCmd).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should exclude permissions command when folder trust is disabled', async () => {
|
||||||
|
(mockConfig.getFolderTrust as Mock).mockReturnValue(false);
|
||||||
|
const loader = new BuiltinCommandLoader(mockConfig);
|
||||||
|
const commands = await loader.loadCommands(new AbortController().signal);
|
||||||
|
const permissionsCmd = commands.find((c) => c.name === 'permissions');
|
||||||
|
expect(permissionsCmd).toBeUndefined();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import { ideCommand } from '../ui/commands/ideCommand.js';
|
|||||||
import { initCommand } from '../ui/commands/initCommand.js';
|
import { initCommand } from '../ui/commands/initCommand.js';
|
||||||
import { mcpCommand } from '../ui/commands/mcpCommand.js';
|
import { mcpCommand } from '../ui/commands/mcpCommand.js';
|
||||||
import { memoryCommand } from '../ui/commands/memoryCommand.js';
|
import { memoryCommand } from '../ui/commands/memoryCommand.js';
|
||||||
|
import { permissionsCommand } from '../ui/commands/permissionsCommand.js';
|
||||||
import { privacyCommand } from '../ui/commands/privacyCommand.js';
|
import { privacyCommand } from '../ui/commands/privacyCommand.js';
|
||||||
import { quitCommand } from '../ui/commands/quitCommand.js';
|
import { quitCommand } from '../ui/commands/quitCommand.js';
|
||||||
import { restoreCommand } from '../ui/commands/restoreCommand.js';
|
import { restoreCommand } from '../ui/commands/restoreCommand.js';
|
||||||
@@ -68,6 +69,7 @@ export class BuiltinCommandLoader implements ICommandLoader {
|
|||||||
initCommand,
|
initCommand,
|
||||||
mcpCommand,
|
mcpCommand,
|
||||||
memoryCommand,
|
memoryCommand,
|
||||||
|
this.config?.getFolderTrust() ? permissionsCommand : null,
|
||||||
privacyCommand,
|
privacyCommand,
|
||||||
quitCommand,
|
quitCommand,
|
||||||
restoreCommand(this.config),
|
restoreCommand(this.config),
|
||||||
|
|||||||
@@ -38,6 +38,13 @@ describe('App', () => {
|
|||||||
quittingMessages: null,
|
quittingMessages: null,
|
||||||
dialogsVisible: false,
|
dialogsVisible: false,
|
||||||
mainControlsRef: { current: null },
|
mainControlsRef: { current: null },
|
||||||
|
historyManager: {
|
||||||
|
addItem: vi.fn(),
|
||||||
|
history: [],
|
||||||
|
updateItem: vi.fn(),
|
||||||
|
clearItems: vi.fn(),
|
||||||
|
loadHistory: vi.fn(),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
it('should render main content and composer when not quitting', () => {
|
it('should render main content and composer when not quitting', () => {
|
||||||
|
|||||||
@@ -29,7 +29,11 @@ export const App = () => {
|
|||||||
<Box flexDirection="column" ref={uiState.mainControlsRef}>
|
<Box flexDirection="column" ref={uiState.mainControlsRef}>
|
||||||
<Notifications />
|
<Notifications />
|
||||||
|
|
||||||
{uiState.dialogsVisible ? <DialogManager /> : <Composer />}
|
{uiState.dialogsVisible ? (
|
||||||
|
<DialogManager addItem={uiState.historyManager.addItem} />
|
||||||
|
) : (
|
||||||
|
<Composer />
|
||||||
|
)}
|
||||||
|
|
||||||
{uiState.dialogsVisible && uiState.ctrlCPressedOnce && (
|
{uiState.dialogsVisible && uiState.ctrlCPressedOnce && (
|
||||||
<Box marginTop={1}>
|
<Box marginTop={1}>
|
||||||
|
|||||||
@@ -157,6 +157,16 @@ export const AppContainer = (props: AppContainerProps) => {
|
|||||||
config.getWorkingDir(),
|
config.getWorkingDir(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const [isPermissionsDialogOpen, setPermissionsDialogOpen] = useState(false);
|
||||||
|
const openPermissionsDialog = useCallback(
|
||||||
|
() => setPermissionsDialogOpen(true),
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
const closePermissionsDialog = useCallback(
|
||||||
|
() => setPermissionsDialogOpen(false),
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
// Helper to determine the effective model, considering the fallback state.
|
// Helper to determine the effective model, considering the fallback state.
|
||||||
const getEffectiveModel = useCallback(() => {
|
const getEffectiveModel = useCallback(() => {
|
||||||
if (config.isInFallbackMode()) {
|
if (config.isInFallbackMode()) {
|
||||||
@@ -424,6 +434,7 @@ Logging in with Google... Please restart Gemini CLI to continue.
|
|||||||
openEditorDialog,
|
openEditorDialog,
|
||||||
openPrivacyNotice: () => setShowPrivacyNotice(true),
|
openPrivacyNotice: () => setShowPrivacyNotice(true),
|
||||||
openSettingsDialog,
|
openSettingsDialog,
|
||||||
|
openPermissionsDialog,
|
||||||
quit: (messages: HistoryItem[]) => {
|
quit: (messages: HistoryItem[]) => {
|
||||||
setQuittingMessages(messages);
|
setQuittingMessages(messages);
|
||||||
setTimeout(async () => {
|
setTimeout(async () => {
|
||||||
@@ -445,6 +456,7 @@ Logging in with Google... Please restart Gemini CLI to continue.
|
|||||||
setShowPrivacyNotice,
|
setShowPrivacyNotice,
|
||||||
setCorgiMode,
|
setCorgiMode,
|
||||||
setExtensionsUpdateState,
|
setExtensionsUpdateState,
|
||||||
|
openPermissionsDialog,
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -985,6 +997,7 @@ Logging in with Google... Please restart Gemini CLI to continue.
|
|||||||
!!loopDetectionConfirmationRequest ||
|
!!loopDetectionConfirmationRequest ||
|
||||||
isThemeDialogOpen ||
|
isThemeDialogOpen ||
|
||||||
isSettingsDialogOpen ||
|
isSettingsDialogOpen ||
|
||||||
|
isPermissionsDialogOpen ||
|
||||||
isAuthenticating ||
|
isAuthenticating ||
|
||||||
isAuthDialogOpen ||
|
isAuthDialogOpen ||
|
||||||
isEditorDialogOpen ||
|
isEditorDialogOpen ||
|
||||||
@@ -999,6 +1012,7 @@ Logging in with Google... Please restart Gemini CLI to continue.
|
|||||||
const uiState: UIState = useMemo(
|
const uiState: UIState = useMemo(
|
||||||
() => ({
|
() => ({
|
||||||
history: historyManager.history,
|
history: historyManager.history,
|
||||||
|
historyManager,
|
||||||
isThemeDialogOpen,
|
isThemeDialogOpen,
|
||||||
themeError,
|
themeError,
|
||||||
isAuthenticating,
|
isAuthenticating,
|
||||||
@@ -1012,6 +1026,7 @@ Logging in with Google... Please restart Gemini CLI to continue.
|
|||||||
debugMessage,
|
debugMessage,
|
||||||
quittingMessages,
|
quittingMessages,
|
||||||
isSettingsDialogOpen,
|
isSettingsDialogOpen,
|
||||||
|
isPermissionsDialogOpen,
|
||||||
slashCommands,
|
slashCommands,
|
||||||
pendingSlashCommandHistoryItems,
|
pendingSlashCommandHistoryItems,
|
||||||
commandContext,
|
commandContext,
|
||||||
@@ -1074,7 +1089,6 @@ Logging in with Google... Please restart Gemini CLI to continue.
|
|||||||
embeddedShellFocused,
|
embeddedShellFocused,
|
||||||
}),
|
}),
|
||||||
[
|
[
|
||||||
historyManager.history,
|
|
||||||
isThemeDialogOpen,
|
isThemeDialogOpen,
|
||||||
themeError,
|
themeError,
|
||||||
isAuthenticating,
|
isAuthenticating,
|
||||||
@@ -1088,6 +1102,7 @@ Logging in with Google... Please restart Gemini CLI to continue.
|
|||||||
debugMessage,
|
debugMessage,
|
||||||
quittingMessages,
|
quittingMessages,
|
||||||
isSettingsDialogOpen,
|
isSettingsDialogOpen,
|
||||||
|
isPermissionsDialogOpen,
|
||||||
slashCommands,
|
slashCommands,
|
||||||
pendingSlashCommandHistoryItems,
|
pendingSlashCommandHistoryItems,
|
||||||
commandContext,
|
commandContext,
|
||||||
@@ -1147,6 +1162,7 @@ Logging in with Google... Please restart Gemini CLI to continue.
|
|||||||
currentModel,
|
currentModel,
|
||||||
extensionsUpdateState,
|
extensionsUpdateState,
|
||||||
activePtyId,
|
activePtyId,
|
||||||
|
historyManager,
|
||||||
embeddedShellFocused,
|
embeddedShellFocused,
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
@@ -1162,6 +1178,7 @@ Logging in with Google... Please restart Gemini CLI to continue.
|
|||||||
exitEditorDialog,
|
exitEditorDialog,
|
||||||
exitPrivacyNotice: () => setShowPrivacyNotice(false),
|
exitPrivacyNotice: () => setShowPrivacyNotice(false),
|
||||||
closeSettingsDialog,
|
closeSettingsDialog,
|
||||||
|
closePermissionsDialog,
|
||||||
setShellModeActive,
|
setShellModeActive,
|
||||||
vimHandleInput,
|
vimHandleInput,
|
||||||
handleIdePromptComplete,
|
handleIdePromptComplete,
|
||||||
@@ -1184,6 +1201,7 @@ Logging in with Google... Please restart Gemini CLI to continue.
|
|||||||
handleEditorSelect,
|
handleEditorSelect,
|
||||||
exitEditorDialog,
|
exitEditorDialog,
|
||||||
closeSettingsDialog,
|
closeSettingsDialog,
|
||||||
|
closePermissionsDialog,
|
||||||
setShellModeActive,
|
setShellModeActive,
|
||||||
vimHandleInput,
|
vimHandleInput,
|
||||||
handleIdePromptComplete,
|
handleIdePromptComplete,
|
||||||
|
|||||||
@@ -0,0 +1,35 @@
|
|||||||
|
/**
|
||||||
|
* @license
|
||||||
|
* Copyright 2025 Google LLC
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { describe, it, expect, beforeEach } from 'vitest';
|
||||||
|
import { permissionsCommand } from './permissionsCommand.js';
|
||||||
|
import { type CommandContext, CommandKind } from './types.js';
|
||||||
|
import { createMockCommandContext } from '../../test-utils/mockCommandContext.js';
|
||||||
|
|
||||||
|
describe('permissionsCommand', () => {
|
||||||
|
let mockContext: CommandContext;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
mockContext = createMockCommandContext();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should have the correct name and description', () => {
|
||||||
|
expect(permissionsCommand.name).toBe('permissions');
|
||||||
|
expect(permissionsCommand.description).toBe('Manage folder trust settings');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be a built-in command', () => {
|
||||||
|
expect(permissionsCommand.kind).toBe(CommandKind.BUILT_IN);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should return an action to open the permissions dialog', () => {
|
||||||
|
const actionResult = permissionsCommand.action?.(mockContext, '');
|
||||||
|
expect(actionResult).toEqual({
|
||||||
|
type: 'dialog',
|
||||||
|
dialog: 'permissions',
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
/**
|
||||||
|
* @license
|
||||||
|
* Copyright 2025 Google LLC
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type { OpenDialogActionReturn, SlashCommand } from './types.js';
|
||||||
|
import { CommandKind } from './types.js';
|
||||||
|
|
||||||
|
export const permissionsCommand: SlashCommand = {
|
||||||
|
name: 'permissions',
|
||||||
|
description: 'Manage folder trust settings',
|
||||||
|
kind: CommandKind.BUILT_IN,
|
||||||
|
action: (): OpenDialogActionReturn => ({
|
||||||
|
type: 'dialog',
|
||||||
|
dialog: 'permissions',
|
||||||
|
}),
|
||||||
|
};
|
||||||
@@ -108,7 +108,14 @@ export interface MessageActionReturn {
|
|||||||
export interface OpenDialogActionReturn {
|
export interface OpenDialogActionReturn {
|
||||||
type: 'dialog';
|
type: 'dialog';
|
||||||
|
|
||||||
dialog: 'help' | 'auth' | 'theme' | 'editor' | 'privacy' | 'settings';
|
dialog:
|
||||||
|
| 'help'
|
||||||
|
| 'auth'
|
||||||
|
| 'theme'
|
||||||
|
| 'editor'
|
||||||
|
| 'privacy'
|
||||||
|
| 'settings'
|
||||||
|
| 'permissions';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -18,15 +18,21 @@ import { EditorSettingsDialog } from './EditorSettingsDialog.js';
|
|||||||
import { PrivacyNotice } from '../privacy/PrivacyNotice.js';
|
import { PrivacyNotice } from '../privacy/PrivacyNotice.js';
|
||||||
import { WorkspaceMigrationDialog } from './WorkspaceMigrationDialog.js';
|
import { WorkspaceMigrationDialog } from './WorkspaceMigrationDialog.js';
|
||||||
import { ProQuotaDialog } from './ProQuotaDialog.js';
|
import { ProQuotaDialog } from './ProQuotaDialog.js';
|
||||||
|
import { PermissionsModifyTrustDialog } from './PermissionsModifyTrustDialog.js';
|
||||||
import { theme } from '../semantic-colors.js';
|
import { theme } from '../semantic-colors.js';
|
||||||
import { useUIState } from '../contexts/UIStateContext.js';
|
import { useUIState } from '../contexts/UIStateContext.js';
|
||||||
import { useUIActions } from '../contexts/UIActionsContext.js';
|
import { useUIActions } from '../contexts/UIActionsContext.js';
|
||||||
import { useConfig } from '../contexts/ConfigContext.js';
|
import { useConfig } from '../contexts/ConfigContext.js';
|
||||||
import { useSettings } from '../contexts/SettingsContext.js';
|
import { useSettings } from '../contexts/SettingsContext.js';
|
||||||
import process from 'node:process';
|
import process from 'node:process';
|
||||||
|
import { type UseHistoryManagerReturn } from '../hooks/useHistoryManager.js';
|
||||||
|
|
||||||
|
interface DialogManagerProps {
|
||||||
|
addItem: UseHistoryManagerReturn['addItem'];
|
||||||
|
}
|
||||||
|
|
||||||
// Props for DialogManager
|
// Props for DialogManager
|
||||||
export const DialogManager = () => {
|
export const DialogManager = ({ addItem }: DialogManagerProps) => {
|
||||||
const config = useConfig();
|
const config = useConfig();
|
||||||
const settings = useSettings();
|
const settings = useSettings();
|
||||||
|
|
||||||
@@ -188,5 +194,14 @@ export const DialogManager = () => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (uiState.isPermissionsDialogOpen) {
|
||||||
|
return (
|
||||||
|
<PermissionsModifyTrustDialog
|
||||||
|
onExit={uiActions.closePermissionsDialog}
|
||||||
|
addItem={addItem}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,204 @@
|
|||||||
|
/**
|
||||||
|
* @license
|
||||||
|
* Copyright 2025 Google LLC
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
/// <reference types="vitest/globals" />
|
||||||
|
|
||||||
|
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
||||||
|
import type { Mock } from 'vitest';
|
||||||
|
import { renderWithProviders } from '../../test-utils/render.js';
|
||||||
|
import { PermissionsModifyTrustDialog } from './PermissionsModifyTrustDialog.js';
|
||||||
|
import { TrustLevel } from '../../config/trustedFolders.js';
|
||||||
|
import { waitFor, act } from '@testing-library/react';
|
||||||
|
import * as processUtils from '../../utils/processUtils.js';
|
||||||
|
import { usePermissionsModifyTrust } from '../hooks/usePermissionsModifyTrust.js';
|
||||||
|
|
||||||
|
// Hoist mocks for dependencies of the usePermissionsModifyTrust hook
|
||||||
|
const mockedCwd = vi.hoisted(() => vi.fn());
|
||||||
|
const mockedLoadTrustedFolders = vi.hoisted(() => vi.fn());
|
||||||
|
const mockedIsWorkspaceTrusted = vi.hoisted(() => vi.fn());
|
||||||
|
const mockedUseSettings = vi.hoisted(() => vi.fn());
|
||||||
|
|
||||||
|
// Mock the modules themselves
|
||||||
|
vi.mock('node:process', async (importOriginal) => {
|
||||||
|
const actual = await importOriginal<typeof import('node:process')>();
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
cwd: mockedCwd,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
vi.mock('../../config/trustedFolders.js', () => ({
|
||||||
|
loadTrustedFolders: mockedLoadTrustedFolders,
|
||||||
|
isWorkspaceTrusted: mockedIsWorkspaceTrusted,
|
||||||
|
TrustLevel: {
|
||||||
|
TRUST_FOLDER: 'TRUST_FOLDER',
|
||||||
|
TRUST_PARENT: 'TRUST_PARENT',
|
||||||
|
DO_NOT_TRUST: 'DO_NOT_TRUST',
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock('../contexts/SettingsContext.js', () => ({
|
||||||
|
useSettings: mockedUseSettings,
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock('../hooks/usePermissionsModifyTrust.js');
|
||||||
|
|
||||||
|
describe('PermissionsModifyTrustDialog', () => {
|
||||||
|
let mockUpdateTrustLevel: Mock;
|
||||||
|
let mockCommitTrustLevelChange: Mock;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
mockUpdateTrustLevel = vi.fn();
|
||||||
|
mockCommitTrustLevelChange = vi.fn();
|
||||||
|
vi.mocked(usePermissionsModifyTrust).mockReturnValue({
|
||||||
|
cwd: '/test/dir',
|
||||||
|
currentTrustLevel: TrustLevel.DO_NOT_TRUST,
|
||||||
|
isInheritedTrustFromParent: false,
|
||||||
|
isInheritedTrustFromIde: false,
|
||||||
|
needsRestart: false,
|
||||||
|
updateTrustLevel: mockUpdateTrustLevel,
|
||||||
|
commitTrustLevelChange: mockCommitTrustLevelChange,
|
||||||
|
isFolderTrustEnabled: true,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
vi.resetAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should render the main dialog with current trust level', async () => {
|
||||||
|
const { lastFrame } = renderWithProviders(
|
||||||
|
<PermissionsModifyTrustDialog onExit={vi.fn()} addItem={vi.fn()} />,
|
||||||
|
);
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(lastFrame()).toContain('Modify Trust Level');
|
||||||
|
expect(lastFrame()).toContain('Folder: /test/dir');
|
||||||
|
expect(lastFrame()).toContain('Current Level: DO_NOT_TRUST');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should display the inherited trust note from parent', async () => {
|
||||||
|
vi.mocked(usePermissionsModifyTrust).mockReturnValue({
|
||||||
|
cwd: '/test/dir',
|
||||||
|
currentTrustLevel: TrustLevel.DO_NOT_TRUST,
|
||||||
|
isInheritedTrustFromParent: true,
|
||||||
|
isInheritedTrustFromIde: false,
|
||||||
|
needsRestart: false,
|
||||||
|
updateTrustLevel: mockUpdateTrustLevel,
|
||||||
|
commitTrustLevelChange: mockCommitTrustLevelChange,
|
||||||
|
isFolderTrustEnabled: true,
|
||||||
|
});
|
||||||
|
const { lastFrame } = renderWithProviders(
|
||||||
|
<PermissionsModifyTrustDialog onExit={vi.fn()} addItem={vi.fn()} />,
|
||||||
|
);
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(lastFrame()).toContain(
|
||||||
|
'Note: This folder behaves as a trusted folder because one of the parent folders is trusted.',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should display the inherited trust note from IDE', async () => {
|
||||||
|
vi.mocked(usePermissionsModifyTrust).mockReturnValue({
|
||||||
|
cwd: '/test/dir',
|
||||||
|
currentTrustLevel: TrustLevel.DO_NOT_TRUST,
|
||||||
|
isInheritedTrustFromParent: false,
|
||||||
|
isInheritedTrustFromIde: true,
|
||||||
|
needsRestart: false,
|
||||||
|
updateTrustLevel: mockUpdateTrustLevel,
|
||||||
|
commitTrustLevelChange: mockCommitTrustLevelChange,
|
||||||
|
isFolderTrustEnabled: true,
|
||||||
|
});
|
||||||
|
const { lastFrame } = renderWithProviders(
|
||||||
|
<PermissionsModifyTrustDialog onExit={vi.fn()} addItem={vi.fn()} />,
|
||||||
|
);
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(lastFrame()).toContain(
|
||||||
|
'Note: This folder behaves as a trusted folder because the connected IDE workspace is trusted.',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should call onExit when escape is pressed', async () => {
|
||||||
|
const onExit = vi.fn();
|
||||||
|
const { stdin, lastFrame } = renderWithProviders(
|
||||||
|
<PermissionsModifyTrustDialog onExit={onExit} addItem={vi.fn()} />,
|
||||||
|
);
|
||||||
|
|
||||||
|
await waitFor(() => expect(lastFrame()).not.toContain('Loading...'));
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
stdin.write('\x1b'); // escape key
|
||||||
|
});
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(onExit).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should commit, restart, and exit on `r` keypress', async () => {
|
||||||
|
const mockRelaunchApp = vi
|
||||||
|
.spyOn(processUtils, 'relaunchApp')
|
||||||
|
.mockResolvedValue(undefined);
|
||||||
|
vi.mocked(usePermissionsModifyTrust).mockReturnValue({
|
||||||
|
cwd: '/test/dir',
|
||||||
|
currentTrustLevel: TrustLevel.DO_NOT_TRUST,
|
||||||
|
isInheritedTrustFromParent: false,
|
||||||
|
isInheritedTrustFromIde: false,
|
||||||
|
needsRestart: true,
|
||||||
|
updateTrustLevel: mockUpdateTrustLevel,
|
||||||
|
commitTrustLevelChange: mockCommitTrustLevelChange,
|
||||||
|
isFolderTrustEnabled: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
const onExit = vi.fn();
|
||||||
|
const { stdin, lastFrame } = renderWithProviders(
|
||||||
|
<PermissionsModifyTrustDialog onExit={onExit} addItem={vi.fn()} />,
|
||||||
|
);
|
||||||
|
|
||||||
|
await waitFor(() => expect(lastFrame()).not.toContain('Loading...'));
|
||||||
|
|
||||||
|
act(() => stdin.write('r')); // Press 'r' to restart
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(mockCommitTrustLevelChange).toHaveBeenCalled();
|
||||||
|
expect(mockRelaunchApp).toHaveBeenCalled();
|
||||||
|
expect(onExit).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
mockRelaunchApp.mockRestore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not commit when escape is pressed during restart prompt', async () => {
|
||||||
|
vi.mocked(usePermissionsModifyTrust).mockReturnValue({
|
||||||
|
cwd: '/test/dir',
|
||||||
|
currentTrustLevel: TrustLevel.DO_NOT_TRUST,
|
||||||
|
isInheritedTrustFromParent: false,
|
||||||
|
isInheritedTrustFromIde: false,
|
||||||
|
needsRestart: true,
|
||||||
|
updateTrustLevel: mockUpdateTrustLevel,
|
||||||
|
commitTrustLevelChange: mockCommitTrustLevelChange,
|
||||||
|
isFolderTrustEnabled: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
const onExit = vi.fn();
|
||||||
|
const { stdin, lastFrame } = renderWithProviders(
|
||||||
|
<PermissionsModifyTrustDialog onExit={onExit} addItem={vi.fn()} />,
|
||||||
|
);
|
||||||
|
|
||||||
|
await waitFor(() => expect(lastFrame()).not.toContain('Loading...'));
|
||||||
|
|
||||||
|
act(() => stdin.write('\x1b')); // Press escape
|
||||||
|
|
||||||
|
await waitFor(() => {
|
||||||
|
expect(mockCommitTrustLevelChange).not.toHaveBeenCalled();
|
||||||
|
expect(onExit).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,122 @@
|
|||||||
|
/**
|
||||||
|
* @license
|
||||||
|
* Copyright 2025 Google LLC
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { Box, Text } from 'ink';
|
||||||
|
import type React from 'react';
|
||||||
|
import { TrustLevel } from '../../config/trustedFolders.js';
|
||||||
|
import { useKeypress } from '../hooks/useKeypress.js';
|
||||||
|
import { usePermissionsModifyTrust } from '../hooks/usePermissionsModifyTrust.js';
|
||||||
|
import { theme } from '../semantic-colors.js';
|
||||||
|
import { RadioButtonSelect } from './shared/RadioButtonSelect.js';
|
||||||
|
import { relaunchApp } from '../../utils/processUtils.js';
|
||||||
|
import { type UseHistoryManagerReturn } from '../hooks/useHistoryManager.js';
|
||||||
|
|
||||||
|
interface PermissionsModifyTrustDialogProps {
|
||||||
|
onExit: () => void;
|
||||||
|
addItem: UseHistoryManagerReturn['addItem'];
|
||||||
|
}
|
||||||
|
|
||||||
|
const TRUST_LEVEL_ITEMS = [
|
||||||
|
{
|
||||||
|
label: 'Trust this folder',
|
||||||
|
value: TrustLevel.TRUST_FOLDER,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: 'Trust parent folder',
|
||||||
|
value: TrustLevel.TRUST_PARENT,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: "Don't trust",
|
||||||
|
value: TrustLevel.DO_NOT_TRUST,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
export function PermissionsModifyTrustDialog({
|
||||||
|
onExit,
|
||||||
|
addItem,
|
||||||
|
}: PermissionsModifyTrustDialogProps): React.JSX.Element {
|
||||||
|
const {
|
||||||
|
cwd,
|
||||||
|
currentTrustLevel,
|
||||||
|
isInheritedTrustFromParent,
|
||||||
|
isInheritedTrustFromIde,
|
||||||
|
needsRestart,
|
||||||
|
updateTrustLevel,
|
||||||
|
commitTrustLevelChange,
|
||||||
|
} = usePermissionsModifyTrust(onExit, addItem);
|
||||||
|
|
||||||
|
useKeypress(
|
||||||
|
(key) => {
|
||||||
|
if (key.name === 'escape') {
|
||||||
|
onExit();
|
||||||
|
}
|
||||||
|
if (needsRestart && key.name === 'r') {
|
||||||
|
commitTrustLevelChange();
|
||||||
|
relaunchApp();
|
||||||
|
onExit();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ isActive: true },
|
||||||
|
);
|
||||||
|
|
||||||
|
const index = TRUST_LEVEL_ITEMS.findIndex(
|
||||||
|
(item) => item.value === currentTrustLevel,
|
||||||
|
);
|
||||||
|
const initialIndex = index === -1 ? 0 : index;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<Box
|
||||||
|
borderStyle="round"
|
||||||
|
borderColor={theme.border.default}
|
||||||
|
flexDirection="column"
|
||||||
|
padding={1}
|
||||||
|
>
|
||||||
|
<Box flexDirection="column" paddingBottom={1}>
|
||||||
|
<Text bold>{'> '}Modify Trust Level</Text>
|
||||||
|
<Box marginTop={1} />
|
||||||
|
<Text>Folder: {cwd}</Text>
|
||||||
|
<Text>
|
||||||
|
Current Level: <Text bold>{currentTrustLevel || 'Not Set'}</Text>
|
||||||
|
</Text>
|
||||||
|
{isInheritedTrustFromParent && (
|
||||||
|
<Text color={theme.text.secondary}>
|
||||||
|
Note: This folder behaves as a trusted folder because one of the
|
||||||
|
parent folders is trusted. It will remain trusted even if you set
|
||||||
|
a different trust level here. To change this, you need to modify
|
||||||
|
the trust setting in the parent folder.
|
||||||
|
</Text>
|
||||||
|
)}
|
||||||
|
{isInheritedTrustFromIde && (
|
||||||
|
<Text color={theme.text.secondary}>
|
||||||
|
Note: This folder behaves as a trusted folder because the
|
||||||
|
connected IDE workspace is trusted. It will remain trusted even if
|
||||||
|
you set a different trust level here.
|
||||||
|
</Text>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
|
|
||||||
|
<RadioButtonSelect
|
||||||
|
items={TRUST_LEVEL_ITEMS}
|
||||||
|
onSelect={updateTrustLevel}
|
||||||
|
isFocused={true}
|
||||||
|
initialIndex={initialIndex}
|
||||||
|
/>
|
||||||
|
<Box marginTop={1}>
|
||||||
|
<Text color={theme.text.secondary}>(Use Enter to select)</Text>
|
||||||
|
</Box>
|
||||||
|
</Box>
|
||||||
|
{needsRestart && (
|
||||||
|
<Box marginLeft={1} marginTop={1}>
|
||||||
|
<Text color={theme.status.warning}>
|
||||||
|
To apply the trust changes, Gemini CLI must be restarted. Press
|
||||||
|
'r' to restart CLI now.
|
||||||
|
</Text>
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -31,6 +31,7 @@ export interface UIActions {
|
|||||||
exitEditorDialog: () => void;
|
exitEditorDialog: () => void;
|
||||||
exitPrivacyNotice: () => void;
|
exitPrivacyNotice: () => void;
|
||||||
closeSettingsDialog: () => void;
|
closeSettingsDialog: () => void;
|
||||||
|
closePermissionsDialog: () => void;
|
||||||
setShellModeActive: (value: boolean) => void;
|
setShellModeActive: (value: boolean) => void;
|
||||||
vimHandleInput: (key: Key) => boolean;
|
vimHandleInput: (key: Key) => boolean;
|
||||||
handleIdePromptComplete: (result: IdeIntegrationNudgeResult) => void;
|
handleIdePromptComplete: (result: IdeIntegrationNudgeResult) => void;
|
||||||
|
|||||||
@@ -35,8 +35,11 @@ export interface ProQuotaDialogRequest {
|
|||||||
resolve: (intent: FallbackIntent) => void;
|
resolve: (intent: FallbackIntent) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
import { type UseHistoryManagerReturn } from '../hooks/useHistoryManager.js';
|
||||||
|
|
||||||
export interface UIState {
|
export interface UIState {
|
||||||
history: HistoryItem[];
|
history: HistoryItem[];
|
||||||
|
historyManager: UseHistoryManagerReturn;
|
||||||
isThemeDialogOpen: boolean;
|
isThemeDialogOpen: boolean;
|
||||||
themeError: string | null;
|
themeError: string | null;
|
||||||
isAuthenticating: boolean;
|
isAuthenticating: boolean;
|
||||||
@@ -50,6 +53,7 @@ export interface UIState {
|
|||||||
debugMessage: string;
|
debugMessage: string;
|
||||||
quittingMessages: HistoryItem[] | null;
|
quittingMessages: HistoryItem[] | null;
|
||||||
isSettingsDialogOpen: boolean;
|
isSettingsDialogOpen: boolean;
|
||||||
|
isPermissionsDialogOpen: boolean;
|
||||||
slashCommands: readonly SlashCommand[];
|
slashCommands: readonly SlashCommand[];
|
||||||
pendingSlashCommandHistoryItems: HistoryItemWithoutId[];
|
pendingSlashCommandHistoryItems: HistoryItemWithoutId[];
|
||||||
commandContext: CommandContext;
|
commandContext: CommandContext;
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ interface SlashCommandProcessorActions {
|
|||||||
openEditorDialog: () => void;
|
openEditorDialog: () => void;
|
||||||
openPrivacyNotice: () => void;
|
openPrivacyNotice: () => void;
|
||||||
openSettingsDialog: () => void;
|
openSettingsDialog: () => void;
|
||||||
|
openPermissionsDialog: () => void;
|
||||||
quit: (messages: HistoryItem[]) => void;
|
quit: (messages: HistoryItem[]) => void;
|
||||||
setDebugMessage: (message: string) => void;
|
setDebugMessage: (message: string) => void;
|
||||||
toggleCorgiMode: () => void;
|
toggleCorgiMode: () => void;
|
||||||
@@ -373,6 +374,9 @@ export const useSlashCommandProcessor = (
|
|||||||
case 'settings':
|
case 'settings':
|
||||||
actions.openSettingsDialog();
|
actions.openSettingsDialog();
|
||||||
return { type: 'handled' };
|
return { type: 'handled' };
|
||||||
|
case 'permissions':
|
||||||
|
actions.openPermissionsDialog();
|
||||||
|
return { type: 'handled' };
|
||||||
case 'help':
|
case 'help':
|
||||||
return { type: 'handled' };
|
return { type: 'handled' };
|
||||||
default: {
|
default: {
|
||||||
|
|||||||
@@ -187,7 +187,10 @@ describe('useExtensionUpdates', () => {
|
|||||||
JSON.stringify({ name: 'test-extension', version: '1.1.0' }),
|
JSON.stringify({ name: 'test-extension', version: '1.1.0' }),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
vi.mocked(isWorkspaceTrusted).mockReturnValue(true);
|
vi.mocked(isWorkspaceTrusted).mockReturnValue({
|
||||||
|
isTrusted: true,
|
||||||
|
source: 'file',
|
||||||
|
});
|
||||||
|
|
||||||
renderHook(() => useExtensionUpdates([extension], addItem, tempHomeDir));
|
renderHook(() => useExtensionUpdates([extension], addItem, tempHomeDir));
|
||||||
|
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ describe('useFolderTrust', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should not open dialog when folder is already trusted', () => {
|
it('should not open dialog when folder is already trusted', () => {
|
||||||
isWorkspaceTrustedSpy.mockReturnValue(true);
|
isWorkspaceTrustedSpy.mockReturnValue({ isTrusted: true, source: 'file' });
|
||||||
const { result } = renderHook(() =>
|
const { result } = renderHook(() =>
|
||||||
useFolderTrust(mockSettings, onTrustChange),
|
useFolderTrust(mockSettings, onTrustChange),
|
||||||
);
|
);
|
||||||
@@ -65,7 +65,7 @@ describe('useFolderTrust', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should not open dialog when folder is already untrusted', () => {
|
it('should not open dialog when folder is already untrusted', () => {
|
||||||
isWorkspaceTrustedSpy.mockReturnValue(false);
|
isWorkspaceTrustedSpy.mockReturnValue({ isTrusted: false, source: 'file' });
|
||||||
const { result } = renderHook(() =>
|
const { result } = renderHook(() =>
|
||||||
useFolderTrust(mockSettings, onTrustChange),
|
useFolderTrust(mockSettings, onTrustChange),
|
||||||
);
|
);
|
||||||
@@ -74,7 +74,10 @@ describe('useFolderTrust', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should open dialog when folder trust is undefined', () => {
|
it('should open dialog when folder trust is undefined', () => {
|
||||||
isWorkspaceTrustedSpy.mockReturnValue(undefined);
|
isWorkspaceTrustedSpy.mockReturnValue({
|
||||||
|
isTrusted: undefined,
|
||||||
|
source: undefined,
|
||||||
|
});
|
||||||
const { result } = renderHook(() =>
|
const { result } = renderHook(() =>
|
||||||
useFolderTrust(mockSettings, onTrustChange),
|
useFolderTrust(mockSettings, onTrustChange),
|
||||||
);
|
);
|
||||||
@@ -83,14 +86,14 @@ describe('useFolderTrust', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should handle TRUST_FOLDER choice', () => {
|
it('should handle TRUST_FOLDER choice', () => {
|
||||||
isWorkspaceTrustedSpy
|
isWorkspaceTrustedSpy.mockReturnValue({
|
||||||
.mockReturnValueOnce(undefined)
|
isTrusted: undefined,
|
||||||
.mockReturnValueOnce(true);
|
source: undefined,
|
||||||
|
});
|
||||||
const { result } = renderHook(() =>
|
const { result } = renderHook(() =>
|
||||||
useFolderTrust(mockSettings, onTrustChange),
|
useFolderTrust(mockSettings, onTrustChange),
|
||||||
);
|
);
|
||||||
|
|
||||||
isWorkspaceTrustedSpy.mockReturnValue(true);
|
|
||||||
act(() => {
|
act(() => {
|
||||||
result.current.handleFolderTrustSelect(FolderTrustChoice.TRUST_FOLDER);
|
result.current.handleFolderTrustSelect(FolderTrustChoice.TRUST_FOLDER);
|
||||||
});
|
});
|
||||||
@@ -105,9 +108,10 @@ describe('useFolderTrust', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should handle TRUST_PARENT choice', () => {
|
it('should handle TRUST_PARENT choice', () => {
|
||||||
isWorkspaceTrustedSpy
|
isWorkspaceTrustedSpy.mockReturnValue({
|
||||||
.mockReturnValueOnce(undefined)
|
isTrusted: undefined,
|
||||||
.mockReturnValueOnce(true);
|
source: undefined,
|
||||||
|
});
|
||||||
const { result } = renderHook(() =>
|
const { result } = renderHook(() =>
|
||||||
useFolderTrust(mockSettings, onTrustChange),
|
useFolderTrust(mockSettings, onTrustChange),
|
||||||
);
|
);
|
||||||
@@ -125,9 +129,10 @@ describe('useFolderTrust', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should handle DO_NOT_TRUST choice and trigger restart', () => {
|
it('should handle DO_NOT_TRUST choice and trigger restart', () => {
|
||||||
isWorkspaceTrustedSpy
|
isWorkspaceTrustedSpy.mockReturnValue({
|
||||||
.mockReturnValueOnce(undefined)
|
isTrusted: undefined,
|
||||||
.mockReturnValueOnce(false);
|
source: undefined,
|
||||||
|
});
|
||||||
const { result } = renderHook(() =>
|
const { result } = renderHook(() =>
|
||||||
useFolderTrust(mockSettings, onTrustChange),
|
useFolderTrust(mockSettings, onTrustChange),
|
||||||
);
|
);
|
||||||
@@ -146,7 +151,10 @@ describe('useFolderTrust', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should do nothing for default choice', () => {
|
it('should do nothing for default choice', () => {
|
||||||
isWorkspaceTrustedSpy.mockReturnValue(undefined);
|
isWorkspaceTrustedSpy.mockReturnValue({
|
||||||
|
isTrusted: undefined,
|
||||||
|
source: undefined,
|
||||||
|
});
|
||||||
const { result } = renderHook(() =>
|
const { result } = renderHook(() =>
|
||||||
useFolderTrust(mockSettings, onTrustChange),
|
useFolderTrust(mockSettings, onTrustChange),
|
||||||
);
|
);
|
||||||
@@ -164,7 +172,7 @@ describe('useFolderTrust', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should set isRestarting to true when trust status changes from false to true', () => {
|
it('should set isRestarting to true when trust status changes from false to true', () => {
|
||||||
isWorkspaceTrustedSpy.mockReturnValueOnce(false).mockReturnValueOnce(true); // Initially untrusted, then trusted
|
isWorkspaceTrustedSpy.mockReturnValue({ isTrusted: false, source: 'file' }); // Initially untrusted
|
||||||
const { result } = renderHook(() =>
|
const { result } = renderHook(() =>
|
||||||
useFolderTrust(mockSettings, onTrustChange),
|
useFolderTrust(mockSettings, onTrustChange),
|
||||||
);
|
);
|
||||||
@@ -178,9 +186,10 @@ describe('useFolderTrust', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should not set isRestarting to true when trust status does not change', () => {
|
it('should not set isRestarting to true when trust status does not change', () => {
|
||||||
isWorkspaceTrustedSpy
|
isWorkspaceTrustedSpy.mockReturnValue({
|
||||||
.mockReturnValueOnce(undefined)
|
isTrusted: undefined,
|
||||||
.mockReturnValueOnce(true); // Initially undefined, then trust
|
source: undefined,
|
||||||
|
});
|
||||||
const { result } = renderHook(() =>
|
const { result } = renderHook(() =>
|
||||||
useFolderTrust(mockSettings, onTrustChange),
|
useFolderTrust(mockSettings, onTrustChange),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ export const useFolderTrust = (
|
|||||||
const folderTrust = settings.merged.security?.folderTrust?.enabled;
|
const folderTrust = settings.merged.security?.folderTrust?.enabled;
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const trusted = isWorkspaceTrusted(settings.merged);
|
const { isTrusted: trusted } = isWorkspaceTrusted(settings.merged);
|
||||||
setIsTrusted(trusted);
|
setIsTrusted(trusted);
|
||||||
setIsFolderTrustDialogOpen(trusted === undefined);
|
setIsFolderTrustDialogOpen(trusted === undefined);
|
||||||
onTrustChange(trusted);
|
onTrustChange(trusted);
|
||||||
|
|||||||
@@ -0,0 +1,263 @@
|
|||||||
|
/**
|
||||||
|
* @license
|
||||||
|
* Copyright 2025 Google LLC
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
/// <reference types="vitest/globals" />
|
||||||
|
|
||||||
|
import {
|
||||||
|
describe,
|
||||||
|
it,
|
||||||
|
expect,
|
||||||
|
vi,
|
||||||
|
beforeEach,
|
||||||
|
afterEach,
|
||||||
|
type Mock,
|
||||||
|
} from 'vitest';
|
||||||
|
import { renderHook, act } from '@testing-library/react';
|
||||||
|
import { usePermissionsModifyTrust } from './usePermissionsModifyTrust.js';
|
||||||
|
import { TrustLevel } from '../../config/trustedFolders.js';
|
||||||
|
import type { LoadedSettings } from '../../config/settings.js';
|
||||||
|
import type { LoadedTrustedFolders } from '../../config/trustedFolders.js';
|
||||||
|
|
||||||
|
// Hoist mocks
|
||||||
|
const mockedCwd = vi.hoisted(() => vi.fn());
|
||||||
|
const mockedLoadTrustedFolders = vi.hoisted(() => vi.fn());
|
||||||
|
const mockedIsWorkspaceTrusted = vi.hoisted(() => vi.fn());
|
||||||
|
const mockedUseSettings = vi.hoisted(() => vi.fn());
|
||||||
|
|
||||||
|
// Mock modules
|
||||||
|
vi.mock('node:process', () => ({
|
||||||
|
cwd: mockedCwd,
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock('../../config/trustedFolders.js', () => ({
|
||||||
|
loadTrustedFolders: mockedLoadTrustedFolders,
|
||||||
|
isWorkspaceTrusted: mockedIsWorkspaceTrusted,
|
||||||
|
TrustLevel: {
|
||||||
|
TRUST_FOLDER: 'TRUST_FOLDER',
|
||||||
|
TRUST_PARENT: 'TRUST_PARENT',
|
||||||
|
DO_NOT_TRUST: 'DO_NOT_TRUST',
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock('../contexts/SettingsContext.js', () => ({
|
||||||
|
useSettings: mockedUseSettings,
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('usePermissionsModifyTrust', () => {
|
||||||
|
let mockOnExit: Mock;
|
||||||
|
let mockAddItem: Mock;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
mockAddItem = vi.fn();
|
||||||
|
mockOnExit = vi.fn();
|
||||||
|
|
||||||
|
mockedCwd.mockReturnValue('/test/dir');
|
||||||
|
mockedUseSettings.mockReturnValue({
|
||||||
|
merged: {
|
||||||
|
security: {
|
||||||
|
folderTrust: {
|
||||||
|
enabled: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as LoadedSettings);
|
||||||
|
mockedIsWorkspaceTrusted.mockReturnValue({
|
||||||
|
isTrusted: undefined,
|
||||||
|
source: undefined,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
vi.resetAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should initialize with the correct trust level', () => {
|
||||||
|
mockedLoadTrustedFolders.mockReturnValue({
|
||||||
|
user: { config: { '/test/dir': TrustLevel.TRUST_FOLDER } },
|
||||||
|
} as unknown as LoadedTrustedFolders);
|
||||||
|
mockedIsWorkspaceTrusted.mockReturnValue({
|
||||||
|
isTrusted: true,
|
||||||
|
source: 'file',
|
||||||
|
});
|
||||||
|
|
||||||
|
const { result } = renderHook(() =>
|
||||||
|
usePermissionsModifyTrust(mockOnExit, mockAddItem),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.current.currentTrustLevel).toBe(TrustLevel.TRUST_FOLDER);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should detect inherited trust from parent', () => {
|
||||||
|
mockedLoadTrustedFolders.mockReturnValue({
|
||||||
|
user: { config: {} },
|
||||||
|
setValue: vi.fn(),
|
||||||
|
} as unknown as LoadedTrustedFolders);
|
||||||
|
mockedIsWorkspaceTrusted.mockReturnValue({
|
||||||
|
isTrusted: true,
|
||||||
|
source: 'file',
|
||||||
|
});
|
||||||
|
|
||||||
|
const { result } = renderHook(() =>
|
||||||
|
usePermissionsModifyTrust(mockOnExit, mockAddItem),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.current.isInheritedTrustFromParent).toBe(true);
|
||||||
|
expect(result.current.isInheritedTrustFromIde).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should detect inherited trust from IDE', () => {
|
||||||
|
mockedLoadTrustedFolders.mockReturnValue({
|
||||||
|
user: { config: {} }, // No explicit trust
|
||||||
|
} as unknown as LoadedTrustedFolders);
|
||||||
|
mockedIsWorkspaceTrusted.mockReturnValue({
|
||||||
|
isTrusted: true,
|
||||||
|
source: 'ide',
|
||||||
|
});
|
||||||
|
|
||||||
|
const { result } = renderHook(() =>
|
||||||
|
usePermissionsModifyTrust(mockOnExit, mockAddItem),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.current.isInheritedTrustFromIde).toBe(true);
|
||||||
|
expect(result.current.isInheritedTrustFromParent).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should set needsRestart but not save when trust changes', () => {
|
||||||
|
const mockSetValue = vi.fn();
|
||||||
|
mockedLoadTrustedFolders.mockReturnValue({
|
||||||
|
user: { config: {} },
|
||||||
|
setValue: mockSetValue,
|
||||||
|
} as unknown as LoadedTrustedFolders);
|
||||||
|
|
||||||
|
mockedIsWorkspaceTrusted
|
||||||
|
.mockReturnValueOnce({ isTrusted: false, source: 'file' })
|
||||||
|
.mockReturnValueOnce({ isTrusted: true, source: 'file' });
|
||||||
|
|
||||||
|
const { result } = renderHook(() =>
|
||||||
|
usePermissionsModifyTrust(mockOnExit, mockAddItem),
|
||||||
|
);
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.updateTrustLevel(TrustLevel.TRUST_FOLDER);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.current.needsRestart).toBe(true);
|
||||||
|
expect(mockSetValue).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should save immediately if trust does not change', () => {
|
||||||
|
const mockSetValue = vi.fn();
|
||||||
|
mockedLoadTrustedFolders.mockReturnValue({
|
||||||
|
user: { config: {} },
|
||||||
|
setValue: mockSetValue,
|
||||||
|
} as unknown as LoadedTrustedFolders);
|
||||||
|
|
||||||
|
mockedIsWorkspaceTrusted.mockReturnValue({
|
||||||
|
isTrusted: true,
|
||||||
|
source: 'file',
|
||||||
|
});
|
||||||
|
|
||||||
|
const { result } = renderHook(() =>
|
||||||
|
usePermissionsModifyTrust(mockOnExit, mockAddItem),
|
||||||
|
);
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.updateTrustLevel(TrustLevel.TRUST_PARENT);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.current.needsRestart).toBe(false);
|
||||||
|
expect(mockSetValue).toHaveBeenCalledWith(
|
||||||
|
'/test/dir',
|
||||||
|
TrustLevel.TRUST_PARENT,
|
||||||
|
);
|
||||||
|
expect(mockOnExit).toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should commit the pending trust level change', () => {
|
||||||
|
const mockSetValue = vi.fn();
|
||||||
|
mockedLoadTrustedFolders.mockReturnValue({
|
||||||
|
user: { config: {} },
|
||||||
|
setValue: mockSetValue,
|
||||||
|
} as unknown as LoadedTrustedFolders);
|
||||||
|
|
||||||
|
mockedIsWorkspaceTrusted
|
||||||
|
.mockReturnValueOnce({ isTrusted: false, source: 'file' })
|
||||||
|
.mockReturnValueOnce({ isTrusted: true, source: 'file' });
|
||||||
|
|
||||||
|
const { result } = renderHook(() =>
|
||||||
|
usePermissionsModifyTrust(mockOnExit, mockAddItem),
|
||||||
|
);
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.updateTrustLevel(TrustLevel.TRUST_FOLDER);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.current.needsRestart).toBe(true);
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.commitTrustLevelChange();
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(mockSetValue).toHaveBeenCalledWith(
|
||||||
|
'/test/dir',
|
||||||
|
TrustLevel.TRUST_FOLDER,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should add warning when setting DO_NOT_TRUST but still trusted by parent', () => {
|
||||||
|
mockedLoadTrustedFolders.mockReturnValue({
|
||||||
|
user: { config: {} },
|
||||||
|
setValue: vi.fn(),
|
||||||
|
} as unknown as LoadedTrustedFolders);
|
||||||
|
mockedIsWorkspaceTrusted.mockReturnValue({
|
||||||
|
isTrusted: true,
|
||||||
|
source: 'file',
|
||||||
|
});
|
||||||
|
|
||||||
|
const { result } = renderHook(() =>
|
||||||
|
usePermissionsModifyTrust(mockOnExit, mockAddItem),
|
||||||
|
);
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.updateTrustLevel(TrustLevel.DO_NOT_TRUST);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(mockAddItem).toHaveBeenCalledWith(
|
||||||
|
{
|
||||||
|
type: 'warning',
|
||||||
|
text: 'Note: This folder is still trusted because a parent folder is trusted.',
|
||||||
|
},
|
||||||
|
expect.any(Number),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should add warning when setting DO_NOT_TRUST but still trusted by IDE', () => {
|
||||||
|
mockedLoadTrustedFolders.mockReturnValue({
|
||||||
|
user: { config: {} },
|
||||||
|
setValue: vi.fn(),
|
||||||
|
} as unknown as LoadedTrustedFolders);
|
||||||
|
mockedIsWorkspaceTrusted.mockReturnValue({
|
||||||
|
isTrusted: true,
|
||||||
|
source: 'ide',
|
||||||
|
});
|
||||||
|
|
||||||
|
const { result } = renderHook(() =>
|
||||||
|
usePermissionsModifyTrust(mockOnExit, mockAddItem),
|
||||||
|
);
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
result.current.updateTrustLevel(TrustLevel.DO_NOT_TRUST);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(mockAddItem).toHaveBeenCalledWith(
|
||||||
|
{
|
||||||
|
type: 'warning',
|
||||||
|
text: 'Note: This folder is still trusted because the connected IDE workspace is trusted.',
|
||||||
|
},
|
||||||
|
expect.any(Number),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,128 @@
|
|||||||
|
/**
|
||||||
|
* @license
|
||||||
|
* Copyright 2025 Google LLC
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { useState, useCallback } from 'react';
|
||||||
|
import * as process from 'node:process';
|
||||||
|
import {
|
||||||
|
loadTrustedFolders,
|
||||||
|
TrustLevel,
|
||||||
|
isWorkspaceTrusted,
|
||||||
|
} from '../../config/trustedFolders.js';
|
||||||
|
import { useSettings } from '../contexts/SettingsContext.js';
|
||||||
|
|
||||||
|
import { MessageType } from '../types.js';
|
||||||
|
import { type UseHistoryManagerReturn } from './useHistoryManager.js';
|
||||||
|
import type { LoadedSettings } from '../../config/settings.js';
|
||||||
|
|
||||||
|
interface TrustState {
|
||||||
|
currentTrustLevel: TrustLevel | undefined;
|
||||||
|
isInheritedTrustFromParent: boolean;
|
||||||
|
isInheritedTrustFromIde: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getInitialTrustState(
|
||||||
|
settings: LoadedSettings,
|
||||||
|
cwd: string,
|
||||||
|
): TrustState {
|
||||||
|
const folders = loadTrustedFolders();
|
||||||
|
const explicitTrustLevel = folders.user.config[cwd];
|
||||||
|
const { isTrusted, source } = isWorkspaceTrusted(settings.merged);
|
||||||
|
|
||||||
|
const isInheritedTrust =
|
||||||
|
isTrusted &&
|
||||||
|
(!explicitTrustLevel || explicitTrustLevel === TrustLevel.DO_NOT_TRUST);
|
||||||
|
|
||||||
|
return {
|
||||||
|
currentTrustLevel: explicitTrustLevel,
|
||||||
|
isInheritedTrustFromParent: !!(source === 'file' && isInheritedTrust),
|
||||||
|
isInheritedTrustFromIde: !!(source === 'ide' && isInheritedTrust),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const usePermissionsModifyTrust = (
|
||||||
|
onExit: () => void,
|
||||||
|
addItem: UseHistoryManagerReturn['addItem'],
|
||||||
|
) => {
|
||||||
|
const settings = useSettings();
|
||||||
|
const cwd = process.cwd();
|
||||||
|
|
||||||
|
const [initialState] = useState(() => getInitialTrustState(settings, cwd));
|
||||||
|
|
||||||
|
const [currentTrustLevel] = useState<TrustLevel | undefined>(
|
||||||
|
initialState.currentTrustLevel,
|
||||||
|
);
|
||||||
|
const [pendingTrustLevel, setPendingTrustLevel] = useState<
|
||||||
|
TrustLevel | undefined
|
||||||
|
>();
|
||||||
|
const [isInheritedTrustFromParent] = useState(
|
||||||
|
initialState.isInheritedTrustFromParent,
|
||||||
|
);
|
||||||
|
const [isInheritedTrustFromIde] = useState(
|
||||||
|
initialState.isInheritedTrustFromIde,
|
||||||
|
);
|
||||||
|
const [needsRestart, setNeedsRestart] = useState(false);
|
||||||
|
|
||||||
|
const isFolderTrustEnabled = !!settings.merged.security?.folderTrust?.enabled;
|
||||||
|
|
||||||
|
const updateTrustLevel = useCallback(
|
||||||
|
(trustLevel: TrustLevel) => {
|
||||||
|
const wasTrusted = isWorkspaceTrusted(settings.merged).isTrusted;
|
||||||
|
|
||||||
|
// Create a temporary config to check the new trust status without writing
|
||||||
|
const currentConfig = loadTrustedFolders().user.config;
|
||||||
|
const newConfig = { ...currentConfig, [cwd]: trustLevel };
|
||||||
|
|
||||||
|
const { isTrusted, source } = isWorkspaceTrusted(
|
||||||
|
settings.merged,
|
||||||
|
newConfig,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (trustLevel === TrustLevel.DO_NOT_TRUST && isTrusted) {
|
||||||
|
let message =
|
||||||
|
'Note: This folder is still trusted because the connected IDE workspace is trusted.';
|
||||||
|
if (source === 'file') {
|
||||||
|
message =
|
||||||
|
'Note: This folder is still trusted because a parent folder is trusted.';
|
||||||
|
}
|
||||||
|
addItem(
|
||||||
|
{
|
||||||
|
type: MessageType.WARNING,
|
||||||
|
text: message,
|
||||||
|
},
|
||||||
|
Date.now(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (wasTrusted !== isTrusted) {
|
||||||
|
setPendingTrustLevel(trustLevel);
|
||||||
|
setNeedsRestart(true);
|
||||||
|
} else {
|
||||||
|
const folders = loadTrustedFolders();
|
||||||
|
folders.setValue(cwd, trustLevel);
|
||||||
|
onExit();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[cwd, settings.merged, onExit, addItem],
|
||||||
|
);
|
||||||
|
|
||||||
|
const commitTrustLevelChange = useCallback(() => {
|
||||||
|
if (pendingTrustLevel) {
|
||||||
|
const folders = loadTrustedFolders();
|
||||||
|
folders.setValue(cwd, pendingTrustLevel);
|
||||||
|
}
|
||||||
|
}, [cwd, pendingTrustLevel]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
cwd,
|
||||||
|
currentTrustLevel,
|
||||||
|
isInheritedTrustFromParent,
|
||||||
|
isInheritedTrustFromIde,
|
||||||
|
needsRestart,
|
||||||
|
updateTrustLevel,
|
||||||
|
commitTrustLevelChange,
|
||||||
|
isFolderTrustEnabled,
|
||||||
|
};
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user