Skip to content

Commit

Permalink
Guard against connecting to non-BacDive URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
katrinleinweber committed Aug 11, 2019
1 parent 4ca7f5d commit c83f089
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

- A cleaner solution for bug #110 was implemented, which does not
rely on supplying `user` and `password` arguments in `download()`.
- `download()` now checks for valid BacDive URLs to prevent
exfiltration of credentials to a non-BacDive domain.

## BacDiveR 0.9.1

Expand Down
7 changes: 7 additions & 0 deletions R/util-download.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
#' @import httr
#' @importFrom jsonlite fromJSON
download <- function(URL) {

# Check for valid BacDive URL
if (!grepl("^https:\\/\\/bacdive\\.dsmz\\.de\\/api\\/bacdive", URL) |
!grepl("?format=json$", URL)) {
stop("I refuse to connect to", URL, "because it's not a BacDive URL!")
}

message(URLs_to_IDs(URL), " ", appendLF = FALSE)
cred <- get_credentials()

Expand Down
19 changes: 17 additions & 2 deletions tests/testthat/test-download.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
random <- paste(sample(letters, 16), collapse = "")

test_that("Downloading without preper credentials raises an error", {
random <- sample(letters, 8)

# arrange
r_env_file <- construct_Renviron_path()
Expand All @@ -14,8 +15,22 @@ test_that("Downloading without preper credentials raises an error", {
}

# act & assert
expect_error(download(construct_url(717)))
expect_error(download(construct_url(717)), regexp = "Check your credentials")

# clean up
file.copy(r_env_backup, r_env_file, overwrite = TRUE)
})

test_that("Downloader refuses unexpected URLs", {
error_regex <- "refus\\w{,3} to connect"

expect_error(
download(paste0("http://evil.", random, "-api.net/?format=json")),
error_regex
)

expect_error(
download(paste0("https://bacdiveZdsmz.de/api/bacdive/?format=json")),
error_regex
)
})

0 comments on commit c83f089

Please sign in to comment.