Skip to content

Adding interface, implementation, and tests for vulnerability capabilities for a package domain model - #6279

Merged
jebriede merged 5 commits into
dev-feature-PackageDomainModelfrom
dev-jebriede-VulnerableCapability
Mar 5, 2025
Merged

jebriede merged 5 commits into
dev-feature-PackageDomainModelfrom
dev-jebriede-VulnerableCapability

Conversation

@jebriede

@jebriede jebriede commented Feb 19, 2025 •

Copy link
Copy Markdown
Contributor

Bug

Fixes:

Description

Encapsulates the vulnerability list and logic. Provides an interface for use in a package model type as well as the implementation and unit tests.

PR Checklist

  • Meaningful title, helpful description and a linked NuGet/Home issue
  • Added tests
  • Link to an issue or pull request to update docs if this PR changes settings, environment variables, new feature, etc.

@jebriede
jebriede requested a review from a team as a code owner February 19, 2025 23:37
@jebriede
jebriede changed the base branch from dev to dev-feature-PackageDomainModel February 19, 2025 23:38
private set => _vulnerabilities = value.OrderByDescending(v => v?.Severity ?? -1);
}

public bool IsVulnerable => Vulnerabilities.Any(v => v != null);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so you're saying it's possible to have a list of vulnerabilities where all entries are null?

@jebriede jebriede Feb 20, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, programmatically. It doesn't make much sense to have that, but there a few strategies I can think of:

  1. Prevent the construction of a VulnerableCapability if the collection of vulnerabilities contains any nulls. This would require iterating over the collection upon construction to validate if any of the entries are null. With big lists this could take a performance hit for constructing many Packages with IVulnerable and I would not favor this.
  2. Allow the collection to contain nulls because it's just a list of objects, but handle them gracefully so we essentially treat them as if they did not exist. This is what I've done in this PR and made that behavior explicit by adding a test around it and to ensure that it is handled gracefully.

Can you think of another way you'd suggest instead?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I think iterating through them to validate there aren't any nulls seems excessive. I guess I didn't realize we had been accounting for this in the old code too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jgonz120 We aren't accounting for it explicitly in the ViewModels today which presents a potential flaw. The goal here is to make this code more robust than what we had before and put it under test. By handling this edge case gracefully, we avoid any NRE's in case a null makes its way into the list at any point.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file has #nullable enable, and the definition of the Vulnerabilities property doesn't allow nulls, so either the definition is wrong, or this not null check shouldn't be needed.

@jebriede jebriede Feb 27, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file has #nullable enable, and the definition of the Vulnerabilities property doesn't allow nulls, so either the definition is wrong, or this not null check shouldn't be needed.

I see what you mean. Both the tests and the VulnerabilityCapability classes themselves have #nullable enable yet the Test allows a null value to be used. As such, it proves that with nullable enabled on the caller, we can still end up with nulls making their way into the list. While a test can show that it's possible, we should have code that handles this edge case and a test that proves it's being handled. If I wrote the test incorrectly though, please let me know how and I'll fix it!

@jebriede jebriede Feb 28, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zivkan I removed the null checks and the test. This bug is open against Roslyn dotnet/roslyn#72142 which is erroneously allowing the list to have a null in only one case that I could tell. I'm deciding not to code around this bug for an unlikely edge case. Let me know what you think!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is @jgonz120's thread 😁

{
get
{
// Vulnerabilities are ordered so the first element is always the highest severity

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is not officially part of the contract, we shouldn't depend on it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, there's no way to guarantee the list will be ordered from any and all vulnerability sources, both current and future. This comment points out that the list is ordered. It's ordered in this class (see the setter) explicitly to avoid making assumptions about the data source. Does that help? Do you have any suggested edits?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does, right? The list of known vulnerabilities for a package should be sorted in descending order of the max version of the version range.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't that talk about the way the version ranges are sorted and not the list of vulnerabilities?

Comment on lines +49 to +54
if (vulnerabilities == null)
{
throw new ArgumentNullException(nameof(vulnerabilities));
}

Vulnerabilities = vulnerabilities;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be shortened, if you prefer

Suggested change
if (vulnerabilities == null)
{
throw new ArgumentNullException(nameof(vulnerabilities));
}
Vulnerabilities = vulnerabilities;
Vulnerabilities = vulnerabilities ?? throw new ArgumentNullException(nameof(vulnerabilities));

Comment on lines +17 to +33
[Theory]
[InlineData(1, true)]
[InlineData(0, false)]
public void IsVulnerable_VariousVulnerabilities_ReturnsExpected(int severity, bool expected)
{
// Arrange
List<PackageVulnerabilityMetadataContextInfo> vulnerabilities = severity > 0
? [new(new Uri("http://example.com"), severity)]
: [];
var vulnerableCapability = new VulnerableCapability(vulnerabilities);

// Act
var result = vulnerableCapability.IsVulnerable;

// Assert
Assert.Equal(expected, result);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[Theory]
[InlineData(1, true)]
[InlineData(0, false)]
public void IsVulnerable_VariousVulnerabilities_ReturnsExpected(int severity, bool expected)
{
// Arrange
List<PackageVulnerabilityMetadataContextInfo> vulnerabilities = severity > 0
? [new(new Uri("http://example.com"), severity)]
: [];
var vulnerableCapability = new VulnerableCapability(vulnerabilities);
// Act
var result = vulnerableCapability.IsVulnerable;
// Assert
Assert.Equal(expected, result);
}
[Fact]
public void IsVulnerable_WhenVulnerabilitiesExist_ReturnsTrue()
{
// Arrange
var vulnerabilities = new List<PackageVulnerabilityMetadataContextInfo>
{
new PackageVulnerabilityMetadataContextInfo(new Uri("http://example.com"), severity: 1)
};
var capability = new VulnerableCapability(vulnerabilities);
// Act
bool isVulnerable = capability.IsVulnerable;
// Assert
Assert.True(isVulnerable);
}
[Fact]
public void IsVulnerable_WhenNoVulnerabilitiesExist_ReturnsFalse()
{
// Arrange
var capability = new VulnerableCapability(new List<PackageVulnerabilityMetadataContextInfo>());
// Act
bool isVulnerable = capability.IsVulnerable;
// Assert
Assert.False(isVulnerable);
}

@jebriede
jebriede merged commit 408cf53 into dev-feature-PackageDomainModel Mar 5, 2025
@jebriede
jebriede deleted the dev-jebriede-VulnerableCapability branch March 5, 2025 18:43
jebriede added a commit that referenced this pull request Mar 24, 2025
jebriede added a commit that referenced this pull request Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants