From 0c454af0a62ec66b1ea1aad67d3c9bdb7c6228a4 Mon Sep 17 00:00:00 2001 From: seonghobae <8172694+seonghobae@users.noreply.github.com> Date: Mon, 21 Sep 2026 15:55:27 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=9B=A1=EF=B8=8F=20Sentinel:=20[=EB=B3=B4?= =?UTF-8?q?=EC=95=88=20=EA=B0=9C=EC=84=A0]=20=EB=8C=80=ED=99=94=ED=98=95?= =?UTF-8?q?=20=ED=94=84=EB=A1=AC=ED=94=84=ED=8A=B8=20=EC=9E=85=EB=A0=A5?= =?UTF-8?q?=EA=B0=92=20=EA=B2=80=EC=A6=9D=20=EA=B0=95=ED=99=94=20(?= =?UTF-8?q?=EC=A0=95=EC=88=98=20=EC=98=A4=EB=B2=84=ED=94=8C=EB=A1=9C=20?= =?UTF-8?q?=EB=B0=A9=EC=A7=80)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .jules/sentinel.md | 5 +++++ DESCRIPTION | 2 +- R/aFIPC.R | 6 +++--- tests/testthat/test-sentinel-interactive.R | 18 ++++++++++++++++++ 4 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 tests/testthat/test-sentinel-interactive.R 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" + ) +})