Repository navigation
fix command injection in filename-driven render/preview handlers - #17803
Conversation
Escape user-controlled file paths before interpolating them into R command strings, so that a filename containing a single quote cannot break out of the string literal and inject arbitrary R code. Covers the Plumber "Run API", R Presentation "Preview", HTML/knitr preview, Sweave and knitr weave, shinytest2/testthat run, and website render_site command paths. While here, port these sites from boost::format / string concatenation to fmt::format, format the generated R conventionally (spaces around operators), and use multi-line raw-string templates. Add a string_utils::heredoc() helper that strips the common leading indent from a raw string so embedded R code can be indented to match the surrounding source. Addresses rstudio/rstudio-pro#10989.
An R Markdown document can specify a custom render command via the `knit:` field in its YAML front matter. RStudio evaluates that value as R code when rendering (RenderRmd::start), so opening an untrusted document and clicking Knit/Preview can execute arbitrary code with no warning. Add a confirmation prompt: before honoring a custom render command, the frontend (the single render chokepoint in RmdOutput) asks the backend whether the document's render function requires confirmation. The new get_custom_render_function RPC only parses the YAML front matter (it does not evaluate the knit: field) and reports the render function plus whether it is custom. RStudio's own defaults and derived render functions (rmarkdown::render/run/render_site, bookdown/pkgdown/blogdown/ xaringan, quarto serve/render) are known-safe and skip the prompt. The user is prompted once per source file per session. Relates to rstudio/rstudio-pro#10989 (Amazon issue #4).
Adds a Playwright test for the custom `knit:` render confirmation: a doc whose knit: field is a function writing a marker file is used to assert that Knit prompts first, that declining runs nothing, that accepting runs the command, and that the approval is remembered for the session. Relates to rstudio/rstudio-pro#10989 (Amazon issue #4).
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
Review summaryThe escaping half of this PR is solid and correct; the confirmation half has a few consistency gaps worth a decision before merge. Findings below were cross-checked against the actual Critical / High -- decide before merge1. The confirmation gate fails open in three places (indeterminate -> "safe").
Practical exploitability is lower than it first appears, because the same 2. Unescaped Important3. 4. The security primitive itself has zero test coverage. Suggestions
Recommended action
|
gtritchie
left a comment
There was a problem hiding this comment.
Feedback posted, some things worth looking at
- fail closed when the document render function can't be determined: getCustomRenderFunction sets requires_confirmation on R error, and the client confirms on null/missing/error rather than rendering silently - validate the Sweave `driver` magic comment against an allow-list, since it is interpolated as a bare R expression and cannot be quote-escaped - re-key per-session custom-render approval on the command value, so a changed knit: command (e.g. file replaced on disk) re-prompts - document why renderRmdSource bypasses the client confirmation gate - add unit tests for singleQuotedStrEscape and requiresRenderConfirmation - collapse over-indented blank lines in heredoc()
Intent
Addresses rstudio/rstudio-pro#10989.
Summary
Two related changes hardening how RStudio constructs R commands for
filename-driven "run/preview/render" actions.
1. Escape interpolated paths (filename command injection).
Several handlers built an R command by concatenating a user-controlled
file path directly into a single-quoted R string literal, so a filename
containing a single quote could break out of the literal and inject R
code. Each interpolated path/stem/encoding is now escaped with
string_utils::singleQuotedStrEscape()(already used elsewhere for thispurpose). Covered sites:
SessionPlumberViewer.cpp)presentation/SlideRequestHandler.cpp)SessionHTMLPreview.cpp)tex/SessionRnwWeave.cpp)render_site(build/SessionBuild.cpp)While here, these sites were ported from
boost::format/stringconcatenation to
fmt::format, the generated R is formattedconventionally (spaces around
=), and astring_utils::heredoc()helper (with unit tests) was added so multi-line R templates can be
indented to match the surrounding source.
2. Confirm custom
knit:render commands.A document can specify a custom render command in its YAML
knit:field,which RStudio evaluates as R code at render time -- so opening an
untrusted document and clicking Knit/Preview could run arbitrary code
with no warning. This is execution-by-design rather than string
injection, so escaping does not apply. The render chokepoint now asks
the backend (via a new read-only
get_custom_render_functionRPC thatonly parses the YAML, it does not evaluate it) whether the document's
render function is custom, and prompts for confirmation before running
it. RStudio's own defaults and derived render functions are known-safe
and skip the prompt; approval is remembered per source file for the
session.
Testing
string_utils::heredoc()(StringUtilsTests.cpp).e2e/.../custom_knit_confirm.test.ts) asserting thecustom-knit confirmation prompts, that declining runs nothing, that
accepting runs the command, and that approval is remembered.
rstudio-core/rsessionbuild and link; GWT compiles; the new e2e testpasses on desktop.