|
42 | 42 |
|
43 | 43 | echo "DEBUG: Starting... [Ok]\n" |
44 | 44 |
|
| 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 | + |
45 | 55 | for i in "${tagname[@]}" |
46 | 56 | do |
47 | 57 | 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"<"` |
52 | 59 | echo "DEBUG: Found the current value for the element <$i> - '$tagvalue'" |
53 | 60 |
|
54 | | - # Setting new substituted value |
55 | 61 | case $i in |
56 | 62 | 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";; |
59 | 64 | 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";; |
62 | 66 | 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";; |
65 | 68 | 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%/}@"`;; |
68 | 70 | 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;; |
72 | 73 | esac |
73 | 74 |
|
74 | 75 | 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" |
78 | 81 | cp "$temp_file" "$auth_conf_source" |
79 | 82 | done |
80 | 83 | # Writing our changes back to the original file ($auth_conf_source) |
|
0 commit comments