Skip to content

fix(nuget): look packages up on nuget.org only when NuGet could not take them elsewhere - #171

Merged
sweepies merged 8 commits into
mainfrom
feat/nuget-origins
Nov 4, 2026
Merged

sweepies merged 8 commits into
mainfrom
feat/nuget-origins

Conversation

@sweepies

@sweepies sweepies commented Nov 4, 2026 •

Copy link
Copy Markdown
Owner

Third per-ecosystem fix from the registry audit, after Bundler (#168) and Composer (#170). NuGet looked every package up on nuget.org, so a package from a private feed could be looked up there, where its name may be someone else's.

Why not the lockfile

packages.lock.json records versions and hashes but not which source a package came from. With several sources and no mapping, NuGet takes a package from whichever source answers, so no committed file says where a package comes from. NuGet's own answer to that is packageSourceMapping.

Why the Count over-counts sources

The first version followed NuGet's config merging (clear, remove, disabledPackageSources, mapping precedence across files). Review found it wrong in several places:

  • remove is not a NuGet element.
  • Disabled sources are matched by exact key, whatever the value.
  • A closer config's packageSource replaces a farther one rather than merging with it.
  • Repeated elements are dropped.
  • Patterns are cut at their first *.

So the reader no longer follows those rules. It counts in the direction that can only fail a lookup.

The rule

  • Sources: every source any nuget.config from the project's folder up to the repository root adds counts, even one a closer config clears or disables, along with the nuget.org source NuGet adds by default.
  • nuget.org alone: every package comes from nuget.org.
  • Any other source: packageSourceMapping decides.
    • Only a mapping that one config on the chain holds alone is read. Its most specific pattern wins: an exact ID, else the longest prefix.
    • Mappings in several configs on one chain fail every lookup. NuGet merges them by replacing, dropping and clearing, which the Count does not follow. An earlier version pooled their patterns, and review broke that twice.
    • A package mapped to nuget.org alone comes from there. Anything else fails its lookup, since private feeds aren't read yet.
    • With no mapping, every lookup fails, and the message names packageSourceMapping.
  • Shared files: Directory.Packages.props, other shared MSBuild files and dotnet-tools.json manifests count every folder below them that has a config of its own.
  • Fails closed: each of these fails every lookup it could affect:
    • a config the Count can't read;
    • a section it doesn't know, such as fallbackPackageFolders, or remove;
    • a repeated mapping section or key, or a clear after mappings;
    • a pattern NuGet reads differently: a * that isn't last, spaces, non-ASCII, or an exact ID that is also a prefix;
    • a config file named other than NuGet reads it, or several configs in one folder;
    • an MSBuild or response file that sets RestoreSources, RestoreAdditionalProject*, RestoreConfigFile or RestoreFallbackFolders (found after decoding XML character references), or one the Count can't read.
  • Always read: nuget.config, .props, .targets, .rsp, .projitems, .user, .msbuild and project files are read whenever NuGet is enabled. Symlinked or non-text ones count as unreadable.

nuget.config is parsed with roxmltree (new dependency), which rejects DTDs. Adding it also re-resolved some unrelated Windows-only windows-sys/getrandom entries in Cargo.lock.

Known residual: an MSBuild <Import> of a file with another extension isn't scanned for restore properties.

Docs: sources.md (package registries), managers.md, compatibility.md, and departures.md (new #nuget-sources).

Follow-ups

  • A repository's own regex extractor with source: "nuget" (or pypi, rubygems, packagist) and no registry template skips these origin rules in every ecosystem (crates/manager/src/custom.rs).
  • An MSBuild <Import> of a file with another extension, or props from an MSBuild SDK package, is not scanned for restore properties.

…ake them elsewhere

NuGet looked every package up on nuget.org, so a package from a private
feed could be looked up there, where its name may be someone else's.
packages.lock.json does not record a package's source, and with several
sources NuGet takes a package from whichever answers, so the nuget.config
files decide through crate::origins:

- The configs from the project's folder up to the repository root apply
  as NuGet applies them (clear, remove and disabledPackageSources), after
  the nuget.org source NuGet adds by default.
- nuget.org alone: every package comes from it.
- Another source: packageSourceMapping must say which source serves the
  package. Its most specific pattern decides; a package mapped to
  nuget.org alone comes from there, and any other fails its lookup until
  private NuGet feeds are read.
- A shared MSBuild file such as Directory.Packages.props counts every
  folder below it with a config of its own.
- A config the Count cannot read, with a section it does not know such as
  fallbackPackageFolders, or spelled other than NuGet reads it, and an
  MSBuild file that sets RestoreSources or a related property, fail every
  lookup they could affect. nuget.config is read with roxmltree, which
  rejects DTDs.
@deepsource-io

deepsource-io Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

DeepSource Code Review

We reviewed changes in e44ef52...80c0726 on this pull request. Below is the summary for the review, and you can see the individual issues we found as inline review comments.

See full review on DeepSource ↗

PR Report Card

Overall Grade   Security  

Reliability  

Complexity  

Hygiene  

Code Review Summary

Analyzer Status Updated (UTC) Details
Rust Nov 4, 2026 1:25a.m. Review ↗
JavaScript Nov 4, 2026 1:25a.m. Review ↗
Shell Nov 4, 2026 1:25a.m. Review ↗

Important

AI Review is run only on demand for your team. We're only showing results of static analysis review right now. To trigger AI Review, comment @deepsourcebot review on this thread.

@socket-security

socket-security Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedcargo/​roxmltree@​0.21.2100100100100100

View full report

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
count-patchula 97fd1cf Commit Preview URL

Branch Preview URL
Nov 04 2026, 01:15 AM

Comment thread crates/manager/src/nuget.rs Outdated
Comment on lines +423 to +425
let (dir, leaf) = path
.rsplit_once('/')
.map_or(("", path.as_str()), |(d, l)| (d, l));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Invoking `map_or(..)` with the identity function, consider using `unwrap_or(..)` instead


Calling map_or with the identity closure is equivalent to using unwrap_or
which is both easier to read and generally more performant.
Consider replacing map_or with unwrap_or.

@context7

context7 Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Docs7 for sweepies/count-patchula

Result Status Action
Deployment ➖ Not used —
Content review ➖ Did not run. This site has no agent runs available this month. Wait for the monthly reset or check your Docs7 plan. —

Commit 80c0726

…rges

Review found NuGet merges configs in ways the first version modelled
wrongly: remove is not an element, disabledPackageSources is matched by
exact key whatever its value, a closer config's packageSource replaces a
farther one, repeated elements are dropped and patterns are cut at their
first star. Rather than follow each rule, the reader now counts in the
direction that can only fail a lookup:

- Every source any config in the chain adds counts, even one a closer
  config clears or disables.
- One config's mapping decides by most specific pattern; with mappings in
  several configs, every matching pattern counts.
- remove, repeated mapping sections or keys, a clear after mappings,
  patterns NuGet reads differently (a star that is not last, spaces,
  non-ASCII, an exact ID that is also a prefix) fail closed.
- A tool manifest covers every config below its folder.
- MSBuild restore properties are found after decoding character
  references, under more property names and file types, and an MSBuild
  file the Count cannot read counts as one that sets them.
- RestoreRootConfigDirectory and RestoreSolutionDirectory, which move the
  folder NuGet reads configs from, override the repository's configs, and
  .pubxml files are scanned too.
- Source and mapping keys with spaces around them or outside ASCII fail
  closed, since NuGet trims mapping keys but not source keys.
- A shared MSBuild file (.props, .targets) can be imported from any
  folder, so it counts every folder with a config.
- Character references are searched within ten bytes, so a long run of
  "&#" stays linear.
- On the GitHub path, a settings or MSBuild file that is not UTF-8 is
  unreadable, as in read_tree, instead of failing the whole scan.
…rences

- A packageSourceMapping clear in one of several configs' mappings can
  turn NuGet's mapping off, so any source serves any package; such a
  chain now fails instead of pooling the patterns. A packageSource with
  no package patterns is not read.
- Character references are decoded whatever leading zeros their digits
  have, still in linear time.
- Each folder's chain of configs is computed once, so shared files in a
  large repository no longer recompute them per package.
Pooling mappings from several configs rested on an argument about how
NuGet merges them, and review broke it twice (a closer clear can turn the
mapping off). Mappings in several configs on a chain now fail every
lookup, so the rule is short: sources are over-counted, and the one
mapping NuGet sees is exactly that config's.
Comment thread crates/manager/src/nuget.rs Outdated
Comment on lines +540 to +542
let (dir, leaf) = path
.rsplit_once('/')
.map_or(("", path.as_str()), |(d, l)| (d, l));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Invoking `map_or(..)` with the identity function, consider using `unwrap_or(..)` instead


Calling map_or with the identity closure is equivalent to using unwrap_or
which is both easier to read and generally more performant.
Consider replacing map_or with unwrap_or.

…urces

- Only a tool manifest's own .config folder stands for its project, not
  any folder ending in .config.
- A project restores in its own folder, so configs below it no longer
  count for it; shared MSBuild files and tool manifests still count
  every folder below them.
- A source value with quotes or spaces around it, which NuGet keeps and
  the Count would trim, is not read.
Comment thread crates/manager/src/nuget.rs Outdated
pub(crate) fn new(files: &Files) -> Self {
let mut configs = BTreeMap::<String, Result<Config, String>>::new();
for (path, text) in files.iter().filter(|(path, _)| config_file(path)) {
let (dir, leaf) = path.rsplit_once('/').unwrap_or(("", path.as_str()));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Use of `unwrap_or` followed by function call, prefer `unwrap_or_else` instead


Calls to unwrap_or followed by a function call should be replaced with
unwrap_or_else or unwrap_or_default, to increase laziness. The argument to
unwrap_or is evaluated eagerly and unconditionally. unwrap_or_else or
unwrap_or_default are evaluated lazily and only if required, sometimes
leading to lower overhead and fewer allocations.

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.

1 participant