Skip to content

Commit 593474e

Browse files
authored
Refactor database name validation into a function
Refactored database name validation to use a separate function for improved readability and maintainability.
1 parent a9f0ee7 commit 593474e

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

scripts/functions/vhost/vhost-install.sh

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ MULTIPART_PUBLIC_SUFFIXES=(
3333
"com.br" "com.sg" "com.my" "com.mx"
3434
"co.za" "com.tr" "com.hk"
3535
)
36+
37+
validate_db_identifier() {
38+
local db_identifier="$1"
39+
local domain_context="$2"
40+
if [[ -z "${db_identifier}" || ! "${db_identifier}" =~ ^[a-z][a-z0-9_]*$ ]]; then
41+
echo "Error: Invalid database name '${db_identifier}' for domain '${domain_context}'." >&2
42+
exit 1
43+
fi
44+
}
3645
MULTIPART_SUFFIX_CASE_PATTERN="$(IFS='|'; echo "${MULTIPART_PUBLIC_SUFFIXES[*]}")"
3746

3847
# Check if services are running
@@ -220,10 +229,7 @@ if [[ "${INSTALL_WORDPRESS}" == "1" ]]; then
220229
# Normalize to lowercase for MySQL/MariaDB portability across platforms
221230
database_name="${database_name,,}"
222231
# Validate DB identifier before writing credentials file or interpolating into SQL
223-
if [[ -z "${database_name}" || ! "${database_name}" =~ ^[a-z][a-z0-9_]*$ ]]; then
224-
echo "Error: Invalid database name '${database_name}' for domain '${DOMAIN}'." >&2
225-
exit 1
226-
fi
232+
validate_db_identifier "${database_name}" "${DOMAIN}"
227233
database_user="${RAND_CHAR16}"
228234
database_password="${RAND_CHAR32}"
229235

@@ -260,12 +266,6 @@ if [[ "${INSTALL_WORDPRESS}" == "1" ]]; then
260266
exit 1
261267
fi
262268

263-
# Validate DB user before interpolating into SQL
264-
if [[ -z "${USR}" || ${#USR} -lt 8 || ! "${USR}" =~ ^[A-Za-z0-9_]+$ ]]; then
265-
echo "Error: Invalid MariaDB user '${USR}' for domain '${DOMAIN}' (must be at least 8 characters and contain only letters, numbers, or underscores)." >&2
266-
exit 1
267-
fi
268-
269269
# Validate DB password before interpolating into SQL single-quoted string.
270270
# Allow printable ASCII generally, but reject characters that would break
271271
# single-quoted SQL interpolation without escaping (' and \).

0 commit comments

Comments
 (0)