Improve sanitize

This commit is contained in:
Alexandre
2025-07-08 11:03:02 +02:00
committed by GitHub
parent 26e4c4835f
commit f183d6bc75

View File

@@ -29,24 +29,30 @@ mapfile -t arr < <(jq -r 'keys[]' "${JSONSOURCE}")
# Escape special characters using printf and enclose in double quotes # Escape special characters using printf and enclose in double quotes
sanitize_variable() { sanitize_variable() {
local input="$1" local raw="$1" # original value
local escaped_input local escaped # value after printf %q
printf -v escaped_input '%q' "$input" printf -v escaped '%q' "$raw"
escaped_input=\"${escaped_input//\'/\'\'}\"
echo "$escaped_input" # If nothing changed, return the original.
[[ "$raw" == "$escaped" ]] && {
printf '%s' "$raw"
return
}
# Otherwise protect the escaped string with double quotes.
printf '"%s"' "$escaped"
} }
for KEYS in "${arr[@]}"; do for KEYS in "${arr[@]}"; do
# export key # export key
VALUE=$(jq ."$KEYS" "${JSONSOURCE}") VALUE=$(jq -r --raw-output ".\"$KEYS\"" "$JSONSOURCE")
# Check if the value is an array # Check if the value is an array
if [[ "$VALUE" == \[* ]]; then if [[ "$VALUE" == \[* ]]; then
bashio::log.warning "One of your option is an array, skipping" bashio::log.warning "One of your option is an array, skipping"
else else
# Sanitize variable # Sanitize variable
if [[ "$VALUE" != \'*\' ]] && [[ "$VALUE" =~ [^a-zA-Z0-9] ]]; then VALUE=$(sanitize_variable "$VALUE")
VALUE=$(sanitize_variable "$VALUE")
fi
# Continue for single values # Continue for single values
line="${KEYS}=${VALUE}" line="${KEYS}=${VALUE}"
# Check if secret # Check if secret