From 274a0c0b3ee0fbb1c214555c83474b0cbdb8203a Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Mon, 14 Sep 2026 16:16:53 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A1=EF=B8=8F=20Sentinel:=20[CRITICAL]?= =?UTF-8?q?=20readline=20=EA=B2=80=EC=A6=9D=20=EC=8B=9C=20=EB=B0=9C?= =?UTF-8?q?=EC=83=9D=ED=95=98=EB=8A=94=20=EC=A0=95=EC=88=98=20=EC=98=A4?= =?UTF-8?q?=EB=B2=84=ED=94=8C=EB=A1=9C=EC=9A=B0=20=EC=B7=A8=EC=95=BD?= =?UTF-8?q?=EC=A0=90=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .jules/sentinel.md | 4 ++ R/aFIPC.R | 6 +-- tests/testthat/test-sentinel-validation.R | 55 +++++++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/.jules/sentinel.md b/.jules/sentinel.md index a8207a48..30e263d4 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-09-14 - Fix weak regex validation for readline inputs +**Vulnerability:** Weak regex `^[0-9]+$` on interactive `readline` inputs allows passing arbitrarily large numbers that exceed the 32-bit integer limit, resulting in coercion to `NA` when passed to `as.integer()` and breaking downstream logic. +**Learning:** When validating `readline()` inputs intended for `as.integer()` coercion, using unbounded regex like `^[0-9]+$` is a security vulnerability because inputs exceeding the 32-bit integer limit coerce to `NA`. +**Prevention:** Always use strictly bounded exact-match regex (e.g., `^[12]$`) when parsing integer choices to prevent coercion to `NA`. 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-sentinel-validation.R b/tests/testthat/test-sentinel-validation.R index 900f0ee3..6a027f09 100644 --- a/tests/testthat/test-sentinel-validation.R +++ b/tests/testthat/test-sentinel-validation.R @@ -35,3 +35,58 @@ 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 strictly validates readline inputs using exact bounds", { + dummy_mod <- function(...) { + mod <- new("SingleGroupClass") + mod@OptimInfo$converged <- TRUE + mod@OptimInfo$secondordertest <- TRUE + mod@Data$data <- data.frame(A=1, B=2) + return(mod) + } + + mockery::stub(autoFIPC, "mirt::mirt", dummy_mod) + mockery::stub(autoFIPC, "interactive", function() TRUE) + mockery::stub(autoFIPC, "readline", mockery::mock("33333333333", "33333333", "2")) + + expect_error( + expect_message( + autoFIPC( + newformXData = data.frame(A=1, B=2), + oldformYData = data.frame(A=1, B=2), + newformCommonItemNames = c('A'), + oldformCommonItemNames = c('A'), + confirmCommonItems = NULL + ), + "Checking correspond common item names" + ), + "Please write down pairs correctly" + ) +}) + +test_that("autoFIPC strictly validates oldformBILOGprior and newformBILOGprior using exact bounds", { + dummy_mod <- function(...) { + mod <- new("SingleGroupClass") + mod@OptimInfo$converged <- TRUE + mod@OptimInfo$secondordertest <- TRUE + mod@Data$data <- data.frame(A=1, B=2) + return(mod) + } + + mockery::stub(autoFIPC, "mirt::mirt", dummy_mod) + mockery::stub(autoFIPC, "interactive", function() TRUE) + mockery::stub(autoFIPC, "readline", mockery::mock("1", "3333333", "333333", "3333333", "33333333")) + + expect_error( + autoFIPC( + newformXData = data.frame(A=1, B=2), + oldformYData = data.frame(A=1, B=2), + newformCommonItemNames = c('A'), + oldformCommonItemNames = c('A'), + oldformBILOGprior = NULL, + newformBILOGprior = NULL, + confirmCommonItems = NULL + ), + "Too many invalid oldform BILOG prior attempts" + ) +})