From cf64e4ce3c51eced4171c9f0e708cba40f408d50 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 21:16:52 -0500 Subject: [PATCH] fix(nuget): respect OPC part-name equivalence (#12691) fix(nuget): preserve ambiguous OPC manifest identity --- docs/guides/managed/nuget-nupkg.md | 4 +- src/nuget/NugetNupkgParser.test.ts | 69 ++++++++++++++++++++++-------- src/nuget/NugetNupkgParser.ts | 28 ++++++------ 3 files changed, 67 insertions(+), 34 deletions(-) diff --git a/docs/guides/managed/nuget-nupkg.md b/docs/guides/managed/nuget-nupkg.md index 1d0f166383..1ff4aca850 100644 --- a/docs/guides/managed/nuget-nupkg.md +++ b/docs/guides/managed/nuget-nupkg.md @@ -75,7 +75,9 @@ not carry that uncertainty warning. `nuspec` names the selected manifest path, decoded byte length, and whether it came from an OPC `_rels/.rels` manifest relationship or a unique root `.nuspec`. When selection is missing or ambiguous, `nuspec` is `null` and the inventory -still succeeds with a limitation. +still succeeds with a limitation. OPC part names match without regard to ASCII +case. Multiple equivalent ZIP member names leave the manifest ambiguous, even +when the target exactly spells one member name. The `.nuspec` is chosen from a unique OPC relationship with Type `http://schemas.microsoft.com/packaging/2010/07/manifest` when that target diff --git a/src/nuget/NugetNupkgParser.test.ts b/src/nuget/NugetNupkgParser.test.ts index 24a0071120..970572d0fe 100644 --- a/src/nuget/NugetNupkgParser.test.ts +++ b/src/nuget/NugetNupkgParser.test.ts @@ -778,24 +778,57 @@ it.for(["constructor/file.txt", "__proto__/file.txt", "toString/file.txt"])( }, ); -it("leaves identity unknown when the manifest relationship matches case-variant members", async () => { - const bytes = await nugetNupkgFixture({ - includeRels: false, - nuspecName: "First.nuspec", - extraMembers: { - "_rels/.rels": manifestRels("/FIRST.nuspec"), - "first.nuspec": ungroupedNuspec, - }, - }); - const report = nugetNupkgSchema.parse( - await parseNugetNupkg(bytes, artifactOf(bytes), defaults), - ); - expect(report.identity).toBeNull(); - expect(report.nuspec).toBeNull(); - expect(report.limitations).toContain( - "OPC manifest relationships resolve to more than one .nuspec ZIP member; identity was not decoded.", - ); -}); +it.each(["/FIRST.nuspec", "/First.nuspec", "/first.nuspec"])( + "leaves identity unknown when OPC target %s matches case-variant members", + async (target) => { + const bytes = await nugetNupkgFixture({ + includeRels: false, + nuspecName: "First.nuspec", + extraMembers: { + "_rels/.rels": manifestRels(target), + "first.nuspec": ungroupedNuspec, + }, + }); + const report = nugetNupkgSchema.parse( + await parseNugetNupkg(bytes, artifactOf(bytes), defaults), + ); + expect(report.identity).toBeNull(); + expect(report.nuspec).toBeNull(); + expect(report.coverage).toMatchObject({ + status: "complete", + examined_nuspec_bytes: 0, + }); + expect(report.contents.map((entry) => entry.name)).toEqual( + expect.arrayContaining(["First.nuspec", "first.nuspec"]), + ); + expect(report.limitations).toContain( + "OPC manifest relationships resolve to more than one .nuspec ZIP member; identity was not decoded.", + ); + }, +); + +it.each([ + { + target: "/CONTENT/É.nuspec", + source: "opc-relationship", + id: "Ungrouped.Package", + }, + { target: "/CONTENT/é.nuspec", source: "root-nuspec", id: "Demo.Package" }, +])( + "matches only ASCII case variants in an OPC target $target", + async ({ target, source, id }) => { + const bytes = await nugetNupkgFixture({ + includeRels: false, + extraMembers: { + "_rels/.rels": manifestRels(target), + "content/É.nuspec": ungroupedNuspec, + }, + }); + const report = await parseNugetNupkg(bytes, artifactOf(bytes), defaults); + expect(report.nuspec?.source).toBe(source); + expect(report.identity?.id).toBe(id); + }, +); it("reports an encrypted manifest above the metadata budget without decoding it", async () => { const writer = new ZipWriter(new Uint8ArrayWriter()); diff --git a/src/nuget/NugetNupkgParser.ts b/src/nuget/NugetNupkgParser.ts index 2732e33716..292434e80d 100644 --- a/src/nuget/NugetNupkgParser.ts +++ b/src/nuget/NugetNupkgParser.ts @@ -491,6 +491,10 @@ export const distinctTargetMatches = ( return [...found]; }; +/** OPC part-name equivalence folds ASCII case only. */ +const opcPartNameKey = (name: string): string => + name.replace(/[A-Z]/gu, (letter) => letter.toLowerCase()); + const findNuspec = ( files: readonly { readonly entry: FileEntry; readonly name: string }[], relsXml: string | undefined, @@ -501,28 +505,22 @@ const findNuspec = ( | undefined; readonly source: "opc-relationship" | "root-nuspec" | undefined; } => { - // Every ZIP occurrence stays distinct: duplicate or case-variant names must - // surface as ambiguity rather than collapse to an archive-order winner. + // Keep every ZIP occurrence in its OPC equivalence bucket. An exact + // spelling cannot resolve two occurrences with equivalent part names. // Index once: every target is then a constant-time lookup, however many // relationships the package declares. - const exactNames = new Map(); - const foldedNames = new Map(); + const partNames = new Map(); for (const file of files) { - const exact = exactNames.get(file.name); - if (exact === undefined) exactNames.set(file.name, [file]); - else exact.push(file); - const lower = file.name.toLowerCase(); - const folded = foldedNames.get(lower); - if (folded === undefined) foldedNames.set(lower, [file]); - else folded.push(file); + const key = opcPartNameKey(file.name); + const members = partNames.get(key); + if (members === undefined) partNames.set(key, [file]); + else members.push(file); } - const matchTarget = (target: string) => - exactNames.get(target) ?? foldedNames.get(target.toLowerCase()) ?? []; if (relsXml !== undefined) { const targets = opcManifestTargets(relsXml, limitations); const resolved = distinctTargetMatches( targets.map((target) => target.lookup), - matchTarget, + (target) => partNames.get(opcPartNameKey(target)) ?? [], ); if (resolved.length > 1) { limitations.push( @@ -658,7 +656,7 @@ export const parseNugetNupkg = async ( }); } const relsMembers = files.filter( - (file) => file.name.toLowerCase() === "_rels/.rels", + (file) => opcPartNameKey(file.name) === "_rels/.rels", ); // Spellings such as "./_rels/.rels" normalize to the same logical path; with // more than one, ZIP order would choose the manifest, so follow none.