Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
Merge pull request #890 from asdf-vm/Stratus3D-patch-1
Browse files Browse the repository at this point in the history
Add `source` to list of banned commands

Fixes #887, #948
  • Loading branch information
Stratus3D authored May 24, 2021
2 parents 0a3865d + 0f50841 commit 3237167
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 46 deletions.
4 changes: 2 additions & 2 deletions asdf.fish
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ set -l asdf_user_shims (
)

# Add asdf to PATH
set -l asdf_bin_dirs $ASDF_DIR/bin $asdf_user_shims
set -l asdf_bin_dirs $ASDF_DIR/bin $asdf_user_shims

for x in $asdf_bin_dirs
if test -d $x
Expand All @@ -24,4 +24,4 @@ for x in $asdf_bin_dirs
end

# Load the asdf wrapper function
source $ASDF_DIR/lib/asdf.fish
. $ASDF_DIR/lib/asdf.fish
6 changes: 3 additions & 3 deletions bin/asdf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

# shellcheck source=lib/utils.bash
source "$(dirname "$(dirname "$0")")/lib/utils.bash"
. "$(dirname "$(dirname "$0")")/lib/utils.bash"

find_cmd() {
local cmd_dir="$1"
Expand Down Expand Up @@ -74,12 +74,12 @@ asdf_cmd() {
exec "$ASDF_CMD_FILE" "${@:${args_offset}}"
elif [ -f "$ASDF_CMD_FILE" ]; then
set -- "${@:${args_offset}}"
source "$ASDF_CMD_FILE"
. "$ASDF_CMD_FILE"
else
local asdf_cmd_dir
asdf_cmd_dir="$(asdf_dir)/lib/commands"
printf "%s\\n" "Unknown command: \`asdf ${*}\`" >&2
source "$asdf_cmd_dir/command-help.bash" >&2
. "$asdf_cmd_dir/command-help.bash" >&2
return 127
fi
}
Expand Down
2 changes: 1 addition & 1 deletion bin/private/asdf-exec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ printf "asdf is self upgrading shims to new asdf exec ...\\n"

asdf_dir="$(dirname "$(dirname "$(dirname "$0")")")"
# shellcheck source=lib/utils.bash
source "$asdf_dir/lib/utils.bash"
. "$asdf_dir/lib/utils.bash"
rm "$(asdf_data_dir)"/shims/*
"$asdf_dir"/bin/asdf reshim
shim_name=$(basename "$2")
Expand Down
2 changes: 1 addition & 1 deletion lib/asdf.fish
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function asdf
switch "$command"
case "shell"
# source commands that need to export variables
source (asdf export-shell-version fish $argv | psub)
. (asdf export-shell-version fish $argv | psub)
case '*'
# forward other commands to asdf script
command asdf "$command" $argv
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/command-global.bash
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- sh -*-

# shellcheck source=lib/commands/version_commands.bash
source "$(dirname "$ASDF_CMD_FILE")/version_commands.bash"
. "$(dirname "$ASDF_CMD_FILE")/version_commands.bash"
version_command global "$@"
36 changes: 15 additions & 21 deletions lib/commands/command-list-all.bash
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,42 @@ list_all_command() {
local plugin_name=$1
local query=$2
local plugin_path
local std_out
local std_err
local std_out_file
local std_err_file
local output
plugin_path=$(get_plugin_path "$plugin_name")
check_if_plugin_exists "$plugin_name"

# Capture return code to allow error handling
return_code=0 && split_outputs std_out std_err "bash ${plugin_path}/bin/list-all" || return_code=$?
std_out_file="$(mktemp "/tmp/asdf-command-list-all-${plugin_name}.stdout.XXXXXX")"
std_err_file="$(mktemp "/tmp/asdf-command-list-all-${plugin_name}.stderr.XXXXXX")"
return_code=0 && bash "${plugin_path}/bin/list-all" >"$std_out_file" 2>"$std_err_file" || return_code=$?

if [[ $return_code -ne 0 ]]; then
# Printing all output to allow plugin to handle error formatting
printf "Plugin %s's list-all callback script failed with output:\\n" "${plugin_name}" >&2
printf "%s\\n" "${std_err}" >&2
printf "%s\\n" "${std_out}" >&2
printf "%s\\n" "$(cat "$std_err_file")" >&2
printf "%s\\n" "$(cat "$std_out_file")" >&2
rm "$std_out_file" "$std_err_file"
exit 1
fi

if [[ $query ]]; then
std_out=$(tr ' ' '\n' <<<"$std_out" |
output=$(tr ' ' '\n' <"$std_out_file" |
grep -E "^\\s*$query" |
tr '\n' ' ')
else
output=$(cat "$std_out_file")
fi

IFS=' ' read -r -a versions_list <<<"$std_out"
IFS=' ' read -r -a versions_list <<<"$output"

for version in "${versions_list[@]}"; do
printf "%s\\n" "${version}"
done
}

# This function splits stdout from std error, whilst preserving the return core
function split_outputs() {
{
IFS=$'\n' read -r -d '' "${1}"
IFS=$'\n' read -r -d '' "${2}"
(
IFS=$'\n' read -r -d '' _ERRNO_
return "${_ERRNO_}"
)
} < <((printf '\0%s\0%d\0' "$( ( ( ({
${3}
printf "%s\n" ${?} 1>&3-
} | tr -d '\0' 1>&4-) 4>&2- 2>&1- | tr -d '\0' 1>&4-) 3>&1- | exit "$(cat)") 4>&1-)" "${?}" 1>&2) 2>&1)
# Remove temp files if they still exist
rm "$std_out_file" "$std_err_file" || true
}

list_all_command "$@"
2 changes: 1 addition & 1 deletion lib/commands/command-local.bash
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- sh -*-

# shellcheck source=lib/commands/version_commands.bash
source "$(dirname "$ASDF_CMD_FILE")/version_commands.bash"
. "$(dirname "$ASDF_CMD_FILE")/version_commands.bash"

local_command() {
local parent=false
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/command-plugin-test.bash
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ plugin_test_command() {
export ASDF_DATA_DIR=$TEST_DIR

# shellcheck disable=SC1090
source "$ASDF_DIR/asdf.sh"
. "$ASDF_DIR/asdf.sh"

if ! (asdf plugin-add "$plugin_name" "$plugin_url"); then
fail_test "could not install $plugin_name from $plugin_url"
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/command-reshim.bash
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- sh -*-

# shellcheck source=lib/commands/reshim.bash
source "$(dirname "$ASDF_CMD_FILE")/reshim.bash"
. "$(dirname "$ASDF_CMD_FILE")/reshim.bash"

reshim_command() {
local plugin_name=$1
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/command-uninstall.bash
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- sh -*-

# shellcheck source=lib/commands/reshim.bash
source "$(dirname "$ASDF_CMD_FILE")/reshim.bash"
. "$(dirname "$ASDF_CMD_FILE")/reshim.bash"

uninstall_command() {
local plugin_name=$1
Expand Down
4 changes: 2 additions & 2 deletions lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ with_plugin_env() {
ASDF_INSTALL_TYPE=$install_type \
ASDF_INSTALL_VERSION=$version \
ASDF_INSTALL_PATH=$install_path \
source "${plugin_path}/bin/exec-env"
. "${plugin_path}/bin/exec-env"

PATH=$path "$callback"
}
Expand Down Expand Up @@ -658,7 +658,7 @@ get_shim_versions() {

preset_versions() {
shim_name=$1
shim_plugin_versions "${shim_name}" | cut -d' ' -f 1 | uniq | xargs -IPLUGIN bash -c "source $(asdf_dir)/lib/utils.bash; printf \"%s %s\\n\" PLUGIN \$(get_preset_version_for PLUGIN)"
shim_plugin_versions "${shim_name}" | cut -d' ' -f 1 | uniq | xargs -IPLUGIN bash -c ". $(asdf_dir)/lib/utils.bash; printf \"%s %s\\n\" PLUGIN \$(get_preset_version_for PLUGIN)"
}

select_from_preset_version() {
Expand Down
10 changes: 5 additions & 5 deletions test/asdf_fish.bats
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ cleaned_path() {
set -e ASDF_DATA_DIR
set PATH $(cleaned_path)
source asdf.fish
. asdf.fish
echo \$ASDF_DIR
")

Expand All @@ -32,7 +32,7 @@ cleaned_path() {
set -e ASDF_DATA_DIR
set PATH $(cleaned_path)
source (pwd)/asdf.fish # if the full path is not passed, status -f will return the relative path
. (pwd)/asdf.fish # if the full path is not passed, status -f will return the relative path
echo \$PATH
")

Expand All @@ -48,8 +48,8 @@ cleaned_path() {
set -e ASDF_DATA_DIR
set PATH $(cleaned_path)
source asdf.fish
source asdf.fish
. asdf.fish
. asdf.fish
echo \$PATH
")

Expand All @@ -64,7 +64,7 @@ cleaned_path() {
set -e ASDF_DIR
set PATH $(cleaned_path)
source asdf.fish
. asdf.fish
type asdf
")

Expand Down
18 changes: 16 additions & 2 deletions test/banned_commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ banned_commands=(
echo
# Process substitution isn't POSIX compliant and cause trouble
"<("
# source isn't POSIX compliant. . behaves the same and is POSIX compliant
source
)

setup() {
Expand All @@ -36,8 +38,20 @@ teardown() {
for cmd in "${banned_commands[@]}"; do
# Assert command is not used in the lib and bin dirs
# or expect an explicit comment at end of line, allowing it.
run bash -c "grep -nHR '$cmd' lib bin | grep -v '# asdf_allow: $cmd'"
echo "banned command $cmd: $output"
# Also ignore matches that are contained in comments or a string or
# followed by an underscore (indicating it's a variable and not a
# command).
run bash -c "grep -nHR '$cmd' lib bin\
| grep -v '#.*$cmd'\
| grep -v '\".*$cmd.*\"' \
| grep -v '${cmd}_'\
| grep -v '# asdf_allow: $cmd'"

# Only print output if we've found a banned command
if [ "$status" -ne 1 ]; then
echo "banned command $cmd: $output"
fi

[ "$status" -eq 1 ]
[ "" == "$output" ]
done
Expand Down
8 changes: 4 additions & 4 deletions test/version_commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,14 @@ teardown() {
}

@test "shell wrapper function should export ENV var" {
source $(dirname "$BATS_TEST_DIRNAME")/asdf.sh
. $(dirname "$BATS_TEST_DIRNAME")/asdf.sh
asdf shell "dummy" "1.1.0"
[ $(echo $ASDF_DUMMY_VERSION) = "1.1.0" ]
unset ASDF_DUMMY_VERSION
}

@test "shell wrapper function with --unset should unset ENV var" {
source $(dirname "$BATS_TEST_DIRNAME")/asdf.sh
. $(dirname "$BATS_TEST_DIRNAME")/asdf.sh
asdf shell "dummy" "1.1.0"
[ $(echo $ASDF_DUMMY_VERSION) = "1.1.0" ]
asdf shell "dummy" --unset
Expand All @@ -247,7 +247,7 @@ teardown() {
}

@test "shell wrapper function should return an error for missing plugins" {
source $(dirname "$BATS_TEST_DIRNAME")/asdf.sh
. $(dirname "$BATS_TEST_DIRNAME")/asdf.sh
expected="No such plugin: nonexistent
version 1.0.0 is not installed for nonexistent"

Expand Down Expand Up @@ -306,7 +306,7 @@ false"
}

@test "shell wrapper function should support latest" {
source $(dirname "$BATS_TEST_DIRNAME")/asdf.sh
. $(dirname "$BATS_TEST_DIRNAME")/asdf.sh
asdf shell "dummy" "latest"
[ $(echo $ASDF_DUMMY_VERSION) = "2.0.0" ]
unset ASDF_DUMMY_VERSION
Expand Down

0 comments on commit 3237167

Please sign in to comment.