From 156a54e79aaa8eac168852f5d8b41a503807cdbd Mon Sep 17 00:00:00 2001 From: Neal Richardson Date: Sat, 31 Aug 2024 13:12:11 -0400 Subject: [PATCH 1/3] Handle package_version in safe_r_metadata --- r/R/metadata.R | 4 +++- r/tests/testthat/test-metadata.R | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/r/R/metadata.R b/r/R/metadata.R index ba73f0857881..f3f2d5e39162 100644 --- a/r/R/metadata.R +++ b/r/R/metadata.R @@ -107,7 +107,9 @@ safe_r_metadata <- function(metadata, on_save = FALSE) { # and mutate the `types_removed` variable outside of it. check_r_metadata_types_recursive <- function(x) { allowed_types <- c("character", "double", "integer", "logical", "complex", "list", "NULL") - if (is.list(x)) { + # Add special handling for numeric_version, which is list but its + # [[ method leads to infinite recursion + if (is.list(x) && !inherits(x, "numeric_version")) { types <- map_chr(x, typeof) x[types == "list"] <- map(x[types == "list"], check_r_metadata_types_recursive) ok <- types %in% allowed_types diff --git a/r/tests/testthat/test-metadata.R b/r/tests/testthat/test-metadata.R index 175e7ef3b6b7..f57dfca22596 100644 --- a/r/tests/testthat/test-metadata.R +++ b/r/tests/testthat/test-metadata.R @@ -149,6 +149,15 @@ arbitrary\040code\040was\040just\040executed ) }) +test_that("R metadata processing doesn't choke on packageVersion() output", { + metadata <- list(version = packageVersion("base")) + expect_identical(safe_r_metadata(metadata), metadata) + + df <- example_data[1:6] + attr(df, "version") <- packageVersion("base") + expect_equal_data_frame(Table$create(df), df) +}) + test_that("Complex or unsafe attributes are pruned from R metadata, if they exist", { tab <- Table$create(example_data[1:6]) bad <- new.env() From 735fdc6d928c9b0efeeedbbecc82bafd6be63e5e Mon Sep 17 00:00:00 2001 From: Neal Richardson Date: Sun, 1 Sep 2024 10:07:50 -0400 Subject: [PATCH 2/3] Update r/R/metadata.R --- r/R/metadata.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/r/R/metadata.R b/r/R/metadata.R index f3f2d5e39162..77f9d04d28ed 100644 --- a/r/R/metadata.R +++ b/r/R/metadata.R @@ -107,9 +107,9 @@ safe_r_metadata <- function(metadata, on_save = FALSE) { # and mutate the `types_removed` variable outside of it. check_r_metadata_types_recursive <- function(x) { allowed_types <- c("character", "double", "integer", "logical", "complex", "list", "NULL") - # Add special handling for numeric_version, which is list but its - # [[ method leads to infinite recursion - if (is.list(x) && !inherits(x, "numeric_version")) { + # Add special handling for some base R classes that are list but + # their [[ methods leads to infinite recursion + if (is.list(x) && !inherits(x, c("numeric_version", "POSIXlt"))) { types <- map_chr(x, typeof) x[types == "list"] <- map(x[types == "list"], check_r_metadata_types_recursive) ok <- types %in% allowed_types From 607a5f0732cdd6c7d9b207b2f507aba0a8e0c7de Mon Sep 17 00:00:00 2001 From: Neal Richardson Date: Sun, 1 Sep 2024 12:25:59 -0400 Subject: [PATCH 3/3] unclass() to handle list subclasses that behave like atomics; also check attributes for invalid types --- r/R/metadata.R | 25 +++++++++++++++++++++---- r/tests/testthat/test-metadata.R | 20 +++++++++++++------- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/r/R/metadata.R b/r/R/metadata.R index 77f9d04d28ed..61e412be6245 100644 --- a/r/R/metadata.R +++ b/r/R/metadata.R @@ -107,17 +107,34 @@ safe_r_metadata <- function(metadata, on_save = FALSE) { # and mutate the `types_removed` variable outside of it. check_r_metadata_types_recursive <- function(x) { allowed_types <- c("character", "double", "integer", "logical", "complex", "list", "NULL") - # Add special handling for some base R classes that are list but - # their [[ methods leads to infinite recursion - if (is.list(x) && !inherits(x, c("numeric_version", "POSIXlt"))) { + # Pull out the attributes so we can also check them + x_attrs <- attributes(x) + + if (is.list(x)) { + # Add special handling for some base R classes that are list but + # their [[ methods leads to infinite recursion. + # We unclass here and then reapply attributes after. + x <- unclass(x) + types <- map_chr(x, typeof) - x[types == "list"] <- map(x[types == "list"], check_r_metadata_types_recursive) ok <- types %in% allowed_types if (!all(ok)) { # Record the invalid types, then remove the offending elements types_removed <<- c(types_removed, setdiff(types, allowed_types)) x <- x[ok] + if ("names" %in% names(x_attrs)) { + # Also prune from the attributes since we'll re-add later + x_attrs[["names"]] <- x_attrs[["names"]][ok] + } } + # For the rest, recurse + x <- map(x, check_r_metadata_types_recursive) + } + + # attributes() of a named list will return a list with a "names" attribute, + # so it will recurse indefinitely. + if (!is.null(x_attrs) && !identical(x_attrs, list(names = names(x)))) { + attributes(x) <- check_r_metadata_types_recursive(x_attrs) } x } diff --git a/r/tests/testthat/test-metadata.R b/r/tests/testthat/test-metadata.R index f57dfca22596..06aa1535e0a3 100644 --- a/r/tests/testthat/test-metadata.R +++ b/r/tests/testthat/test-metadata.R @@ -170,18 +170,24 @@ i Type: \"environment\" > If you trust the source, you can set `options(arrow.unsafe_metadata = TRUE)` to preserve them.", fixed = TRUE ) + # Try hiding it even further, in attributes + bad_meta <- list(attributes = structure(list(), hidden_attr = bad)) + tab$metadata <- list(r = rawToChar(serialize(bad_meta, NULL, ascii = TRUE))) + expect_warning( + as.data.frame(tab), + "Potentially unsafe or invalid elements have been discarded from R metadata. +i Type: \"environment\" +> If you trust the source, you can set `options(arrow.unsafe_metadata = TRUE)` to preserve them.", + fixed = TRUE + ) + # You can set an option to allow them through. # It still warns, just differently, and it doesn't prune the attributes withr::local_options(list("arrow.unsafe_metadata" = TRUE)) expect_warning( - expect_warning( - as.data.frame(tab), - "R metadata may have unsafe or invalid elements + as.data.frame(tab), + "R metadata may have unsafe or invalid elements i Type: \"environment\"" - ), - # This particular example ultimately fails because it's not a list - "Invalid metadata$r", - fixed = TRUE ) })