From 677c69dda012414dab143d23651c4dfc9a63003e Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Tue, 26 Apr 2016 23:07:10 -0700 Subject: [PATCH] `nvm alias`: explicitly forbid user aliases in subdirs. --- nvm.sh | 8 ++++++ ...\" should not accept aliases with slashes" | 27 +++++++++++++++++++ ...\" should not accept aliases with slashes" | 27 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100755 "test/fast/Aliases/\"nvm alias\" should not accept aliases with slashes" create mode 100755 "test/fast/Aliases/\"nvm unalias\" should not accept aliases with slashes" diff --git a/nvm.sh b/nvm.sh index f3689e4..81e4ccb 100755 --- a/nvm.sh +++ b/nvm.sh @@ -2318,6 +2318,10 @@ $NVM_LS_REMOTE_POST_MERGED_OUTPUT" | command grep -v "N/A" | command sed '/^$/d' nvm unalias "${2-}" return $? fi + if [ "${2#*\/}" != "${2-}" ]; then + >&2 echo "Aliases in subdirectories are not supported." + return 1 + fi VERSION="$(nvm_version "${3-}")" if [ $? -ne 0 ]; then echo "! WARNING: Version '${3-}' does not exist." >&2 @@ -2337,6 +2341,10 @@ $NVM_LS_REMOTE_POST_MERGED_OUTPUT" | command grep -v "N/A" | command sed '/^$/d' >&2 nvm help return 127 fi + if [ "${2#*\/}" != "${2-}" ]; then + >&2 echo "Aliases in subdirectories are not supported." + return 1 + fi [ ! -f "$NVM_ALIAS_DIR/$2" ] && echo "Alias $2 doesn't exist!" >&2 && return local NVM_ALIAS_ORIGINAL NVM_ALIAS_ORIGINAL="$(nvm_alias "$2")" diff --git "a/test/fast/Aliases/\"nvm alias\" should not accept aliases with slashes" "b/test/fast/Aliases/\"nvm alias\" should not accept aliases with slashes" new file mode 100755 index 0000000..f5b1d0f --- /dev/null +++ "b/test/fast/Aliases/\"nvm alias\" should not accept aliases with slashes" @@ -0,0 +1,27 @@ +#!/bin/sh + +. ../../../nvm.sh + +die () { echo $@ ; exit 1; } + +OUTPUT="$(nvm alias foo/bar baz 2>&1)" +EXPECTED_OUTPUT="Aliases in subdirectories are not supported." +[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "trying to create an alias with a slash should fail with '$EXPECTED_OUTPUT', got '$OUTPUT'" + +EXIT_CODE="$(nvm alias foo/bar baz >/dev/null 2>&1 ; echo $?)" +[ "$EXIT_CODE" = "1" ] || die "trying to create an alias with a slash should fail with code 1, got '$EXIT_CODE'" + +OUTPUT="$(nvm alias foo/ baz 2>&1)" +EXPECTED_OUTPUT="Aliases in subdirectories are not supported." +[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "trying to create an alias ending with a slash should fail with '$EXPECTED_OUTPUT', got '$OUTPUT'" + +EXIT_CODE="$(nvm alias foo/ baz >/dev/null 2>&1 ; echo $?)" +[ "$EXIT_CODE" = "1" ] || die "trying to create an alias ending with a slash should fail with code 1, got '$EXIT_CODE'" + +OUTPUT="$(nvm alias /bar baz 2>&1)" +EXPECTED_OUTPUT="Aliases in subdirectories are not supported." +[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "trying to create an alias starting with a slash should fail with '$EXPECTED_OUTPUT', got '$OUTPUT'" + +EXIT_CODE="$(nvm alias /bar baz >/dev/null 2>&1 ; echo $?)" +[ "$EXIT_CODE" = "1" ] || die "trying to create an alias starting with a slash should fail with code 1, got '$EXIT_CODE'" + diff --git "a/test/fast/Aliases/\"nvm unalias\" should not accept aliases with slashes" "b/test/fast/Aliases/\"nvm unalias\" should not accept aliases with slashes" new file mode 100755 index 0000000..ff0a304 --- /dev/null +++ "b/test/fast/Aliases/\"nvm unalias\" should not accept aliases with slashes" @@ -0,0 +1,27 @@ +#!/bin/sh + +. ../../../nvm.sh + +die () { echo $@ ; exit 1; } + +OUTPUT="$(nvm unalias foo/bar 2>&1)" +EXPECTED_OUTPUT="Aliases in subdirectories are not supported." +[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "trying to remove an alias with a slash should fail with '$EXPECTED_OUTPUT', got '$OUTPUT'" + +EXIT_CODE="$(nvm unalias foo/bar >/dev/null 2>&1 ; echo $?)" +[ "$EXIT_CODE" = "1" ] || die "trying to remove an alias with a slash should fail with code 1, got '$EXIT_CODE'" + +OUTPUT="$(nvm unalias foo/ 2>&1)" +EXPECTED_OUTPUT="Aliases in subdirectories are not supported." +[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "trying to remove an alias ending with a slash should fail with '$EXPECTED_OUTPUT', got '$OUTPUT'" + +EXIT_CODE="$(nvm unalias foo/ >/dev/null 2>&1 ; echo $?)" +[ "$EXIT_CODE" = "1" ] || die "trying to remove an alias ending with a slash should fail with code 1, got '$EXIT_CODE'" + +OUTPUT="$(nvm unalias /bar 2>&1)" +EXPECTED_OUTPUT="Aliases in subdirectories are not supported." +[ "$OUTPUT" = "$EXPECTED_OUTPUT" ] || die "trying to remove an alias starting with a slash should fail with '$EXPECTED_OUTPUT', got '$OUTPUT'" + +EXIT_CODE="$(nvm unalias /bar >/dev/null 2>&1 ; echo $?)" +[ "$EXIT_CODE" = "1" ] || die "trying to remove an alias starting with a slash should fail with code 1, got '$EXIT_CODE'" +