-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
167 lines (152 loc) · 4.73 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const Joi = require("joi");
const program = require("commander");
const fs = require("fs");
const pkg = require("./package.json");
/* arguments parsing */
program
.version(pkg.version)
.option("-f, --file [filePath]", "Read from file")
.option("-s, --stdin", "Read from stdin")
.parse(process.argv);
/* schemas */
const processSchema = Joi.object().keys({
name: Joi.string().required(),
version: Joi.string().required()
});
const lineitemSchema = Joi.object().keys({
type: Joi.string().required().valid("sast", "sca", "secret", "dast"),
ruleId: Joi.string().required(),
location: Joi.alternatives().conditional("type", {
is: "dast",
then: Joi.array().min(1).items(Joi.object().keys({
path: Joi.string().uri({ scheme: ["http", "https"] }).required(),
method: Joi.string().valid("GET", "POST", "PUT", "DELETE").required(),
evidence: Joi.string(),
param: Joi.string(),
attack: Joi.string()
})),
otherwise: Joi.object().keys({
path: Joi.string()
.regex(/^(?!\/opt\/mount\/|.\/|\/).*/)
.required(),
positions: Joi.object().keys({
begin: Joi.object()
.keys({
line: Joi.number(),
column: Joi.number().optional()
})
.required(),
end: Joi.object()
.keys({
line: Joi.number(),
column: Joi.number().optional()
})
.optional()
})
})
}),
metadata: Joi.object().required()
});
const envelopeSchema = Joi.object().keys({
engine: Joi.object()
.keys({
name: Joi.string()
.regex(/guardrails\-engine\-\w+?\-\w+/)
.required(),
version: Joi.string().required()
})
.required(),
language: Joi.string()
.valid(
"javascript",
"python",
"ruby",
"go",
"solidity",
"general",
"php",
"java",
"dotnet",
"elixir",
"c",
"rust",
"terraform",
"typescript",
"apex",
"mobile"
)
.required(),
status: Joi.string().valid("success", "error", "unsuccessful").required(),
executionTime: Joi.number().required(),
issues: Joi.number().required(),
errors: [Joi.array(), null],
output: Joi.array().required(),
rawOutput: [Joi.string().required(), Joi.object().required()],
process: processSchema.required(),
sutUrl: Joi.string().uri({ scheme: ["http", "https"] })
});
const metadataSchemaSAST = Joi.object()
.keys({
lineContent: Joi.string().required(),
confidence: Joi.string().optional(),
severity: Joi.string().optional(),
description: Joi.string().optional(),
title: Joi.string().optional(),
cweID: Joi.string().optional(),
references: Joi.array().optional()
})
.options({ stripUnknown: true });
const metadataSchemaSCA = Joi.object()
.keys({
cweID: Joi.string().optional(),
cve: Joi.object().optional(),
title: Joi.string().optional(),
vulnerableVersions: Joi.string().allow("").optional(),
patchedVersions: Joi.string().allow("").optional(),
currentVersion: Joi.string().optional(),
references: Joi.array().optional(),
severity: Joi.string().optional(),
dependencyName: Joi.string().optional()
})
.options({ stripUnknown: true });
const metadataSchemaDAST = Joi.object()
.keys({
riskcode: Joi.number().integer().min(0).max(3).required(),
confidence: Joi.number().integer().min(0).max(3).required(),
desc: Joi.string().required(),
solution: Joi.string().required(),
otherinfo: Joi.string().optional(),
reference: Joi.string().optional(),
cweid: Joi.number().integer().positive().optional(),
wascid: Joi.number().integer().positive().optional()
})
.options({ stripUnknown: true });
/* data loading */
function readFromStdin() {
return readFromFile("/dev/stdin");
}
function readFromFile(filePath) {
try {
let data = fs.readFileSync(filePath).toString();
return JSON.parse(data);
} catch (err) {
console.log(err.message);
process.exit(1);
}
}
const reportData = {
stdin: readFromStdin,
file: readFromFile
}[(program.opts().stdin && 'stdin') || (program.opts().file && 'file')](program.opts().file);
if (!reportData) {
console.log("No data was supplied to validate. Run `-h` for help.");
process.exit(1);
}
/* validating the envelope structure */
console.log(envelopeSchema.validate(reportData).error ?? "envelope ✅");
/* validating the line items */
reportData.output.forEach((lineItem) => {
console.log(lineitemSchema.validate(lineItem).error ?? lineItem.type + " ✅");
const metadataSchema = { sast: metadataSchemaSAST, secret: metadataSchemaSAST, sca: metadataSchemaSCA, dast: metadataSchemaDAST }[lineItem.type];
console.log(metadataSchema.validate(lineItem.metadata).error ?? lineItem.type + " metadata ✅");
});