Skip to content

Commit e79ba88

Browse files
committed
[trivy] Add a method to validate trivy option
1 parent f3e9097 commit e79ba88

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

src/trivy.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ export class Downloader {
2626

2727
public async download(
2828
version: string,
29-
trivyCmdDir: string = __dirname,
29+
trivyCmdDir: string = __dirname
3030
): Promise<string> {
3131
const os: string = this.checkPlatform(process.platform);
3232
const downloadUrl: string = await this.getDownloadUrl(version, os);
3333
console.debug(`Download URL: ${downloadUrl}`);
3434
const trivyCmdBaseDir: string = process.env.GITHUB_WORKSPACE || trivyCmdDir;
3535
const trivyCmdPath: string = await this.downloadTrivyCmd(
3636
downloadUrl,
37-
trivyCmdBaseDir,
37+
trivyCmdBaseDir
3838
);
3939
console.debug(`Trivy Command Path: ${trivyCmdPath}`);
4040
return trivyCmdPath;
@@ -89,7 +89,7 @@ export class Downloader {
8989

9090
private async downloadTrivyCmd(
9191
downloadUrl: string,
92-
savedPath: string = '.',
92+
savedPath: string = '.'
9393
): Promise<string> {
9494
const response: Response = await fetch(downloadUrl);
9595

@@ -122,20 +122,22 @@ export class Trivy {
122122
static scan(
123123
trivyPath: string,
124124
image: string,
125-
options: TrivyOption,
125+
option: TrivyOption
126126
): Vulnerability[] {
127+
Trivy.validateOption(option);
128+
127129
const args: string[] = [
128130
'--severity',
129-
options.severity,
131+
option.severity,
130132
'--vuln-type',
131-
options.vulnType,
133+
option.vulnType,
132134
'--format',
133135
'json',
134136
'--quiet',
135137
'--no-progress',
136138
];
137139

138-
if (options.ignoreUnfixed) {
140+
if (option.ignoreUnfixed) {
139141
args.push('--ignore-unfixed');
140142
}
141143

@@ -145,7 +147,10 @@ export class Trivy {
145147
});
146148

147149
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+
}
149154
}
150155

151156
throw new Error(`Failed vulnerability scan using Trivy.
@@ -183,4 +188,27 @@ export class Trivy {
183188
console.debug(issueContent);
184189
return issueContent;
185190
}
191+
192+
static validateOption(option: TrivyOption): boolean {
193+
const allowedSeverities = /UNKNOWN|LOW|MEDIUM|HIGH|CRITICAL/;
194+
const allowedVulnTypes = /os|library/;
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+
}
186214
}

0 commit comments

Comments
 (0)