ropensci / magick

Magic, madness, heaven, sin
https://docs.ropensci.org/magick
Other
461 stars 65 forks source link

image write Error only when deployed on shinyapps.io #306

Closed dcaud closed 3 years ago

dcaud commented 3 years ago

Any ideas how to overcome this error:

Error in magick_image_write: R: attempt to perform an operation not allowed by the security policyPDF' @ error/constitute.c/IsCoderAuthorized/408`

(I'm trying to create a simple reprex, but it is difficult when having to wait for a shinyapps.io deployment)

jeroen commented 3 years ago

I don't know. It looks like the shiny server prevents you from reading/writing some file but from the information above I cannot guess what that could be

ShotaOchi commented 3 years ago

I think the error message means the operation is disabled by the security policy of ImageMagick. If so, you can change the security policy and enable the operation even though the change may make some vulnerability. However, I don't know if you can access the file of ImageMagick library on shinyapps.io environment.

dcaud commented 3 years ago

The code below gives no error on my local computer (a M1 mac and a linux machine).

The code errors on shinyapps.io

The doesn't really do anything, but shouldn't error on load like it does.

library(shiny)
library(magick)

ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    wizard %>% image_write(
        tempfile(pattern = "pdftemp", fileext = '.pdf'),
        density = 55,
        format = 'pdf',
        comment = "Edit PDFs and Images"
    )

    output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)

        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
}

# Run the application 
shinyApp(ui = ui, server = server)

Here's the error:

2021-04-06T06:37:14.108727+00:00 shinyapps[3915826]: Warning: Error in magick_image_write: R: attempt to perform an operation not allowed by the security policy PDF' @ error/constitute.c/IsCoderAuthorized/408 2021-04-06T06:37:14.120607+00:00 shinyapps[3915826]: 64: <Anonymous> 2021-04-06T06:37:14.120829+00:00 shinyapps[3915826]: Error in magick_image_write(image, format, quality, depth, density, comment, : 2021-04-06T06:37:14.120830+00:00 shinyapps[3915826]: R: attempt to perform an operation not allowed by the security policyPDF' @ error/constitute.c/IsCoderAuthorized/408

dcaud commented 3 years ago

The purpose of that function in the real app is to create a PDF file from an edited image that a user can then download. Are there alternative ways to do that if the error can't be resolved?

jeroen commented 3 years ago

Ah maybe it is indeed related to imagemagick : https://stackoverflow.com/questions/52998331/imagemagick-security-policy-pdf-blocking-conversion

@dlemstra is there no way for users to store pdf files on linux, without editing the policy.xml file?

dlemstra commented 3 years ago

If the policy.xml file does not allow you to write PDF files you cannot turn it on. It is a bit strange though that they disable writing PDF files though. The security risk is when reading PDF files because we use Ghostscript then. You might want to contact ShinyApps and ask them to allow writing of PDF files. Our policy.xml file supports only disabling reading specific formats.

jeroen commented 3 years ago

The shinyapps servers are based on standard ubuntu linux distributions, they don't have any customization of config files. So apparently the policy.xml file in ubuntu linux by default does not allow writing pdf :/

Also mentioned somewhere in their changelog: http://changelogs.ubuntu.com/changelogs/pool/universe/i/imagemagick/imagemagick_6.9.10.23+dfsg-2.1ubuntu11.2/changelog

ShotaOchi commented 3 years ago

An idea is to use pdf function instead of image_write function. The following shiny app works on shinyapps.io.

library(shiny)
library(magick)

ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            sliderInput("bins",
                        "Number of bins:",
                        min = 1,
                        max = 50,
                        value = 30)
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

    pdf("test.pdf")
    plot(wizard) 
    dev.off()

    output$distPlot <- renderPlot({
        # generate bins based on input$bins from ui.R
        x    <- faithful[, 2]
        bins <- seq(min(x), max(x), length.out = input$bins + 1)

        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
    })
}

# Run the application 
shinyApp(ui = ui, server = server)
dcaud commented 3 years ago

Thanks, @ShotaOchi for the example work-around. However, I realized that I had already established a pretty good workflow that used some of the features of magick's image_write (e.g., working with multi-page/vectors of images and size calculations ) that aren't straightforward to replicate with a simple pdf() call to the device.

I could write Rstudio and ask them to change the policy.xml file, but I don't know exactly what I would be asking them to change. Any ideas?

ShotaOchi commented 3 years ago

You should tell them

You don't have to tell them what is a thing they should change. They will discuss and determine what is a thing they should change if you tell the things listed above. Don't be afraid to tell them. I believe they will welcome you.

dcaud commented 3 years ago

This looks to have been solved by Rstudio changing policy.xml.