Policy rules and the workspace check match the command text or the tool name, not what the tool call does

Aperta
#30,510 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
45/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
node.js, shell, typescript

Direzione di ricerca

Start with packages/core/src/policy/utils.ts, packages/core/src/policy/policy-engine.ts, packages/core/src/tools/write-file.ts, and packages/core/src/services/fileSystemService.ts, then review docs/reference/policy-engine.md and docs/tools/file-system.md. Reproduction steps and the three suggested fixes define the scope: command matching, tool-boundary documentation, and symlink-safe writes. Done means the behavior is consistently specified, the documented policy boundaries are explicit, and focused tests cover command variants, alternative file access paths, and workspace escape attempts.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

status/need-triage
What happened?

Policy rules and the file tools' workspace check decide on what a tool call looks like (the literal command text, the tool name, a path resolved once), not on what the call then does. There are three ways around them:

  1. commandPrefix rules match the literal command text. A user or admin rule with toolName = "run_shell_command", commandPrefix = "touch", decision = "deny" denies touch <project>/canary/canary.txt and also catches true && touch ..., (touch ...) and sh -c 'touch ...', but /usr/bin/touch, \touch, env FOO=1 touch, t'o'uch, $(echo touch) and X=touch; $X run and create the file. The same happens with decision = "ask_user" (headless treats it as deny), with the rule in an admin policy (--admin-policy <dir>), and over ACP (gemini --acp), where an ask_user rule sent no permission request for /usr/bin/touch. commandPrefix becomes the regular expression "command":"touch(?:[\s"]|\\") (packages/core/src/policy/utils.ts:56-83), tested against the raw arguments of each sub-command; the program name is not normalised (path, backslash, quotes, env, expansions).
  2. File rules, admin file rules and the workspace limit bind only Gemini's own file tools. A rule that denies read_file, read_many_files and grep_search on <project>/secret/ stops read_file("<project>/secret/secret.txt"), but the same file is read by run_shell_command("cat <project>/secret/secret.txt"), by a program the shell starts (python3 -c "print(open(...).read())") and by an MCP tool (canary_read(path)); the same holds for the same rules in an admin policy. Likewise for the workspace limit ("All file system tools operate within a rootDirectory (the current working directory or workspace root) for security.", docs/tools/file-system.md:8-9): read_file of a file outside the workspace is refused with "Path not in workspace", but the shell, a Python process started by the shell, and an MCP tool read and write outside the workspace (no sandbox set). Rules match the tool name and its arguments (packages/core/src/policy/policy-engine.ts:214-226), and the workspace check is inside the file tools. This follows from the documented design ("The toolName in the rule must match the name of the tool being called", docs/reference/policy-engine.md:74), so this item is a documentation request.
  3. write_file's workspace check and its write resolve a symlink separately. write_file refuses paths that resolve outside the workspace, but the check (packages/core/src/tools/write-file.ts:372, this.config.validatePathAccess(this.resolvedPath)) and the write (packages/core/src/services/fileSystemService.ts:57-95, writeTextFile resolves the path again with resolveToRealPath, then writes a temp file next to the real path and renames it) each resolve the path on their own. If a directory in the path is swapped for a symlink between the two, the write lands outside the workspace. This was seen in a plain gemini -p session, in a session resumed with --resume after context compression, and with subagents; it is intermittent and needs a second local process that swaps the link at the right moment, so I'm filing it as hardening.

See also the issue about configuration and hooks that fail open (project settings that auto-approve tools, a policy file dropped for one bad entry, a blocking hook that fails), which uses the same policy files.

What did you expect to happen?

Setup for all steps (headless, trusted folder <project>, no sandbox): ~/.gemini/settings.json with {"security": {"auth": {"selectedType": "gemini-api-key"}}}, and ~/.gemini/policies/base.toml allowing every tool in the default mode, so only the deny decides (step 3 uses only the first five lines):

[[rule]]
toolName = "*"
decision = "allow"
priority = 1
modes = ["default"]
allowRedirection = true
allowCommandSubstitution = true

