Skip to content
Open
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
5 changes: 5 additions & 0 deletions .jules/sentinel.md
Original file line number Diff line number Diff line change
Expand Up @@ -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-20 - Prevent Integer Overflow Coercion Vulnerability in Interactive Prompts
**Vulnerability:** Unbounded digit classes (`^[0-9]+$`) in interactive input validation allow excessively large numeric strings that evaluate to `NA` when coerced with `as.integer()`, which could lead to downstream crashes.
**Learning:** `readline()` input validation should strictly match the exact expected options (e.g., `^[12]$`) rather than generic digit patterns.
**Prevention:** Always use precise regex patterns limiting the length and allowed characters when validating inputs intended for integer conversion in R.
13 changes: 10 additions & 3 deletions R/aFIPC.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
.is_binary_prompt_choice <- function(value) {
is.character(value) &&
length(value) == 1L &&
!is.na(value) &&
grepl("^[12]$", value)
}

#' automated fixed item parameter linking
#'
#' @import mirt
Expand Down Expand Up @@ -141,7 +148,7 @@ autoFIPC <-
}
for (attempt in seq_len(3)) {
n <- readline(prompt = "Is it correct? (1: Yes 2: No) : ")
if (grepl("^[0-9]+$", n)) {
if (.is_binary_prompt_choice(n)) {
return(as.integer(n))
}
}
Expand Down Expand Up @@ -171,7 +178,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 (.is_binary_prompt_choice(n)) {
return(as.integer(n))
}
}
Expand Down Expand Up @@ -390,7 +397,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 (.is_binary_prompt_choice(n)) {
return(as.integer(n))
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-prompt-validation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
test_that("binary prompt choices accept only the documented scalar inputs", {
expect_true(aFIPC:::.is_binary_prompt_choice("1"))
expect_true(aFIPC:::.is_binary_prompt_choice("2"))

invalid <- list(
"0",
"3",
"01",
"1 ",
" 2",
"999999999999999999999999999999999999999999",
"not-a-number",
NA_character_,
character(),
c("1", "2"),
1L
)

for (value in invalid) {
expect_false(aFIPC:::.is_binary_prompt_choice(value))
}
})
Loading