I would like to use R-Shiny to develop a web app that accesses an SQL database with sensitive data.
So far I have written the database query as a pure SQL string.
However, this approach is very susceptible to SQL injections.
My intention is therefore to write the database query with dplyr
, as the query is written in R and not in pure SQL.
Unfortunately, I have not yet been able to find any more detailed information or an estimate on this topic online. Therefore I would like to clarify my request in this question.
So far I have only read a statement about the dbplyr::translate_sql()
function that is supposed to protect SQL injections:
https://dbplyr.tidyverse.org/articles/sql-translation.html#vectors
As my data is very confidential, dbplyr / dplyr should provide 100% protection against SQL injections.
I would be very pleased to receive further information and assessments on this topic.
UPDATE
As suggested in the comments, I have added an example for reproducibility:
(adapted from: https://shiny.posit.co/r/articles/build/pool-dplyr/)
library(shiny)
library(DBI)
library(pool)
library(tidyverse)
library(dbplyr)
pool <- dbPool(
drv = RMySQL::MySQL(),
dbname = "shinydemo",
host = "shiny-demo.csa7qlmguqrf.us-east-1.rds.amazonaws.com",
username = "guest",
password = "guest")
ui <- fluidPage(
textInput("ID", "Enter your ID:", "5"),
tableOutput("tbl"),
numericInput("nrows", "How many cities to show?", 10),
plotOutput("popPlot"))
server <- function(input, output, session) {
output$tbl <- renderTable({
pool %>% tbl("City") %>%
filter(ID == !!input$ID)
})
output$popPlot <- renderPlot({
df <- pool %>% tbl("City") %>%
head(as.integer(input$nrows)[1]) %>% collect()
pop <- df$Population
names(pop) <- df$Name
barplot(pop)
})
}
shinyApp(ui, server)