Skip to content

Commit 3dbf115

Browse files
authored
VM's: add input validation and hostname sanitization to all VM scripts (#12973)
* Sanitize hostname generation from VM_NAME Replace the previous simple space-removal with stricter sanitization when deriving the hostname from VM_NAME. Non-alphanumeric/hyphen sequences are collapsed to a single hyphen and leading/trailing hyphens are trimmed, preserving lowercase and ensuring a cleaner, more valid hostname string. * fix(vm): validate CORE_COUNT input - require positive integer, re-ask on invalid * Validate RAM input in VM scripts Add input validation and retry loop for RAM size prompts across multiple VM scripts. Each modified advanced_settings() now wraps the whiptail RAM input in a while-true loop, ensures a default of 2048 when empty, validates that the value is a positive integer, shows an "INVALID INPUT" msgbox on bad values, and calls exit-script when the dialog is canceled. Also fixes quoting of RAM_SIZE in several scripts. Affected files: vm/archlinux-vm.sh, vm/debian-13-vm.sh, vm/mikrotik-routeros.sh, vm/nextcloud-vm.sh, vm/owncloud-vm.sh, vm/ubuntu2204-vm.sh, vm/ubuntu2404-vm.sh, vm/ubuntu2504-vm.sh, vm/umbrel-os-vm.sh. These changes prevent invalid RAM entries and improve user experience when configuring VMs. * Validate RAM input for VM scripts Add robust RAM input validation to multiple VM helper scripts (debian, docker, haos, openwrt, opnsense, pimox-haos, truenas). Each RAM prompt is now wrapped in a while loop that: provides a sensible default when the input is empty, ensures the value is a positive integer via regex, shows an "INVALID INPUT" whiptail message on bad input, and exits cleanly when the user cancels. Also fixed quoting of variable tests and normalized echoing of the allocated RAM. The pimox change preserves exit-status handling while integrating the same validation loop. * fix(vm): validate VLAN input - require 1-4094 range, re-ask on invalid * fix(vm): validate MTU input - require 576-65520 range, re-ask on invalid * fix(vm): validate MAC address input - require XX:XX:XX:XX:XX:XX format, re-ask on invalid
1 parent 185dd55 commit 3dbf115

16 files changed

+1090
-689
lines changed

vm/archlinux-vm.sh

