`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.
55 lines
1.4 KiB
Bash
Executable File
55 lines
1.4 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
WORK="${PWD}/nvm-download-artifact-stdout-work.$$"
|
|
|
|
cleanup() {
|
|
command rm -rf "${WORK}"
|
|
NVM_DIR="${OLD_NVM_DIR}"
|
|
unset -f cleanup die nvm_download nvm_get_checksum nvm_compare_checksum
|
|
}
|
|
die () { echo "$@" ; cleanup ; exit 1; }
|
|
|
|
: nvm.sh
|
|
\. ../../../nvm.sh
|
|
|
|
\. ../../common.sh
|
|
|
|
OLD_NVM_DIR="${NVM_DIR}"
|
|
NVM_DIR="${WORK}/nvm"
|
|
command mkdir -p "${NVM_DIR}"
|
|
|
|
nvm_get_checksum() { echo checksum; }
|
|
nvm_compare_checksum() { return 0; }
|
|
|
|
# a downloader that prints to stdout, as curl does with a `write-out` setting
|
|
nvm_download() {
|
|
while [ $# -gt 0 ]; do
|
|
if [ "$1" = '-o' ]; then
|
|
echo tarball > "$2"
|
|
fi
|
|
shift
|
|
done
|
|
echo 200
|
|
}
|
|
|
|
try nvm_download_artifact node binary std 8.0.0
|
|
[ "${CAPTURED_EXIT_CODE}" = 0 ] || die "nvm_download_artifact failed; got exit code ${CAPTURED_EXIT_CODE}"
|
|
case "${CAPTURED_STDOUT}" in
|
|
"$(nvm_cache_dir)"/bin/*) ;;
|
|
*) die "nvm_download_artifact printed more than the cached tarball path; got '${CAPTURED_STDOUT}'" ;;
|
|
esac
|
|
[ -f "${CAPTURED_STDOUT}" ] || die "nvm_download_artifact printed a path that does not exist: '${CAPTURED_STDOUT}'"
|
|
|
|
command rm -rf "$(nvm_cache_dir)"
|
|
|
|
nvm_download() {
|
|
echo 404
|
|
return 22
|
|
}
|
|
|
|
try nvm_download_artifact node binary std 8.0.0
|
|
[ "${CAPTURED_EXIT_CODE}" = 4 ] || die "a failed download should exit with 4; got ${CAPTURED_EXIT_CODE}"
|
|
[ -z "${CAPTURED_STDOUT}" ] || die "a failed download should print nothing to stdout; got '${CAPTURED_STDOUT}'"
|
|
|
|
cleanup
|