Skip to content

Commit

Permalink
change loading environement variables
Browse files Browse the repository at this point in the history
- prevent executing code when loading variables
- avoid double escaping of envirtonment variables
- avoid extra quotation if it was used in the file
  • Loading branch information
mpapis committed Feb 12, 2017
1 parent a36055c commit 83edbd9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 23 deletions.
55 changes: 43 additions & 12 deletions rvm-test-rvm1/ruby-env_comment_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,32 @@ __rvm_project_ruby_env_check_unload
: load from file no execution
printf "a=\`uname\`\nb=\$(uname)\n" > $d/.env
__rvm_project_ruby_env_load $d/.env
# env[a]=/^`uname`$/
# env[a]=/^\\`uname\\`$/
# env[b]=/uname/

: cd + .ruby-env
: cd + .ruby-env + ';;'
rvm_saved_env=()
cd
RAILS_ENV=""
printf "2.1.1" > $d/.ruby-version
printf "RAILS_ENV=development" > $d/.ruby-env
printf "RAILS_ENV=development';echo bla>&2;'" > $d/.ruby-env
cd "$d"
# match!=/bla/
# match[stderr]!=/bla/
# env[RAILS_ENV]=/^development'/
# env[rvm_saved_env][]=1
# env[rvm_saved_env][0]=/^RAILS_ENV=$/
cd
# env[RAILS_ENV]=/^$/
# env[rvm_saved_env][]=0
rm $d/.ruby-version

: cd + .ruby-env + ''
rvm_saved_env=()
cd
RAILS_ENV=""
printf "2.1.1" > $d/.ruby-version
printf "RAILS_ENV='development'" > $d/.ruby-env
cd "$d"
# env[RAILS_ENV]=/^development$/
# env[rvm_saved_env][]=1
Expand All @@ -83,11 +100,11 @@ cd
# env[rvm_saved_env][]=0
rm $d/.ruby-version

: cd + Gemfile
: cd + Gemfile + ""
cd
RAILS_ENV="production"
printf "\043ruby=2.1.1\n" > $d/Gemfile
printf "\043ruby-env-RAILS_ENV=development\n" >> $d/Gemfile
printf "\043ruby-env-RAILS_ENV=\"development\"\n" >> $d/Gemfile
printf "gem 'rvm'\n" >> $d/Gemfile
cd "$d"
# env[RAILS_ENV]=/^development$/
Expand All @@ -100,15 +117,29 @@ rm $d/Gemfile

: cd + .versions.conf
cd
RAILS_ENV="test"
printf "ruby=2.1.1\n" > $d/.versions.conf
printf "env-RAILS_ENV=development\n" >> $d/.versions.conf
BLA0="test"
printf 'ruby=2.1.1\n' > $d/.versions.conf
printf 'env-BLA0=`development`\n' >> $d/.versions.conf
printf 'env-BLA1=\`development\`\n' >> $d/.versions.conf
printf 'env-BLA2=\\`development\\`\n' >> $d/.versions.conf
printf 'env-BLA3=\\\`development\\\`\n' >> $d/.versions.conf
printf 'env-BLA4=\\\\`development\\\\`\n' >> $d/.versions.conf
printf 'env-BLA5=\\\\\`development\\\\\`\n' >> $d/.versions.conf
printf 'env-BLA6=\\\\\\`development\\\\\\`\n' >> $d/.versions.conf
cd "$d"
# env[RAILS_ENV]=/^development$/
# env[rvm_saved_env][]=1
# env[rvm_saved_env][0]=/^RAILS_ENV=test$/
# match!=/bla/
# match[stderr]!=/bla/
# env[BLA0]=/^\\`development\\`$/
# env[BLA1]=/^\\`development\\`$/
# env[BLA2]=/^\\`development\\`$/
# env[BLA3]=/^\\\\`development\\\\`$/
# env[BLA4]=/^\\\\`development\\\\`$/
# env[BLA5]=/^\\\\\\`development\\\\\\`$/
# env[BLA6]=/^\\\\\\`development\\\\\\`$/
# env[rvm_saved_env][]=7
# env[rvm_saved_env][0]=/^BLA0=test$/
cd
# env[RAILS_ENV]=/^test$/
# env[BLA0]=/^test$/
# env[rvm_saved_env][]=0
rm $d/.versions.conf

Expand Down
24 changes: 13 additions & 11 deletions scripts/functions/rvmrc_project
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,12 @@ __rvm_project_ruby_env_load_parse_file()
\typeset -a __sed_commands
__sed_commands=()
if [[ -n "${2:-}" ]]
then __sed_commands+=( -e "/^$2/ !d" -e "s/^$2//" ) # filter other content and remove prefix
else __sed_commands+=( -e "/^#/ d" ) # remove comments
then __sed_commands+=( -e "/^$2/ !d" -e "s/^$2//" ) # filter other content and remove prefix
else __sed_commands+=( -e "/^#/ d" ) # remove comments
fi
__sed_commands+=( -e 's/`/\\`/g' -e 's/$(/\\$(/g' ) # do not allow command execution `` / $()
__sed_commands+=( -e "/^$/ d" ) # remove empty lines
__sed_commands+=( -e 's/`/\\`/g' -e 's/$(/\\$(/g' ) # do not allow command execution `` / $()
__sed_commands+=( -e 's/\\\(\\\(\\{2}\)*[$`]\)/\1/g' ) # fix escaping if it was already done
__sed_commands+=( -e "/^$/ d" ) # remove empty lines

__rvm_read_lines __variables <( { cat "$1"; echo ""; } | __rvm_sed "${__sed_commands[@]}" )
}
Expand All @@ -279,21 +280,22 @@ __rvm_project_ruby_env_load_set_env()
do
__key="${__set%%=*}"
__value="${__set#*=}"
# fix quoting
rvm_debug "key=$__key; value=$__value;"
case "$__value" in
(""|"\""*"\""|"'"*"'")
true
(\"*\")
__value="${__value#\"}"
__value="${__value%\"}"
;;
(*)
__value="\"${__value//\"/\\\"}\""
(\'*\')
__value="${__value#\'}"
__value="${__value%\'}"
;;
esac
rvm_debug "key=$__key; value=$__value;"
if [[ -n "${__save_to}" ]]
then eval "${__save_to}+=( \"\${__key}=\${${__key}}\" )"
fi
if [[ -n "${__value}" ]]
then eval "export \${__key}=${__value}"
then eval "export \${__key}=\"\${__value}\""
else eval "unset \${__key}"
fi
done
Expand Down

0 comments on commit 83edbd9

Please sign in to comment.