diff --git a/.jules/sentinel.md b/.jules/sentinel.md index a8207a48..1796220e 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-13 - Integer overflow prevention in interactive prompts +**Vulnerability:** Input validation using regex `^[0-9]+$` on interactive prompts combined with `as.integer()` allows inputs larger than R's max integer (e.g., `999999999999999999999`) to cause `NA` coercion via integer overflow. This missing value can crash execution when evaluated in an `if()` condition (e.g., `if (confirm != 1)`). +**Learning:** `grepl("^[0-9]+$")` only checks if characters are digits, but does not enforce bounds checking for the data type it's subsequently parsed into. +**Prevention:** Strictly validate inputs from `readline()` against specific expected discrete values (e.g., `if (n == "1" || n == "2")`) to explicitly reject any out-of-bounds or malformed entries prior to type coercion. diff --git a/DESCRIPTION b/DESCRIPTION index f31d3e1a..c90753c5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,7 +10,7 @@ Description: Automates fixed item parameter linking for test linking under the item response theory paradigm using mirt package estimates. License: GPL-3 | file LICENSE Imports: mirt, methods -Suggests: testthat (>= 3.0.0) +Suggests: testthat (>= 3.0.0), mockery Encoding: UTF-8 Config/testthat/edition: 3 Config/roxygen2/version: 8.0.0 diff --git a/R/aFIPC.R b/R/aFIPC.R index 62546519..d32d09bd 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 (n == "1" || n == "2") { 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 (n == "1" || n == "2") { 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 (n == "1" || n == "2") { return(as.integer(n)) } } diff --git a/tests/testthat/test-sentinel-interactive.R b/tests/testthat/test-sentinel-interactive.R new file mode 100644 index 00000000..88d7a2de --- /dev/null +++ b/tests/testthat/test-sentinel-interactive.R @@ -0,0 +1,18 @@ +test_that("interactive prompts strictly validate input and prevent integer overflow", { + df_x <- data.frame(A = c(1,0,1,0), B = c(1,0,1,0)) + df_y <- data.frame(A = c(1,0,1,0), C = c(1,0,1,0)) + + mockery::stub(aFIPC::autoFIPC, "interactive", function() TRUE) + mockery::stub(aFIPC::autoFIPC, "readline", mockery::mock("999999999999999999999", "3", "abc", cycle = TRUE)) + + expect_error( + aFIPC::autoFIPC( + newformXData = df_x, + oldformYData = df_y, + newformCommonItemNames = "A", + oldformCommonItemNames = "A", + confirmCommonItems = NULL + ), + "Too many invalid common item confirmation attempts" + ) +})