Skip to content

Commit 68cc95f

Browse files
committed
Fix and optimized patched CVE sort to exclude duplicates
1 parent 8d95f79 commit 68cc95f

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

sonar-project.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ sonar.sources=./src
66
sonar.tests=./tests/unit
77
#sonar.php.tests.reportPath=./tests/_output/test-results.xml
88
sonar.php.coverage.reportPaths=./tests/_output/coverage.xml
9-
sonar.verbose=true
9+
#sonar.verbose=true
1010
sonar.scm.revision=${env.GITHUB_SHA}
1111
sonar.buildString=${env.GITHUB_SHA}

src/PhpRelease.php

+16-6
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ private function __construct(private PhpVersion $version, private ?string $relea
1515
{
1616
}
1717

18-
public static function fromReleaseDescription(PhpVersion $version, ?string $releaseDate, ?string $releaseDescription): PhpRelease
19-
{
18+
public static function fromReleaseDescription(
19+
PhpVersion $version,
20+
?string $releaseDate,
21+
?string $releaseDescription
22+
): PhpRelease {
2023
$release = new self($version, $releaseDate);
2124
if (!empty($releaseDescription) && preg_match_all('#CVE-\d+-\d+#i', $releaseDescription, $cveMatches)) {
2225
foreach ($cveMatches[0] as $match) {
@@ -42,10 +45,17 @@ public static function sort(array $releases): array
4245

4346
private function addPatchedCveIds(CveId $cveId): void
4447
{
45-
if (!in_array($cveId, $this->patchedCveIds, true)) {
46-
$this->patchedCveIds[] = $cveId;
47-
$this->patchedCveIds = CveId::sort($this->patchedCveIds);
48+
for ($i = 0; $i < sizeof($this->patchedCveIds); $i++) {
49+
$comparison = $this->patchedCveIds[$i]->compareTo($cveId);
50+
if ($comparison === 0) {
51+
return;
52+
}
53+
if ($comparison > 0) {
54+
array_splice($this->patchedCveIds, $i, 0, [$cveId]);
55+
return;
56+
}
4857
}
58+
$this->patchedCveIds[] = $cveId;
4959
}
5060

5161
public function getVersion(): PhpVersion
@@ -66,7 +76,7 @@ public function getPatchedCveIds(): array
6676
return $this->patchedCveIds;
6777
}
6878

69-
79+
7080
public function jsonSerialize(): array
7181
{
7282
return [

tests/unit/PhpReleaseTest.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@ public function testItParsesASimpleString()
2828

2929
public function testItParsesMultipleCves()
3030
{
31-
$release = PhpRelease::fromReleaseDescription(self::$PHP_VERSION, self::$PHP_RELEASE_DATE, "CVE-2019-1234 CVE-2018-1234");
31+
$release = PhpRelease::fromReleaseDescription(self::$PHP_VERSION, self::$PHP_RELEASE_DATE, "CVE-2019-1234 CVE-2017-1234 CVE-2018-1234 CVE-2020-1234 CVE-2020-1234");
3232
$this->assertNotEmpty($release);
3333
$this->assertNotEmpty($release->getPatchedCveIds());
3434
$this->assertEquals(json_encode([
3535
'releaseDate' => self::$PHP_RELEASE_DATE,
3636
'patchedCves' => [
37+
'CVE-2017-1234',
3738
'CVE-2018-1234',
38-
'CVE-2019-1234'
39+
'CVE-2019-1234',
40+
'CVE-2020-1234'
3941
]
4042
]), json_encode($release));
4143
}
@@ -57,4 +59,18 @@ public function testItSorts()
5759
$this->assertEquals((string) $smallest->getVersion(), (string) $sorted[0]->getVersion());
5860
$this->assertEquals((string) $largest->getVersion(), (string) $sorted[1]->getVersion());
5961
}
62+
63+
public function testItRemovesDuplicateCves()
64+
{
65+
$release = PhpRelease::fromReleaseDescription(self::$PHP_VERSION, self::$PHP_RELEASE_DATE, "CVE-2023-1234 CVE-2018-1234 CVE-2023-1234 CVE-2018-1234");
66+
$this->assertNotEmpty($release);
67+
$this->assertNotEmpty($release->getPatchedCveIds());
68+
$this->assertEquals(json_encode([
69+
'releaseDate' => self::$PHP_RELEASE_DATE,
70+
'patchedCves' => [
71+
'CVE-2018-1234',
72+
'CVE-2023-1234',
73+
]
74+
]), json_encode($release));
75+
}
6076
}

0 commit comments

Comments
 (0)