Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,15 +211,19 @@ Additionally, volumes may be specified via the agent environment variable `BUILD

When set to true, it will activate interpolation of variables in the elements within the `push` configuration array. When turned off (the default), attempting to use variables will fail as the literal `$VARIABLE_NAME` string will be used as the target..

:warning: **Important:** this is considered an unsafe option as the most compatible way to achieve this is to run the strings through `eval` which could lead to arbitrary code execution or information leaking if you don't have complete control of the pipeline
:warning: **Important:** this is considered an unsafe option as the most compatible way to achieve this is to run the strings through `eval` which could lead to arbitrary code execution or information leaking if you don't have complete control of the pipeline. On a system with `envsubst` available, that tool will be used in preference to `eval`. See `expand-vars-allowlist` to restrict the variables allowed to be substituted when using `envsubst`.

Note that rules regarding [environment variable interpolation](https://buildkite.com/docs/pipelines/environment-variables#runtime-variable-interpolation) apply here. That means that `$VARIABLE_NAME` is resolved at pipeline upload time, whereas `$$VARIABLE_NAME` will be at run time. All things being equal, you likely want to use `$$VARIABLE_NAME` on the variables mentioned in this option.

#### `expand-vars-allowlist` (push or run, string, when using one of the `expand-*-vars` options, and on a system with `envsubst`)

This variable is used to define the commands `SHELL-FORMAT` argument to `envsubst`, which defines the environment variable allowlist so that unexpected interpolations are not permitted. It is only relevant when using `expand-push-vars` or `expand-volume-vars`

#### `expand-volume-vars` (run only, boolean, unsafe)

When set to true, it will activate interpolation of variables in the elements of the `volumes` configuration array. When turned off (the default), attempting to use variables will fail as the literal `$VARIABLE_NAME` string will be passed to the `-v` option.

:warning: **Important:** this is considered an unsafe option as the most compatible way to achieve this is to run the strings through `eval` which could lead to arbitrary code execution or information leaking if you don't have complete control of the pipeline
:warning: **Important:** this is considered an unsafe option as the most compatible way to achieve this is to run the strings through `eval` which could lead to arbitrary code execution or information leaking if you don't have complete control of the pipeline. On a system with `envsubst` available, that tool will be used in preference to `eval`. See `expand-vars-allowlist` to restrict the variables allowed to be substituted when using `envsubst`.

Note that rules regarding [environment variable interpolation](https://buildkite.com/docs/pipelines/environment-variables#runtime-variable-interpolation) apply here. That means that `$VARIABLE_NAME` is resolved at pipeline upload time, whereas `$$VARIABLE_NAME` will be at run time. All things being equal, you likely want to use `$$VARIABLE_NAME` on the variables mentioned in this option.

Expand Down
2 changes: 1 addition & 1 deletion commands/push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ prebuilt_image_namespace="$(plugin_read_config PREBUILT_IMAGE_NAMESPACE 'docker-
# Then we figure out what to push, and where
for line in $(plugin_read_list PUSH) ; do
if [[ "$(plugin_read_config EXPAND_PUSH_VARS 'false')" == "true" ]]; then
push_target=$(eval echo "$line")
push_target="$(expand_var "$line")"
else
push_target="$line"
fi
Expand Down
2 changes: 1 addition & 1 deletion lib/run.bash
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ expand_relative_volume_path() {
local path

if [[ "$(plugin_read_config EXPAND_VOLUME_VARS 'false')" == "true" ]]; then
path=$(eval echo "$1")
path="$(expand_var "$1")"
else
path="$1"
fi
Expand Down
25 changes: 25 additions & 0 deletions lib/shared.bash
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,31 @@ function retry {
done
}

# Expands the env vars in a string, using envsubst if present, falling back to
# eval when it's missing.
function expand_var() {
# Try to use the safest approach possible
if command -v envsubst > /dev/null; then
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's occurred to me just now that some folk could potentially be leaning on that other pre-existing eval method in ways that had not occurred to me before

eg.

    plugins:
      - docker-compose: 
          expand-volume-vars: true
          build: target
          run: target
          volumes: 
            - "$(command -v oh-no):/bin/oh-no"

Assuming that's possible, I guess we'd want to tie this to an option rather than just finding it on the system. Not sure if it would make sense to have an option for each case, or just a single option that covers all cases.

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is indeed a very good point... a good middle-ground could be changing the existing options to not be booleans but instead one of true/false/eval/envsubst so that users can decide what to do on each case. A modular implementation of that would be to change the expand_var function to accept the value of the corresponding setting and use the corresponding implementation. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds pretty sensible 👍 I'll find some time this week to update again

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @lparry! Thanks for your contributions here! Are you able to update this PR based on the previous recommendations? 🙏 thanks!

expand_var_with_envsubst "$@"
else
expand_var_with_eval "$1"
fi
}

function expand_var_with_envsubst() {
allowlist="$(plugin_read_config 'EXPAND_VARS_ALLOWLIST' '')"
if [[ "$allowlist" == "" ]]; then
envsubst <<<"$1"
else
envsubst "$allowlist" <<<"$1"
fi
}

function expand_var_with_eval() {
echo "$(eval echo "$1")"
}


function is_windows() {
[[ "$OSTYPE" =~ ^(win|msys|cygwin) ]]
}
Expand Down
3 changes: 3 additions & 0 deletions plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ configuration:
type: string
expand-push-vars:
type: boolean
expand-vars-allowlist:
type: string
expand-volume-vars:
type: boolean
graceful-shutdown:
Expand Down Expand Up @@ -203,6 +205,7 @@ configuration:
env-propagation-list: [ run ]
environment: [ run ]
expand-push-vars: [ push ]
expand-vars-allowlist: [ push, run ]
expand-volume-vars: [ volumes ]
graceful-shutdown: [ run ]
leave-volumes: [ run ]
Expand Down
89 changes: 89 additions & 0 deletions tests/shared_lib.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env bats

load "${BATS_PLUGIN_PATH}/load.bash"
load '../lib/shared.bash'

@test "expand_var works" {
export MY_VAR="llamas"
MY_STRING="foo:bar:\$MY_VAR"

run expand_var "$MY_STRING"

assert_success
assert_output "foo:bar:llamas"
}

@test "expand_var works via envsubst" {
export MY_VAR="llamas"
MY_STRING="foo:bar:\$MY_VAR"

ENVSUB_STUB_STDIN="$BATS_TEST_TMPDIR/envsubst_input"
stub envsubst "cat > '$ENVSUB_STUB_STDIN'; echo 'foo:bar:llamas'"

run expand_var_with_envsubst "$MY_STRING"

assert_success
assert_output "foo:bar:llamas"

run cat "$ENVSUB_STUB_STDIN"

assert_success
assert_output "$MY_STRING"

unstub envsubst
}

@test "expand_var works via envsubst with an allowlist" {
export MY_VAR="llamas"
MY_STRING="foo:bar:\$MY_VAR"
ALLOWLIST='$MY_VAR'
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_EXPAND_VARS_ALLOWLIST="$ALLOWLIST"

ENVSUB_STUB_STDIN="$BATS_TEST_TMPDIR/envsubst_input"
stub envsubst "'$ALLOWLIST' : cat > '$ENVSUB_STUB_STDIN'; echo 'foo:bar:llamas'"

run expand_var_with_envsubst "$MY_STRING"

assert_success
assert_output "foo:bar:llamas"

run cat "$ENVSUB_STUB_STDIN"

assert_success
assert_output "$MY_STRING"

unstub envsubst
}

@test "expand_var works via envsubst with an allowlist not including the var" {
export MY_VAR="llamas"
export MY_OTHER_VAR="more_llamas"
MY_STRING="foo:bar:\$MY_VAR:\$MY_OTHER_VAR"
ALLOWLIST='$MY_OTHER_VAR'
export BUILDKITE_PLUGIN_DOCKER_COMPOSE_EXPAND_VARS_ALLOWLIST="$ALLOWLIST"

ENVSUB_STUB_STDIN="$BATS_TEST_TMPDIR/envsubst_input"
stub envsubst "'$ALLOWLIST' : cat > '$ENVSUB_STUB_STDIN'; echo 'foo:bar:\$MY_VAR:more_llamas'"

run expand_var_with_envsubst "$MY_STRING"

assert_success
assert_output "foo:bar:\$MY_VAR:more_llamas"

run cat "$ENVSUB_STUB_STDIN"

assert_success
assert_output "$MY_STRING"

unstub envsubst
}

@test "expand_var works via eval" {
export MY_VAR="llamas"
MY_STRING="foo:bar:\$MY_VAR"

run expand_var_with_eval "$MY_STRING"

assert_success
assert_output "foo:bar:llamas"
}