Skip to content

Commit 653d743

Browse files
authored
Merge pull request #195 from EngineScript/copilot/fix-regex-for-single-character-domains
fix: allow single-char domains, add early DB name validation, validate USR/PSWD before SQL
2 parents 32d6802 + e5a1374 commit 653d743

1 file changed

Lines changed: 37 additions & 3 deletions

File tree

scripts/functions/vhost/vhost-install.sh

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,27 @@ echo "Then, select a valid TLD from the provided list."
4343
echo ""
4444

4545
# Prompt for domain name
46-
# Single character domain names are not allowed in the regex because they are technically valid, even though they are rarely used in practice. The regex will still enforce that only lowercase letters, numbers, and hyphens are allowed, and it will ensure that the domain name does not start or end with a hyphen. This allows for a wide range of valid domain names while still enforcing the necessary restrictions for a typical domain name format.
46+
# IMPORTANT: Single-character domain names (e.g., 'x.com', 'a.io') MUST be accepted by this regex.
47+
# They are fully valid under DNS and ICANN rules, and EngineScript must support them.
48+
#
49+
# INTENTIONAL DESIGN — DO NOT CHANGE THIS REGEX:
50+
# ^[a-z0-9]([a-z0-9-]*[a-z0-9])?$
51+
#
52+
# The optional group `([a-z0-9-]*[a-z0-9])?` makes the entire suffix optional, which means a
53+
# single alphanumeric character (e.g., "x") satisfies the pattern on its own. The group is still
54+
# required for multi-character names to prevent leading or trailing hyphens (e.g., "-bad" or
55+
# "bad-" would not match). Changing this back to `^[a-z0-9][a-z0-9-]*[a-z0-9]$` would silently
56+
# reject every single-character label and break installs for legitimate one-letter domains.
57+
#
58+
# Rules enforced by this regex:
59+
# - Minimum length: 1 character (single-char labels are valid DNS labels per RFC 1035)
60+
# - Only lowercase letters (a-z), digits (0-9), and hyphens (-) are permitted
61+
# - The label must not start or end with a hyphen (per RFC 952 / RFC 1123)
62+
#
63+
# This is intentional behaviour. Do not "fix" it to require at least two characters.
4764
while true; do
4865
read -p "Enter the domain name (e.g., 'wordpresstesting'): " DOMAIN_NAME
49-
if [[ "$DOMAIN_NAME" =~ ^[a-z0-9][a-z0-9-]*[a-z0-9]$ ]]; then
66+
if [[ "$DOMAIN_NAME" =~ ^[a-z0-9]([a-z0-9-]*[a-z0-9])?$ ]]; then
5067
echo "You entered: ${DOMAIN_NAME}"
5168
break
5269
else
@@ -174,6 +191,11 @@ if [[ "${INSTALL_WORDPRESS}" == "1" ]]; then
174191
# RAND_CHAR4, RAND_CHAR16, and RAND_CHAR32 are random strings (length 4/16/32)
175192
# sourced from /usr/local/bin/enginescript/enginescript-variables.txt.
176193
database_name="${domain_without_tld}_${RAND_CHAR4}"
194+
# Validate DB identifier before writing credentials file or interpolating into SQL
195+
if [[ -z "${database_name}" || ! "${database_name}" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then
196+
echo "Error: Invalid database name '${database_name}' for domain '${DOMAIN}'." >&2
197+
exit 1
198+
fi
177199
database_user="${RAND_CHAR16}"
178200
database_password="${RAND_CHAR32}"
179201

@@ -188,11 +210,23 @@ if [[ "${INSTALL_WORDPRESS}" == "1" ]]; then
188210

189211
source "${credentials_file}"
190212

191-
# Validate DB identifier before interpolating into SQL
213+
# Validate DB identifier before interpolating into SQL
192214
if [[ -z "${DB}" || ! "${DB}" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then
193215
echo "Error: Invalid database name '${DB}' for domain '${DOMAIN}'." >&2
194216
exit 1
195217
fi
218+
219+
# Validate DB user before interpolating into SQL
220+
if [[ -z "${USR}" || ! "${USR}" =~ ^[A-Za-z0-9_]+$ ]]; then
221+
echo "Error: Invalid MariaDB user '${USR}' for domain '${DOMAIN}'." >&2
222+
exit 1
223+
fi
224+
225+
# Validate DB password before interpolating into SQL single-quoted string
226+
if [[ -z "${PSWD}" || ! "${PSWD}" =~ ^[A-Za-z0-9@%+=:,./_-]+$ ]]; then
227+
echo "Error: Invalid database password for domain '${DOMAIN}'." >&2
228+
exit 1
229+
fi
196230

197231
echo "Randomly generated MySQL database credentials for ${DOMAIN}."
198232

0 commit comments

Comments
 (0)