extended input validation

This commit is contained in:
2025-11-03 12:24:47 +01:00
parent cb0bbb1254
commit 594cc884a0

View File

@@ -84,15 +84,6 @@ usage() {
printf -- " -h | -help | --help Show this help screen\n\n" printf -- " -h | -help | --help Show this help screen\n\n"
} }
#
# Function inputcheck
#
inputcheck () {
[[ -z "$hostname" ]] && { usage; echo "ERROR: DOMAIN CANNOT BE EMPTY!"; exit 1; }
[[ "$hostname" =~ [[:space:]/] ]] && { echo "ERROR: DOMAIN INVALID (no spaces or slashes allowed)"; exit 1; }
[[ -z "$dbpass" ]] && { usage; echo "ERROR: DATABASE PASSWORD CANNOT BE EMPTY!"; exit 1; }
}
# #
# Let's go # Let's go
# #
@@ -116,7 +107,57 @@ while getopts "n:p:m:a:h" option; do
esac esac
done done
inputcheck "$hostname" "$dbpass" #
# Parse and validate input
#
if [[ -z "$hostname" || -z "$dbpass" ]]; then
usage
printf "\nERROR: Both -n (domain) and -p (database password) are required.\n\n"
exit 1
fi
# Convert to lowercase (domains are case-insensitive)
hostname=$(echo "$hostname" | tr '[:upper:]' '[:lower:]')
# Disallow leading hyphen (breaks getopts and invalid by RFC)
if [[ "$hostname" =~ ^- ]]; then
printf "\nERROR: Domain cannot start with a hyphen.\n"
printf "Example of valid input: git1.example.com\n\n"
exit 1
fi
# Disallow spaces, slashes, underscores
if [[ "$hostname" =~ [[:space:]/_] ]]; then
printf "\nERROR: Domain cannot contain spaces, slashes, or underscores.\n\n"
exit 1
fi
# Validate domain format (RFC 1123)
if [[ ! "$hostname" =~ ^([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}$ ]]; then
printf "\nERROR: Invalid domain format.\n"
printf "Example of valid input: git1.example.com\n\n"
exit 1
fi
# Check for spaces in DB credentials
if [[ "$dbpass" =~ [[:space:]] ]]; then
printf "\nERROR: Database password cannot contain spaces.\n\n"
exit 1
fi
# Prevent accidental overwrite of existing Nginx config
if [[ -f "/etc/nginx/conf.d/${hostname}.conf" ]]; then
printf "\nERROR: A configuration file already exists for %s.\n" "$hostname"
printf "Refusing to overwrite existing site.\n\n"
exit 1
fi
# Check for existing references in other Nginx configs
hostcheck=$(grep -r --exclude="README.md" "$hostname" /etc/nginx/ 2>/dev/null || true)
if [[ -n "$hostcheck" ]]; then
printf "\nFound existing configuration mentioning %s — aborting to avoid collision.\n\n" "$hostname"
exit 1
fi
# #
# Check for the latest Gitea version # Check for the latest Gitea version
@@ -125,12 +166,6 @@ version=$(curl -s "https://dl.gitea.com/gitea/version.json" | grep -oP '"version
if [[ -z "$version" ]]; then printf "Could not determine latest version. Falling back to version %s\n\n" "$fallbackversion" ; version="$fallbackversion" ; fi if [[ -z "$version" ]]; then printf "Could not determine latest version. Falling back to version %s\n\n" "$fallbackversion" ; version="$fallbackversion" ; fi
printf "\nUsing Gitea version: %s\n" "$version" printf "\nUsing Gitea version: %s\n" "$version"
#
# Check for existing Nginx host file with same domain
#
hostcheck=$(grep -r --exclude="giteainstall" --exclude="postinstall" --exclude="README.md" "$hostname" /etc/nginx/ 2>/dev/null || true)
if [[ -n "$hostcheck" ]]; then printf "\nFound existing configuration mentioning %s — aborting to avoid collision.\n\n" "$hostname" ; exit 1 ; fi
# #
# Create Gitea counter logic # Create Gitea counter logic
# #