Skip to content

Commit 8bbdc84

Browse files
committed
[trivy] Modify a method to validate trivy option
1 parent 57170cd commit 8bbdc84

File tree

3 files changed

+66
-36
lines changed

3 files changed

+66
-36
lines changed

dist/index.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6588,14 +6588,15 @@ function run() {
65886588
};
65896589
const downloader = new trivy_1.Downloader();
65906590
const trivyCmdPath = yield downloader.download(trivyVersion);
6591-
const result = trivy_1.Trivy.scan(trivyCmdPath, image, trivyOption);
6591+
const trivy = new trivy_1.Trivy();
6592+
const result = trivy.scan(trivyCmdPath, image, trivyOption);
65926593
if (!issueFlag) {
65936594
core.info(`Not create a issue because issue parameter is false.
65946595
Vulnerabilities:
65956596
${result}`);
65966597
return;
65976598
}
6598-
const issueContent = trivy_1.Trivy.parse(result);
6599+
const issueContent = trivy.parse(result);
65996600
if (issueContent === '') {
66006601
core.info('Vulnerabilities were not found.\nYour maintenance looks good 👍');
66016602
return;
@@ -13315,8 +13316,8 @@ Downloader.trivyRepository = {
1331513316
repo: 'trivy',
1331613317
};
1331713318
class Trivy {
13318-
static scan(trivyPath, image, option) {
13319-
Trivy.validateOption(option);
13319+
scan(trivyPath, image, option) {
13320+
this.validateOption(option);
1332013321
const args = [
1332113322
'--severity',
1332213323
option.severity,
@@ -13345,7 +13346,7 @@ class Trivy {
1334513346
erorr: ${result.error}
1334613347
`);
1334713348
}
13348-
static parse(vulnerabilities) {
13349+
parse(vulnerabilities) {
1334913350
let issueContent = '';
1335013351
for (const vuln of vulnerabilities) {
1335113352
if (vuln.Vulnerabilities === null)
@@ -13368,23 +13369,36 @@ class Trivy {
1336813369
}
1336913370
return issueContent;
1337013371
}
13371-
static validateOption(option) {
13372+
validateOption(option) {
13373+
this.validateSeverity(option.severity.split(','));
13374+
this.validateVulnType(option.vulnType.split(','));
13375+
}
13376+
validateSeverity(severities) {
1337213377
const allowedSeverities = /UNKNOWN|LOW|MEDIUM|HIGH|CRITICAL/;
13373-
const allowedVulnTypes = /os|library/;
13374-
for (const severity of option.severity.split(',')) {
13375-
if (!allowedSeverities.test(severity)) {
13376-
throw new Error(`severity option error: ${severity} is unknown severity`);
13377-
}
13378+
if (!validateArrayOption(allowedSeverities, severities)) {
13379+
throw new Error(`Trivy option error: ${severities.join(',')} is unknown severity.
13380+
Trivy supports UNKNOWN, LOW, MEDIUM, HIGH and CRITICAL.`);
1337813381
}
13379-
for (const vulnType of option.vulnType.split(',')) {
13380-
if (!allowedVulnTypes.test(vulnType)) {
13381-
throw new Error(`vuln-type option error: ${vulnType} is unknown vuln-type`);
13382-
}
13382+
return true;
13383+
}
13384+
validateVulnType(vulnTypes) {
13385+
const allowedVulnTypes = /os|library/;
13386+
if (!validateArrayOption(allowedVulnTypes, vulnTypes)) {
13387+
throw new Error(`Trivy option error: ${vulnTypes.join(',')} is unknown vuln-type.
13388+
Trivy supports os and library.`);
1338313389
}
1338413390
return true;
1338513391
}
1338613392
}
1338713393
exports.Trivy = Trivy;
13394+
function validateArrayOption(allowedValue, options) {
13395+
for (const option of options) {
13396+
if (!allowedValue.test(option)) {
13397+
return false;
13398+
}
13399+
}
13400+
return true;
13401+
}
1338813402

1338913403

1339013404
/***/ }),

src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ async function run() {
3030

3131
const downloader = new Downloader();
3232
const trivyCmdPath: string = await downloader.download(trivyVersion);
33-
const result: Vulnerability[] | string = Trivy.scan(
33+
34+
const trivy = new Trivy();
35+
const result: Vulnerability[] | string = trivy.scan(
3436
trivyCmdPath,
3537
image,
3638
trivyOption
@@ -45,7 +47,7 @@ async function run() {
4547
return;
4648
}
4749

48-
const issueContent: string = Trivy.parse(result as Vulnerability[]);
50+
const issueContent: string = trivy.parse(result as Vulnerability[]);
4951

5052
if (issueContent === '') {
5153
core.info(

src/trivy.ts

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import fetch, { Response } from 'node-fetch';
66
import { spawnSync, SpawnSyncReturns } from 'child_process';
77

88
import { TrivyOption, Vulnerability } from './interface';
9+
import { defaultCoreCipherList } from 'constants';
910

1011
interface Repository {
1112
owner: string;
@@ -119,12 +120,12 @@ export class Downloader {
119120
}
120121

121122
export class Trivy {
122-
static scan(
123+
public scan(
123124
trivyPath: string,
124125
image: string,
125126
option: TrivyOption
126127
): Vulnerability[] | string {
127-
Trivy.validateOption(option);
128+
this.validateOption(option);
128129

129130
const args: string[] = [
130131
'--severity',
@@ -159,7 +160,7 @@ export class Trivy {
159160
`);
160161
}
161162

162-
static parse(vulnerabilities: Vulnerability[]): string {
163+
public parse(vulnerabilities: Vulnerability[]): string {
163164
let issueContent: string = '';
164165

165166
for (const vuln of vulnerabilities) {
@@ -187,26 +188,39 @@ export class Trivy {
187188
return issueContent;
188189
}
189190

190-
static validateOption(option: TrivyOption): boolean {
191-
const allowedSeverities = /UNKNOWN|LOW|MEDIUM|HIGH|CRITICAL/;
192-
const allowedVulnTypes = /os|library/;
191+
private validateOption(option: TrivyOption): void {
192+
this.validateSeverity(option.severity.split(','));
193+
this.validateVulnType(option.vulnType.split(','));
194+
}
193195

194-
for (const severity of option.severity.split(',')) {
195-
if (!allowedSeverities.test(severity)) {
196-
throw new Error(
197-
`severity option error: ${severity} is unknown severity`
198-
);
199-
}
196+
private validateSeverity(severities: string[]): boolean {
197+
const allowedSeverities = /UNKNOWN|LOW|MEDIUM|HIGH|CRITICAL/;
198+
if (!validateArrayOption(allowedSeverities, severities)) {
199+
throw new Error(
200+
`Trivy option error: ${severities.join(',')} is unknown severity.
201+
Trivy supports UNKNOWN, LOW, MEDIUM, HIGH and CRITICAL.`
202+
);
200203
}
204+
return true;
205+
}
201206

202-
for (const vulnType of option.vulnType.split(',')) {
203-
if (!allowedVulnTypes.test(vulnType)) {
204-
throw new Error(
205-
`vuln-type option error: ${vulnType} is unknown vuln-type`
206-
);
207-
}
207+
private validateVulnType(vulnTypes: string[]): boolean {
208+
const allowedVulnTypes = /os|library/;
209+
if (!validateArrayOption(allowedVulnTypes, vulnTypes)) {
210+
throw new Error(
211+
`Trivy option error: ${vulnTypes.join(',')} is unknown vuln-type.
212+
Trivy supports os and library.`
213+
);
208214
}
209-
210215
return true;
211216
}
212217
}
218+
219+
function validateArrayOption(allowedValue: RegExp, options: string[]): boolean {
220+
for (const option of options) {
221+
if (!allowedValue.test(option)) {
222+
return false;
223+
}
224+
}
225+
return true;
226+
}

0 commit comments

Comments
 (0)