`nvm_install_binary` and `nvm_install_source` took the tarball path from the last line of `nvm_download_artifact`'s output via `| tail -1`. That pipeline's exit status is `tail`'s, so a failed download went unnoticed, and the downloader's own stdout was not separated from the path: anything it printed (eg, from a curl `write-out` setting) could be the last line. If that line happened to name an existing file relative to the current directory, it was extracted and installed as if it were the downloaded tarball, instead of the install failing. `nvm_download_artifact` now sends the downloader's output to stderr, so its stdout is only ever the tarball path. Both install functions capture its output directly and honor its exit status, and accept the result only if it is an existing file inside nvm's cache directory, via a new `nvm_is_cached_artifact` helper Discovered by Anthropic's OSS Scanner, as ANT-2026-DESJ2ESZ; the scanner reported it as a vulnerability, but it is not one.
60 lines
2.1 KiB
Bash
Executable File
60 lines
2.1 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
WORK="${PWD}/nvm-tarball-from-cache-work.$$"
|
|
EXTRACT_LOG="${WORK}/extract.log"
|
|
|
|
cleanup() {
|
|
command rm -rf "${WORK}"
|
|
NVM_DIR="${OLD_NVM_DIR}"
|
|
unset -f cleanup die nvm_download_artifact nvm_extract_tarball run_in_project
|
|
}
|
|
die () { echo "$@" ; cleanup ; exit 1; }
|
|
|
|
: nvm.sh
|
|
\. ../../../nvm.sh
|
|
|
|
OLD_NVM_DIR="${NVM_DIR}"
|
|
NVM_DIR="${WORK}/nvm"
|
|
CACHED_TARBALL="$(nvm_cache_dir)/bin/node-v8.0.0-linux-x64/node-v8.0.0-linux-x64.tar.xz"
|
|
|
|
command mkdir -p "${WORK}/project" "$(dirname "${CACHED_TARBALL}")"
|
|
echo tarball > "${CACHED_TARBALL}"
|
|
# a file in the current directory named like a line a downloader might print
|
|
echo not-a-cached-tarball > "${WORK}/project/404"
|
|
|
|
nvm_extract_tarball() {
|
|
echo "$3" >> "${EXTRACT_LOG}"
|
|
return 1
|
|
}
|
|
|
|
run_in_project() {
|
|
command rm -f "${EXTRACT_LOG}"
|
|
(
|
|
cd "${WORK}/project" || exit 1
|
|
"$@" < /dev/null > /dev/null 2>&1
|
|
)
|
|
[ -f "${EXTRACT_LOG}" ] && command cat "${EXTRACT_LOG}"
|
|
:
|
|
}
|
|
|
|
# a failed download's last stdout line must not be taken as the tarball
|
|
nvm_download_artifact() { echo 404; return 4; }
|
|
|
|
[ -z "$(run_in_project nvm_install_binary node std 8.0.0 1)" ] || die 'nvm_install_binary extracted the output of a failed download'
|
|
[ -z "$(run_in_project nvm_install_source node std 8.0.0)" ] || die 'nvm_install_source extracted the output of a failed download'
|
|
|
|
# nor may any path outside nvm's cache
|
|
nvm_download_artifact() { echo 404; }
|
|
|
|
[ -z "$(run_in_project nvm_install_binary node std 8.0.0 1)" ] || die 'nvm_install_binary extracted a tarball outside the cache'
|
|
[ -z "$(run_in_project nvm_install_source node std 8.0.0)" ] || die 'nvm_install_source extracted a tarball outside the cache'
|
|
|
|
# a tarball in nvm's cache is still extracted
|
|
nvm_download_artifact() { nvm_echo "${CACHED_TARBALL}"; }
|
|
|
|
[ "$(run_in_project nvm_install_binary node std 8.0.0 1)" = "${CACHED_TARBALL}" ] || die 'nvm_install_binary did not extract the cached tarball'
|
|
echo tarball > "${CACHED_TARBALL}"
|
|
[ "$(run_in_project nvm_install_source node std 8.0.0)" = "${CACHED_TARBALL}" ] || die 'nvm_install_source did not extract the cached tarball'
|
|
|
|
cleanup
|