Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions docs/get-started/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,11 @@ their corresponding top-level category object in your `settings.json` file.
- **Description:** The format to use when importing memory.
- **Default:** `undefined`

- **`context.includeDirectoryTree`** (boolean):
- **Description:** Whether to include the directory tree of the current
working directory in the initial request to the model.
- **Default:** `true`

- **`context.discoveryMaxDirs`** (number):
- **Description:** Maximum number of directories to search for memory.
- **Default:** `200`
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ export async function loadCliConfig(
}

const memoryImportFormat = settings.context?.importFormat || 'tree';
const includeDirectoryTree = settings.context?.includeDirectoryTree ?? true;

const ideMode = settings.ide?.enabled ?? false;

Expand Down Expand Up @@ -745,6 +746,7 @@ export async function loadCliConfig(
embeddingModel: DEFAULT_GEMINI_EMBEDDING_MODEL,
sandbox: sandboxConfig,
targetDir: cwd,
includeDirectoryTree,
includeDirectories,
loadMemoryFromIncludeDirectories:
settings.context?.loadMemoryFromIncludeDirectories || false,
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/src/config/settingsSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,16 @@ const SETTINGS_SCHEMA = {
description: 'The format to use when importing memory.',
showInDialog: false,
},
includeDirectoryTree: {
type: 'boolean',
label: 'Include Directory Tree',
category: 'Context',
requiresRestart: false,
default: true,
description:
'Whether to include the directory tree of the current working directory in the initial request to the model.',
showInDialog: false,
},
discoveryMaxDirs: {
type: 'number',
label: 'Memory Discovery Max Dirs',
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ export interface ConfigParameters {
folderTrust?: boolean;
ideMode?: boolean;
loadMemoryFromIncludeDirectories?: boolean;
includeDirectoryTree?: boolean;
importFormat?: 'tree' | 'flat';
discoveryMaxDirs?: number;
compressionThreshold?: number;
Expand Down Expand Up @@ -603,6 +604,7 @@ export class Config {
| undefined;
private readonly experimentalZedIntegration: boolean = false;
private readonly loadMemoryFromIncludeDirectories: boolean = false;
private readonly includeDirectoryTree: boolean = true;
private readonly importFormat: 'tree' | 'flat';
private readonly discoveryMaxDirs: number;
private readonly compressionThreshold: number | undefined;
Expand Down Expand Up @@ -786,6 +788,7 @@ export class Config {
this.summarizeToolOutput = params.summarizeToolOutput;
this.folderTrust = params.folderTrust ?? false;
this.ideMode = params.ideMode ?? false;
this.includeDirectoryTree = params.includeDirectoryTree ?? true;
this.loadMemoryFromIncludeDirectories =
params.loadMemoryFromIncludeDirectories ?? false;
this.importFormat = params.importFormat ?? 'tree';
Expand Down Expand Up @@ -1161,6 +1164,10 @@ export class Config {
return this.loadMemoryFromIncludeDirectories;
}

getIncludeDirectoryTree(): boolean {
return this.includeDirectoryTree;
}

getImportFormat(): 'tree' | 'flat' {
return this.importFormat;
}
Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/utils/environmentContext.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ describe('getEnvironmentContext', () => {
getDirectories: vi.fn().mockReturnValue(['/test/dir']),
}),
getFileService: vi.fn(),
getIncludeDirectoryTree: vi.fn().mockReturnValue(true),
getEnvironmentMemory: vi.fn().mockReturnValue('Mock Environment Memory'),

getToolRegistry: vi.fn().mockReturnValue(mockToolRegistry),
Expand Down Expand Up @@ -146,6 +147,24 @@ describe('getEnvironmentContext', () => {
expect(getFolderStructure).toHaveBeenCalledTimes(2);
});

it('should omit directory structure when getIncludeDirectoryTree is false', async () => {
(vi.mocked(mockConfig.getIncludeDirectoryTree!) as Mock).mockReturnValue(
false,
);

const parts = await getEnvironmentContext(mockConfig as Config);

expect(parts.length).toBe(1);
const context = parts[0].text;

expect(context).toContain('<session_context>');
expect(context).not.toContain('Directory Structure:');
expect(context).not.toContain('Mock Folder Structure');
expect(context).toContain('Mock Environment Memory');
expect(context).toContain('</session_context>');
expect(getFolderStructure).not.toHaveBeenCalled();
});

it('should handle read_many_files returning no content', async () => {
const mockReadManyFilesTool = {
build: vi.fn().mockReturnValue({
Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/utils/environmentContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ export async function getEnvironmentContext(config: Config): Promise<Part[]> {
day: 'numeric',
});
const platform = process.platform;
const directoryContext = await getDirectoryContextString(config);
const directoryContext = config.getIncludeDirectoryTree()
? await getDirectoryContextString(config)
: '';
const tempDir = config.storage.getProjectTempDir();
const environmentMemory = config.getEnvironmentMemory();

Expand All @@ -63,7 +65,7 @@ This is the Gemini CLI. We are setting up the context for our chat.
Today's date is ${today} (formatted according to the user's locale).
My operating system is: ${platform}
The project's temporary directory is: ${tempDir}
${directoryContext}
${directoryContext ? directoryContext + '\n' : ''}

Copy link
Copy Markdown
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 directoryContext variable is directly embedded into the LLM prompt within a <session_context> block without sanitization, making it vulnerable to prompt injection. An attacker could create a file or directory with a name containing the </session_context> closing tag followed by malicious instructions, potentially leading to unauthorized tool execution. Remediation: Sanitize directoryContext by escaping or removing any occurrences of the </session_context> tag before embedding it. Additionally, this line introduces an extra newline in the session context, which could subtly affect model behavior.

Suggested change
${directoryContext ? directoryContext + '\n' : ''}
${directoryContext ? directoryContext.replace(/<\/session_context>/g, '') + '\n' : ''}


${environmentMemory}
</session_context>`.trim();
Expand Down
7 changes: 7 additions & 0 deletions schemas/settings.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,13 @@
"markdownDescription": "The format to use when importing memory.\n\n- Category: `Context`\n- Requires restart: `no`",
"type": "string"
},
"includeDirectoryTree": {
"title": "Include Directory Tree",
"description": "Whether to include the directory tree of the current working directory in the initial request to the model.",
"markdownDescription": "Whether to include the directory tree of the current working directory in the initial request to the model.\n\n- Category: `Context`\n- Requires restart: `no`\n- Default: `true`",
"default": true,
"type": "boolean"
},
"discoveryMaxDirs": {
"title": "Memory Discovery Max Dirs",
"description": "Maximum number of directories to search for memory.",
Expand Down
Loading