Lines changed: 71 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,10 @@ function advanced_settings() {
310310
HN="arch-linux"
311311
echo -e "${HOSTNAME}${BOLD}${DGN}Hostname: ${BGN}$HN${CL}"
312312
else
313-
HN=$(echo "${VM_NAME,,}" | tr -d ' ')
313+
HN=$(echo "${VM_NAME,,}" | tr -cs 'a-z0-9-' '-' | sed 's/^-//;s/-$//')
314+
if [ "$HN" != "${VM_NAME,,}" ]; then
315+
whiptail --backtitle "Proxmox VE Helper Scripts" --title "HOSTNAME ADJUSTED" --msgbox "Invalid characters detected. Hostname has been adjusted to:\n\n $HN" 10 58
316+
fi
314317
echo -e "${HOSTNAME}${BOLD}${DGN}Hostname: ${BGN}$HN${CL}"
315318
fi
316319
else
@@ -332,27 +335,31 @@ function advanced_settings() {
332335
exit-script
333336
fi
334337

335-
if CORE_COUNT=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Allocate CPU Cores" 8 58 2 --title "CORE COUNT" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
336-
if [ -z "$CORE_COUNT" ]; then
337-
CORE_COUNT="2"
338-
echo -e "${CPUCORE}${BOLD}${DGN}CPU Cores: ${BGN}$CORE_COUNT${CL}"
338+
while true; do
339+
if CORE_COUNT=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Allocate CPU Cores" 8 58 2 --title "CORE COUNT" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
340+
if [ -z "$CORE_COUNT" ]; then CORE_COUNT="2"; fi
341+
if [[ "$CORE_COUNT" =~ ^[1-9][0-9]*$ ]]; then
342+
echo -e "${CPUCORE}${BOLD}${DGN}CPU Cores: ${BGN}$CORE_COUNT${CL}"
343+
break
344+
fi
345+
whiptail --backtitle "Proxmox VE Helper Scripts" --title "INVALID INPUT" --msgbox "CPU Cores must be a positive integer (e.g., 2)." 8 58
339346
else
340-
echo -e "${CPUCORE}${BOLD}${DGN}CPU Cores: ${BGN}$CORE_COUNT${CL}"
347+
exit-script
341348
fi
342-
else
343-
exit-script
344-
fi
349+
done
345350

346-
if RAM_SIZE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Allocate RAM in MiB" 8 58 2048 --title "RAM" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
347-
if [ -z "$RAM_SIZE" ]; then
348-
RAM_SIZE="2048"
349-
echo -e "${RAMSIZE}${BOLD}${DGN}RAM Size: ${BGN}$RAM_SIZE${CL}"
351+
while true; do
352+
if RAM_SIZE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Allocate RAM in MiB" 8 58 2048 --title "RAM" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
353+
if [ -z "$RAM_SIZE" ]; then RAM_SIZE="2048"; fi
354+
if [[ "$RAM_SIZE" =~ ^[1-9][0-9]*$ ]]; then
355+
echo -e "${RAMSIZE}${BOLD}${DGN}RAM Size: ${BGN}$RAM_SIZE${CL}"
356+
break
357+
fi
358+
whiptail --backtitle "Proxmox VE Helper Scripts" --title "INVALID INPUT" --msgbox "RAM Size must be a positive integer in MiB (e.g., 2048)." 8 58
350359
else
351-
echo -e "${RAMSIZE}${BOLD}${DGN}RAM Size: ${BGN}$RAM_SIZE${CL}"
360+
exit-script
352361
fi
353-
else
354-
exit-script
355-
fi
362+
done
356363

357364
if BRG=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a Bridge" 8 58 vmbr0 --title "BRIDGE" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
358365
if [ -z "$BRG" ]; then
@@ -365,43 +372,61 @@ function advanced_settings() {
365372
exit-script
366373
fi
367374

368-
if MAC1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a MAC Address" 8 58 "$GEN_MAC" --title "MAC ADDRESS" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
369-
if [ -z "$MAC1" ]; then
370-
MAC="$GEN_MAC"
371-
echo -e "${MACADDRESS}${BOLD}${DGN}MAC Address: ${BGN}$MAC${CL}"
375+
while true; do
376+
if MAC1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a MAC Address" 8 58 "$GEN_MAC" --title "MAC ADDRESS" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
377+
if [ -z "$MAC1" ]; then
378+
MAC="$GEN_MAC"
379+
echo -e "${MACADDRESS}${BOLD}${DGN}MAC Address: ${BGN}$MAC${CL}"
380+
break
381+
fi
382+
if [[ "$MAC1" =~ ^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$ ]]; then
383+
MAC="$MAC1"
384+
echo -e "${MACADDRESS}${BOLD}${DGN}MAC Address: ${BGN}$MAC1${CL}"
385+
break
386+
fi
387+
whiptail --backtitle "Proxmox VE Helper Scripts" --title "INVALID INPUT" --msgbox "Invalid MAC address format. Use XX:XX:XX:XX:XX:XX (e.g., AA:BB:CC:DD:EE:FF)." 8 58
372388
else
373-
MAC="$MAC1"
374-
echo -e "${MACADDRESS}${BOLD}${DGN}MAC Address: ${BGN}$MAC1${CL}"
389+
exit-script
375390
fi
376-
else
377-
exit-script
378-
fi
391+
done
379392

380-
if VLAN1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a Vlan(leave blank for default)" 8 58 --title "VLAN" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
381-
if [ -z "$VLAN1" ]; then
382-
VLAN1="Default"
383-
VLAN=""
384-
echo -e "${VLANTAG}${BOLD}${DGN}VLAN: ${BGN}$VLAN1${CL}"
393+
while true; do
394+
if VLAN1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a Vlan(leave blank for default)" 8 58 --title "VLAN" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
395+
if [ -z "$VLAN1" ]; then
396+
VLAN1="Default"
397+
VLAN=""
398+
echo -e "${VLANTAG}${BOLD}${DGN}VLAN: ${BGN}$VLAN1${CL}"
399+
break
400+
fi
401+
if [[ "$VLAN1" =~ ^[0-9]+$ ]] && [ "$VLAN1" -ge 1 ] && [ "$VLAN1" -le 4094 ]; then
402+
VLAN=",tag=$VLAN1"
403+
echo -e "${VLANTAG}${BOLD}${DGN}VLAN: ${BGN}$VLAN1${CL}"
404+
break
405+
fi
406+
whiptail --backtitle "Proxmox VE Helper Scripts" --title "INVALID INPUT" --msgbox "VLAN must be a number between 1 and 4094, or leave blank for default." 8 58
385407
else
386-
VLAN=",tag=$VLAN1"
387-
echo -e "${VLANTAG}${BOLD}${DGN}VLAN: ${BGN}$VLAN1${CL}"
408+
exit-script
388409
fi
389-
else
390-
exit-script
391-
fi
410+
done
392411

393-
if MTU1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Interface MTU Size (leave blank for default)" 8 58 --title "MTU SIZE" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
394-
if [ -z "$MTU1" ]; then
395-
MTU1="Default"
396-
MTU=""
397-
echo -e "${DEFAULT}${BOLD}${DGN}Interface MTU Size: ${BGN}$MTU1${CL}"
412+
while true; do
413+
if MTU1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Interface MTU Size (leave blank for default)" 8 58 --title "MTU SIZE" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
414+
if [ -z "$MTU1" ]; then
415+
MTU1="Default"
416+
MTU=""
417+
echo -e "${DEFAULT}${BOLD}${DGN}Interface MTU Size: ${BGN}$MTU1${CL}"
418+
break
419+
fi
420+
if [[ "$MTU1" =~ ^[0-9]+$ ]] && [ "$MTU1" -ge 576 ] && [ "$MTU1" -le 65520 ]; then
421+
MTU=",mtu=$MTU1"
422+
echo -e "${DEFAULT}${BOLD}${DGN}Interface MTU Size: ${BGN}$MTU1${CL}"
423+
break
424+
fi
425+
whiptail --backtitle "Proxmox VE Helper Scripts" --title "INVALID INPUT" --msgbox "MTU Size must be a number between 576 and 65520, or leave blank for default." 8 58
398426
else
399-
MTU=",mtu=$MTU1"
400-
echo -e "${DEFAULT}${BOLD}${DGN}Interface MTU Size: ${BGN}$MTU1${CL}"
427+
exit-script
401428
fi
402-
else
403-
exit-script
404-
fi
429+
done
405430

406431
if (whiptail --backtitle "Proxmox VE Helper Scripts" --title "START VIRTUAL MACHINE" --yesno "Start VM when completed?" 10 58); then
407432
echo -e "${GATEWAY}${BOLD}${DGN}Start VM when completed: ${BGN}yes${CL}"

vm/debian-13-vm.sh

Lines changed: 71 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,10 @@ function advanced_settings() {
325325
HN="debian"
326326
echo -e "${HOSTNAME}${BOLD}${DGN}Hostname: ${BGN}$HN${CL}"
327327
else
328-
HN=$(echo "${VM_NAME,,}" | tr -d ' ')
328+
HN=$(echo "${VM_NAME,,}" | tr -cs 'a-z0-9-' '-' | sed 's/^-//;s/-$//')
329+
if [ "$HN" != "${VM_NAME,,}" ]; then
330+
whiptail --backtitle "Proxmox VE Helper Scripts" --title "HOSTNAME ADJUSTED" --msgbox "Invalid characters detected. Hostname has been adjusted to:\n\n $HN" 10 58
331+
fi
329332
echo -e "${HOSTNAME}${BOLD}${DGN}Hostname: ${BGN}$HN${CL}"
330333
fi
331334
else
@@ -347,27 +350,31 @@ function advanced_settings() {
347350
exit-script
348351
fi
349352

350-
if CORE_COUNT=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Allocate CPU Cores" 8 58 2 --title "CORE COUNT" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
351-
if [ -z "$CORE_COUNT" ]; then
352-
CORE_COUNT="2"
353-
echo -e "${CPUCORE}${BOLD}${DGN}CPU Cores: ${BGN}$CORE_COUNT${CL}"
353+
while true; do
354+
if CORE_COUNT=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Allocate CPU Cores" 8 58 2 --title "CORE COUNT" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
355+
if [ -z "$CORE_COUNT" ]; then CORE_COUNT="2"; fi
356+
if [[ "$CORE_COUNT" =~ ^[1-9][0-9]*$ ]]; then
357+
echo -e "${CPUCORE}${BOLD}${DGN}CPU Cores: ${BGN}$CORE_COUNT${CL}"
358+
break
359+
fi
360+
whiptail --backtitle "Proxmox VE Helper Scripts" --title "INVALID INPUT" --msgbox "CPU Cores must be a positive integer (e.g., 2)." 8 58
354361
else
355-
echo -e "${CPUCORE}${BOLD}${DGN}CPU Cores: ${BGN}$CORE_COUNT${CL}"
362+
exit-script
356363
fi
357-
else
358-
exit-script
359-
fi
364+
done
360365

361-
if RAM_SIZE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Allocate RAM in MiB" 8 58 2048 --title "RAM" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
362-
if [ -z "$RAM_SIZE" ]; then
363-
RAM_SIZE="2048"
364-
echo -e "${RAMSIZE}${BOLD}${DGN}RAM Size: ${BGN}$RAM_SIZE${CL}"
366+
while true; do
367+
if RAM_SIZE=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Allocate RAM in MiB" 8 58 2048 --title "RAM" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
368+
if [ -z "$RAM_SIZE" ]; then RAM_SIZE="2048"; fi
369+
if [[ "$RAM_SIZE" =~ ^[1-9][0-9]*$ ]]; then
370+
echo -e "${RAMSIZE}${BOLD}${DGN}RAM Size: ${BGN}$RAM_SIZE${CL}"
371+
break
372+
fi
373+
whiptail --backtitle "Proxmox VE Helper Scripts" --title "INVALID INPUT" --msgbox "RAM Size must be a positive integer in MiB (e.g., 2048)." 8 58
365374
else
366-
echo -e "${RAMSIZE}${BOLD}${DGN}RAM Size: ${BGN}$RAM_SIZE${CL}"
375+
exit-script
367376
fi
368-
else
369-
exit-script
370-
fi
377+
done
371378

372379
if BRG=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a Bridge" 8 58 vmbr0 --title "BRIDGE" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
373380
if [ -z "$BRG" ]; then
@@ -380,43 +387,61 @@ function advanced_settings() {
380387
exit-script
381388
fi
382389

383-
if MAC1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a MAC Address" 8 58 "$GEN_MAC" --title "MAC ADDRESS" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
384-
if [ -z "$MAC1" ]; then
385-
MAC="$GEN_MAC"
386-
echo -e "${MACADDRESS}${BOLD}${DGN}MAC Address: ${BGN}$MAC${CL}"
390+
while true; do
391+
if MAC1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a MAC Address" 8 58 "$GEN_MAC" --title "MAC ADDRESS" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
392+
if [ -z "$MAC1" ]; then
393+
MAC="$GEN_MAC"
394+
echo -e "${MACADDRESS}${BOLD}${DGN}MAC Address: ${BGN}$MAC${CL}"
395+
break
396+
fi
397+
if [[ "$MAC1" =~ ^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$ ]]; then
398+
MAC="$MAC1"
399+
echo -e "${MACADDRESS}${BOLD}${DGN}MAC Address: ${BGN}$MAC1${CL}"
400+
break
401+
fi
402+
whiptail --backtitle "Proxmox VE Helper Scripts" --title "INVALID INPUT" --msgbox "Invalid MAC address format. Use XX:XX:XX:XX:XX:XX (e.g., AA:BB:CC:DD:EE:FF)." 8 58
387403
else
388-
MAC="$MAC1"
389-
echo -e "${MACADDRESS}${BOLD}${DGN}MAC Address: ${BGN}$MAC1${CL}"
404+
exit-script
390405
fi
391-
else
392-
exit-script
393-
fi
406+
done
394407

395-
if VLAN1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a Vlan(leave blank for default)" 8 58 --title "VLAN" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
396-
if [ -z "$VLAN1" ]; then
397-
VLAN1="Default"
398-
VLAN=""
399-
echo -e "${VLANTAG}${BOLD}${DGN}VLAN: ${BGN}$VLAN1${CL}"
408+
while true; do
409+
if VLAN1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set a Vlan(leave blank for default)" 8 58 --title "VLAN" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
410+
if [ -z "$VLAN1" ]; then
411+
VLAN1="Default"
412+
VLAN=""
413+
echo -e "${VLANTAG}${BOLD}${DGN}VLAN: ${BGN}$VLAN1${CL}"
414+
break
415+
fi
416+
if [[ "$VLAN1" =~ ^[0-9]+$ ]] && [ "$VLAN1" -ge 1 ] && [ "$VLAN1" -le 4094 ]; then
417+
VLAN=",tag=$VLAN1"
418+
echo -e "${VLANTAG}${BOLD}${DGN}VLAN: ${BGN}$VLAN1${CL}"
419+
break
420+
fi
421+
whiptail --backtitle "Proxmox VE Helper Scripts" --title "INVALID INPUT" --msgbox "VLAN must be a number between 1 and 4094, or leave blank for default." 8 58
400422
else
401-
VLAN=",tag=$VLAN1"
402-
echo -e "${VLANTAG}${BOLD}${DGN}VLAN: ${BGN}$VLAN1${CL}"
423+
exit-script
403424
fi
404-
else
405-
exit-script
406-
fi
425+
done
407426

408-
if MTU1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Interface MTU Size (leave blank for default)" 8 58 --title "MTU SIZE" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
409-
if [ -z "$MTU1" ]; then
410-
MTU1="Default"
411-
MTU=""
412-
echo -e "${DEFAULT}${BOLD}${DGN}Interface MTU Size: ${BGN}$MTU1${CL}"
427+
while true; do
428+
if MTU1=$(whiptail --backtitle "Proxmox VE Helper Scripts" --inputbox "Set Interface MTU Size (leave blank for default)" 8 58 --title "MTU SIZE" --cancel-button Exit-Script 3>&1 1>&2 2>&3); then
429+
if [ -z "$MTU1" ]; then
430+
MTU1="Default"
431+
MTU=""
432+
echo -e "${DEFAULT}${BOLD}${DGN}Interface MTU Size: ${BGN}$MTU1${CL}"
433+
break
434+
fi
435+
if [[ "$MTU1" =~ ^[0-9]+$ ]] && [ "$MTU1" -ge 576 ] && [ "$MTU1" -le 65520 ]; then
436+
MTU=",mtu=$MTU1"
437+
echo -e "${DEFAULT}${BOLD}${DGN}Interface MTU Size: ${BGN}$MTU1${CL}"
438+
break
439+
fi
440+
whiptail --backtitle "Proxmox VE Helper Scripts" --title "INVALID INPUT" --msgbox "MTU Size must be a number between 576 and 65520, or leave blank for default." 8 58
413441
else
414-
MTU=",mtu=$MTU1"
415-
echo -e "${DEFAULT}${BOLD}${DGN}Interface MTU Size: ${BGN}$MTU1${CL}"
442+
exit-script
416443
fi
417-
else
418-
exit-script
419-
fi
444+
done
420445

421446
select_cloud_init
422447

0 commit comments

Comments
 (0)