Skip to content
Open
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
10 changes: 10 additions & 0 deletions RaspberryPi/Scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ die() {
declare -A cfg=([dry_run]=0 [skip_external]=0 [minimal]=0 [quiet]=0 [insecure_ssh]=0)
run() { ((cfg[dry_run])) && log "[DRY] $*" || "$@"; }
# Safe cleanup workspace

download_and_install() {
local url="$1"
local dest="$2"
local tmp="$WORKDIR/${url##*/}"
curl -fsSL "$url" -o "$tmp"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: Missing security features compared to existing run_url function (line 48). This curl call lacks:

  1. TLS enforcement - No --proto '=https' --tlsv1.3 flags, allowing HTTP downgrade attacks
  2. URL validation - No filename validation like [[ $name =~ ^[[:alnum:]._-]+$ ]]
  3. Retry logic - Missing --retry 3 --retry-delay 2 for network failures
  4. Empty file check - No [[ -s $tmp ]] validation
  5. Error handling - Missing || { err ...; return 1; } after curl

Comment on lines +29 to +33
Comment on lines +32 to +33
sudo mv "$tmp" "$dest"
sudo chmod +x "$dest"
Comment on lines +33 to +35
}
Comment on lines +29 to +36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This new function is currently unused. If it's intended for future use, it should be made more robust. The current implementation has several issues:

  • Unused Code: The function is defined but never called. Unused code should be removed to avoid confusion and maintenance overhead.
  • Security Vulnerability: It is vulnerable to path traversal. A malicious URL could cause writes outside of $WORKDIR. The filename should be validated, as recommended by the style guide.
  • Lacks Robustness:
    • No validation for required url and dest arguments.
    • The curl command lacks retry logic and error handling, which is a standard pattern in this repository (see run_url and the style guide on safe fetching).
    • It doesn't check if the downloaded file is empty before attempting to install it.

A more robust implementation that aligns with other functions in this file would look like this:

download_and_install() {
  local url="${1:?URL is required}"
  local dest="${2:?Destination is required}"
  local name="${url##*/}"

  # Validate filename to prevent path traversal
  [[ $name =~ ^[[:alnum:]._-]+$ ]] || { err "Invalid filename from URL: $url"; return 1; }

  local tmp="$WORKDIR/$name"

  # Download with retries and error handling
  if ! curl --proto '=https' --tlsv1.3 -fsSL --retry 3 --retry-delay 2 -o "$tmp" "$url"; then
    err "Failed to download $url"
    return 1
  fi

  # Check for empty file
  [[ -s "$tmp" ]] || { err "Downloaded file is empty: $url"; return 1; }

  sudo mv "$tmp" "$dest"
  sudo chmod +x "$dest"
}
References
  1. The style guide provides patterns and functions (e.g., validate_path) to prevent path traversal vulnerabilities. The filename extracted from the URL is not validated against these patterns. (link)
  2. The style guide specifies patterns for safe and hardened URL fetching, including retry logic and secure protocol enforcement, which are missing from the curl command in this function. (link)
  3. The style guide emphasizes context-rich error handling and checking for command failures, which is not implemented for the download operation. (link)


Comment on lines +29 to +37
run_url() {
local url="$1"
shift
Expand Down
Loading