|
1 |
| -const Joi = require("joi"); |
| 1 | +const Joi = require('joi'); |
| 2 | +const program = require('commander'); |
| 3 | +const fs = require('fs'); |
| 4 | + |
| 5 | +program |
| 6 | + .option('-s, --stdin', 'Read from stdin') |
| 7 | + .option('-f, --file [filePath]', 'Read from file') |
| 8 | + .parse(process.argv); |
2 | 9 |
|
3 |
| -let data = { |
4 |
| - engine: { |
5 |
| - name: "guardrails-engine-javascript", |
6 |
| - version: "1.11.0" |
7 |
| - }, |
8 |
| - language: "javascript", |
9 |
| - type: "mixed", |
10 |
| - status: "success", |
11 |
| - executionTime: 3, |
12 |
| - issues: 12, |
13 |
| - errors: null, |
14 |
| - output: [ |
15 |
| - { |
16 |
| - type: "issue", |
17 |
| - process: { |
18 |
| - name: "eslint", |
19 |
| - version: "^4.19.1" |
20 |
| - }, |
21 |
| - rule: "@guardrails/guardrails/detect-unsafe-regex", |
22 |
| - description: "[GR:0001:stable] Unsafe Regular Expression", |
23 |
| - location: { |
24 |
| - path: "/src/GR0001.js", |
25 |
| - positions: { |
26 |
| - begin: { |
27 |
| - line: 8, |
28 |
| - column: 19 |
29 |
| - }, |
30 |
| - end: { |
31 |
| - line: 8, |
32 |
| - column: 19 |
33 |
| - } |
34 |
| - } |
35 |
| - } |
36 |
| - }, |
37 |
| - { |
38 |
| - id: 566, |
39 |
| - updated_at: "2018-05-08T14:27:01.549Z", |
40 |
| - created_at: "2018-02-15T16:45:53.321Z", |
41 |
| - publish_date: "2018-02-15T16:59:37.240Z", |
42 |
| - recommendation: "Update to version 4.2.1, 5.0.3 or later.", |
43 |
| - cvss_vector: "CVSS:3.0/AV:L/AC:L/PR:N/UI:N/S:U/C:N/I:N/A:L", |
44 |
| - cvss_score: 4, |
45 |
| - module: "hoek", |
46 |
| - version: "5.0.0", |
47 |
| - vulnerable_versions: "<= 4.2.0 || >= 5.0.0 < 5.0.3", |
48 |
| - patched_versions: "> 4.2.0 < 5.0.0 || >= 5.0.3", |
49 |
| - title: "Prototype pollution attack", |
50 |
| - path: ["guardrails-test-javascript@1.0.0", "hoek@5.0.0"], |
51 |
| - advisory: "https://nodesecurity.io/advisories/566", |
52 |
| - type: "advisory", |
53 |
| - process: { |
54 |
| - name: "nsp", |
55 |
| - version: "^3.2.1" |
56 |
| - } |
57 |
| - } |
58 |
| - ] |
59 |
| -}; |
60 | 10 | const processSchema = Joi.object().keys({
|
61 | 11 | name: Joi.string().required(),
|
62 | 12 | version: Joi.string().required()
|
@@ -108,40 +58,62 @@ const baseSchema = Joi.object().keys({
|
108 | 58 | })
|
109 | 59 | .required(),
|
110 | 60 | language: Joi.string()
|
111 |
| - .valid("javascript", "python", "mixed") |
| 61 | + .valid('javascript', 'python', 'mixed') |
112 | 62 | .required(),
|
113 | 63 | type: Joi.string().required(),
|
114 | 64 | status: Joi.string()
|
115 |
| - .valid("success", "failure") |
| 65 | + .valid('success', 'failure') |
116 | 66 | .required(),
|
117 | 67 | executionTime: Joi.number().required(),
|
118 | 68 | issues: Joi.number().required(),
|
119 | 69 | errors: [Joi.array(), null],
|
120 | 70 | output: Joi.array().required()
|
121 | 71 | });
|
122 | 72 |
|
123 |
| -// ========== Validate Outer structure: |
124 |
| -Joi.validate(data, baseSchema, (err, value) => { |
125 |
| - if (err) { |
126 |
| - console.log(err); |
127 |
| - } else { |
128 |
| - console.log("------------------> No Error outer"); |
| 73 | +let reportData = { output: [] }; |
| 74 | + |
| 75 | +function readFromStdin() { |
| 76 | + return readFromFile('/dev/stdin'); |
| 77 | +} |
| 78 | + |
| 79 | +function readFromFile(filePath) { |
| 80 | + try { |
| 81 | + let data = fs.readFileSync(filePath).toString(); |
| 82 | + return JSON.parse(data); |
| 83 | + } catch (err) { |
| 84 | + console.log(err.message); |
| 85 | + process.exit(1); |
129 | 86 | }
|
130 |
| -}); |
| 87 | +} |
| 88 | + |
| 89 | +if (program.stdin) { |
| 90 | + reportData = readFromStdin(); |
| 91 | +} else if (program.file) { |
| 92 | + reportData = readFromFile(program.file); |
| 93 | +} |
131 | 94 |
|
132 |
| -// ========== Validate Output |
133 |
| -Joi.validate(data.output[0], sourceCodeSchema, (err, value) => { |
| 95 | +// ========== Validate Outer structure: |
| 96 | +Joi.validate(reportData, baseSchema, (err, value) => { |
134 | 97 | if (err) {
|
135 | 98 | console.log(err);
|
136 | 99 | } else {
|
137 |
| - console.log("------------------> No Error code"); |
| 100 | + console.log('envelope ✅'); |
138 | 101 | }
|
139 | 102 | });
|
140 | 103 |
|
141 |
| -Joi.validate(data.output[1], dependenciesSchema, (err, value) => { |
142 |
| - if (err) { |
143 |
| - console.log(err); |
144 |
| - } else { |
145 |
| - console.log("------------------> No Error"); |
| 104 | +reportData.output.forEach(lineItem => { |
| 105 | + let schema = Joi.object(); |
| 106 | + if (lineItem.type === 'issue' || lineItem.type === 'sourcecode') { |
| 107 | + schema = sourceCodeSchema; |
| 108 | + } else if (lineItem.type === 'advisory') { |
| 109 | + schema = dependenciesSchema; |
| 110 | + } else if (lineItem.type === 'secrets') { |
146 | 111 | }
|
| 112 | + Joi.validate(lineItem, schema, (err, value) => { |
| 113 | + if (err) { |
| 114 | + console.log(err); |
| 115 | + } else { |
| 116 | + console.log(lineItem.type + ' ✅'); |
| 117 | + } |
| 118 | + }); |
147 | 119 | });
|
0 commit comments