Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: regex validate plugin names on plugin add cmd #1010

Merged
merged 9 commits into from Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/commands/command-plugin-add.bash
Expand Up @@ -2,12 +2,17 @@

plugin_add_command() {
if [[ $# -lt 1 || $# -gt 2 ]]; then
display_error "usage: asdf plugin-add <name> [<git-url>]"
display_error "usage: asdf plugin add <name> [<git-url>]"
exit 1
fi

local plugin_name=$1

if ! printf "%s" "$plugin_name" | grep --quiet --extended-regexp "^[a-zA-Z0-9_-]+$"; then
display_error "$plugin_name is invalid. Name must match regex ^[a-zA-Z0-9_-]+$"
exit 1
fi

if [ -n "$2" ]; then
local source_url=$2
else
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/command-plugin-test.bash
Expand Up @@ -57,7 +57,7 @@ plugin_test_command() {
# shellcheck disable=SC1090
. "$ASDF_DIR/asdf.sh"

if ! (asdf plugin-add "$plugin_name" "$plugin_url"); then
if ! (asdf plugin add "$plugin_name" "$plugin_url"); then
fail_test "could not install $plugin_name from $plugin_url"
fi

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/dummy_plugin/bin/post-plugin-add
@@ -1,3 +1,3 @@
#!/usr/bin/env bash

echo "plugin-add path=${ASDF_PLUGIN_PATH} source_url=${ASDF_PLUGIN_SOURCE_URL}"
echo "plugin add path=${ASDF_PLUGIN_PATH} source_url=${ASDF_PLUGIN_SOURCE_URL}"
55 changes: 38 additions & 17 deletions test/plugin_add_command.bats
Expand Up @@ -10,8 +10,30 @@ teardown() {
clean_asdf_dir
}

@test "plugin_add command with plugin name matching all valid regex chars succeeds" {
install_mock_plugin_repo "plugin_with-all-valid-CHARS-123"

run asdf plugin add "plugin_with-all-valid-CHARS-123" "${BASE_DIR}/repo-plugin_with-all-valid-CHARS-123"
[ "$status" -eq 0 ]

run asdf plugin-list
[ "$output" = "plugin_with-all-valid-CHARS-123" ]
}

@test "plugin_add command with plugin name not matching valid regex fails" {
run asdf plugin add "invalid\$plugin\$name"
[ "$status" -eq 1 ]
[ "$output" = "invalid\$plugin\$name is invalid. Name must match regex ^[a-zA-Z0-9_-]+$" ]
}

@test "plugin_add command with plugin name not matching valid regex fails again" {
run asdf plugin add "#invalid#plugin#name"
[ "$status" -eq 1 ]
[ "$output" = "#invalid#plugin#name is invalid. Name must match regex ^[a-zA-Z0-9_-]+$" ]
}

@test "plugin_add command with no URL specified adds a plugin using repo" {
run asdf plugin-add "elixir"
run asdf plugin add "elixir"
[ "$status" -eq 0 ]

run asdf plugin-list
Expand All @@ -22,7 +44,7 @@ teardown() {
@test "plugin_add command with URL specified adds a plugin using repo" {
install_mock_plugin_repo "dummy"

run asdf plugin-add "dummy" "${BASE_DIR}/repo-dummy"
run asdf plugin add "dummy" "${BASE_DIR}/repo-dummy"
[ "$status" -eq 0 ]

run asdf plugin-list
Expand All @@ -33,24 +55,23 @@ teardown() {
@test "plugin_add command with URL specified run twice returns error second time" {
install_mock_plugin_repo "dummy"

run asdf plugin-add "dummy" "${BASE_DIR}/repo-dummy"
run asdf plugin-add "dummy" "${BASE_DIR}/repo-dummy"
run asdf plugin add "dummy" "${BASE_DIR}/repo-dummy"
run asdf plugin add "dummy" "${BASE_DIR}/repo-dummy"
[ "$status" -eq 2 ]
[ "$output" = "Plugin named dummy already added" ]
}

@test "plugin_add command with no URL specified fails if the plugin doesn't exist" {
run asdf plugin-add "does-not-exist"
run asdf plugin add "does-not-exist"
[ "$status" -eq 1 ]
echo "$output" | grep "plugin does-not-exist not found in repository"
}

@test "plugin_add command executes post-plugin-add script" {
@test "plugin_add command executes post-plugin add script" {
install_mock_plugin_repo "dummy"

run asdf plugin-add "dummy" "${BASE_DIR}/repo-dummy"

[ "$output" = "plugin-add path=${ASDF_DIR}/plugins/dummy source_url=${BASE_DIR}/repo-dummy" ]
run asdf plugin add "dummy" "${BASE_DIR}/repo-dummy"
[ "$output" = "plugin add path=${ASDF_DIR}/plugins/dummy source_url=${BASE_DIR}/repo-dummy" ]
}

@test "plugin_add command executes configured pre hook (generic)" {
Expand All @@ -60,10 +81,10 @@ teardown() {
pre_asdf_plugin_add = echo ADD ${@}
EOM

run asdf plugin-add "dummy" "${BASE_DIR}/repo-dummy"
run asdf plugin add "dummy" "${BASE_DIR}/repo-dummy"

local expected_output="ADD dummy
plugin-add path=${ASDF_DIR}/plugins/dummy source_url=${BASE_DIR}/repo-dummy"
plugin add path=${ASDF_DIR}/plugins/dummy source_url=${BASE_DIR}/repo-dummy"
[ "$output" = "${expected_output}" ]
}

Expand All @@ -74,10 +95,10 @@ plugin-add path=${ASDF_DIR}/plugins/dummy source_url=${BASE_DIR}/repo-dummy"
pre_asdf_plugin_add_dummy = echo ADD
EOM

run asdf plugin-add "dummy" "${BASE_DIR}/repo-dummy"
run asdf plugin add "dummy" "${BASE_DIR}/repo-dummy"

local expected_output="ADD
plugin-add path=${ASDF_DIR}/plugins/dummy source_url=${BASE_DIR}/repo-dummy"
plugin add path=${ASDF_DIR}/plugins/dummy source_url=${BASE_DIR}/repo-dummy"
[ "$output" = "${expected_output}" ]
}

Expand All @@ -88,9 +109,9 @@ plugin-add path=${ASDF_DIR}/plugins/dummy source_url=${BASE_DIR}/repo-dummy"
post_asdf_plugin_add = echo ADD ${@}
EOM

run asdf plugin-add "dummy" "${BASE_DIR}/repo-dummy"
run asdf plugin add "dummy" "${BASE_DIR}/repo-dummy"

local expected_output="plugin-add path=${ASDF_DIR}/plugins/dummy source_url=${BASE_DIR}/repo-dummy
local expected_output="plugin add path=${ASDF_DIR}/plugins/dummy source_url=${BASE_DIR}/repo-dummy
ADD dummy"
[ "$output" = "${expected_output}" ]
}
Expand All @@ -102,9 +123,9 @@ ADD dummy"
post_asdf_plugin_add_dummy = echo ADD
EOM

run asdf plugin-add "dummy" "${BASE_DIR}/repo-dummy"
run asdf plugin add "dummy" "${BASE_DIR}/repo-dummy"

local expected_output="plugin-add path=${ASDF_DIR}/plugins/dummy source_url=${BASE_DIR}/repo-dummy
local expected_output="plugin add path=${ASDF_DIR}/plugins/dummy source_url=${BASE_DIR}/repo-dummy
ADD"
[ "$output" = "${expected_output}" ]
}
2 changes: 1 addition & 1 deletion test/plugin_update_command.bats
Expand Up @@ -5,7 +5,7 @@ load test_helpers
setup() {
setup_asdf_dir
install_mock_plugin_repo "dummy"
run asdf plugin-add "dummy" "${BASE_DIR}/repo-dummy"
run asdf plugin add "dummy" "${BASE_DIR}/repo-dummy"
}

teardown() {
Expand Down