1
1
"""Unit tests for the Trivy JSON security warnings collector."""
2
2
3
- from typing import ClassVar
4
-
5
- from source_collectors .trivy .security_warnings import TrivyJSON
6
-
7
3
from tests .source_collectors .source_collector_test_case import SourceCollectorTestCase
8
4
9
5
@@ -12,74 +8,88 @@ class TrivyJSONSecurityWarningsTest(SourceCollectorTestCase):
12
8
13
9
SOURCE_TYPE = "trivy_json"
14
10
METRIC_TYPE = "security_warnings"
15
- VULNERABILITIES_JSON : ClassVar [TrivyJSON ] = [
16
- {
17
- "Target" : "php-app/composer.lock" ,
18
- "Vulnerabilities" : None ,
19
- },
20
- {
21
- "Target" : "trivy-ci-test (alpine 3.7.1)" ,
22
- "Vulnerabilities" : [
23
- {
24
- "VulnerabilityID" : "CVE-2018-16840" ,
25
- "PkgName" : "curl" ,
26
- "InstalledVersion" : "7.61.0-r0" ,
27
- "FixedVersion" : "7.61.1-r1" ,
28
- "Title" : 'curl: Use-after-free when closing "easy" handle in Curl_close()' ,
29
- "Description" : "A heap use-after-free flaw was found in curl versions from 7.59.0 through ..." ,
30
- "Severity" : "HIGH" ,
31
- "References" : [
32
- "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-16840" ,
33
- ],
34
- },
35
- {
36
- "VulnerabilityID" : "CVE-2019-3822" ,
37
- "PkgName" : "curl" ,
38
- "InstalledVersion" : "7.61.1-r0" ,
39
- "FixedVersion" : "7.61.2-r2" ,
40
- "Title" : "curl: NTLMv2 type-3 header stack buffer overflow" ,
41
- "Description" : "libcurl versions from 7.36.0 to before 7.64.0 are vulnerable to ..." ,
42
- "Severity" : "MEDIUM" ,
43
- "References" : [
44
- "https://curl.haxx.se/docs/CVE-2019-3822.html" ,
45
- "https://lists.apache.org/thread.html" ,
46
- ],
47
- },
48
- ],
49
- },
50
- ]
51
- EXPECTED_ENTITIES : ClassVar [list [dict [str , str ]]] = [
52
- {
53
- "key" : "CVE-2018-16840@curl@trivy-ci-test (alpine 3_7_1)" ,
54
- "vulnerability_id" : "CVE-2018-16840" ,
55
- "title" : 'curl: Use-after-free when closing "easy" handle in Curl_close()' ,
56
- "description" : "A heap use-after-free flaw was found in curl versions from 7.59.0 through ..." ,
57
- "level" : "HIGH" ,
58
- "package_name" : "curl" ,
59
- "installed_version" : "7.61.0-r0" ,
60
- "fixed_version" : "7.61.1-r1" ,
61
- "url" : "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-16840" ,
62
- },
63
- {
64
- "key" : "CVE-2019-3822@curl@trivy-ci-test (alpine 3_7_1)" ,
65
- "vulnerability_id" : "CVE-2019-3822" ,
66
- "title" : "curl: NTLMv2 type-3 header stack buffer overflow" ,
67
- "description" : "libcurl versions from 7.36.0 to before 7.64.0 are vulnerable to ..." ,
68
- "level" : "MEDIUM" ,
69
- "package_name" : "curl" ,
70
- "installed_version" : "7.61.1-r0" ,
71
- "fixed_version" : "7.61.2-r2" ,
72
- "url" : "https://curl.haxx.se/docs/CVE-2019-3822.html" ,
73
- },
74
- ]
11
+ SCHEMA_VERSIONS = (1 , 2 )
12
+
13
+ def vulnerabilities_json (self , schema_version : int = 1 ):
14
+ """Return the Trivy Vulnerabilities JSON."""
15
+ results = [
16
+ {
17
+ "Target" : "php-app/composer.lock" ,
18
+ "Vulnerabilities" : None ,
19
+ },
20
+ {
21
+ "Target" : "trivy-ci-test (alpine 3.7.1)" ,
22
+ "Vulnerabilities" : [
23
+ {
24
+ "VulnerabilityID" : "CVE-2018-16840" ,
25
+ "PkgName" : "curl" ,
26
+ "InstalledVersion" : "7.61.0-r0" ,
27
+ "FixedVersion" : "7.61.1-r1" ,
28
+ "Title" : 'curl: Use-after-free when closing "easy" handle in Curl_close()' ,
29
+ "Description" : "A heap use-after-free flaw was found in curl versions from 7.59.0 through ..." ,
30
+ "Severity" : "HIGH" ,
31
+ "References" : [
32
+ "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-16840" ,
33
+ ],
34
+ },
35
+ {
36
+ "VulnerabilityID" : "CVE-2019-3822" ,
37
+ "PkgName" : "curl" ,
38
+ "InstalledVersion" : "7.61.1-r0" ,
39
+ "FixedVersion" : "7.61.2-r2" ,
40
+ "Title" : "curl: NTLMv2 type-3 header stack buffer overflow" ,
41
+ "Description" : "libcurl versions from 7.36.0 to before 7.64.0 are vulnerable to ..." ,
42
+ "Severity" : "MEDIUM" ,
43
+ "References" : [
44
+ "https://curl.haxx.se/docs/CVE-2019-3822.html" ,
45
+ "https://lists.apache.org/thread.html" ,
46
+ ],
47
+ },
48
+ ],
49
+ },
50
+ ]
51
+ if schema_version == 1 :
52
+ return results
53
+ return {"SchemaVersion" : 2 , "Results" : results }
54
+
55
+ def expected_entities (self ):
56
+ """Return the expected entities."""
57
+ return [
58
+ {
59
+ "key" : "CVE-2018-16840@curl@trivy-ci-test (alpine 3_7_1)" ,
60
+ "vulnerability_id" : "CVE-2018-16840" ,
61
+ "title" : 'curl: Use-after-free when closing "easy" handle in Curl_close()' ,
62
+ "description" : "A heap use-after-free flaw was found in curl versions from 7.59.0 through ..." ,
63
+ "level" : "HIGH" ,
64
+ "package_name" : "curl" ,
65
+ "installed_version" : "7.61.0-r0" ,
66
+ "fixed_version" : "7.61.1-r1" ,
67
+ "url" : "https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2018-16840" ,
68
+ },
69
+ {
70
+ "key" : "CVE-2019-3822@curl@trivy-ci-test (alpine 3_7_1)" ,
71
+ "vulnerability_id" : "CVE-2019-3822" ,
72
+ "title" : "curl: NTLMv2 type-3 header stack buffer overflow" ,
73
+ "description" : "libcurl versions from 7.36.0 to before 7.64.0 are vulnerable to ..." ,
74
+ "level" : "MEDIUM" ,
75
+ "package_name" : "curl" ,
76
+ "installed_version" : "7.61.1-r0" ,
77
+ "fixed_version" : "7.61.2-r2" ,
78
+ "url" : "https://curl.haxx.se/docs/CVE-2019-3822.html" ,
79
+ },
80
+ ]
75
81
76
82
async def test_warnings (self ):
77
83
"""Test the number of security warnings."""
78
- response = await self .collect (get_request_json_return_value = self .VULNERABILITIES_JSON )
79
- self .assert_measurement (response , value = "2" , entities = self .EXPECTED_ENTITIES )
84
+ for schema_version in self .SCHEMA_VERSIONS :
85
+ with self .subTest (schema_version = schema_version ):
86
+ response = await self .collect (get_request_json_return_value = self .vulnerabilities_json (schema_version ))
87
+ self .assert_measurement (response , value = "2" , entities = self .expected_entities ())
80
88
81
89
async def test_warning_levels (self ):
82
90
"""Test the number of security warnings when specifying a level."""
83
91
self .set_source_parameter ("levels" , ["high" , "critical" ])
84
- response = await self .collect (get_request_json_return_value = self .VULNERABILITIES_JSON )
85
- self .assert_measurement (response , value = "1" , entities = [self .EXPECTED_ENTITIES [0 ]])
92
+ for schema_version in self .SCHEMA_VERSIONS :
93
+ with self .subTest (schema_version = schema_version ):
94
+ response = await self .collect (get_request_json_return_value = self .vulnerabilities_json (schema_version ))
95
+ self .assert_measurement (response , value = "1" , entities = [self .expected_entities ()[0 ]])
0 commit comments