Debian Codegraph @

R/vulns.R

raw
1
2 #' Request Vulnerability Information for a Package
3 #'
4 #' This function acts as an interface to Posit Package Manager's vulnerability
5 #' API, making it possible to ascertain if the provided packages have any
6 #' known vulnerabilities.
7 #'
8 #' This function requires the \pkg{curl} package to be installed.
9 #'
10 #' @inheritParams renv-params
11 #'
12 #' @param packages A vector of package specifications, of the form
13 #' `<package>==<version>` or `<package>@<version>`.
14 #'
15 #' @param lockfile The path to an `renv` lockfile. When specified, `packages`
16 #' is ignored, and vulnerabilities are queried based on the packages defined
17 #' in the lockfile.
18 #'
19 #' @param repos The Package Manager repository to be queried.
20 #'
21 #' @param verbose Boolean; when `TRUE`, verbose information from the `curl`
22 #' web request will be printed to the console.
23 #'
24 #' @returns An \R list with one entry per requested package. Each entry
25 #' contains metadata returned by Posit Package Manager (e.g. `name`,
26 #' `version`, `licenses`, `blocked`), plus a `vulns` field. The `vulns`
27 #' field is an empty list when the package has no known vulnerabilities,
28 #' and otherwise holds one or more vulnerability records.
29 #'
30 #' @keywords internal
31 #' @export
32 vulns <- function(packages = NULL,
33 lockfile = NULL,
34 ...,
35 repos = NULL,
36 verbose = FALSE,
37 project = NULL)
38 {
39 if (!requireNamespace("curl", quietly = TRUE))
40 stop("renv::vulns() requires the 'curl' package")
41
42 renv_scope_error_handler()
43 renv_dots_check(...)
44 project <- renv_project_resolve(project)
45
46 packages <- packages %||% {
47 lockfile <- lockfile %||% renv_paths_lockfile(project)
48 lockfile <- renv_lockfile_read(lockfile)
49 records <- renv_lockfile_records(lockfile)
50 map_chr(records, function(record) {
51 paste(record[["Package"]], record[["Version"]], sep = "==")
52 })
53 }
54
55 packages <- gsub("@", "==", packages, fixed = TRUE)
56
57 repos <- repos %||% getOption("repos")[[1L]]
58 parts <- renv_ppm_parse(repos)
59 if (length(parts) == 0L) {
60 warningf("failed to parse repository '%s'", repos)
61 return(list())
62 }
63
64 # begin building a curl handle
65 handle <- curl::new_handle(verbose = verbose)
66
67 # set headers for request
68 headers <- list("Content-Type" = "application/json")
69 curl::handle_setheaders(handle, .list = headers)
70
71 # start building POST options
72 data <- list(
73 repo = parts[["repos"]],
74 snapshot = parts[["snapshot"]],
75 names = as.list(unname(packages)),
76 metadata = TRUE,
77 vulns = TRUE,
78 omit_dependencies = TRUE,
79 omit_downloads = TRUE,
80 omit_package_details = TRUE
81 )
82
83 json <- renv_json_convert(data)
84
85 # get netrc file path
86 curl::handle_setopt(
87 handle = handle,
88 post = TRUE,
89 postfields = json
90 )
91
92 # use netrc if available
93 netrcFile <- getOption("netrc", default = Sys.getenv("NETRC", unset = "~/.netrc"))
94 if (file.exists(netrcFile))
95 {
96 curl::handle_setopt(
97 handle = handle,
98 httpauth = 1L,
99 netrc = 1L,
100 netrc_file = path.expand(netrcFile)
101 )
102 }
103
104 # make the request, collect the response
105 endpoint <- file.path(parts[["root"]], "__api__/filter/packages")
106 response <- curl::curl_fetch_memory(endpoint, handle = handle)
107 contents <- enc2utf8(rawToChar(response$content))
108 splat <- strsplit(contents, "\n", fixed = TRUE)[[1L]]
109 data <- lapply(splat, function(text) {
110 renv_json_read(text = text)
111 })
112
113 # handle errors
114 for (i in seq_along(data))
115 {
116 error <- data[[i]][["error"]]
117 if (!is.character(error))
118 next
119
120 code <- data[[i]][["code"]] %||% "unknown"
121 fmt <- "error requesting package metadata; %s [error code %s]"
122 msg <- sprintf(fmt, error, as.character(code))
123 stop(msg, call. = FALSE)
124 }
125
126 # PPM omits the vulns field when a package has no known vulnerabilities;
127 # normalize so callers can uniformly inspect record[["vulns"]].
128 for (i in seq_along(data))
129 data[[i]][["vulns"]] <- data[[i]][["vulns"]] %||% list()
130
131 data
132 }