Skip to content

fix(npm): use case-insensitive match for trust-policy-exclude - #35520

Merged
bartlomieju merged 5 commits into
denoland:mainfrom
kapil971390:fix/trust-policy-exclude-case-insensitive
Jun 29, 2026
Merged

bartlomieju merged 5 commits into
denoland:mainfrom
kapil971390:fix/trust-policy-exclude-case-insensitive

Conversation

@kapil971390

Copy link
Copy Markdown
Contributor

Problem

trust-policy-exclude[] in .npmrc uses a BTreeSet<String>::contains check, which is a byte-exact comparison. However, npm package names are case-insensitive by specification.

This means a user writing:

trust-policy-exclude[]=@MyScope/MyPkg

in their .npmrc would not match the registry-normalised name @myscope/mypkg, so the no-downgrade trust policy remains enforced for a package they explicitly intended to exempt.

Affected code: libs/npm/resolution/common.rs — NpmVersionResolver::get_for_package()

// Before (byte-exact — broken for mixed-case names):
if !self.trust_policy.exclude.contains(info.name.as_str()) =>

// After (case-insensitive — matches npm spec):
if !self.trust_policy.exclude.iter().any(|e| e.eq_ignore_ascii_case(info.name.as_str())) =>

Tests

Four regression tests added to libs/npm/resolution/common.rs:

Test Exclude in .npmrc Registry name Expected
EC1 TEST test downgrade allowed (excluded)
EC2 Test test downgrade allowed (excluded)
EC3 @MyScope/MyPkg @myscope/mypkg downgrade allowed (excluded)
EC4 mypkg MyPkg downgrade allowed (excluded)
EC5 (baseline) test test downgrade allowed (already worked)

All 6 test_trust_policy_exclude* tests pass after the fix.

AI Disclosure

I used Claude (Anthropic) to assist with identifying the bug and writing the regression tests. The analysis was: reviewing the trust-policy-exclude implementation in #34927, noticing BTreeSet::contains is byte-exact while npm names are case-insensitive, and verifying the failure with 4 concrete test cases before applying the one-line fix.

npm package names are case-insensitive by specification, but the
`trust-policy-exclude[]` check was using `BTreeSet::contains`, which
is a byte-exact comparison.

This meant that a user writing `trust-policy-exclude[]=@MyScope/MyPkg`
in `.npmrc` would not match the registry-normalised name `@myscope/mypkg`,
leaving the `no-downgrade` trust policy enforced for a package the user
explicitly intended to exempt.

Fix: replace `BTreeSet::contains` with an `Iterator::any` using
`eq_ignore_ascii_case`, which matches npm's case-insensitive package
name semantics.

Added four regression tests covering:
- simple uppercase name in exclude (`TEST` vs registry `test`)
- mixed-case exclude (`Test` vs `test`)
- scoped package with mixed-case scope (`@MyScope/MyPkg` vs `@myscope/mypkg`)
- reversed direction (lowercase exclude vs mixed-case registry name)
@deno-cla-assistant

deno-cla-assistant Bot commented Jun 25, 2026 •

Copy link
Copy Markdown

Deno Individual Contributor License Agreement

All contributors have signed the CLA. Thank you!

Re-run CLA check


This is an automated message from CLA Assistant

@kapil971390

kapil971390 commented Jun 25, 2026 •

Copy link
Copy Markdown
Contributor Author

Reproduction screenshots

Before fix — BTreeSet::contains (byte-exact): 4 case-insensitive tests FAIL

deno-BEFORE-real

After fix — eq_ignore_ascii_case: all 6 tests pass

deno-AFTER-real

@bartlomieju bartlomieju left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix and the thorough write-up — the bug is real and your analysis is spot on: npm package names are case-insensitive, so the byte-exact BTreeSet::contains was wrong. The regression tests covering both directions (mixed-case exclude vs. mixed-case registry name) are a nice touch.

The fix is correct as-is. Two non-blocking design notes to consider for an even cleaner version — see the inline comment. Neither blocks merge; both are about where normalization lives. Waiting on CI before this can land.

Comment thread libs/npm/resolution/common.rs Resolved
Per bartlomieju's review suggestion: instead of a per-call linear scan
with eq_ignore_ascii_case(), lowercase the exclude entries once when the
BTreeSet is built. The lookup then becomes a plain BTreeSet::contains()
with O(log n) rather than O(n).

Changes:
- factory.rs: .iter().cloned() → .iter().map(|s| s.to_ascii_lowercase())
- common.rs: .iter().any(|e| e.eq_ignore_ascii_case(...)) → .contains(&info.name.to_ascii_lowercase())
- test helper: also normalizes to lowercase at construction to match

All 7 trust_policy tests pass.
@kapil971390

kapil971390 commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the design notes! Adopted Suggestion 1 in 86e4197:

  • Lowercased entries at parse boundary in factory.rs (.map(|s| s.to_ascii_lowercase())) so the set is always normalized on construction
  • Replaced the per-call .iter().any(|e| e.eq_ignore_ascii_case(...)) linear scan with .contains(&info.name.to_ascii_lowercase()) — plain O(log n) BTreeSet lookup
  • Updated the test helper to normalize the same way so tests stay consistent

@bartlomieju bartlomieju left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@bartlomieju
bartlomieju enabled auto-merge (squash) June 29, 2026 09:50
@bartlomieju
bartlomieju merged commit c815982 into denoland:main Jun 29, 2026
268 of 270 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants