From 7f67c72410ff8b575302c095f360a5cb95b25051 Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Sun, 27 Sep 2026 16:28:55 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A1=EF=B8=8F=20Sentinel:=20[MEDIUM]=20?= =?UTF-8?q?=ED=94=84=EB=A1=AC=ED=94=84=ED=8A=B8=20=EA=B2=80=EC=A6=9D=20?= =?UTF-8?q?=EC=8B=9C=20=EB=B0=9C=EC=83=9D=ED=95=98=EB=8A=94=20=EC=A0=95?= =?UTF-8?q?=EC=88=98=20=EC=98=A4=EB=B2=84=ED=94=8C=EB=A1=9C=EC=9A=B0=20?= =?UTF-8?q?=EC=B7=A8=EC=95=BD=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 +++ DESCRIPTION | 2 +- R/aFIPC.R | 6 ++--- .../test-sentinel-integer-validation.R | 27 +++++++++++++++++++ 4 files changed, 35 insertions(+), 4 deletions(-) create mode 100644 tests/testthat/test-sentinel-integer-validation.R diff --git a/.jules/sentinel.md b/.jules/sentinel.md index a8207a48..7e341e50 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-27 - Fix unconstrained integer validation +**Vulnerability:** Unconstrained regex matching `grepl("^[0-9]+$", n)` allows excessively large numbers to be converted by `as.integer(n)`, resulting in integer overflow (`NA` warning) which crashes downstream conditional checks expecting a boolean or integer outcome. +**Learning:** In R, validating input bounds is important especially for menu selections. Overly broad regexes leave the type coercion vulnerable to overflow logic errors. +**Prevention:** Rather than regex checks and coercion, validate the raw string explicitly against expected enum values (e.g., `n == "1" || n == "2"`) to safely guard internal logic flow. 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-integer-validation.R b/tests/testthat/test-sentinel-integer-validation.R new file mode 100644 index 00000000..e3718145 --- /dev/null +++ b/tests/testthat/test-sentinel-integer-validation.R @@ -0,0 +1,27 @@ +test_that("autoFIPC integer overflow in readline input parsing is prevented", { + # Provide small dataset + dummy_data <- data.frame( + Item1 = c(1, 0, 1, 0, 1, 1), + Item2 = c(0, 1, 0, 1, 0, 0), + Item3 = c(0, 0, 1, 1, 0, 0), + Item4 = c(1, 1, 0, 0, 1, 1) + ) + + # Mock interactive to return TRUE + mockery::stub(aFIPC::autoFIPC, "interactive", function() TRUE) + + # Mock readline to return a huge number first, then a valid "1" + mockery::stub(aFIPC::autoFIPC, "readline", mockery::mock("100000000000000", "1", cycle = TRUE)) + + # As we fixed the bug, it won't crash with "missing value where TRUE/FALSE needed" NA coercion error. + # We use try() to catch whatever it does down the line (which is estimation failing). + expect_error( + aFIPC::autoFIPC( + newformXData = dummy_data, + oldformYData = dummy_data, + newformCommonItemNames = "Item1", + oldformCommonItemNames = "Item1", + confirmCommonItems = NULL + ), regexp = "completely failed|could not estimate" + ) +})