parse_object_kv_list_non_eval()

This commit is contained in:
myvesta 2025-04-25 20:06:07 +02:00 committed by GitHub
commit 175b89b404
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1155,17 +1155,16 @@ check_if_service_exists() {
fi fi
} }
# Checking the format of a string with key='value' pairs and setting variables, without using Perl # Parsing config variables with key='value' and key="value" pairs and setting them as variables, without using Perl.
# Code taken from HestiaCP and improved # Inspired by HestiaCP function and improved
parse_object_kv_list_non_eval() { parse_object_kv_list_non_eval() {
# Let's combine all the parameters into one string, replace the new lines with a space # Let's combine all the parameters into one string, replace the new lines with a space
local str="${*//$'\n'/ }" local str="${*//$'\n'/ }"
local backup_str=$str local backup_str=$str
# Escape " and $
# str="${str//\"/\\\"}"
# str="${str//\$/\\\$}"
local key val match local key val match i
i=0
# Searching for key='value' blocks
# Loop until we find the next key='value' # Loop until we find the next key='value'
while [[ $str =~ ([A-Za-z][[:alnum:]_]*)=\'([^\']*)\' ]]; do while [[ $str =~ ([A-Za-z][[:alnum:]_]*)=\'([^\']*)\' ]]; do
key="${BASH_REMATCH[1]}" key="${BASH_REMATCH[1]}"
@ -1177,23 +1176,25 @@ parse_object_kv_list_non_eval() {
check_result "$E_INVALID" "Invalid key format [$key]" check_result "$E_INVALID" "Invalid key format [$key]"
fi fi
# Value validation: must not contain an apostrophe
if ! [[ "$val" =~ ^[^\']*$ ]]; then
check_result "$E_INVALID" "Invalid value format [$val]"
fi
# Declaring a global variable # Declaring a global variable
declare -g "$key"="$val" declare -g "$key"="$val"
# Let's remove the processed part from str to continue # Let's remove the processed part from str to continue
str="${str#*$match}" str="${str#*$match}"
((i++))
if [ $i -eq 100 ]; then
check_result "$E_INVALID" "Potentially conf-parsing infinite loop detected"
fi
done done
# Terminate function if we don't expect strings with double apostrophes
if [ -z "$PARSE_DOUBLE_QUOTES_VAR" ]; then if [ -z "$PARSE_DOUBLE_QUOTES_VAR" ]; then
return; return;
fi fi
# Searching for key="value" blocks
str=$backup_str str=$backup_str
i=0
# Loop until we find the next key="value" # Loop until we find the next key="value"
while [[ $str =~ ([A-Za-z][[:alnum:]_]*)=\"([^\"]*)\" ]]; do while [[ $str =~ ([A-Za-z][[:alnum:]_]*)=\"([^\"]*)\" ]]; do
key="${BASH_REMATCH[1]}" key="${BASH_REMATCH[1]}"
@ -1205,15 +1206,14 @@ parse_object_kv_list_non_eval() {
check_result "$E_INVALID" "Invalid key format [$key]" check_result "$E_INVALID" "Invalid key format [$key]"
fi fi
# Value validation: must not contain an apostrophe
if ! [[ "$val" =~ ^[^\']*$ ]]; then
check_result "$E_INVALID" "Invalid value format [$val]"
fi
# Declaring a global variable # Declaring a global variable
declare -g "$key"="$val" declare -g "$key"="$val"
# Let's remove the processed part from str to continue # Let's remove the processed part from str to continue
str="${str#*$match}" str="${str#*$match}"
((i++))
if [ $i -eq 100 ]; then
check_result "$E_INVALID" "Potentially conf-parsing infinite loop detected"
fi
done done
} }