Skip to content

fix chat guardrails blocking package installs under a restrictive umask - #18567

Merged
kevinushey merged 2 commits into
mainfrom
bugfix/chat-guardrails-umask-reads
Aug 15, 2026
Merged

kevinushey merged 2 commits into
mainfrom
bugfix/chat-guardrails-umask-reads

Conversation

@kevinushey

@kevinushey kevinushey commented Aug 14, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Posit Assistant's install.packages() fails in Workbench sessions running under a restrictive umask (common in hardened containers), with repeated "One or more agent file operations were blocked" errors that eventually push the model to work around the sandbox via Rscript in the terminal.

Root cause: the guardrails' world-readable check in .rs.chat.isFileReadAllowed() denied reads of any file lacking the o+r permission bit, unconditionally. Under umask 077 every file the session creates is mode 600, so agent code was blocked from reading files R itself had just written. The most visible victim is the installed.packages() cache (tempdir()/libloc_*.rds): the cache gets seeded by the IDE or an earlier command, and the next read -- during install.packages() dependency resolution -- hits the hooked readRDS() and errors. The same check also blocked the agent from reading any user-owned file (project files included) in such environments. The chat.safeFunctions trusted-caller allowlist did not help, because trust only bypasses the deny-path patterns, not the permission check.

Changes

  • Scope the world-readable check to paths outside the allowed roots (tempdir, working directory, project directory, scratch path, .libPaths(), R user dirs). These are locations the agent may already write to, so denying reads there was incoherent -- and they are exactly where the session's own non-world-readable files live. The allowed roots are now enumerated in one place (chat.allowedRoots()), shared by the edit check and the read check.
  • Neutralize the check when the umask itself strips world-read from new files. On a default-umask system a 600-mode file is a deliberate signal (ssh keys, tokens) and the heuristic has value; under umask 077 every file is private by default, the bit carries no signal, and the heuristic just denied everything.
  • Allow edits within a not-yet-created user library (R_LIBS_USER), so install.packages() can create the personal library on first use (a nonexistent user library is not in .libPaths(), so it was previously denied).

The deny-path patterns (credentials, .env, .Renviron, ~/.ssh, system files) still apply everywhere, including within the allowed roots and regardless of umask.

Security trade-offs

These guardrails target accidental misuse, not malicious code (per the note at the top of SessionChat.R). Two deliberate loosenings here:

  1. A non-world-readable file inside the project/tempdir/library roots is now readable by agent code. Deny patterns still catch the common credential shapes there. The home directory itself is never a read-exempt root (it is the fallback working directory when no project is open), and the .ssh read-deny pattern covers all non-public files.
  2. On umask-restricted systems, a private file outside the allowed roots that matches no deny pattern is now readable. Previously the heuristic blocked it -- along with literally everything else, which in practice pushed the model to escape to an unhooked Rscript subprocess where no checks apply at all.

Testing

  • New testthat coverage in test-chat-guardrails.R for the allowed-roots exemption, the umask neutralization, the not-yet-created user library, pattern denials still applying within allowed roots, and an end-to-end installed.packages()-twice regression test under umask 077 (the exact failure from the report). All six new tests fail before the fix and pass after; the pre-existing 89 assertions pass unchanged.
  • Verified end-to-end in an rsession: under umask 077 with a pre-seeded libloc_*.rds cache, agent-driven install.packages("desc") (source, with dependencies) and install.packages("praise") (macOS in-process binary) both failed before the fix and now succeed; reads of ~/.aws/credentials and /etc/passwd remain blocked.

No NEWS.md entry here since the affected feature ships in rstudio-pro; release notes are tracked in the pro repository.

Fixes rstudio/rstudio-pro#11975.

The world-readable permission check in the chat guardrails denied reads
of any file lacking the o+r bit, unconditionally. In environments with
a restrictive umask (e.g. Workbench sessions in hardened containers
with umask 077), every file the session creates is private, so agent
code tripped over files R itself had just written -- most visibly the
installed.packages() cache in tempdir(), which fails dependency
resolution inside install.packages().

Three changes:

- Skip the world-readable check for files within the allowed roots
  (tempdir, working directory, project, scratch path, library paths,
  R user dirs) -- locations the agent is already allowed to write to.
- Skip the check entirely when the umask itself strips world-read from
  new files, since then a missing o+r bit is the environment's default
  and carries no signal about a file's sensitivity.
- Allow edits within a not-yet-created user library (R_LIBS_USER), so
  first-time personal library creation works.

The deny-path patterns (credentials, .env, .Renviron, system files)
still apply everywhere, including within the allowed roots.

Addresses rstudio/rstudio-pro#11975.
@kevinushey kevinushey added this to the 2026.09 (Autumn Hawkbit) milestone Aug 14, 2026
@kevinushey
kevinushey requested a review from gtritchie August 14, 2026 20:33
@statik

statik commented Aug 14, 2026

Copy link
Copy Markdown
Collaborator

thank you! nice fix

@gtritchie

Copy link
Copy Markdown
Member

Code review findings

Actionable items from an automated review of this PR.

