mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 14:05:49 +00:00
fix(setup): normalize Gemini trust rules before selection (#10378)
This commit is contained in:
1 parent
3b006841e2
commit
bf63760cb4
2 files changed
+77
-17
No files matched your search
@@ -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<string> => {
|
||||
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<SetupClient["geminiSettings"]>,
|
||||
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<typeof trustRulesSchema>;
|
||||
let rules: ReadonlyMap<string, z.output<typeof trustRulesSchema>[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,
|
||||
);
|
||||
|
||||
@@ -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 }) => {
|
||||
|
||||
Reference in new issue
Block a user