R/utils__validators.R

Defines functions .scalar_chr oauth_callback_limits validate_untrusted_query_string validate_untrusted_query_param is_valid_cookie_path is_valid_string compact_list

# This file contains small validation and coercion helpers shared across many
# other files
# Used for common checks such as string validity, cookie-path safety, and
# callback query size limits

# 1 Shared validators ----------------------------------------------------------

## 1.1 Common list, string, and query checks -----------------------------------

#' Internal: drop NULL/NA values from a list (for query building)
#'
#' @param x List-like input to filter.
#' @return `x` with `NULL` values and scalar `NA` entries removed, or `x`
#'   unchanged when it is not a list.
#' @keywords internal
#' @noRd
compact_list <- function(x) {
  if (!is.list(x)) {
    return(x)
  }
  x[
    !vapply(
      x,
      function(v) is.null(v) || (length(v) == 1 && is.na(v)),
      logical(1)
    )
  ]
}

#' Internal: check if input is a non-empty string
#'
#' @param x Value to check.
#' @param min_char Minimum allowed character count.
#' @return `TRUE` when `x` is a single non-empty string meeting the minimum
#'   length; otherwise `FALSE`.
#' @keywords internal
#' @noRd
is_valid_string <- function(x, min_char = 1) {
  is.character(x) &&
    length(x) == 1 &&
    !is.na(x) &&
    nzchar(x) &&
    nchar(x) >= min_char
}

#' Internal: validate cookie Path attribute input
#'
#' Accepts only RFC 6265-safe path values for explicit cookie Path attributes:
#' must start with `/` and must not contain semicolons or control characters.
#'
#' @param x Cookie path candidate.
#' @return `TRUE` when `x` is a valid cookie-path value; otherwise `FALSE`.
#' @keywords internal
#' @noRd
is_valid_cookie_path <- function(x) {
  if (!is_valid_string(x)) {
    return(FALSE)
  }

  starts_with_slash <- startsWith(x, "/")
  has_semicolon <- grepl(";", x, fixed = TRUE)
  has_ctl <- grepl("[[:cntrl:]]", x)

  isTRUE(starts_with_slash) && !isTRUE(has_semicolon) && !isTRUE(has_ctl)
}

#' Internal: validate untrusted scalar query param sizes
#'
#' This helper is used for values that originate from URL query strings.
#' It is intentionally strict about scalar-ness to avoid vector amplification.
#'
#' @param name Query-parameter name used in error messages.
#' @param value Query-parameter value to validate.
#' @param max_bytes Maximum allowed size in bytes.
#' @param allow_empty Whether empty strings are allowed.
#' @return Invisibly returns `NULL` on success. Otherwise this function raises a
#'   state error.
#' @keywords internal
#' @noRd
validate_untrusted_query_param <- function(
  name,
  value,
  max_bytes,
  allow_empty = FALSE
) {
  if (is.null(value)) {
    return(invisible(NULL))
  }

  if (!is.character(value) || length(value) != 1L || is.na(value)) {
    err_invalid_state(
      sprintf("Callback query parameter '%s' must be a single string", name),
      context = list(param = name)
    )
  }

  if (!isTRUE(allow_empty) && !nzchar(value)) {
    err_invalid_state(
      sprintf("Callback query parameter '%s' must be non-empty", name),
      context = list(param = name)
    )
  }

  max_bytes <- as.numeric(max_bytes)
  if (!is.finite(max_bytes) || is.na(max_bytes) || max_bytes <= 0) {
    err_invalid_state(
      "Internal error: invalid max_bytes in query param validator",
      context = list(param = name)
    )
  }

  actual_bytes <- nchar(value, type = "bytes")
  if (!is.finite(actual_bytes) || is.na(actual_bytes)) {
    err_invalid_state(
      sprintf("Callback query parameter '%s' had invalid length", name),
      context = list(param = name)
    )
  }

  if (actual_bytes > max_bytes) {
    err_invalid_state(
      sprintf(
        "Callback query parameter '%s' exceeded maximum length (%s bytes)",
        name,
        format(max_bytes, scientific = FALSE, trim = TRUE)
      ),
      context = list(
        param = name,
        max_bytes = max_bytes,
        actual_bytes = actual_bytes
      )
    )
  }

  invisible(NULL)
}