The base file sets allowCommandSubstitution = true because without it an allow becomes ask on $(...) (docs/reference/policy-engine.md:355-359); the deny rules themselves have no such option.

  1. ~/.gemini/policies/deny.toml:
    [[rule]]
    toolName = "run_shell_command"
    commandPrefix = "touch"
    decision = "deny"
    priority = 500
    
    Run gemini -p "Run exactly this shell command: /usr/bin/touch <project>/canary/canary.txt" (tool call: run_shell_command({"command": "/usr/bin/touch <project>/canary/canary.txt"})): the file is created. Comparison: gemini -p "Run exactly this shell command: touch <project>/canary/canary.txt" gives Tool execution denied by policy.
  2. <project>/secret/secret.txt containing SECRET-1, and ~/.gemini/policies/deny.toml replaced by:
    [[rule]]
    toolName = ["read_file", "read_many_files", "grep_search"]
    argsPattern = '<project>/secret/'
    decision = "deny"
    priority = 500
    
    Run gemini -p "Run this shell command and show its output: cat <project>/secret/secret.txt" (tool call: run_shell_command({"command": "cat <project>/secret/secret.txt"})): the tool output is SECRET-1. Comparison: gemini -p "Read <project>/secret/secret.txt with the read_file tool" gives Tool execution denied by policy.
  3. No deny.toml. OUT=$(mktemp -d) (use it as <outside>, a writable folder outside the workspace) and start this loop in the background:
    while true; do rm -rf <project>/rc-x; mkdir -p <project>/rc-x; rm -rf <project>/rc-x; ln -s <outside> <project>/rc-x; done
    
    Run gemini -p "Use write_file four times in parallel to write PT to <project>/rc-x/x.txt"; repeat until <outside>/x.txt appears. One session's four calls write_file({"file_path": "<project>/rc-x/x.txt", "content": "PT"}) returned:
    Successfully created and wrote to new file: <project>/rc-x/x.txt ...
    Error writing to file '<project>/rc-x/x.txt': ENOENT: no such file or directory, open '<project>/rc-x/x.txt.<uuid>.tmp'
    Path not in workspace: Attempted path "<project>/rc-x/x.txt" resolves outside the allowed workspace directories ...
    Successfully created and wrote to new file: <project>/rc-x/x.txt ...
    
    and <outside>/x.txt existed afterwards.
Client information
Client Information

Run gemini to enter the interactive CLI, then run the /about command.

> /about
About Gemini CLI

CLI Version                              0.69.0
Git Commit                               58840eb92
Model                                    Auto
Sandbox                                  no sandbox
OS                                       linux
Auth Method                              gemini-api-key

Platform: Linux (Debian GNU/Linux 12 bookworm, kernel 6.8.0), Node.js v22.23.5. Gemini CLI installed with npm install -g @google/[email protected].

Login information

API key

Anything else we need to know?
  • Suggested fix 1: normalise the program name of each sub-command before matching: strip the path and a leading \, remove quotes, skip env VAR=..., and treat a program name that comes from an expansion as not matching an allow and matching a deny. If that is not wanted, please state in the docs that command rules are not a boundary against a model that rewrites the command, and point to the sandbox.
  • Suggested fix 2: add a clear sentence in reference/policy-engine.md, tools/file-system.md and the enterprise guide that path rules and the workspace limit cover only the built-in file tools, and that protecting a path from the shell or MCP servers needs the sandbox (or denying those tools). Administrators reading docs/cli/enterprise.md may otherwise take an admin path deny as complete.
  • Suggested fix 3: resolve once, check that real path and write to it with O_NOFOLLOW or through an open directory handle, or re-check the real path of the opened file before the rename.
Lingua principale
TypeScript
Stelle
109k
Fork
15k
Merge medio
1g 24m
PR unite (30g)
91

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di google-gemini/gemini-cli

Tutte le issue di google-gemini/gemini-cli

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.