From bf63760cb42c74eac388a314519c7ac01c6f2fc8 Mon Sep 17 00:00:00 2001 From: Kaoru0822 Date: Fri, 13 Nov 2026 10:48:50 -0800 Subject: [PATCH] fix(setup): normalize Gemini trust rules before selection (#10378) --- src/application/GeminiClientSettings.ts | 40 ++++++++------ .../setup/geminiWorkspaceTrust.test.ts | 54 ++++++++++++++++++- 2 files changed, 77 insertions(+), 17 deletions(-) diff --git a/src/application/GeminiClientSettings.ts b/src/application/GeminiClientSettings.ts index a9398d1624..60b47cf169 100644 --- a/src/application/GeminiClientSettings.ts +++ b/src/application/GeminiClientSettings.ts @@ -2,6 +2,7 @@ import { realpath, stat } from "node:fs/promises"; import { dirname, resolve } from "node:path"; import { stripComments } from "jsonc-parser"; import { z } from "zod"; +import { jsonRecordSchema } from "../domain/jsonRecord.js"; import { err, ok, type Result } from "../domain/result.js"; import { PRODUCT_IDENTITY } from "../identity.js"; import type { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js"; @@ -34,8 +35,7 @@ const folderTrustSchema = z .optional(), }) .passthrough(); -const trustRulesSchema = z.record( - z.string(), +const trustRulesSchema = jsonRecordSchema( z.enum(["TRUST_FOLDER", "TRUST_PARENT", "DO_NOT_TRUST"]), ); const sourceFailure = ( @@ -47,18 +47,18 @@ const sourceFailure = ( detail: `Gemini settings in ${path} are malformed: ${cause instanceof Error ? cause.message : String(cause)}`, }); -// Gemini resolves symlinks when possible and compares macOS/Windows paths without case. -const normalizedPath = async ( - path: string, - platform: NodeJS.Platform, -): Promise => { - const physical = await realpath(path).catch(() => path); - const absolute = resolve(physical).replaceAll("\\", "/"); +// Gemini normalizes trust-file keys before replacement and rule precedence. +const normalizedPath = (path: string, platform: NodeJS.Platform): string => { + const absolute = resolve(path).replaceAll("\\", "/"); return platform === "win32" || platform === "darwin" ? absolute.toLowerCase() : absolute; }; +// Applicability additionally resolves symlinks, without changing rule identity. +const physicalPath = async (path: string, platform: NodeJS.Platform) => + normalizedPath(await realpath(path).catch(() => path), platform); + const workspaceTrusted = async ( settings: NonNullable, sources: readonly SettingsSource[], @@ -78,21 +78,29 @@ const workspaceTrusted = async ( ); if (!text.ok) return text; if (text.value === undefined) return ok(false); - let rules: z.output; + let rules: ReadonlyMap[string]>; try { // Gemini strips comments, but rejects trailing commas, BOMs and invalid trust levels. - rules = trustRulesSchema.parse(JSON.parse(stripComments(text.value))); + const parsed = trustRulesSchema.parse( + JSON.parse(stripComments(text.value)), + ); + rules = new Map( + Object.entries(parsed).map(([path, level]) => [ + normalizedPath(path, settings.platform), + level, + ]), + ); } catch (cause: unknown) { return err(sourceFailure(settings.trustedFoldersPath, cause)); } - const workspace = await normalizedPath( + const workspace = await physicalPath( settings.workspaceDirectory, settings.platform, ); let longest = -1; let trusted = false; - for (const [path, level] of Object.entries(rules)) { - const effective = await normalizedPath( + for (const [path, level] of rules) { + const effective = await physicalPath( level === "TRUST_PARENT" ? dirname(path) : path, settings.platform, ); @@ -138,11 +146,11 @@ export const readClientPolicyBlock = async ( if (parsed.value !== undefined) sources.push({ path, document: parsed.value }); } - const workspace = await normalizedPath( + const workspace = await physicalPath( settings.workspaceDirectory, settings.platform, ); - const home = await normalizedPath( + const home = await physicalPath( dirname(dirname(client.configPath)), settings.platform, ); diff --git a/tests/acceptance/setup/geminiWorkspaceTrust.test.ts b/tests/acceptance/setup/geminiWorkspaceTrust.test.ts index 32d4711cff..fef2a13d58 100644 --- a/tests/acceptance/setup/geminiWorkspaceTrust.test.ts +++ b/tests/acceptance/setup/geminiWorkspaceTrust.test.ts @@ -31,9 +31,61 @@ const geminiWorkspace = async (cli: TestCli) => { const originalUser = await readFile(user); const run = (arguments_: readonly string[]) => cli.run({ arguments: arguments_, cwd: project, environment }); - return { workspace, user, originalUser, environment, run }; + return { project, workspace, user, originalUser, environment, run }; }; +for (const scenario of [ + ...["/", "/."].flatMap((suffix) => + [false, true].map((trusted) => ({ + name: `the final normalized rule (${suffix}, trusted: ${String(trusted)})`, + trusted, + rules: (project: string) => + Object.fromEntries([ + [`${project}${suffix}`, trusted ? "DO_NOT_TRUST" : "TRUST_FOLDER"], + [project, trusted ? "TRUST_FOLDER" : "DO_NOT_TRUST"], + ]), + })), + ), + ...["__proto__", "ordinary"].map((child) => ({ + name: `the relative TRUST_PARENT rule ${child}`, + trusted: true, + rules: () => Object.fromEntries([[child, "TRUST_PARENT"]]), + })), + { + name: "the more specific TRUST_PARENT key", + trusted: true, + rules: (project: string) => + Object.fromEntries([ + [project, "DO_NOT_TRUST"], + [join(project, "child"), "TRUST_PARENT"], + ]), + }, +]) + cliTest(`Gemini setup and doctor honor ${scenario.name}`, async ({ cli }) => { + const fixture = await geminiWorkspace(cli); + const trust = fixture.environment.GEMINI_CLI_TRUSTED_FOLDERS_PATH; + if (trust === undefined) throw new Error("Expected a trust-file path"); + const trustText = JSON.stringify(scenario.rules(fixture.project)); + const workspaceText = JSON.stringify({ mcp: { excluded: ["rea"] } }); + await writeFile(trust, trustText); + await writeFile(fixture.workspace, workspaceText); + + const setup = await fixture.run(setupArguments); + expect(setup.exitCode, setup.stdout).toBe(scenario.trusted ? 1 : 0); + expect(setup.json).toMatchObject({ + status: scenario.trusted ? "needs_human" : "ready", + appliedActions: [], + }); + const doctor = await fixture.run(doctorArguments); + expect(doctor.exitCode, doctor.stdout).toBe(scenario.trusted ? 1 : 0); + expect(doctor.json).toMatchObject({ healthy: !scenario.trusted }); + expect(await readFile(trust, "utf8")).toBe(trustText); + expect(await readFile(fixture.workspace, "utf8")).toBe(workspaceText); + expect( + Buffer.compare(await readFile(fixture.user), fixture.originalUser), + ).toBe(0); + }); + cliTest( "Gemini does not require workspace trust until workspace settings exist", async ({ cli }) => {