Repository navigation
Conversation
|
💖 Thanks for opening this pull request! 💖 |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThis PR updates ChangesExact Version Preference for Transitive Updates
Estimated code review effort: 2 (Simple) | ~15 minutes Sequence Diagram(s)sequenceDiagram
participant CLI as update.handler
participant installDeps as installDeps
participant Parser as parseUpdateParam
participant Resolver as install resolver
CLI->>installDeps: run update with params
installDeps->>installDeps: save updateSpecs before reassignment
installDeps->>Parser: parseUpdateParam(updateSpecs)
Parser-->>installDeps: pattern, versionSpec
installDeps->>installDeps: build preferredVersions
installDeps->>Resolver: merge preferredVersions into installOpts
Resolver-->>installDeps: resolve requested exact version
Assessment against linked issues
Suggested labels: Suggested reviewers: ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
PR Summary by QodoFix pnpm update to respect exact versions for transitive-only dependencies
AI Description
Diagram
High-Level Assessment
Files changed (3)
|
Code Review by Qodo
1. Update spec prototype pollution
|
| function getPreferredVersionsForUpdateSpecs (updateSpecs: string[]): PreferredVersions { | ||
| const preferredVersions: PreferredVersions = {} | ||
| for (const spec of updateSpecs) { | ||
| const { pattern, versionSpec } = parseUpdateParam(spec) | ||
| if (versionSpec == null || pattern.includes('*') || pattern.startsWith('!')) continue | ||
| // Only an exact version of a single named package can be preferred. | ||
| const selector = getVersionSelectorType(versionSpec) | ||
| if (selector?.type !== 'version') continue | ||
| preferredVersions[pattern] = preferredVersions[pattern] ?? {} | ||
| preferredVersions[pattern][selector.normalized] = 'version' | ||
| } |
There was a problem hiding this comment.
1. Update spec prototype pollution 🐞 Bug ⛨ Security
getPreferredVersionsForUpdateSpecs writes the user-controlled update selector pattern into a plain object without filtering __proto__/constructor/prototype, so pnpm update [email protected] can end up mutating Object.prototype. This can corrupt runtime state or crash pnpm during/after dependency resolution.
Agent Prompt
### Issue description
`getPreferredVersionsForUpdateSpecs()` builds `preferredVersions` as a plain object and indexes it with `pattern` coming from CLI args. Special keys like `__proto__`, `constructor`, or `prototype` can trigger prototype pollution (including writing onto `Object.prototype`) via the nested assignment.
### Issue Context
`parseUpdateParam()` does not validate/sanitize `pattern`, so this path must defensively guard keys or avoid prototype-bearing objects.
### Fix Focus Areas
- pnpm11/installing/commands/src/installDeps.ts[601-613]
### Suggested fix
1) Build maps with null prototypes:
- `const preferredVersions = Object.create(null) as PreferredVersions`
- for nested maps: `preferredVersions[pattern] ??= Object.create(null) as any`
2) Add explicit key guards before writing:
- Skip/throw if `pattern` is `__proto__`, `constructor`, or `prototype`.
3) (Recommended) Add a regression test that calls update with a selector like `[email protected]` and asserts `({} as any).someInjectedProp` is still `undefined` (i.e., Object.prototype wasn’t polluted).
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
There was a problem hiding this comment.
Good catch. Switched getPreferredVersionsForUpdateSpecs to a null-prototype map (Object.create(null)), so a crafted selector like [email protected] becomes a plain key instead of mutating Object.prototype. Added a regression test that runs update with a __proto__@… selector and asserts the prototype is untouched. Done in 662aa70.
|
Code review by qodo was updated up to the latest commit f4c0211 |
f4c0211 to
662aa70
Compare
|
Code review by qodo was updated up to the latest commit 662aa70 |
1 similar comment
|
Code review by qodo was updated up to the latest commit 662aa70 |
pnpm update <dep>@<version> ignored <version> when <dep> exists only as a transitive dependency: updateMatching only flags a package for re-resolution and cannot carry a version, so the resolver fell back to highest-in-range. Adopts the fix from pnpm#12752, reconciled with the updateRequested preferred-version bypass in this PR: - The requested version joins preferredVersions as a range selector (an exact version is a valid range matching only itself) at a new maximum REQUESTED_VERSION_SELECTOR_WEIGHT, instead of a version-type pin. version-type selectors are exactly what the updateRequested bypass strips for update targets, so a plain pin would be discarded and the fix would never fire; range selectors survive by design. - The install layer now merges caller-supplied preferredVersions over the lockfile-derived seed per package name instead of replacing it. Replacement would have disabled the unrelated-packages-stay-pinned fix (pnpm#10662) whenever a caller passes preferred versions — including pnpm audit --fix's vulnerability penalties. The merge target is null-prototype so a crafted package name such as __proto__ lands as a plain own key. pacquet (same fix, same encoding): - UpdateSeedPolicy::DropOnly gains a requested_versions map; the versioned-selector/no-direct-match branch of update fills it from selectors whose version parses as an exact semver (glob and negated patterns excluded). - install_with_fresh_lockfile folds each requested version into the preferred-versions seed as a range selector at REQUESTED_VERSION_SELECTOR_WEIGHT. Tests: the TS e2e regression from pnpm#12752 (explicit in-range version wins over latest; prototype-pollution guard) plus a pacquet integration test (update_transitive_to_explicitly_requested_version), verified to fail with the seed fold disabled. Closes pnpm#12744. Co-authored-by: Minha Kang <[email protected]>
|
Thanks for this fix and the clear root-cause analysis! We've folded it into #12558 (with your commits credited via
Your regression tests (including the prototype-pollution guard) are carried over unchanged, and the same fix is also implemented in the Rust Written by an agent (Claude Code, claude-fable-5) on behalf of zkochan. |
fixes #12744
pnpm update <dep>@<version>ignores<version>when<dep>exists only as a transitive dependency: it re-resolves the dependency to the highest version in range instead of the requested one.Reproduction
A project depends on
@swc/cli, which depends onpiscina(^4.3.1) transitively. Withpiscinaresolved to an older in-range version (e.g.4.5.0):The requested version is dropped whether it is higher or lower than the currently resolved one.
Root cause
When an update selector matches no direct dependency,
installDepstreats it as a transitive-only update and builds anupdateMatchingpredicate:updateMatchingonly flags a package for re-resolution — it returns a boolean and cannot carry the version parsed from<dep>@<version>. The version is therefore discarded and the resolver falls back to the highest version in range.Fix
For a transitive-only update whose selector pins an exact version, pass that version through
preferredVersionsso the resolver prefers it during re-resolution — the same mechanismgetExactSinglePreferredVersionsand the audit-fix path already use. Version ranges, dist-tags, glob and negation selectors are left to normal resolution.Scope
This covers the non-recursive
pnpm update <dep>@<version>from the reproduction. Recursive (-r) updates of a transitive dependency by name, and range/tag selectors, are unchanged.Test
Added a regression test that pins a transitive dependency, then updates it to an explicit in-range version and asserts the requested version is installed rather than the latest.
Summary by CodeRabbit