[Fix] nvm_download_artifact: give each download a path no other process knows
Every download went to the fixed `<archive>.unverified`, and curl opens its `-o` file only on the first body byte, so a curl left running by an earlier install of the same version (past the install lock via `nvm cache clear`, a stale-lock takeover, or a removed lock) survived the `rm -f`, then opened the same file and could rewrite it after the checksum passed, while `tar` read it, and leave it in the cache that `--offline` trusts. Downloading into a `mktemp -d` directory in the same cache dir keeps the rename atomic and the file's mode following the umask. Leftover `.unverified*` paths from interrupted installs are still removed first. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-XUXR5LQ4.
This commit is contained in:
2 files changed
+52
-9
No files matched your search
@@ -4026,35 +4026,44 @@ nvm_download_artifact() {
|
||||
nvm_err "Removing the unverified local cache..."
|
||||
command rm -rf "${TARBALL}"
|
||||
fi
|
||||
# `--offline` uses the cached archive without a checksum, so only a verified archive may ever sit at ${TARBALL}
|
||||
# `--offline` uses the cached archive without a checksum, so only a verified archive may ever sit at ${TARBALL}.
|
||||
# Each download gets a path no other process knows: a downloader left running by another install
|
||||
# can open its output late (curl does on the first body byte), and would then rewrite the verified file.
|
||||
nvm_is_zsh && setopt local_options nonomatch
|
||||
command rm -rf "${TARBALL}.unverified"*
|
||||
local UNVERIFIED_DIR
|
||||
UNVERIFIED_DIR="$(command mktemp -d "${TARBALL}.unverified.XXXXXX")" || {
|
||||
nvm_err "creating a temporary directory in $(nvm_sanitize_path "${tmpdir}") failed"
|
||||
return 3
|
||||
}
|
||||
local UNVERIFIED_TARBALL
|
||||
UNVERIFIED_TARBALL="${TARBALL}.unverified"
|
||||
command rm -f "${UNVERIFIED_TARBALL}"
|
||||
UNVERIFIED_TARBALL="${UNVERIFIED_DIR}/${SLUG}.${COMPRESSION}"
|
||||
|
||||
nvm_err "Downloading $(nvm_sanitize_url "${TARBALL_URL}")..."
|
||||
# stdout is reserved for the tarball path, so nothing the downloader prints may reach it
|
||||
nvm_download -L -C - "${PROGRESS_BAR}" "${TARBALL_URL}" -o "${UNVERIFIED_TARBALL}" >&2 || {
|
||||
command rm -rf "${UNVERIFIED_TARBALL}" "${tmpdir}"
|
||||
command rm -rf "${UNVERIFIED_DIR}" "${tmpdir}"
|
||||
nvm_err "download from $(nvm_sanitize_url "${TARBALL_URL}") failed"
|
||||
return 4
|
||||
}
|
||||
|
||||
if nvm_grep '404 Not Found' "${UNVERIFIED_TARBALL}" >/dev/null; then
|
||||
command rm -rf "${UNVERIFIED_TARBALL}" "${tmpdir}"
|
||||
command rm -rf "${UNVERIFIED_DIR}" "${tmpdir}"
|
||||
nvm_err "HTTP 404 at URL $(nvm_sanitize_url "${TARBALL_URL}")"
|
||||
return 5
|
||||
fi
|
||||
|
||||
nvm_compare_checksum "${UNVERIFIED_TARBALL}" "${CHECKSUM}" || {
|
||||
command rm -rf "${UNVERIFIED_TARBALL}" "${tmpdir}"
|
||||
command rm -rf "${UNVERIFIED_DIR}" "${tmpdir}"
|
||||
return 6
|
||||
}
|
||||
|
||||
command mv -f "${UNVERIFIED_TARBALL}" "${TARBALL}" || {
|
||||
command rm -rf "${UNVERIFIED_TARBALL}" "${tmpdir}"
|
||||
command rm -rf "${UNVERIFIED_DIR}" "${tmpdir}"
|
||||
nvm_err "moving the downloaded archive into the cache at $(nvm_sanitize_path "${TARBALL}") failed"
|
||||
return 7
|
||||
}
|
||||
command rm -rf "${UNVERIFIED_DIR}"
|
||||
|
||||
nvm_echo "${TARBALL}"
|
||||
}
|
||||
|
||||
@@ -85,11 +85,13 @@ try download_offline
|
||||
nvm_compare_checksum() { return 0; }
|
||||
command mkdir -p "${TEST_CACHE_DIR}"
|
||||
nvm_echo 'partial' > "${TEST_TARBALL}.unverified"
|
||||
command mkdir -p "${TEST_TARBALL}.unverified.abc123"
|
||||
nvm_echo 'partial' > "${TEST_TARBALL}.unverified.abc123/${TEST_SLUG}.tar.gz"
|
||||
try nvm_download_artifact node binary std v20.0.0
|
||||
[ "${CAPTURED_EXIT_CODE}" = '0' ] || die "verified download: expected exit code 0, got ${CAPTURED_EXIT_CODE}"
|
||||
[ "${CAPTURED_STDOUT}" = "${TEST_TARBALL}" ] || die "verified download: expected >${TEST_TARBALL}<, got >${CAPTURED_STDOUT}<"
|
||||
[ "$(command cat "${TEST_TARBALL}")" = 'downloaded' ] || die "verified download: expected the cached archive to hold only the new download, got >$(command cat "${TEST_TARBALL}")<"
|
||||
[ ! -e "${TEST_TARBALL}.unverified" ] || die 'verified download: expected no unverified download to remain in the cache'
|
||||
[ "$(command ls "${TEST_CACHE_DIR}")" = "$(nvm_echo 'files'; nvm_echo "${TEST_SLUG}.tar.gz")" ] || die "verified download: expected no unverified download to remain in the cache, got >$(command ls "${TEST_CACHE_DIR}")<"
|
||||
[ ! -e "${WORK}/downloaded-to-final-name" ] || die 'an unverified download was written to the final cache name, where an interrupted download would be left for --offline'
|
||||
|
||||
# without any checksum tool, nothing is downloaded and the verified cache is left alone
|
||||
@@ -97,6 +99,38 @@ nvm_get_checksum_binary() { return 1; }
|
||||
try nvm_download_artifact node binary std v20.0.0
|
||||
[ "${CAPTURED_EXIT_CODE}" = '6' ] || die "no checksum tool: expected exit code 6, got ${CAPTURED_EXIT_CODE}"
|
||||
[ "$(command cat "${TEST_TARBALL}")" = 'downloaded' ] || die "no checksum tool: expected the cached archive to be left alone, got >$(command cat "${TEST_TARBALL}")<"
|
||||
[ ! -e "${TEST_TARBALL}.unverified" ] || die 'no checksum tool: expected nothing to be downloaded'
|
||||
[ "$(command ls "${TEST_CACHE_DIR}")" = "$(nvm_echo 'files'; nvm_echo "${TEST_SLUG}.tar.gz")" ] || die "no checksum tool: expected nothing to be downloaded, got >$(command ls "${TEST_CACHE_DIR}")<"
|
||||
nvm_get_checksum_binary() { nvm_echo 'sha256sum'; }
|
||||
|
||||
# a downloader left running by an earlier install, which opens its output only once a later install is downloading
|
||||
# (as curl does when the response body is late), must not be able to write to the archive that the later install verified
|
||||
command rm -rf "${WORK}/.cache"
|
||||
nvm_compare_checksum() { [ "$(command cat "$1")" = 'published' ]; }
|
||||
nvm_download() {
|
||||
while [ "$#" -gt 1 ] && [ "$1" != '-o' ]; do
|
||||
shift
|
||||
done
|
||||
if [ ! -e "${WORK}/first-download" ]; then
|
||||
nvm_echo "$2" > "${WORK}/first-download"
|
||||
nvm_download_artifact node binary std v20.0.0 > "${WORK}/second-install" 2>/dev/null
|
||||
nvm_echo "$?" >> "${WORK}/second-install"
|
||||
if [ -e "${WORK}/late-open" ]; then
|
||||
nvm_echo 'tampered' >&9
|
||||
exec 9>&-
|
||||
fi
|
||||
command cat "${TEST_TARBALL}" > "${WORK}/cached-after-late-write"
|
||||
else
|
||||
# a failed `exec` redirection would end the shell; where the path is gone, curl would fail to open it
|
||||
if [ -d "$(command dirname "$(command cat "${WORK}/first-download")")" ]; then
|
||||
exec 9> "$(command cat "${WORK}/first-download")"
|
||||
command touch "${WORK}/late-open"
|
||||
fi
|
||||
nvm_echo 'published' > "$2"
|
||||
fi
|
||||
}
|
||||
try nvm_download_artifact node binary std v20.0.0
|
||||
[ "$(command cat "${WORK}/second-install")" = "$(nvm_echo "${TEST_TARBALL}"; nvm_echo 0)" ] || die "late write: expected the second install to cache its download, got >$(command cat "${WORK}/second-install")<"
|
||||
[ "$(command cat "${WORK}/cached-after-late-write")" = 'published' ] || die "late write: expected the verified archive in the cache to stay as it was, got >$(command cat "${WORK}/cached-after-late-write")<"
|
||||
[ "${CAPTURED_EXIT_CODE}" = '6' ] || die "late write: expected the first install to reject its own download (exit code 6), got ${CAPTURED_EXIT_CODE}"
|
||||
|
||||
cleanup
|
||||
Reference in new issue
Block a user