Skip to content

Agent-mode NuGet apply patches one copy when two projects of a solution restore the package into different folders (packages.config beside PackageReference): success, apply --check in sync and vex not_affected, while one build loads the unpatched assembly #2519

Backlog

Description

[agent] Filed by the scheduled architecture refactor routine. Found by following the nuget_stale_output_copies remedy with the real .NET SDK (8.0.428, offline, a folder feed) on solutions whose projects restore one package into two folders. main at cd7c5c5.

Problem

In agent mode a NuGet package is patched where NuGet restored it. The crawler knows every folder the projects under --cwd restore into: the packages.config folder, the packageFolders of each obj/project.assets.json, and the global packages folder. The dispatcher then keeps one copy per package for the whole run, the first folder that holds it (merge_first_wins).

That is right when the folders are the fallback chain of one restore: NuGet itself reads the first one that holds the package. It is wrong when two projects of the solution read different folders, because each folder then holds a copy that a build loads:

  • a packages.config project beside a PackageReference project (a solution part way through its move to SDK-style projects): packages/<Id>.<Version>/ and the global packages folder each hold the package;
  • two PackageReference projects with different package folders (RestorePackagesPath in one project, or a globalPackagesFolder set for one subtree).

apply patches the first copy and reports the package as patched. apply --check says the patches are in sync. vex writes not_affected. The other project keeps building from, and loading, the unpatched assembly. The stale-output warning names that project's bin/ copy and says a rebuild refreshes it; the rebuild copies the unpatched file again, so the warning comes back on every run and its remedy cannot work.

Evidence on main

A packages.config project beside a PackageReference project. Vuln 1.0.0 is a one-class library packed into a folder feed; the patch replaces lib/net8.0/Vuln.dll with a build whose Marker says PATCHED!. Modern was restored, built and run with the real SDK. Legacy is a .NET Framework project file with a HintPath into packages/Vuln.1.0.0/, and its folder was laid out from the same .nupkg (dotnet cannot restore packages.config, and this sandbox has no Windows MSBuild, so Legacy itself was not built).

Mixed.sln
Legacy/Legacy.csproj  Legacy/packages.config     # <package id="Vuln" version="1.0.0" … />
Modern/Modern.csproj                             # <PackageReference Include="Vuln" Version="1.0.0" />
packages/Vuln.1.0.0/lib/net8.0/Vuln.dll          # the packages.config copy
<global packages folder>/vuln/1.0.0/lib/net8.0/Vuln.dll   # the copy Modern's restore reads

$ socket-patch apply --offline                   # --cwd is the solution directory
Warning: pkg:nuget/Vuln@1.0.0: 1 copy of Vuln.dll under this project's bin and publish directories still holds the unpatched file: Modern/bin/Debug/net8.0/Vuln.dll. An application run from that output loads the unpatched code. `dotnet build` (or `dotnet publish`) copies the package's file into the output it writes, one configuration per run: rebuild or republish each output listed, or delete it. Output written to another directory was not searched.

Patched packages:
  pkg:nuget/Vuln@1.0.0 (via blob)

Summary: 1 of 1 targeted patch applied, 0 already patched, 0 not found on disk          # exit 0

$ sha256sum …/Vuln.dll
fbab9f6f  packages/Vuln.1.0.0/lib/net8.0/Vuln.dll                       # patched
ce288356  <global packages folder>/vuln/1.0.0/lib/net8.0/Vuln.dll       # unpatched
ce288356  Modern/bin/Debug/net8.0/Vuln.dll                              # unpatched

$ (cd Modern && dotnet build && dotnet bin/Debug/net8.0/Modern.dll)      # the remedy the warning names
marker=ORIGINAL

$ socket-patch apply --offline                   # the same warning again; "1 already patched"; exit 0
$ socket-patch apply --check --offline
Patches are in sync (1 patch checked).                                                  # exit 0
$ socket-patch vex --offline --product pkg:nuget/mixed@1.0.0 --output vex.json
Wrote OpenVEX document with 1 statement to vex.json                                     # exit 0
#   status not_affected, justification inline_mitigations_already_exist,
#   products pkg:nuget/mixed@1.0.0 (subcomponent pkg:nuget/Vuln@1.0.0) and pkg:nuget/Vuln@1.0.0

Two PackageReference projects, one with <RestorePackagesPath>$(MSBuildThisFileDirectory)../pkgs-b</RestorePackagesPath>. Both built and run with the real SDK:

$ socket-patch apply --offline
Summary: 1 of 1 targeted patch applied, 0 already patched, 0 not found on disk          # exit 0
$ dotnet build && dotnet A/bin/Debug/net8.0/A.dll && dotnet B/bin/Debug/net8.0/B.dll
marker=PATCHED!
marker=ORIGINAL
fbab9f6f  <global packages folder>/vuln/1.0.0/lib/net8.0/Vuln.dll       # patched
ce288356  pkgs-b/vuln/1.0.0/lib/net8.0/Vuln.dll                         # unpatched
$ socket-patch apply --check --offline
Patches are in sync (1 patch checked).                                                  # exit 0

