diff --git a/src/main/java/org/openrewrite/csharp/dependencies/DependencyVulnerabilityCheck.java b/src/main/java/org/openrewrite/csharp/dependencies/DependencyVulnerabilityCheck.java index 392e2f7..496f83e 100644 --- a/src/main/java/org/openrewrite/csharp/dependencies/DependencyVulnerabilityCheck.java +++ b/src/main/java/org/openrewrite/csharp/dependencies/DependencyVulnerabilityCheck.java @@ -146,9 +146,15 @@ public TreeVisitor getScanner(Accumulator acc) { private static final Comparator vc = new StaticVersionComparator(); private boolean isVulnerable(String dependencyVersion, Vulnerability v) { - return vc.compare( - versionParser.transform(dependencyVersion), - versionParser.transform(v.getFixedVersion())) < 0; + Version actual = versionParser.transform(dependencyVersion); + if (vc.compare(actual, versionParser.transform(v.getIntroducedVersion())) < 0) { + return false; + } + String fixedVersion = v.getFixedVersion(); + if (StringUtils.isBlank(fixedVersion)) { + return true; + } + return vc.compare(actual, versionParser.transform(fixedVersion)) < 0; } private static final LatestPatch latestPatch = new LatestPatch(null); @@ -168,9 +174,7 @@ public TreeVisitor getVisitor(Accumulator acc) { String dependencyVersion = ref.getVersion(); Map> vulnerabilities = acc.vulnerabilities .getOrDefault(new Accumulator.NameVersion(ref.getInclude(), ref.getVersion()), emptySet()) - .stream() - .filter(v -> StringUtils.isBlank(v.getFixedVersion()) || isVulnerable(dependencyVersion, v)) - .collect(partitioningBy(v -> isFixWithPatchVersionUpdateOnly(dependencyVersion, v))); + .stream().collect(partitioningBy(v -> isFixWithPatchVersionUpdateOnly(dependencyVersion, v))); // Bump to highest fixed patch version String highestFixedPatchVersion = vulnerabilities.get(true).stream() diff --git a/src/test/java/org/openrewrite/csharp/dependencies/DependencyVulnerabilityCheckTest.java b/src/test/java/org/openrewrite/csharp/dependencies/DependencyVulnerabilityCheckTest.java index 03664ca..fdb0336 100644 --- a/src/test/java/org/openrewrite/csharp/dependencies/DependencyVulnerabilityCheckTest.java +++ b/src/test/java/org/openrewrite/csharp/dependencies/DependencyVulnerabilityCheckTest.java @@ -133,4 +133,76 @@ void upgradePackageWithMultipleVulnerablePatchVersions() { ) ); } + + @Test + void vulnerableWithNoFixedVersion() { + rewriteRun( + spec -> spec.recipe(new DependencyVulnerabilityCheck(true)), + xml( + //language=xml + """ + + + + + + """, + //language=xml + """ + + + + + + """, + spec -> spec.path("MyFirst.csproj") + ) + ); + } + + @Test + void notVulnerableOnOlderVersion() { + rewriteRun( + spec -> spec.recipe(new DependencyVulnerabilityCheck(true)), + xml( + //language=xml + """ + + + + + + """, + spec -> spec.path("MyFirst.csproj") + ) + ); + } + + @Test + void vulnerableOnMinimumVersion() { + rewriteRun( + spec -> spec.recipe(new DependencyVulnerabilityCheck(true)), + xml( + //language=xml + """ + + + + + + """, + //language=xml + """ + + + + + + """, + spec -> spec.path("MyFirst.csproj") + ) + ); + } }