Skip to content

Commit da9f01e

Browse files
committed
Fix replacemente of empty contant and chars escape in Geoserver data configs
1 parent c1050fc commit da9f01e

1 file changed

Lines changed: 22 additions & 19 deletions

File tree

docker/geoserver/set_geoserver_auth.sh

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,39 +42,42 @@ done
4242

4343
echo "DEBUG: Starting... [Ok]\n"
4444

45+
# Escape a string for safe use in sed's REGEX (pattern) side, given @ delimiter
46+
escape_regex() {
47+
printf '%s' "$1" | sed -e 's/[][\\.^$*+?(){}|@/-]/\\&/g'
48+
}
49+
50+
# Escape a string for safe use in sed's REPLACEMENT side, given @ delimiter
51+
escape_replacement() {
52+
printf '%s' "$1" | sed -e 's/[\\&@/]/\\&/g'
53+
}
54+
4555
for i in "${tagname[@]}"
4656
do
4757
echo "DEBUG: Working on '$auth_conf_source' for tagname <$i>"
48-
# Extracting the value from the <$tagname> element
49-
# echo -ne "<$i>$tagvalue</$i>" | xmlstarlet sel -t -m "//a" -v . -n
50-
tagvalue=`grep "<$i>.*<.$i>" "$auth_conf_source" | sed -e "s/^.*<$i/<$i/" | cut -f2 -d">"| cut -f1 -d"<"`
51-
58+
tagvalue=`grep "<$i>.*<.$i>" "$auth_conf_source" | sed -e "s/^.*<$i/<$i/" | cut -f2 -d">" | cut -f1 -d"<"`
5259
echo "DEBUG: Found the current value for the element <$i> - '$tagvalue'"
5360

54-
# Setting new substituted value
5561
case $i in
5662
authApiKey)
57-
echo "DEBUG: Editing '$auth_conf_source' for tagname <$i> and replacing its value with '$OAUTH2_API_KEY'"
58-
newvalue=`echo -ne "$tagvalue" | sed -re "s@.*@$OAUTH2_API_KEY@"`;;
63+
newvalue="$OAUTH2_API_KEY";;
5964
cliendId)
60-
echo "DEBUG: Editing '$auth_conf_source' for tagname <$i> and replacing its value with '$OAUTH2_CLIENT_ID'"
61-
newvalue=`echo -ne "$tagvalue" | sed -re "s@.*@$OAUTH2_CLIENT_ID@"`;;
65+
newvalue="$OAUTH2_CLIENT_ID";;
6266
clientSecret)
63-
echo "DEBUG: Editing '$auth_conf_source' for tagname <$i> and replacing its value with '$OAUTH2_CLIENT_SECRET'"
64-
newvalue=`echo -ne "$tagvalue" | sed -re "s@.*@$OAUTH2_CLIENT_SECRET@"`;;
67+
newvalue="$OAUTH2_CLIENT_SECRET";;
6568
proxyBaseUrl | redirectUri | userAuthorizationUri | logoutUri )
66-
echo "DEBUG: Editing '$auth_conf_source' for tagname <$i> and replacing its value with '$GEOSERVER_LOCATION'"
67-
newvalue=`echo -ne "$tagvalue" | sed -re "s@^(https?://[^/]+)@${GEOSERVER_LOCATION%/}@"`;;
69+
newvalue=`printf '%s' "$tagvalue" | sed -re "s@^(https?://[^/]+)@${GEOSERVER_LOCATION%/}@"`;;
6870
baseUrl | accessTokenUri | checkTokenEndpointUrl )
69-
echo "DEBUG: Editing '$auth_conf_source' for tagname <$i> and replacing its value with '$GEONODE_LOCATION'"
70-
newvalue=`echo -ne "$tagvalue" | sed -re "s@^(https?://[^/]+)@${GEONODE_LOCATION%/}@"`;;
71-
*) echo -n "an unknown variable has been found";;
71+
newvalue=`printf '%s' "$tagvalue" | sed -re "s@^(https?://[^/]+)@${GEONODE_LOCATION%/}@"`;;
72+
*) echo "an unknown variable has been found"; continue;;
7273
esac
7374

7475
echo "DEBUG: Found the new value for the element <$i> - '$newvalue'"
75-
# Replacing element’s value with $SUBSTITUTION_URL
76-
# echo -ne "<$i>$tagvalue</$i>" | xmlstarlet sel -t -m "//a" -v . -n
77-
sed -e "s@<$i>$tagvalue<\/$i>@<$i>$newvalue<\/$i>@g" "$auth_conf_source" > "$temp_file"
76+
77+
# Match the whole element (empty or not); only the tag name is interpolated,
78+
# so the user-controlled content goes through the escapers.
79+
new_esc=`escape_replacement "$newvalue"`
80+
sed -E "s@(<$i>)[^<]*(</$i>)@\1${new_esc}\2@g" "$auth_conf_source" > "$temp_file"
7881
cp "$temp_file" "$auth_conf_source"
7982
done
8083
# Writing our changes back to the original file ($auth_conf_source)

0 commit comments

Comments
 (0)