Controls, same SDK. One PackageReference project: apply patches the global copy, and dotnet build and dotnet publish -c Release then refresh each output (a patched file of the same size as the original included); after rollback they put the original back. A package that sits only in a fallback folder (RestoreFallbackFolders): NuGet reads it from there, apply patches that copy, and the rebuilt application loads it. So one copy per restore is right; one copy per run is not.

A test without an SDK, in crates/socket-patch-cli/tests/apply/nuget_output_and_signing.rs (its Project is a PackageReference project restored into pkgs/):

#[test]
fn apply_patches_the_copy_each_project_builds_from() {
    let project = Project::new();
    // The packages.config project's copy: `<cwd>/packages/<Id>.<Version>/`.
    project.write(
        "Legacy/packages.config",
        b"<packages><package id=\"Newtonsoft.Json\" version=\"13.0.3\" /></packages>",
    );
    project.write(
        "packages/Newtonsoft.Json.13.0.3/Newtonsoft.Json.nuspec",
        b"<package/>",
    );
    let legacy = project.write(
        "packages/Newtonsoft.Json.13.0.3/lib/net6.0/Newtonsoft.Json.dll",
        &project.original,
    );

    let env = project.run_json(&["apply"]);
    let state = |path: &std::path::Path| {
        let bytes = std::fs::read(path).unwrap();
        if bytes == project.patched {
            "patched"
        } else if bytes == project.original {
            "unpatched"
        } else {
            "other"
        }
    };
    assert_eq!(
        (state(&legacy), state(&project.assembly())),
        ("patched", "patched"),
        "(packages.config copy, PackageReference copy) after apply: {env}"
    );
}
$ cargo test -p socket-patch-cli --test apply apply_patches_the_copy_each_project_builds_from
assertion `left == right` failed: (packages.config copy, PackageReference copy) after apply: {"command":"apply","status":"success",…,"summary":{…,"applied":1,…,"failed":0,…}}
  left: ("patched", "unpatched")
 right: ("patched", "patched")

Where

  • merge_first_wins and its use for NuGet. Its comment calls NuGet a single-copy ecosystem: "the same install is legitimately reachable from several source roots". Two unit tests pin it ([merge_first_wins_keeps_only_first_of_two_distinct_paths](https://github.com/SocketDev/socket-patch/blob/cd7c5c56e3e0049a5c06260c354a597b8f07658b/crates/socket-patch-cli/src/ecosystem_dispatch.rs#L2046-L2098)), and name the regression that set it: the same directory reached through two roots gave a second, already_patched event.
  • [package_roots](https://github.com/SocketDev/socket-patch/blob/cd7c5c56e3e0049a5c06260c354a597b8f07658b/crates/socket-patch-core/src/crawlers/nuget_crawler.rs#L564-L636) lists the folders in priority order, and restore_walk flattens every restore's packageFolders into one list, so which restore a folder belongs to is lost before the dispatcher sees it.
  • The contract's VEX row for an agent record says every installed copy the crawler finds must hash to the patched bytes, "as apply patches every copy". docs/ecosystems.md ("NuGet agent mode: rebuild after apply") says agent mode patches a package where NuGet restored it and lists all three kinds of folder.

The same class, fixed elsewhere

One package, several copies that builds read, one of them patched and success reported:

Planned fix

Resolve a NuGet package to one copy per restore, not one per run:

  1. The crawler keeps the folders in groups: each restore's packageFolders; the packages.config folder; and the global packages folder where project_restore_folders counts it today (a project with no parsed restore, or no restore that names a folder).
  2. Within a group the first folder that holds the package is the copy, which is NuGet's own order.
  3. Across groups every distinct directory, compared by canonical path, is a copy. apply, rollback, apply --check and vex then see each one through the multi-copy path npm, cargo and gems use, and the stale-output check tracks each patched copy.
  4. The NuGet arm of the dispatcher stops using merge_first_wins. The two unit tests that pin it for NuGet are rewritten; the case they guard (one directory through two roots) is covered by the canonical-path comparison.
  5. Tests: the one above; two restores with different folders; a fallback folder beside a global folder that also holds the package (the fallback copy is not written).

A folder that no restore of the solution reads stays out. If a maintainer would rather not write a second folder, the smaller change is to make apply --check and vex see the unpatched copy and say so; this issue plans the fix above unless told otherwise.


Generated by Claude Code

Activity

  1. added
    bug
    arch-audit
    on Dec 5, 2026
  2. added a commit that references this issue on Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent:triagedarch-auditbugpriority:p3

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions