diff --git a/.jules/sentinel.md b/.jules/sentinel.md index a8207a4..cdacd63 100644 --- a/.jules/sentinel.md +++ b/.jules/sentinel.md @@ -2,3 +2,8 @@ **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-20 - Prevent Integer Overflow Coercion Vulnerability in Interactive Prompts +**Vulnerability:** Unbounded digit classes (`^[0-9]+$`) in interactive input validation allow excessively large numeric strings that evaluate to `NA` when coerced with `as.integer()`, which could lead to downstream crashes. +**Learning:** `readline()` input validation should strictly match the exact expected options (e.g., `^[12]$`) rather than generic digit patterns. +**Prevention:** Always use precise regex patterns limiting the length and allowed characters when validating inputs intended for integer conversion in R. diff --git a/R/aFIPC.R b/R/aFIPC.R index 6254651..c04ec0c 100644 --- a/R/aFIPC.R +++ b/R/aFIPC.R @@ -1,3 +1,10 @@ +.is_binary_prompt_choice <- function(value) { + is.character(value) && + length(value) == 1L && + !is.na(value) && + grepl("^[12]$", value) +} + #' automated fixed item parameter linking #' #' @import mirt @@ -141,7 +148,7 @@ autoFIPC <- } for (attempt in seq_len(3)) { n <- readline(prompt = "Is it correct? (1: Yes 2: No) : ") - if (grepl("^[0-9]+$", n)) { + if (.is_binary_prompt_choice(n)) { return(as.integer(n)) } } @@ -171,7 +178,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 (.is_binary_prompt_choice(n)) { return(as.integer(n)) } } @@ -390,7 +397,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 (.is_binary_prompt_choice(n)) { return(as.integer(n)) } } diff --git a/tests/testthat/test-prompt-validation.R b/tests/testthat/test-prompt-validation.R new file mode 100644 index 0000000..98b00ac --- /dev/null +++ b/tests/testthat/test-prompt-validation.R @@ -0,0 +1,22 @@ +test_that("binary prompt choices accept only the documented scalar inputs", { + expect_true(aFIPC:::.is_binary_prompt_choice("1")) + expect_true(aFIPC:::.is_binary_prompt_choice("2")) + + invalid <- list( + "0", + "3", + "01", + "1 ", + " 2", + "999999999999999999999999999999999999999999", + "not-a-number", + NA_character_, + character(), + c("1", "2"), + 1L + ) + + for (value in invalid) { + expect_false(aFIPC:::.is_binary_prompt_choice(value)) + } +})