| 1 |
|
| 2 |
renv_sandbox_init <- function() { |
| 3 |
|
| 4 |
# check for envvar override |
| 5 |
enabled <- Sys.getenv("RENV_SANDBOX_LOCKING_ENABLED", unset = NA) |
| 6 |
if (!is.na(enabled)) { |
| 7 |
enabled <- truthy(enabled, default = FALSE) |
| 8 |
options(renv.sandbox.locking_enabled = enabled) |
| 9 |
} |
| 10 |
|
| 11 |
# don't use sandbox in watchdog process |
| 12 |
type <- Sys.getenv("RENV_PROCESS_TYPE") |
| 13 |
if (type == "watchdog-server") |
| 14 |
return() |
| 15 |
|
| 16 |
# if renv was launched with a sandbox path on the library paths, |
| 17 |
# then immediately try to activate the sandbox |
| 18 |
# https://github.com/rstudio/renv/issues/1565 |
| 19 |
for (libpath in .libPaths()) { |
| 20 |
if (file.exists(file.path(libpath, ".renv-sandbox"))) { |
| 21 |
renv_sandbox_activate_impl(sandbox = libpath) |
| 22 |
break |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
} |
| 27 |
|
| 28 |
renv_sandbox_activate <- function(project = NULL) { |
| 29 |
|
| 30 |
# record start time |
| 31 |
before <- Sys.time() |
| 32 |
|
| 33 |
# attempt the activation |
| 34 |
status <- catch(renv_sandbox_activate_impl(project)) |
| 35 |
if (inherits(status, "error")) |
| 36 |
warnify(status) |
| 37 |
|
| 38 |
# record end time |
| 39 |
after <- Sys.time() |
| 40 |
|
| 41 |
# check for long elapsed time |
| 42 |
elapsed <- difftime(after, before, units = "secs") |
| 43 |
|
| 44 |
# if it took too long to activate the sandbox, warn the user |
| 45 |
if (elapsed > 10) { |
| 46 |
|
| 47 |
fmt <- heredoc(" |
| 48 |
renv took longer than expected (%s) to activate the sandbox. |
| 49 |
|
| 50 |
The sandbox can be disabled by setting: |
| 51 |
|
| 52 |
RENV_CONFIG_SANDBOX_ENABLED = FALSE |
| 53 |
|
| 54 |
within an appropriate start-up .Renviron file. |
| 55 |
|
| 56 |
See `?renv::config` for more details. |
| 57 |
") |
| 58 |
|
| 59 |
|
| 60 |
warningf(fmt, renv_difftime_format(elapsed)) |
| 61 |
|
| 62 |
} |
| 63 |
|
| 64 |
# return status |
| 65 |
status |
| 66 |
|
| 67 |
} |
| 68 |
|
| 69 |
renv_sandbox_activate_impl <- function(project = NULL, sandbox = NULL) { |
| 70 |
|
| 71 |
# lock access to the sandbox |
| 72 |
if (config$sandbox.enabled()) { |
| 73 |
sandbox <- sandbox %||% renv_sandbox_path(project = project) |
| 74 |
lockfile <- paste(sandbox, "lock", sep = ".") |
| 75 |
ensure_parent_directory(lockfile) |
| 76 |
renv_scope_lock(lockfile) |
| 77 |
ensure_directory(sandbox) |
| 78 |
} |
| 79 |
|
| 80 |
# get current library paths |
| 81 |
oldlibs <- .libPaths() |
| 82 |
syslibs <- c(renv_libpaths_site(), renv_libpaths_system()) |
| 83 |
syslibs <- renv_path_normalize(syslibs) |
| 84 |
|
| 85 |
# override .Library.site |
| 86 |
base <- .BaseNamespaceEnv |
| 87 |
renv_binding_replace(base, ".Library.site", NULL) |
| 88 |
|
| 89 |
# generate sandbox |
| 90 |
if (config$sandbox.enabled()) { |
| 91 |
renv_sandbox_generate(sandbox) |
| 92 |
renv_binding_replace(base, ".Library", sandbox) |
| 93 |
} |
| 94 |
|
| 95 |
# update library paths |
| 96 |
newlibs <- renv_vector_diff(oldlibs, syslibs) |
| 97 |
renv_libpaths_set(newlibs) |
| 98 |
|
| 99 |
# protect against user profiles that might update library paths |
| 100 |
if (config$sandbox.enabled()) |
| 101 |
renv_sandbox_activate_check(newlibs) |
| 102 |
|
| 103 |
# return new library paths |
| 104 |
renv_libpaths_all() |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
renv_sandbox_activated <- function() { |
| 109 |
!identical(.Library, renv_libpaths_system()) |
| 110 |
} |
| 111 |
|
| 112 |
renv_sandbox_activate_check <- function(libs) { |
| 113 |
|
| 114 |
envir <- globalenv() |
| 115 |
|
| 116 |
danger <- exists(".First", envir = envir, inherits = FALSE) && autoloading() |
| 117 |
if (!danger) |
| 118 |
return(FALSE) |
| 119 |
|
| 120 |
.First <- get(".First", envir = envir, inherits = FALSE) |
| 121 |
wrapper <- function() { |
| 122 |
|
| 123 |
# scope the library paths as currently defined |
| 124 |
renv_scope_libpaths() |
| 125 |
|
| 126 |
# call the user-defined .First function |
| 127 |
status <- tryCatch(.First(), error = warnify) |
| 128 |
|
| 129 |
# double-check if we should restore .First (this is extra |
| 130 |
# paranoid but in theory .First could remove itself) |
| 131 |
if (identical(wrapper, get(".First", envir = envir))) |
| 132 |
assign(".First", .First, envir = envir) |
| 133 |
|
| 134 |
# return result of .First |
| 135 |
invisible(status) |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
assign(".First", wrapper, envir = envir) |
| 140 |
return(TRUE) |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
renv_sandbox_generate <- function(sandbox) { |
| 145 |
|
| 146 |
# make the library temporarily writable |
| 147 |
lockable <- renv_sandbox_lockable() |
| 148 |
|
| 149 |
if (lockable) { |
| 150 |
dlog("sandbox", "unlocking sandbox") |
| 151 |
renv_sandbox_unlock(sandbox) |
| 152 |
} |
| 153 |
|
| 154 |
# find system packages in the system library |
| 155 |
priority <- getOption("renv.sandbox.priority", default = c("base", "recommended")) |
| 156 |
syspkgs <- installed_packages( |
| 157 |
lib.loc = renv_libpaths_system(), |
| 158 |
priority = priority |
| 159 |
) |
| 160 |
|
| 161 |
# link into sandbox |
| 162 |
sources <- with(syspkgs, file.path(LibPath, Package)) |
| 163 |
targets <- with(syspkgs, file.path(sandbox, Package)) |
| 164 |
names(targets) <- sources |
| 165 |
enumerate(targets, function(source, target) { |
| 166 |
if (!renv_file_same(source, target)) |
| 167 |
renv_file_link(source, target, overwrite = TRUE) |
| 168 |
}) |
| 169 |
|
| 170 |
# create marker indicating this is a sandbox |
| 171 |
# (or, if it already exists, re-create it and update its ctime / mtime) |
| 172 |
marker <- file.path(sandbox, ".renv-sandbox") |
| 173 |
file.create(marker) |
| 174 |
|
| 175 |
# update mtime on the sandbox itself as well |
| 176 |
Sys.setFileTime(sandbox, time = Sys.time()) |
| 177 |
|
| 178 |
# make the library unwritable again |
| 179 |
if (lockable) { |
| 180 |
dlog("sandbox", "locking sandbox") |
| 181 |
renv_sandbox_lock(sandbox) |
| 182 |
} |
| 183 |
|
| 184 |
# return sandbox path |
| 185 |
sandbox |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
renv_sandbox_deactivate <- function() { |
| 190 |
|
| 191 |
# get library paths sans .Library, .Library.site |
| 192 |
old <- renv_libpaths_all() |
| 193 |
syslibs <- renv_path_normalize(c(.Library, .Library.site)) |
| 194 |
|
| 195 |
# restore old bindings |
| 196 |
base <- .BaseNamespaceEnv |
| 197 |
renv_binding_replace(base, ".Library", renv_libpaths_system()) |
| 198 |
renv_binding_replace(base, ".Library.site", renv_libpaths_site()) |
| 199 |
|
| 200 |
# update library paths |
| 201 |
new <- renv_vector_diff(old, syslibs) |
| 202 |
renv_libpaths_set(new) |
| 203 |
|
| 204 |
renv_libpaths_all() |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
renv_sandbox_task <- function(...) { |
| 209 |
|
| 210 |
# check if we're enabled |
| 211 |
if (!renv_sandbox_activated()) |
| 212 |
return() |
| 213 |
|
| 214 |
# allow opt-out if necessary |
| 215 |
enabled <- getOption("renv.sandbox.task", default = TRUE) |
| 216 |
if (!enabled) |
| 217 |
return() |
| 218 |
|
| 219 |
# get sandbox path |
| 220 |
sandbox <- tail(.libPaths(), n = 1L) |
| 221 |
|
| 222 |
# make sure it exists |
| 223 |
if (!file.exists(sandbox)) { |
| 224 |
warning("the renv sandbox was deleted; it will be re-generated", call. = FALSE) |
| 225 |
ensure_directory(sandbox) |
| 226 |
renv_sandbox_generate(sandbox) |
| 227 |
} |
| 228 |
|
| 229 |
# update the sandbox write time / mtime |
| 230 |
Sys.setFileTime(sandbox, time = Sys.time()) |
| 231 |
|
| 232 |
} |
| 233 |
|
| 234 |
renv_sandbox_path <- function(project = NULL) { |
| 235 |
renv_paths_sandbox(project = project) |
| 236 |
} |
| 237 |
|
| 238 |
renv_sandbox_lockable <- function(sandbox = NULL) { |
| 239 |
getOption("renv.sandbox.locking_enabled", default = TRUE) |
| 240 |
} |
| 241 |
|
| 242 |
renv_sandbox_lock <- function(sandbox = NULL, project = NULL) { |
| 243 |
sandbox <- sandbox %||% renv_sandbox_path(project = project) |
| 244 |
mode <- file.mode(sandbox) & "577" |
| 245 |
Sys.chmod(sandbox, mode = mode) |
| 246 |
} |
| 247 |
|
| 248 |
renv_sandbox_locked <- function(sandbox = NULL, project = NULL) { |
| 249 |
sandbox <- sandbox %||% renv_sandbox_path(project = project) |
| 250 |
file.exists(sandbox) && file.access(sandbox, mode = 7L) != 0L |
| 251 |
} |
| 252 |
|
| 253 |
renv_sandbox_unlock <- function(sandbox = NULL, project = NULL) { |
| 254 |
sandbox <- sandbox %||% renv_sandbox_path(project = project) |
| 255 |
mode <- file.mode(sandbox) | "200" |
| 256 |
Sys.chmod(sandbox, mode = mode) |
| 257 |
} |
| 258 |
|
| 259 |
#' The default library sandbox |
| 260 |
#' |
| 261 |
#' @description |
| 262 |
#' An \R installation can have up to three types of library paths available |
| 263 |
#' to the user: |
| 264 |
#' |
| 265 |
#' - The _user library_, where \R packages downloaded and installed by the |
| 266 |
#' current user are installed. This library path is only visible to that |
| 267 |
#' specific user. |
| 268 |
#' |
| 269 |
#' - The _site library_, where \R packages maintained by administrators of a |
| 270 |
#' system are installed. This library path, if it exists, is visible to all |
| 271 |
#' users on the system. |
| 272 |
#' |
| 273 |
#' - The _default library_, where \R packages distributed with \R itself are |
| 274 |
#' installed. This library path is visible to all users on the system. |
| 275 |
#' |
| 276 |
#' Normally, only so-called "base" and "recommended" packages should be installed |
| 277 |
#' in the default library. (You can get a list of these packages with |
| 278 |
#' `installed.packages(priority = c("base", "recommended"))`). However, it is |
| 279 |
#' possible for users and administrators to install packages into the default |
| 280 |
#' library, if the filesystem permissions permit them to do so. (This, for |
| 281 |
#' example, is the default behavior on macOS.) |
| 282 |
#' |
| 283 |
#' Because the site and default libraries are visible to all users, having those |
| 284 |
#' accessible in renv projects can potentially break isolation -- that is, |
| 285 |
#' if a package were updated in the default library, that update would be visible |
| 286 |
#' to all \R projects on the system. |
| 287 |
#' |
| 288 |
#' To help defend against this, renv uses something called the "sandbox" to |
| 289 |
#' isolate renv projects from non-"base" packages that are installed into the |
| 290 |
#' default library. When an renv project is loaded, renv will: |
| 291 |
#' |
| 292 |
#' - Create a new, empty library path (called the "sandbox"), |
| 293 |
#' |
| 294 |
#' - Link only the "base" and "recommended" packages from the default library |
| 295 |
#' into the sandbox, |
| 296 |
#' |
| 297 |
#' - Mark the sandbox as read-only, so that users are unable to install packages |
| 298 |
#' into this library, |
| 299 |
#' |
| 300 |
#' - Instruct the \R session to use the "sandbox" as the default library. |
| 301 |
#' |
| 302 |
#' This process is mostly transparent to the user. However, because the sandbox |
| 303 |
#' is read-only, if you later need to remove the sandbox, you'll need to reset |
| 304 |
#' file permissions manually; for example, with `renv::sandbox$unlock()`. |
| 305 |
#' |
| 306 |
#' If you'd prefer to keep the sandbox unlocked, you can also set: |
| 307 |
#' |
| 308 |
#' ``` |
| 309 |
#' RENV_SANDBOX_LOCKING_ENABLED = FALSE |
| 310 |
#' ``` |
| 311 |
#' |
| 312 |
#' in an appropriate startup `.Renviron` or `Renviron.site` file. |
| 313 |
#' |
| 314 |
#' The sandbox can also be disabled entirely with: |
| 315 |
#' |
| 316 |
#' ``` |
| 317 |
#' RENV_CONFIG_SANDBOX_ENABLED = FALSE |
| 318 |
#' ``` |
| 319 |
#' |
| 320 |
#' The sandbox library path can also be configured using the `RENV_PATHS_SANDBOX` |
| 321 |
#' environment variable: see [paths] for more details. |
| 322 |
#' |
| 323 |
#' @format NULL |
| 324 |
#' @export |
| 325 |
sandbox <- list( |
| 326 |
path = renv_sandbox_path, |
| 327 |
lock = renv_sandbox_lock, |
| 328 |
locked = renv_sandbox_locked, |
| 329 |
unlock = renv_sandbox_unlock |
| 330 |
) |