Skip to content

Commit 2024816

Browse files
Merge changes from the private Gallery repo (#10689)
* Add clean MSRC copy This is GitHub main commit 4f542d8, with ApiKeyV5 secrets purged, automated SDK updates, and ES metadata file. * Merged PR 2802: Fixed bug in nuspec path parsing Fixed nuspec path handling to correctly handle paths with a backslash. Co-authored-by: Joel Verhagen <[email protected]>
1 parent efb4c49 commit 2024816

3 files changed

Lines changed: 108 additions & 3 deletions

File tree

global.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"sdk": {
3-
"version": "8.0.303",
3+
"version": "8.0.318",
44
"rollForward": "latestFeature",
55
"allowPrerelease": false
66
}

src/Catalog/Helpers/Utils.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public static class Utils
3636

3737
private static readonly char[] TagTrimChars = { ',', ' ', '\t', '|', ';' };
3838

39+
private static readonly char[] Slashes = { '/', '\\' };
40+
3941
public static string[] SplitTags(string original)
4042
{
4143
var fields = original
@@ -118,12 +120,13 @@ public static XDocument GetNuspec(ZipArchive package)
118120

119121
foreach (ZipArchiveEntry part in package.Entries)
120122
{
121-
if (part.FullName.EndsWith(".nuspec") && part.FullName.IndexOf('/') == -1)
123+
if (part.FullName.EndsWith(".nuspec") && part.FullName.IndexOfAny(Slashes) == -1)
122124
{
123125
XDocument nuspec = XDocument.Load(part.Open());
124126
return nuspec;
125127
}
126128
}
129+
127130
return null;
128131
}
129132

tests/CatalogTests/Helpers/UtilsTests.cs

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) .NET Foundation. All rights reserved.
1+
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
@@ -69,6 +69,108 @@ public void GetNupkgMetadata_WhenNuspecNotFound_Throws()
6969
}
7070
}
7171

72+
[Fact]
73+
public void GetNupkgMetadata_WhenNuspecAtRootLevel_Succeeds()
74+
{
75+
using (var stream = new MemoryStream())
76+
{
77+
using (var zipArchive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true))
78+
{
79+
var entry = zipArchive.CreateEntry("package.nuspec");
80+
using (var writer = new StreamWriter(entry.Open()))
81+
{
82+
writer.Write("<?xml version=\"1.0\"?><package xmlns=\"http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd\"><metadata><id>TestPackage</id><version>1.0.0</version><authors>Test</authors><description>Test</description></metadata></package>");
83+
}
84+
}
85+
86+
stream.Position = 0;
87+
88+
var metadata = Utils.GetNupkgMetadata(stream, packageHash: null);
89+
90+
Assert.NotNull(metadata);
91+
Assert.NotNull(metadata.Nuspec);
92+
}
93+
}
94+
95+
[Fact]
96+
public void GetNupkgMetadata_WhenNuspecInSubdirectoryWithForwardSlash_Throws()
97+
{
98+
using (var stream = new MemoryStream())
99+
{
100+
using (var zipArchive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true))
101+
{
102+
var entry = zipArchive.CreateEntry("subdir/package.nuspec");
103+
using (var writer = new StreamWriter(entry.Open()))
104+
{
105+
writer.Write("<?xml version=\"1.0\"?><package></package>");
106+
}
107+
}
108+
109+
stream.Position = 0;
110+
111+
var exception = Assert.Throws<InvalidDataException>(
112+
() => Utils.GetNupkgMetadata(stream, packageHash: null));
113+
114+
Assert.StartsWith("Unable to find nuspec", exception.Message);
115+
}
116+
}
117+
118+
[Fact]
119+
public void GetNupkgMetadata_WhenNuspecInSubdirectoryWithBackslash_Throws()
120+
{
121+
using (var stream = new MemoryStream())
122+
{
123+
using (var zipArchive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true))
124+
{
125+
var entry = zipArchive.CreateEntry("subdir\\package.nuspec");
126+
using (var writer = new StreamWriter(entry.Open()))
127+
{
128+
writer.Write("<?xml version=\"1.0\"?><package></package>");
129+
}
130+
}
131+
132+
stream.Position = 0;
133+
134+
var exception = Assert.Throws<InvalidDataException>(
135+
() => Utils.GetNupkgMetadata(stream, packageHash: null));
136+
137+
Assert.StartsWith("Unable to find nuspec", exception.Message);
138+
}
139+
}
140+
141+
142+
[Fact]
143+
public void GetNupkgMetadata_WhenNuspecAtRootAndInSubdirectory_UsesRootNuspec()
144+
{
145+
using (var stream = new MemoryStream())
146+
{
147+
using (var zipArchive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true))
148+
{
149+
var subdirEntry = zipArchive.CreateEntry("subdir\\malicious.nuspec");
150+
using (var writer = new StreamWriter(subdirEntry.Open()))
151+
{
152+
writer.Write("<?xml version=\"1.0\"?><package xmlns=\"http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd\"><metadata><id>MaliciousPackage</id><version>2.0.0</version><authors>MaliciousAuthor</authors><description>Malicious Description</description></metadata></package>");
153+
}
154+
155+
var rootEntry = zipArchive.CreateEntry("package.nuspec");
156+
using (var writer = new StreamWriter(rootEntry.Open()))
157+
{
158+
writer.Write("<?xml version=\"1.0\"?><package xmlns=\"http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd\"><metadata><id>RootPackage</id><version>1.0.0</version><authors>RootAuthor</authors><description>Root Description</description></metadata></package>");
159+
}
160+
}
161+
162+
stream.Position = 0;
163+
164+
var metadata = Utils.GetNupkgMetadata(stream, packageHash: null);
165+
166+
Assert.NotNull(metadata);
167+
Assert.NotNull(metadata.Nuspec);
168+
var packageId = metadata.Nuspec.Descendants().FirstOrDefault(x => x.Name.LocalName == "id");
169+
Assert.NotNull(packageId);
170+
Assert.Equal("RootPackage", packageId.Value);
171+
}
172+
}
173+
72174
[Theory]
73175
[InlineData(null)]
74176
[InlineData("")]

0 commit comments

Comments
 (0)