#' Internal: validate untrusted callback query string sizes
#'
#' This helper guards against extremely large callback query strings causing
#' substantial allocation during parsing.
#'
#' @param query_string Raw callback query string.
#' @param max_bytes Maximum allowed size in bytes.
#' @return Invisibly returns `NULL` on success. Otherwise this function raises a
#'   state error.
#' @keywords internal
#' @noRd
validate_untrusted_query_string <- function(query_string, max_bytes) {
  if (is.null(query_string)) {
    return(invisible(NULL))
  }

  if (
    !is.character(query_string) ||
      length(query_string) != 1L ||
      is.na(query_string)
  ) {
    err_invalid_state(
      "Callback query string must be a single string",
      context = list(component = "query_string")
    )
  }

  max_bytes <- as.numeric(max_bytes)
  if (!is.finite(max_bytes) || is.na(max_bytes) || max_bytes <= 0) {
    err_invalid_state(
      "Internal error: invalid max_bytes in query string validator",
      context = list(component = "query_string")
    )
  }

  actual_bytes <- nchar(query_string, type = "bytes")
  if (!is.finite(actual_bytes) || is.na(actual_bytes)) {
    err_invalid_state(
      "Callback query string had invalid length",
      context = list(component = "query_string")
    )
  }

  if (actual_bytes > max_bytes) {
    err_invalid_state(
      sprintf(
        "Callback query string exceeded maximum length (%s bytes)",
        format(max_bytes, scientific = FALSE, trim = TRUE)
      ),
      context = list(
        component = "query_string",
        max_bytes = max_bytes,
        actual_bytes = actual_bytes
      )
    )
  }

  invisible(NULL)
}

#' Internal: callback size limits
#'
#' Centralizes callback limits used by URL query callbacks and form_post
#' callbacks so both transports enforce the same field caps.
#'
#' @return Named list of byte limits.
#' @keywords internal
#' @noRd
oauth_callback_limits <- function() {
  max_code_bytes <- get_option_positive_number(
    "shinyOAuth.callback_max_code_bytes",
    4096
  )
  max_state_bytes <- get_option_positive_number(
    "shinyOAuth.callback_max_state_bytes",
    8192
  )
  max_error_bytes <- get_option_positive_number(
    "shinyOAuth.callback_max_error_bytes",
    256
  )
  max_error_desc_bytes <- get_option_positive_number(
    "shinyOAuth.callback_max_error_description_bytes",
    4096
  )
  max_error_uri_bytes <- get_option_positive_number(
    "shinyOAuth.callback_max_error_uri_bytes",
    2048
  )
  max_iss_bytes <- get_option_positive_number(
    "shinyOAuth.callback_max_iss_bytes",
    2048
  )
  max_form_post_handle_bytes <- get_option_positive_number(
    "shinyOAuth.callback_max_form_post_handle_bytes",
    128
  )
  max_form_post_id_bytes <- get_option_positive_number(
    "shinyOAuth.callback_max_form_post_id_bytes",
    256
  )

  max_query_overhead_bytes <- 2048
  derived_query_bytes <-
    max_code_bytes +
    max_state_bytes +
    max_error_bytes +
    max_error_desc_bytes +
    max_error_uri_bytes +
    max_iss_bytes +
    max_form_post_handle_bytes +
    max_form_post_id_bytes +
    max_query_overhead_bytes

  list(
    code = max_code_bytes,
    state = max_state_bytes,
    error = max_error_bytes,
    error_description = max_error_desc_bytes,
    error_uri = max_error_uri_bytes,
    iss = max_iss_bytes,
    form_post_handle = max_form_post_handle_bytes,
    form_post_id = max_form_post_id_bytes,
    query = get_option_positive_number(
      "shinyOAuth.callback_max_query_bytes",
      derived_query_bytes
    ),
    form_post_body = get_option_positive_number(
      "shinyOAuth.callback_max_form_post_body_bytes",
      derived_query_bytes,
      max_value = .Machine$integer.max - 1L
    )
  )
}

#' Safely coerce one value to a scalar character string
#'
#' @param x Value to coerce.
#' @return Scalar character value, or `NA_character_`.
#' @keywords internal
#' @noRd
.scalar_chr <- function(x) {
  if (is.null(x) || length(x) == 0) {
    return(NA_character_)
  }
  as.character(x[[1]])
}

Try the shinyOAuth package in your browser

Any scripts or data that you put into this service are public.

shinyOAuth documentation built on May 23, 2026, 9:06 a.m.