diff --git a/.jules/sentinel.md b/.jules/sentinel.md index a8207a48..c142542a 100644 --- a/.jules/sentinel.md +++ b/.jules/sentinel.md @@ -2,3 +2,7 @@ **Vulnerability:** Unvalidated inputs passed to `if()` statements can cause process crashes (`condition has length > 1`) or unexpected coercion vulnerabilities. **Learning:** In R, optional boolean parameters that default to `NULL` should be validated using explicit runtime type validation (e.g., `if (!is.null(flag) && (!is.logical(flag) || length(flag) != 1 || is.na(flag)))`). **Prevention:** Always implement explicit runtime type validation for optional boolean parameters. +## 2024-07-28 - Fix integer coercion DoS vulnerability via interactive readline +**Vulnerability:** Interactive `readline()` prompts parsing integers via `as.integer()` were loosely validated with `^[0-9]+$`. Large inputs coerced to `NA`, breaking `if` conditionals and causing crashes (Denial of Service). +**Learning:** Weak regex for integers is dangerous since R's 32-bit limits can easily cause silent `NA` generation upon coercion. +**Prevention:** Strictly bound validations for menu prompts (e.g., `^[12]$` instead of `^[0-9]+$`). diff --git a/R/aFIPC.R b/R/aFIPC.R index 62546519..918e19b1 100644 --- a/R/aFIPC.R +++ b/R/aFIPC.R @@ -141,7 +141,7 @@ autoFIPC <- } for (attempt in seq_len(3)) { n <- readline(prompt = "Is it correct? (1: Yes 2: No) : ") - if (grepl("^[0-9]+$", n)) { + if (grepl("^[12]$", n)) { return(as.integer(n)) } } @@ -171,7 +171,7 @@ autoFIPC <- readline( prompt = "Do you want to use default BILOG-MG priors for oldform Data? (1: Yes 2: No) : " ) - if (grepl("^[0-9]+$", n)) { + if (grepl("^[12]$", n)) { return(as.integer(n)) } } @@ -390,7 +390,7 @@ autoFIPC <- readline( prompt = "Do you want to use default BILOG-MG priors for newform Data? (1: Yes 2: No) : " ) - if (grepl("^[0-9]+$", n)) { + if (grepl("^[12]$", n)) { return(as.integer(n)) } } diff --git a/tests/testthat/test-autoFIPC.R b/tests/testthat/test-autoFIPC.R index 13cecd92..0a99b4cd 100644 --- a/tests/testthat/test-autoFIPC.R +++ b/tests/testthat/test-autoFIPC.R @@ -89,3 +89,13 @@ test_that("autoFIPC validates input types securely", { "Security Error: tryEM must be a single non-NA logical value" ) }) + +test_that("autoFIPC securely restricts input via regex for prompts", { + expect_true(grepl("^[12]$", "1")) + expect_true(grepl("^[12]$", "2")) + expect_false(grepl("^[12]$", "3")) + expect_false(grepl("^[12]$", "12")) + expect_false(grepl("^[12]$", "0")) + expect_false(grepl("^[12]$", "abc")) + expect_false(grepl("^[12]$", "")) +})