mirror of
https://github.com/google-gemini/gemini-cli.git
synced 2026-04-18 01:00:39 -07:00
Override Gemini CLI trust with VScode workspace trust when in IDE (#7433)
This commit is contained in:
@@ -255,3 +255,62 @@ describe('isWorkspaceTrusted', () => {
|
||||
expect(isWorkspaceTrusted(mockSettings)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
import { getIdeTrust } from '@google/gemini-cli-core';
|
||||
|
||||
vi.mock('@google/gemini-cli-core', async (importOriginal) => {
|
||||
const actual = await importOriginal<Record<string, unknown>>();
|
||||
return {
|
||||
...actual,
|
||||
getIdeTrust: vi.fn(),
|
||||
};
|
||||
});
|
||||
|
||||
describe('isWorkspaceTrusted with IDE override', () => {
|
||||
const mockSettings: Settings = {
|
||||
security: {
|
||||
folderTrust: {
|
||||
enabled: true,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
it('should return true when ideTrust is true, ignoring config', () => {
|
||||
vi.mocked(getIdeTrust).mockReturnValue(true);
|
||||
// Even if config says don't trust, ideTrust should win.
|
||||
vi.spyOn(fs, 'readFileSync').mockReturnValue(
|
||||
JSON.stringify({ [process.cwd()]: TrustLevel.DO_NOT_TRUST }),
|
||||
);
|
||||
expect(isWorkspaceTrusted(mockSettings)).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false when ideTrust is false, ignoring config', () => {
|
||||
vi.mocked(getIdeTrust).mockReturnValue(false);
|
||||
// Even if config says trust, ideTrust should win.
|
||||
vi.spyOn(fs, 'readFileSync').mockReturnValue(
|
||||
JSON.stringify({ [process.cwd()]: TrustLevel.TRUST_FOLDER }),
|
||||
);
|
||||
expect(isWorkspaceTrusted(mockSettings)).toBe(false);
|
||||
});
|
||||
|
||||
it('should fall back to config when ideTrust is undefined', () => {
|
||||
vi.mocked(getIdeTrust).mockReturnValue(undefined);
|
||||
vi.spyOn(fs, 'existsSync').mockReturnValue(true);
|
||||
vi.spyOn(fs, 'readFileSync').mockReturnValue(
|
||||
JSON.stringify({ [process.cwd()]: TrustLevel.TRUST_FOLDER }),
|
||||
);
|
||||
expect(isWorkspaceTrusted(mockSettings)).toBe(true);
|
||||
});
|
||||
|
||||
it('should always return true if folderTrust setting is disabled', () => {
|
||||
const settings: Settings = {
|
||||
security: {
|
||||
folderTrust: {
|
||||
enabled: false,
|
||||
},
|
||||
},
|
||||
};
|
||||
vi.mocked(getIdeTrust).mockReturnValue(false);
|
||||
expect(isWorkspaceTrusted(settings)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,7 +7,11 @@
|
||||
import * as fs from 'node:fs';
|
||||
import * as path from 'node:path';
|
||||
import { homedir } from 'node:os';
|
||||
import { getErrorMessage, isWithinRoot } from '@google/gemini-cli-core';
|
||||
import {
|
||||
getErrorMessage,
|
||||
isWithinRoot,
|
||||
getIdeTrust,
|
||||
} from '@google/gemini-cli-core';
|
||||
import type { Settings } from './settings.js';
|
||||
import stripJsonComments from 'strip-json-comments';
|
||||
|
||||
@@ -159,11 +163,7 @@ export function isFolderTrustEnabled(settings: Settings): boolean {
|
||||
return folderTrustSetting;
|
||||
}
|
||||
|
||||
export function isWorkspaceTrusted(settings: Settings): boolean | undefined {
|
||||
if (!isFolderTrustEnabled(settings)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
function getWorkspaceTrustFromLocalConfig(): boolean | undefined {
|
||||
const folders = loadTrustedFolders();
|
||||
|
||||
if (folders.errors.length > 0) {
|
||||
@@ -176,3 +176,17 @@ export function isWorkspaceTrusted(settings: Settings): boolean | undefined {
|
||||
|
||||
return folders.isPathTrusted(process.cwd());
|
||||
}
|
||||
|
||||
export function isWorkspaceTrusted(settings: Settings): boolean | undefined {
|
||||
if (!isFolderTrustEnabled(settings)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const ideTrust = getIdeTrust();
|
||||
if (ideTrust !== undefined) {
|
||||
return ideTrust;
|
||||
}
|
||||
|
||||
// Fall back to the local user configuration
|
||||
return getWorkspaceTrustFromLocalConfig();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user