Skip to content

Commit 93954a6

Browse files
authored
fix(nuget): null ref when parsing paket.lock (#725)
1 parent 4698b58 commit 93954a6

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/Microsoft.ComponentDetection.Detectors/nuget/NuGetComponentDetector.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ private async Task ProcessFileAsync(ProcessRequest processRequest)
105105
else if ("paket.lock".Equals(stream.Pattern, StringComparison.OrdinalIgnoreCase))
106106
{
107107
this.ParsePaketLock(processRequest);
108+
return;
108109
}
109110
else
110111
{
@@ -166,10 +167,18 @@ private void ParsePaketLock(ProcessRequest processRequest)
166167
var matches = Regex.Matches(line, @"\s*([a-zA-Z0-9-.]*) \([<>=]*[ ]*([0-9a-zA-Z-.]*)\)", RegexOptions.Singleline);
167168
foreach (var match in matches.Cast<Match>())
168169
{
169-
var name = match.Groups[1].Value;
170-
var version = match.Groups[2].Value;
171-
var component = new NuGetComponent(name, version);
172-
singleFileComponentRecorder.RegisterUsage(new DetectedComponent(component));
170+
try
171+
{
172+
var name = match.Groups[1].Value;
173+
var version = match.Groups[2].Value;
174+
var component = new NuGetComponent(name, version);
175+
singleFileComponentRecorder.RegisterUsage(new DetectedComponent(component));
176+
}
177+
catch (Exception e)
178+
{
179+
this.Logger.LogWarning(e, "Failed to parse paket.lock component from line `{Line}` in {Location}", line, stream.Location);
180+
singleFileComponentRecorder.RegisterPackageParseFailure(stream.Location);
181+
}
173182
}
174183
}
175184
}

test/Microsoft.ComponentDetection.Detectors.Tests/NuGetComponentDetectorTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,23 @@ public async Task TestNugetDetector_ReturnsValidPaketComponentAsync()
151151

152152
var (scanResult, componentRecorder) = await this.DetectorTestUtility
153153
.WithFile("paket.lock", paketLock)
154+
.AddServiceMock(this.mockLogger)
154155
.ExecuteDetectorAsync();
155156

156157
Assert.AreEqual(ProcessingResultCode.Success, scanResult.ResultCode);
157158

158159
// While there are 26 lines in the sample, several dependencies are identical, so there are only 11 matches.
159160
Assert.AreEqual(11, componentRecorder.GetDetectedComponents().Count());
161+
162+
// Verify that we stop executing after parsing the paket.lock file.
163+
this.mockLogger.Verify(
164+
x => x.Log(
165+
It.IsAny<LogLevel>(),
166+
It.IsAny<EventId>(),
167+
It.IsAny<It.IsAnyType>(),
168+
It.IsAny<Exception>(),
169+
(Func<It.IsAnyType, Exception, string>)It.IsAny<object>()),
170+
Times.Once());
160171
}
161172

162173
[TestMethod]

0 commit comments

Comments
 (0)