Critical

  1. src/cpp/session/modules/SessionChat.R:336 — exempting the allowed roots from the world-readable check removes the last protection on private files under $HOME, because getwd() is an allowed root and defaults to the home directory when no project is open (SessionDirs.cpp:50 falls back to userHomePath()). chat.denyReadPatterns only covers /\.ssh/config$ and /\.ssh/id.*(?<!\.pub)$, so mode-600 ~/.ssh/deploy_key, ~/.databrickscfg, ~/.config/rclone/rclone.conf and ~/*.pem all match no pattern and are now readable by agent code; before this change they were denied. ssh refuses to use private keys that aren't mode 600, so the permission check was covering this case in practice. Fix: add /\.ssh(/|$) to chat.denyReadPatterns (the same pattern chat.denyEditPatterns already carries), and/or don't treat getwd() as a read-exempt root when it resolves to $HOME.

    Related: the PR description states that the deny-path patterns "(credentials, .env, .Renviron, ~/.ssh, system files) still apply everywhere". That holds for edits but not for reads — worth correcting.

Important

  1. src/cpp/session/modules/SessionChat.R:432 — Sys.umask(NA) is not a read-only query. R implements it as res = umask(0); umask(res); (src/main/platform.c), so every call briefly sets the process-global umask to 0, and this now runs on every hooked file read. rsession has background threads that create files (client event service, monitor worker, log rotation — SessionMain.cpp:2081-2131); one creating a file inside that window gets mode 666/777. Fix: compute the umask once and cache it.

  2. src/cpp/session/modules/SessionChat.R:333 — the check is gated on the live process umask, and Sys.umask is not among the bindings hooked by chat.injectBindings(). Agent code can run Sys.umask("077") once and the world-readable check is skipped for the rest of the session, for every path including those outside the allowed roots. This sits outside the module's stated threat model (accidental misuse, not deliberately malicious code), so it may be a documentation change rather than a code change — but sampling the umask once at session start, before hooks are injected, would keep the guardrail from being switchable by the code it guards.

Suggestions

  1. src/cpp/session/modules/SessionChat.R:383 — chat.allowedRoots() is recomputed on every read validation, where the previous code did a single file.info(). Each call runs .Call("rs_userDataDir") (which resolves through xdg::resolveXdgPath() with createDirectory = true — several getenv/exists() checks plus a possible ensureDirectory(), Xdg.cpp:296-313), tools::R_user_dir() three times, .libPaths(), and chat.normalizePath() over ~8-10 paths, each a Boost weakly-canonical resolution. validateFileRead fires once per readLines/readRDS/file(), so agent code iterating over a few thousand files pays this per file. Fix: cache the roots for the duration of a chat.safeEval / chat.withGuardrails invocation. Same cache addresses finding 2.

Checked and found correct: Sys.getenv("R_LIBS_USER") is already %p/%v-expanded by R's startup, so the not-yet-created-user-library root resolves; HAVE_UMASK is defined in R's Windows config.h, so Sys.umask(NA) doesn't warn there; getWeaklyCanonicalPath() handles nonexistent roots.

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feedback posted

@github-actions

github-actions Bot commented Aug 14, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

1 test(s) failed

###################- 99% pass rate

Passed Failed Skipped Flaky
2273 1 159 1
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 2 0 9 0
✅ desktop-linux 592 0 31 0
❌ desktop-macos 587 1 35 1
✅ desktop-windows 499 0 31 0
✅ server-linux 569 0 44 0
✅ setup 24 0 9 0
View / download the full report

View report online -- opens in the browser, no download needed. Available for 14 days.

Download playwright-report.zip -- extract and open index.html

Includes screenshots, traces, and step-by-step details for every test. Expires in 15 days.


All platforms - commit d115b763122156f883e3131df8ab11bd757e05a4 - Run #625

…e roots

- generalize the .ssh deny-read pattern to all non-.pub files, so
  private keys under any name (deploy_key, etc.) stay blocked
- exclude the home directory itself from the read-exemption roots:
  it is the fallback working directory when no project is open, and
  exempting it would hide mode-600 dotfiles from the permission check
- sample the umask once (Sys.umask(NA) briefly zeroes the process
  umask, and a live query would let agent code disable the check)
- memoize allowedRoots() on its dynamic inputs; it runs on every
  hooked file operation and the scratch-path lookup touches the
  filesystem
@kevinushey

Copy link
Copy Markdown
Collaborator Author

All four findings were real; addressed in d115b76.

  1. Fixed two ways: the home directory itself is excluded from the read-exemption roots (it remains an edit root, as before), and the .ssh deny-read pattern now covers every non-.pub file under .ssh, so private keys under any name stay blocked even under a restrictive umask where the permission check is off. I kept the .pub carve-out instead of the blanket /\.ssh(/|$) -- public keys being readable is deliberate and tested; known_hosts does become newly denied for untrusted callers. Also corrected the PR description's claim about read patterns covering ~/.ssh.
  2. Confirmed against R's do_sysumask (umask(0); umask(res)); the value is now sampled once and cached.
  3. The sample is forced in injectBindings() before agent code can run, so Sys.umask() from agent code no longer flips the check. A legitimate mid-session umask change isn't re-honored until restart.
  4. allowedRoots() is memoized on its dynamic inputs (cwd, .libPaths(), R_LIBS_USER) rather than snapshotted per invocation, so mid-eval setwd() behavior is unchanged while repeated reads skip the rs_userDataDir/R_user_dir/normalization work.

New tests cover each: pattern gaps and a readable ~/.ssh/deploy_key (both fail on the previous head), a mode-600 file directly under $HOME staying denied while $HOME is the cwd, umask sample-once semantics, and memo invalidation on setwd().

@kevinushey
kevinushey merged commit 4f0e00a into main Aug 15, 2026
27 of 29 checks passed
@kevinushey
kevinushey deleted the bugfix/chat-guardrails-umask-reads branch August 15, 2026 00:24
@gtritchie gtritchie mentioned this pull request Aug 31, 2026
3 of 4 tasks

This branch was previously deployed

1 inactive deployment
e2e-playwright — d115b763 Deployed Aug 14, 2026 by kevinushey via linux-desktop / E2E (ubuntu-24-x86_64) #625
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants