diff --git a/.jules/sentinel.md b/.jules/sentinel.md index a8207a4..4065d3d 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-25 - Fix missing input validation in readline prompts +**Vulnerability:** Weak regex `^[0-9]+$` in interactive `readline()` prompts allowed arbitrarily large numeric strings to be passed. When cast via `as.integer()`, these resulted in `NA`, which bypassed conditionals and risked `condition has length > 1` process crashes (DoS). +**Learning:** `readline()` input representing exact integer choices (e.g. `1: Yes, 2: No`) must be validated using strictly bounded regex matching like `^[12]$`. Coercion functions (`as.integer()`) are not safe without prior bounding. +**Prevention:** Always use strictly bounded regular expressions when validating exact interactive inputs to prevent unexpected NA coercion crashes. diff --git a/DESCRIPTION b/DESCRIPTION index f31d3e1..c90753c 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 6254651..918e19b 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-sentinel-validation.R b/tests/testthat/test-sentinel-validation.R index 900f0ee..956c5c5 100644 --- a/tests/testthat/test-sentinel-validation.R +++ b/tests/testthat/test-sentinel-validation.R @@ -35,3 +35,64 @@ test_that("autoFIPC validates boolean flags for newformBILOGprior, oldformBILOGp "Security Error: confirmCommonItems must be a single non-NA logical value or NULL" ) }) + +test_that("autoFIPC input validation for readline protects against coercion DoS", { + # Mock interactive to TRUE to enter readline paths + mock_interactive <- mockery::mock(TRUE, TRUE, TRUE) + mockery::stub(aFIPC::autoFIPC, 'interactive', mock_interactive) + + # 1. confirmCommonItems + mock_readline_confirm <- mockery::mock("99999999999999999999", "3", "invalid") + mockery::stub(aFIPC::autoFIPC, 'readline', mock_readline_confirm) + + expect_error( + aFIPC::autoFIPC( + newformXData = data.frame(A=1), + oldformYData = data.frame(A=2), + newformCommonItemNames = c('A'), + oldformCommonItemNames = c('A'), + confirmCommonItems = NULL + ), + "Too many invalid common item confirmation attempts" + ) + + # 2. oldformBILOGprior + mockery::stub(aFIPC::autoFIPC, 'interactive', mockery::mock(TRUE, TRUE, TRUE)) + mockery::stub(aFIPC::autoFIPC, 'readline', mockery::mock("1")) # Pass confirmCommonItems + + # Stub nested function call to simulate readline failures specifically for oldformBILOGprior + mock_readline_oldform <- mockery::mock("99999999999999999999", "3", "invalid") + mockery::stub(aFIPC::autoFIPC, 'readline', mock_readline_oldform, depth = 2) # checkoldformBILOGprior + + expect_error( + aFIPC::autoFIPC( + newformXData = data.frame(A=1, B=2), + oldformYData = data.frame(A=1, B=2), + newformCommonItemNames = c('A'), + oldformCommonItemNames = c('A'), + confirmCommonItems = TRUE, + itemtype = "3PL" + ), + "Too many invalid oldform BILOG prior attempts" + ) + + # 3. newformBILOGprior + mock_readline_newform <- mockery::mock("99999999999999999999", "3", "invalid") + mockery::stub(aFIPC::autoFIPC, 'readline', mock_readline_newform, depth = 2) + + # Need an oldFormModel so it reaches newform estimation + mock_old_model <- mirt::mirt(data.frame(A=c(1,0,1,0,1,0,1,0,1,0,1,0), B=c(0,1,0,1,0,1,0,1,0,1,0,1), C=c(1,1,0,0,1,1,0,0,1,1,0,0), D=c(0,0,1,1,0,0,1,1,0,0,1,1)), 1, itemtype="2PL", TOL=0.1, SE=FALSE, GenRandomPars=TRUE) + + expect_error( + aFIPC::autoFIPC( + newformXData = data.frame(A=c(1,0,1,0,1,0,1,0,1,0,1,0), B=c(0,1,0,1,0,1,0,1,0,1,0,1), C=c(1,1,0,0,1,1,0,0,1,1,0,0), D=c(0,0,1,1,0,0,1,1,0,0,1,1)), + oldformYData = mock_old_model, + newformCommonItemNames = c('A'), + oldformCommonItemNames = c('A'), + confirmCommonItems = TRUE, + itemtype = "3PL", + oldformBILOGprior = TRUE + ), + "Too many invalid newform BILOG prior attempts" + ) +})