@@ -26,15 +26,15 @@ export class Downloader {
26
26
27
27
public async download (
28
28
version : string ,
29
- trivyCmdDir : string = __dirname ,
29
+ trivyCmdDir : string = __dirname
30
30
) : Promise < string > {
31
31
const os : string = this . checkPlatform ( process . platform ) ;
32
32
const downloadUrl : string = await this . getDownloadUrl ( version , os ) ;
33
33
console . debug ( `Download URL: ${ downloadUrl } ` ) ;
34
34
const trivyCmdBaseDir : string = process . env . GITHUB_WORKSPACE || trivyCmdDir ;
35
35
const trivyCmdPath : string = await this . downloadTrivyCmd (
36
36
downloadUrl ,
37
- trivyCmdBaseDir ,
37
+ trivyCmdBaseDir
38
38
) ;
39
39
console . debug ( `Trivy Command Path: ${ trivyCmdPath } ` ) ;
40
40
return trivyCmdPath ;
@@ -89,7 +89,7 @@ export class Downloader {
89
89
90
90
private async downloadTrivyCmd (
91
91
downloadUrl : string ,
92
- savedPath : string = '.' ,
92
+ savedPath : string = '.'
93
93
) : Promise < string > {
94
94
const response : Response = await fetch ( downloadUrl ) ;
95
95
@@ -122,20 +122,22 @@ export class Trivy {
122
122
static scan (
123
123
trivyPath : string ,
124
124
image : string ,
125
- options : TrivyOption ,
125
+ option : TrivyOption
126
126
) : Vulnerability [ ] {
127
+ Trivy . validateOption ( option ) ;
128
+
127
129
const args : string [ ] = [
128
130
'--severity' ,
129
- options . severity ,
131
+ option . severity ,
130
132
'--vuln-type' ,
131
- options . vulnType ,
133
+ option . vulnType ,
132
134
'--format' ,
133
135
'json' ,
134
136
'--quiet' ,
135
137
'--no-progress' ,
136
138
] ;
137
139
138
- if ( options . ignoreUnfixed ) {
140
+ if ( option . ignoreUnfixed ) {
139
141
args . push ( '--ignore-unfixed' ) ;
140
142
}
141
143
@@ -145,7 +147,10 @@ export class Trivy {
145
147
} ) ;
146
148
147
149
if ( result . stdout && result . stdout . length > 0 ) {
148
- return JSON . parse ( result . stdout ) ;
150
+ const vulnerabilities : Vulnerability [ ] = JSON . parse ( result . stdout ) ;
151
+ if ( vulnerabilities . length > 0 ) {
152
+ return vulnerabilities ;
153
+ }
149
154
}
150
155
151
156
throw new Error ( `Failed vulnerability scan using Trivy.
@@ -183,4 +188,27 @@ export class Trivy {
183
188
console . debug ( issueContent ) ;
184
189
return issueContent ;
185
190
}
191
+
192
+ static validateOption ( option : TrivyOption ) : boolean {
193
+ const allowedSeverities = / U N K N O W N | L O W | M E D I U M | H I G H | C R I T I C A L / ;
194
+ const allowedVulnTypes = / o s | l i b r a r y / ;
195
+
196
+ for ( const severity of option . severity . split ( ',' ) ) {
197
+ if ( ! allowedSeverities . test ( severity ) ) {
198
+ throw new Error (
199
+ `severity option error: ${ severity } is unknown severity`
200
+ ) ;
201
+ }
202
+ }
203
+
204
+ for ( const vulnType of option . vulnType . split ( ',' ) ) {
205
+ if ( ! allowedVulnTypes . test ( vulnType ) ) {
206
+ throw new Error (
207
+ `vuln-type option error: ${ vulnType } is unknown vuln-type`
208
+ ) ;
209
+ }
210
+ }
211
+
212
+ return true ;
213
+ }
186
214
}
0 commit comments