Skip to content

Commit 0d9b5c2

Browse files
zlotnleoljharb
authored andcommitted
[Fix] fix directory traversal when workdir path is not readable
1 parent 44e1d9c commit 0d9b5c2

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

nvm.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ nvm_tree_contains_path() {
418418
nvm_find_project_dir() {
419419
local path_
420420
path_="${PWD}"
421-
while [ "${path_}" != "" ] && [ ! -f "${path_}/package.json" ] && [ ! -d "${path_}/node_modules" ]; do
421+
while [ "${path_}" != "" ] && [ "${path_}" != '.' ] && [ ! -f "${path_}/package.json" ] && [ ! -d "${path_}/node_modules" ]; do
422422
path_=${path_%/*}
423423
done
424424
nvm_echo "${path_}"
@@ -428,7 +428,7 @@ nvm_find_project_dir() {
428428
nvm_find_up() {
429429
local path_
430430
path_="${PWD}"
431-
while [ "${path_}" != "" ] && [ ! -f "${path_}/${1-}" ]; do
431+
while [ "${path_}" != "" ] && [ "${path_}" != '.' ] && [ ! -f "${path_}/${1-}" ]; do
432432
path_=${path_%/*}
433433
done
434434
nvm_echo "${path_}"

test/fast/Unit tests/nvm_find_project_dir

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ ACTUAL="$(PWD=$TEST_DIR/no-nesting-n_m nvm_find_project_dir)"
3131

3232
ACTUAL="$(PWD=$TEST_DIR/no-nesting-pkg nvm_find_project_dir)"
3333
[ "${ACTUAL}" = "$TEST_DIR/no-nesting-pkg" ] || die "no-nesting-pkg: got ${ACTUAL}"
34+
35+
ACTUAL="$(PWD="." nvm_find_project_dir)"
36+
[ "${ACTUAL}" = "." ] || die "insufficient permissions for pwd: got ${ACTUAL}"

test/fast/Unit tests/nvm_find_up

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ TEST_DIR="$PWD"
2121
[ "~$(PWD=$TEST_DIR/tmp_nvm_find_up/a/b nvm_find_up 'test')" = "~$TEST_DIR/tmp_nvm_find_up" ] || die "failed to find 2 dirs up"
2222
[ "~$(PWD=$TEST_DIR/tmp_nvm_find_up/a/b/c nvm_find_up 'test')" = "~$TEST_DIR/tmp_nvm_find_up/a/b/c" ] || die "failed to find in current dir"
2323
[ "~$(PWD=$TEST_DIR/tmp_nvm_find_up/a/b/c/d nvm_find_up 'test')" = "~$TEST_DIR/tmp_nvm_find_up/a/b/c" ] || die "failed to find 1 level up from current dir"
24+
[ "~$(PWD="." nvm_find_up 'test')" = "~." ] || die "failed to handle '.' output from pwd"
2425

2526
cleanup

0 commit comments

Comments
 (0)