@@ -19,18 +19,25 @@ function Capitalize(str: string): string {
19
19
return modStr
20
20
}
21
21
22
+ function assert ( expr : unknown , msg ?: string ) : asserts expr {
23
+ if ( ! expr ) throw new Error ( msg )
24
+ }
25
+
22
26
type ODictByString = {
23
27
[ key : string ] : object
24
28
}
25
29
26
30
export class RlJsonReportProcessor {
31
+ debug : boolean
27
32
filename : string
28
33
data : ODictByString
29
34
30
35
name : string
31
36
purl : string
32
37
33
- assessments : ODictByString // report/metadata.assessments
38
+ metadata : ODictByString // report.metadata
39
+
40
+ assessments : ODictByString // report.metadata.assessments
34
41
violations : ODictByString // report.metadata.violations
35
42
components : ODictByString // report.metadata.components
36
43
vulnerabilities : ODictByString // report.metadata.vulnerabilities
@@ -39,26 +46,31 @@ export class RlJsonReportProcessor {
39
46
indent : string = ' '
40
47
out : string [ ]
41
48
42
- constructor ( filename : string ) {
43
- this . filename = filename
49
+ constructor ( filename : string , debug : boolean = false ) {
44
50
this . viols = [ ]
45
51
this . out = [ ]
52
+ this . debug = debug
53
+
54
+ this . filename = filename
46
55
this . data = JSON . parse ( fs . readFileSync ( this . filename , 'utf-8' ) )
47
56
48
57
this . name = this . jpath2string ( this . data , 'report.info.file.name' ) || '<no name>'
49
58
this . purl = this . jpath2string ( this . data , 'report.info.file.identity.purl' ) || '<no purl>'
50
-
51
- this . assessments = this . jpath2dict ( this . data , 'report.metadata.assessments' )
52
- this . violations = this . jpath2dict ( this . data , 'report.metadata.violations' )
53
- this . components = this . jpath2dict ( this . data , 'report.metadata.components' )
54
- this . vulnerabilities = this . jpath2dict ( this . data , 'report.metadata.vulnerabilities' )
59
+ console . log ( `# filePath: ${ this . filename } purl: ${ this . purl } ` )
60
+
61
+ this . metadata = this . jpath2dict ( this . data , 'report.metadata' )
62
+ this . assessments = this . jpath2dict ( this . metadata , 'assessments' )
63
+ assert ( this . assessments , 'has no data' )
64
+ this . violations = this . jpath2dict ( this . metadata , 'violations' )
65
+ this . components = this . jpath2dict ( this . metadata , 'components' )
66
+ this . vulnerabilities = this . jpath2dict ( this . metadata , 'vulnerabilities' )
55
67
}
56
68
57
69
jpath2string ( data : ODictByString , path_str : string ) : string {
58
70
const path_list : string [ ] = path_str . split ( '.' )
59
71
let z : ODictByString = data
60
72
for ( const item of path_list ) {
61
- z = data [ item ] as ODictByString // the last item is actually a string
73
+ z = z [ item ] as ODictByString // the last item is actually a string
62
74
}
63
75
const u = z as unknown
64
76
return u as string
@@ -68,7 +80,7 @@ export class RlJsonReportProcessor {
68
80
const path_list : string [ ] = path_str . split ( '.' )
69
81
let z : ODictByString = data
70
82
for ( const item of path_list ) {
71
- z = data [ item ] as ODictByString // the last item is actually a string
83
+ z = z [ item ] as ODictByString // the last item is actually a string[]
72
84
}
73
85
const u = z as unknown
74
86
return u as string [ ]
@@ -78,17 +90,18 @@ export class RlJsonReportProcessor {
78
90
const path_list : string [ ] = path_str . split ( '.' )
79
91
let z : ODictByString = data
80
92
for ( const item of path_list ) {
81
- z = data [ item ] as ODictByString // the last item is actually a string
93
+ z = z [ item ] as ODictByString // the last item is actually a number
82
94
}
83
95
const u = z as unknown
84
96
return u as number
85
97
}
86
98
87
99
jpath2dict ( data : ODictByString , path_str : string ) : ODictByString {
88
100
const path_list : string [ ] = path_str . split ( '.' )
101
+
89
102
let z : ODictByString = data
90
103
for ( const item of path_list ) {
91
- z = data [ item ] as ODictByString // the last item is actually a string
104
+ z = z [ item ] as ODictByString
92
105
}
93
106
return z
94
107
}
@@ -185,7 +198,7 @@ export class RlJsonReportProcessor {
185
198
const lines : string [ ] = [ ]
186
199
187
200
const url : string = `https://www.cve.org/CVERecord?id=${ cve } `
188
- const baseScore = this . jpath2number ( this . vulnerabilities , ' cve.cvss.baseScore' )
201
+ const baseScore = this . jpath2number ( this . vulnerabilities , ` ${ cve } .cvss.baseScore` )
189
202
let severity : string = this . cveSeverity ( baseScore )
190
203
severity = this . colorSeverity ( severity )
191
204
0 commit comments