Skip to content

Commit cbaaff3

Browse files
authored
Merge pull request #172 from infosiftr/safe-escaping
Fix escaping issues, especially with values that begin with "-"
2 parents e4f5126 + d6294d1 commit cbaaff3

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

apache/docker-entrypoint.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ if [[ "$1" == apache2* ]] || [ "$1" == php-fpm ]; then
5050
# version 4.4.1 decided to switch to windows line endings, that breaks our seds and awks
5151
# https://github.com/docker-library/wordpress/issues/116
5252
# https://github.com/WordPress/WordPress/commit/1acedc542fba2482bab88ec70d4bea4b997a92e4
53-
sed -ri 's/\r\n|\r/\n/g' wp-config*
53+
sed -ri -e 's/\r\n|\r/\n/g' wp-config*
5454

5555
if [ ! -e wp-config.php ]; then
5656
awk '/^\/\*.*stop editing.*\*\/$/ && c == 0 { c = 1; system("cat") } { print }' wp-config-sample.php > wp-config.php <<'EOPHP'
@@ -66,13 +66,13 @@ EOPHP
6666

6767
# see http://stackoverflow.com/a/2705678/433558
6868
sed_escape_lhs() {
69-
echo "$@" | sed 's/[]\/$*.^|[]/\\&/g'
69+
echo "$@" | sed -e 's/[]\/$*.^|[]/\\&/g'
7070
}
7171
sed_escape_rhs() {
72-
echo "$@" | sed 's/[\/&]/\\&/g'
72+
echo "$@" | sed -e 's/[\/&]/\\&/g'
7373
}
7474
php_escape() {
75-
php -r 'var_export(('$2') $argv[1]);' "$1"
75+
php -r 'var_export(('$2') $argv[1]);' -- "$1"
7676
}
7777
set_config() {
7878
key="$1"
@@ -84,7 +84,7 @@ EOPHP
8484
start="^(\s*)$(sed_escape_lhs "$key")\s*="
8585
end=";"
8686
fi
87-
sed -ri "s/($start\s*).*($end)$/\1$(sed_escape_rhs "$(php_escape "$value" "$var_type")")\3/" wp-config.php
87+
sed -ri -e "s/($start\s*).*($end)$/\1$(sed_escape_rhs "$(php_escape "$value" "$var_type")")\3/" wp-config.php
8888
}
8989

9090
set_config 'DB_HOST' "$WORDPRESS_DB_HOST"
@@ -110,7 +110,7 @@ EOPHP
110110
set_config "$unique" "$unique_value"
111111
else
112112
# if not specified, let's generate a random value
113-
current_set="$(sed -rn "s/define\((([\'\"])$unique\2\s*,\s*)(['\"])(.*)\3\);/\4/p" wp-config.php)"
113+
current_set="$(sed -rn -e "s/define\((([\'\"])$unique\2\s*,\s*)(['\"])(.*)\3\);/\4/p" wp-config.php)"
114114
if [ "$current_set" = 'put your unique phrase here' ]; then
115115
set_config "$unique" "$(head -c1M /dev/urandom | sha1sum | cut -d' ' -f1)"
116116
fi

docker-entrypoint.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ if [[ "$1" == apache2* ]] || [ "$1" == php-fpm ]; then
5050
# version 4.4.1 decided to switch to windows line endings, that breaks our seds and awks
5151
# https://github.com/docker-library/wordpress/issues/116
5252
# https://github.com/WordPress/WordPress/commit/1acedc542fba2482bab88ec70d4bea4b997a92e4
53-
sed -ri 's/\r\n|\r/\n/g' wp-config*
53+
sed -ri -e 's/\r\n|\r/\n/g' wp-config*
5454

5555
if [ ! -e wp-config.php ]; then
5656
awk '/^\/\*.*stop editing.*\*\/$/ && c == 0 { c = 1; system("cat") } { print }' wp-config-sample.php > wp-config.php <<'EOPHP'
@@ -66,13 +66,13 @@ EOPHP
6666

6767
# see http://stackoverflow.com/a/2705678/433558
6868
sed_escape_lhs() {
69-
echo "$@" | sed 's/[]\/$*.^|[]/\\&/g'
69+
echo "$@" | sed -e 's/[]\/$*.^|[]/\\&/g'
7070
}
7171
sed_escape_rhs() {
72-
echo "$@" | sed 's/[\/&]/\\&/g'
72+
echo "$@" | sed -e 's/[\/&]/\\&/g'
7373
}
7474
php_escape() {
75-
php -r 'var_export(('$2') $argv[1]);' "$1"
75+
php -r 'var_export(('$2') $argv[1]);' -- "$1"
7676
}
7777
set_config() {
7878
key="$1"
@@ -84,7 +84,7 @@ EOPHP
8484
start="^(\s*)$(sed_escape_lhs "$key")\s*="
8585
end=";"
8686
fi
87-
sed -ri "s/($start\s*).*($end)$/\1$(sed_escape_rhs "$(php_escape "$value" "$var_type")")\3/" wp-config.php
87+
sed -ri -e "s/($start\s*).*($end)$/\1$(sed_escape_rhs "$(php_escape "$value" "$var_type")")\3/" wp-config.php
8888
}
8989

9090
set_config 'DB_HOST' "$WORDPRESS_DB_HOST"
@@ -110,7 +110,7 @@ EOPHP
110110
set_config "$unique" "$unique_value"
111111
else
112112
# if not specified, let's generate a random value
113-
current_set="$(sed -rn "s/define\((([\'\"])$unique\2\s*,\s*)(['\"])(.*)\3\);/\4/p" wp-config.php)"
113+
current_set="$(sed -rn -e "s/define\((([\'\"])$unique\2\s*,\s*)(['\"])(.*)\3\);/\4/p" wp-config.php)"
114114
if [ "$current_set" = 'put your unique phrase here' ]; then
115115
set_config "$unique" "$(head -c1M /dev/urandom | sha1sum | cut -d' ' -f1)"
116116
fi

fpm/docker-entrypoint.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ if [[ "$1" == apache2* ]] || [ "$1" == php-fpm ]; then
5050
# version 4.4.1 decided to switch to windows line endings, that breaks our seds and awks
5151
# https://github.com/docker-library/wordpress/issues/116
5252
# https://github.com/WordPress/WordPress/commit/1acedc542fba2482bab88ec70d4bea4b997a92e4
53-
sed -ri 's/\r\n|\r/\n/g' wp-config*
53+
sed -ri -e 's/\r\n|\r/\n/g' wp-config*
5454

5555
if [ ! -e wp-config.php ]; then
5656
awk '/^\/\*.*stop editing.*\*\/$/ && c == 0 { c = 1; system("cat") } { print }' wp-config-sample.php > wp-config.php <<'EOPHP'
@@ -66,13 +66,13 @@ EOPHP
6666

6767
# see http://stackoverflow.com/a/2705678/433558
6868
sed_escape_lhs() {
69-
echo "$@" | sed 's/[]\/$*.^|[]/\\&/g'
69+
echo "$@" | sed -e 's/[]\/$*.^|[]/\\&/g'
7070
}
7171
sed_escape_rhs() {
72-
echo "$@" | sed 's/[\/&]/\\&/g'
72+
echo "$@" | sed -e 's/[\/&]/\\&/g'
7373
}
7474
php_escape() {
75-
php -r 'var_export(('$2') $argv[1]);' "$1"
75+
php -r 'var_export(('$2') $argv[1]);' -- "$1"
7676
}
7777
set_config() {
7878
key="$1"
@@ -84,7 +84,7 @@ EOPHP
8484
start="^(\s*)$(sed_escape_lhs "$key")\s*="
8585
end=";"
8686
fi
87-
sed -ri "s/($start\s*).*($end)$/\1$(sed_escape_rhs "$(php_escape "$value" "$var_type")")\3/" wp-config.php
87+
sed -ri -e "s/($start\s*).*($end)$/\1$(sed_escape_rhs "$(php_escape "$value" "$var_type")")\3/" wp-config.php
8888
}
8989

9090
set_config 'DB_HOST' "$WORDPRESS_DB_HOST"
@@ -110,7 +110,7 @@ EOPHP
110110
set_config "$unique" "$unique_value"
111111
else
112112
# if not specified, let's generate a random value
113-
current_set="$(sed -rn "s/define\((([\'\"])$unique\2\s*,\s*)(['\"])(.*)\3\);/\4/p" wp-config.php)"
113+
current_set="$(sed -rn -e "s/define\((([\'\"])$unique\2\s*,\s*)(['\"])(.*)\3\);/\4/p" wp-config.php)"
114114
if [ "$current_set" = 'put your unique phrase here' ]; then
115115
set_config "$unique" "$(head -c1M /dev/urandom | sha1sum | cut -d' ' -f1)"
116116
fi

0 commit comments

Comments
 (0)