mirror of
https://github.com/morluto/rea.git
synced 2026-11-20 05:32:44 +00:00
fix(nuget): respect OPC part-name equivalence (#12691)
fix(nuget): preserve ambiguous OPC manifest identity
This commit is contained in:
1 parent
870f9b7432
commit
cf64e4ce3c
3 files changed
+67
-34
No files matched your search
@@ -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
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -491,6 +491,10 @@ export const distinctTargetMatches = <T>(
|
||||
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<string, (typeof files)[number][]>();
|
||||
const foldedNames = new Map<string, (typeof files)[number][]>();
|
||||
const partNames = new Map<string, (typeof files)[number][]>();
|
||||
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.
|
||||
|
||||
Reference in new issue
Block a user