Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .jules/sentinel.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 2024-07-12 - Fix missing parameter validations
**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.
**Prevention:** Always implement explicit runtime type validation for optional boolean parameters.
6 changes: 3 additions & 3 deletions R/aFIPC.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand Down Expand Up @@ -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))
}
}
Expand Down Expand Up @@ -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))
}
}
Expand Down
46 changes: 46 additions & 0 deletions tests/testthat/test-readline-validation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
make_interactive_auto_fipc <- function(responses) {
prompt_count <- 0L
subject <- autoFIPC
overrides <- new.env(parent = environment(subject))
overrides$interactive <- function() TRUE
overrides$readline <- function(prompt = "") {
prompt_count <<- prompt_count + 1L
responses[[prompt_count]]
}
environment(subject) <- overrides

list(
run = function() {
subject(
newformXData = matrix(c(0, 1), ncol = 1),
oldformYData = matrix(c(0, 1), ncol = 1),
newformCommonItemNames = "item1",
oldformCommonItemNames = "item1",
itemtype = "2PL"
)
},
prompt_count = function() prompt_count
)
}

test_that("autoFIPC rejects non-menu numeric input before integer coercion", {
harness <- make_interactive_auto_fipc(c("3", "99999999999999999999", "10"))

expect_error(
harness$run(),
"Too many invalid common item confirmation attempts",
fixed = TRUE
)
expect_equal(harness$prompt_count(), 3L)
})

test_that("autoFIPC accepts the exact menu choice 2", {
harness <- make_interactive_auto_fipc("2")

expect_error(
harness$run(),
"Please write down pairs correctly",
fixed = TRUE
)
expect_equal(harness$prompt_count(), 1L)
})
Loading