fix(ng-dev): validate nvmrc-sourced Node.js version before fetch#3738
fix(ng-dev): validate nvmrc-sourced Node.js version before fetch#3738adilburaksen wants to merge 1 commit into
Conversation
#75773923 added `semver.valid()` guards to the PNPM and TypeScript version strings before they are interpolated into registry fetch URLs, but the Node.js version was left unguarded. In `processNodeToolchainArgs`, `effectiveVersion` can originate from the `.nvmrc` file and flows into `getNodeJsRepositories`, which interpolates it into `fetch(`https://nodejs.org/dist/v${version}/SHASUMS256.txt`)` without validation. Apply the same `semver.valid()` check to `effectiveVersion` before it is used, matching the guards already added for PNPM and TypeScript.
There was a problem hiding this comment.
Code Review
This pull request introduces validation for the Node.js version using semver.valid in sync-module-bazel.ts to ensure only valid versions are processed. The reviewer identified an issue where using the original effectiveVersion instead of the normalized version returned by semver.valid could result in a double 'v' prefix (e.g., 'vv24.16.0') in URLs, and provided a code suggestion to fix this.
| if (!semver.valid(effectiveVersion)) { | ||
| throw new Error(`Invalid Node.js version: ${effectiveVersion}`); | ||
| } |
There was a problem hiding this comment.
While semver.valid() validates the version, it also returns the cleaned/normalized version string (e.g., stripping any leading v or whitespace). If effectiveVersion contains a leading v (for example, if manually specified in MODULE.bazel), using it directly in getNodeJsRepositories will result in a URL with a double v prefix (e.g., vv24.16.0), causing the fetch to fail.
To prevent this, we should assign the normalized version returned by semver.valid() back to effectiveVersion.
| if (!semver.valid(effectiveVersion)) { | |
| throw new Error(`Invalid Node.js version: ${effectiveVersion}`); | |
| } | |
| const validatedVersion = semver.valid(effectiveVersion); | |
| if (!validatedVersion) { | |
| throw new Error('Invalid Node.js version: ' + effectiveVersion); | |
| } | |
| effectiveVersion = validatedVersion; |
Summary
#75773923 added
semver.valid()validation to the PNPM and TypeScript version strings before they are interpolated into registry fetch URLs (syncPnpm,syncTypeScript), but the Node.js version path was left unguarded.In
processNodeToolchainArgs(ng-dev/misc/sync-module-bazel/sync-module-bazel.ts),effectiveVersioncan originate from the.nvmrcfile (node_version_from_nvmrc) and is passed togetNodeJsRepositories, which interpolates it into:without the same validation, leaving the URL path open to manipulation via the version string.
Fix
Apply the existing
semver.valid()guard toeffectiveVersionbefore it is used, consistent with the PNPM and TypeScript checks added in #75773923.