File size: 8,698 Bytes
56b6519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
module.exports = function (app) {
  var Response = require('../lib/httpResponse.js');
  var acl = require('../lib/auth').acl;
  var Vulnerability = require('mongoose').model('Vulnerability');
  var VulnerabilityType = require('mongoose').model('VulnerabilityType');
  var VulnerabilityCategory = require('mongoose').model(
    'VulnerabilityCategory',
  );
  var VulnerabilityUpdate = require('mongoose').model('VulnerabilityUpdate');

  // Get vulnerabilities list
  app.get(
    '/api/vulnerabilities',
    acl.hasPermission('vulnerabilities:read'),
    function (req, res) {
      Vulnerability.getAll()
        .then(msg => Response.Ok(res, msg))
        .catch(err => Response.Internal(res, err));
    },
  );

  // Get vulnerabilities for export
  app.get(
    '/api/vulnerabilities/export',
    acl.hasPermission('vulnerabilities:read'),
    function (req, res) {
      Vulnerability.export()
        .then(msg => Response.Ok(res, msg))
        .catch(err => Response.Internal(res, err));
    },
  );

  // Create vulnerabilities (array of vulnerabilities)
  app.post(
    '/api/vulnerabilities',
    acl.hasPermission('vulnerabilities:create'),
    function (req, res) {
      for (var i = 0; i < req.body.length; i++) {
        var vuln = req.body[i];
        if (!vuln.details) {
          Response.BadParameters(
            res,
            'Required parameters: details.locale, details.title',
          );
          return;
        }
        var index = vuln.details.findIndex(
          obj =>
            obj.locale && obj.locale !== '' && obj.title && obj.title !== '',
        );
        if (index < 0) {
          Response.BadParameters(
            res,
            'Required parameters: details.locale, details.title',
          );
          return;
        }
      }

      var vulnerabilities = [];
      for (var i = 0; i < req.body.length; i++) {
        var vuln = {};
        vuln.cvssv3 = req.body[i].cvssv3 || null;
        if (req.body[i].priority) vuln.priority = req.body[i].priority;
        if (req.body[i].remediationComplexity)
          vuln.remediationComplexity = req.body[i].remediationComplexity;
        if (req.body[i].category) {
          vuln.category = req.body[i].category;
          VulnerabilityCategory.create({ name: vuln.category }).catch(e => {});
        }
        vuln.details = [];
        req.body[i].details.forEach(d => {
          if (!d.title || !d.locale)
            // Array of details may contain entries without title or locale but we don't want to save them
            return;
          var details = {};
          if (d.locale) details.locale = d.locale;
          if (d.title) details.title = d.title;
          if (d.vulnType) {
            details.vulnType = d.vulnType;
            VulnerabilityType.create({
              locale: d.locale,
              name: d.vulnType,
            }).catch(e => {});
          }
          if (d.description) details.description = d.description;
          if (d.observation) details.observation = d.observation;
          if (d.remediation) details.remediation = d.remediation;
          if (d.references) details.references = d.references;
          if (d.cwes) details.cwes = d.cwes;
          if (d.customFields) details.customFields = d.customFields;
          vuln.details.push(details);
        });
        vuln.status = 0;
        vulnerabilities.push(vuln);
      }
      Vulnerability.create(vulnerabilities)
        .then(msg => Response.Created(res, msg))
        .catch(err => Response.Internal(res, err));
    },
  );

  // Update vulnerability
  app.put(
    '/api/vulnerabilities/:vulnerabilityId',
    acl.hasPermission('vulnerabilities:update'),
    function (req, res) {
      if (!req.body.details) {
        Response.BadParameters(
          res,
          'Required parameters: details.locale, details.title',
        );
        return;
      }
      var index = req.body.details.findIndex(
        obj => obj.locale && obj.locale !== '' && obj.title && obj.title !== '',
      );
      if (index < 0) {
        Response.BadParameters(
          res,
          'Required parameters: details.locale, details.title',
        );
        return;
      }

      var vuln = {};
      if (req.body.cvssv3) vuln.cvssv3 = req.body.cvssv3;
      if (req.body.priority) vuln.priority = req.body.priority;
      if (req.body.remediationComplexity)
        vuln.remediationComplexity = req.body.remediationComplexity;
      vuln.category = req.body.category || null;
      vuln.details = [];
      req.body.details.forEach(d => {
        if (!d.title || !d.locale) return;
        var details = {};
        if (d.locale) details.locale = d.locale;
        if (d.title) details.title = d.title;
        if (d.vulnType) details.vulnType = d.vulnType;
        if (d.description) details.description = d.description;
        if (d.observation) details.observation = d.observation;
        if (d.remediation) details.remediation = d.remediation;
        if (d.cwes) details.cwes = d.cwes;
        if (d.references) details.references = d.references;
        if (d.customFields) details.customFields = d.customFields;
        vuln.details.push(details);
      });
      vuln.status = 0;

      Vulnerability.update(req.params.vulnerabilityId, vuln)
        .then(msg => {
          if (req.body.status === 2)
            VulnerabilityUpdate.deleteAllByVuln(req.params.vulnerabilityId);
          Response.Ok(res, msg);
        })
        .catch(err => Response.Internal(res, err));
    },
  );

  // Delete vulnerability
  app.delete(
    '/api/vulnerabilities/:vulnerabilityId',
    acl.hasPermission('vulnerabilities:delete'),
    function (req, res) {
      Vulnerability.delete(req.params.vulnerabilityId)
        .then(msg => Response.Ok(res, msg))
        .catch(err => Response.Internal(res, err));
    },
  );

  // Delete all vulnerabilities
  app.delete(
    '/api/vulnerabilities',
    acl.hasPermission('vulnerabilities:delete-all'),
    function (req, res) {
      Vulnerability.deleteAll()
        .then(msg => Response.Ok(res, msg))
        .catch(err => Response.Internal(res, err));
    },
  );

  // Get vulnerabilities list by language
  app.get(
    '/api/vulnerabilities/:locale',
    acl.hasPermission('vulnerabilities:read'),
    function (req, res) {
      Vulnerability.getAllByLanguage(req.params.locale)
        .then(msg => Response.Ok(res, msg))
        .catch(err => Response.Internal(res, err));
    },
  );

  // Create or Update vulnerability from finding for validation
  app.post(
    '/api/vulnerabilities/finding/:locale',
    acl.hasPermission('vulnerability-updates:create'),
    function (req, res) {
      if (!req.body.title) {
        Response.BadParameters(res, 'Required parameters: title');
        return;
      }

      var vuln = {};
      // Required params
      vuln.title = req.body.title;
      vuln.locale = req.params.locale;

      // Optional params
      vuln.cvssv3 = req.body.cvssv3 || '';
      vuln.priority = req.body.priority || null;
      vuln.remediationComplexity = req.body.remediationComplexity || null;
      vuln.references = req.body.references || [];
      vuln.cwes = req.body.cwes || [];
      vuln.vulnType = req.body.vulnType || null;
      vuln.description = req.body.description || null;
      vuln.observation = req.body.observation || null;
      vuln.remediation = req.body.remediation || null;
      vuln.category = req.body.category || null;
      vuln.customFields = req.body.customFields || [];

      VulnerabilityUpdate.create(req.decodedToken.username, vuln)
        .then(msg => {
          if (msg === 'Finding created as new Vulnerability')
            Response.Created(res, msg);
          else Response.Ok(res, msg);
        })
        .catch(err => Response.Internal(res, err));
    },
  );

  // Get vulnerability updates form vuln id
  app.get(
    '/api/vulnerabilities/updates/:vulnId',
    acl.hasPermission('vulnerabilities:update'),
    function (req, res) {
      VulnerabilityUpdate.getAllByVuln(req.params.vulnId)
        .then(msg => Response.Ok(res, msg))
        .catch(err => Response.Internal(res, err));
    },
  );

  // Merge vulnerability with locale part of another one
  app.put(
    '/api/vulnerabilities/merge/:vulnId',
    acl.hasPermission('vulnerabilities:update'),
    function (req, res) {
      if (!req.body.vulnId || !req.body.locale) {
        Response.BadParameters(res, 'Required parameters: vulnId, locale');
        return;
      }

      Vulnerability.Merge(req.params.vulnId, req.body.vulnId, req.body.locale)
        .then(() => Response.Ok(res, 'Vulnerability merge successfully'))
        .catch(err => Response.Internal(res, err));
    },
  );
};