forked from buzzcode/veracode-flaws-to-issues
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathpolicy.js
379 lines (303 loc) · 13.7 KB
/
policy.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//
// handle policy & sandbox scan flaws
//
const { request } = require('@octokit/request');
const label = require('./label');
const addVeracodeIssue = require('./issue').addVeracodeIssue;
const addVeracodeIssueComment = require('./issue_comment').addVeracodeIssueComment;
const core = require('@actions/core');
const fs = require('fs');
const path = require('path');
// sparse array, element = true if the flaw exists, undefined otherwise
var existingFlaws = [];
var existingFlawNumber = [];
var existingIssueState = [];
var pr_link
function createVeracodeFlawID(flaw) {
// [VID:FlawID]
return('[VID:' + flaw.issue_id + ']')
}
// given an Issue title, extract the FlawID string (for existing issues)
function getVeracodeFlawID(title) {
let start = title.indexOf('[VID');
if(start == -1) {
return null;
}
let end = title.indexOf(']', start);
return title.substring(start, end+1);
}
function parseVeracodeFlawID(vid) {
let parts = vid.split(':');
return ({
"prefix": parts[0],
"flawNum": parts[1].substring(0, parts[1].length - 1)
})
}
// get existing Veracode-entered issues, to avoid dups
async function getAllVeracodeIssues(options) {
const githubOwner = options.githubOwner;
const githubRepo = options.githubRepo;
const githubToken = options.githubToken;
var authToken = 'token ' + githubToken;
// when searching for issues, the label list is AND-ed (all requested labels must exist for the issue),
// so we need to loop through each severity level manually
for(const element of label.flawLabels) {
// get list of all flaws with the VeracodeFlaw label
console.log(`Getting list of existing \"${element.name}\" issues`);
let done = false;
let pageNum = 1;
let uriSeverity = encodeURIComponent(element.name);
let uriType = encodeURIComponent(label.otherLabels.find( val => val.id === 'policy').name);
let reqStr = `GET /repos/{owner}/{repo}/issues?labels=${uriSeverity},${uriType}&state=open&page={page}`
//let reqStr = `GET /repos/{owner}/{repo}/issues?labels=${uriName},${uriType}&state=open&page={page}&per_page={pageMax}`
while(!done) {
await request(reqStr, {
headers: {
authorization: authToken
},
owner: githubOwner,
repo: githubRepo,
page: pageNum,
//pageMax: 3
})
.then( result => {
console.log(`${result.data.length} flaw(s) found, (result code: ${result.status})`);
// walk findings and populate VeracodeFlaws map
result.data.forEach(element => {
let flawID = getVeracodeFlawID(element.title);
let issue_number = element.number
let issueState = element.state
// Map using VeracodeFlawID as index, for easy searching. Line # for simple flaw matching
if(flawID === null){
console.log(`Flaw \"${element.title}\" has no Veracode Flaw ID, ignored.`)
} else {
flawNum = parseVeracodeFlawID(flawID).flawNum;
existingFlaws[parseInt(flawNum)] = true;
existingFlawNumber[parseInt(flawNum)] = issue_number;
existingIssueState[parseInt(flawNum)] = issueState;
}
})
// check if we need to loop
// (if there is a link field in the headers, we have more than will fit into 1 query, so
// need to loop. On the last query we'll still have the link, but the data will be empty)
if( (result.headers.link !== undefined) && (result.data.length > 0)) {
pageNum += 1;
}
else
done = true;
})
.catch( error => {
throw new Error (`Error ${error.status} getting VeracodeFlaw issues: ${error.message}`);
});
}
}
}
function issueExists(vid) {
if(existingFlaws[parseInt(parseVeracodeFlawID(vid).flawNum)] === true)
return true;
else
return false;
}
function getIssueNumber(vid) {
return existingFlawNumber[parseInt(parseVeracodeFlawID(vid).flawNum)]
}
function getIssueState(vid) {
return existingIssueState[parseInt(parseVeracodeFlawID(vid).flawNum)]
}
async function processPolicyFlaws(options, flawData) {
const util = require('./util');
const waitTime = parseInt(options.waitTime);
// get a list of all open VeracodeSecurity issues in the repo
await getAllVeracodeIssues(options)
// walk through the list of flaws in the input file
console.log(`Processing input file: \"${options.resultsFile}\" with ${flawData._embedded.findings.length} flaws to process.`)
var index;
for( index=0; index < flawData._embedded.findings.length; index++) {
let flaw = flawData._embedded.findings[index];
let vid = createVeracodeFlawID(flaw);
let issue_number = getIssueNumber(vid)
let issueState = getIssueState(vid)
console.debug(`processing flaw ${flaw.issue_id}, VeracodeID: ${vid}`);
// check for mitigation
if(flaw.finding_status.resolution_status == 'APPROVED') {
console.log('Flaw mitigated, skipping import');
continue;
}
// check for duplicate
if(issueExists(vid)) {
console.log('Issue already exists, skipping import');
if ( options.debug == "true" ){
core.info('#### DEBUG START ####')
core.info('policy.js')
console.log("isPr?: "+options.isPR)
core.info('#### DEBUG END ####')
}
if ( options.isPR >= 1 && issueState == "open" ){
console.log('We are on a PR, need to link this issue to this PR')
pr_link = `Veracode issue link to PR: https://github.com/`+options.githubOwner+`/`+options.githubRepo+`/pull/`+options.pr_commentID
let issueComment = {
'issue_number': issue_number,
'pr_link': pr_link
};
await addVeracodeIssueComment(options, issueComment)
.catch( error => {
if(error instanceof util.ApiError) {
throw error;
} else {
//console.error(error.message);
throw error;
}
})
}
else{
console.log('GitHub issue is closed no need to update.')
}
continue;
}
// new auto rewrite path
// new autorewrite file path
function searchFile(dir, filename) {
//console.log('Inside search: Directory: '+dir+' - Filename: '+filename)
let result = null;
const files = fs.readdirSync(dir);
for (const file of files) {
if (file === '.git') continue;
const fullPath = path.join(dir, file);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
result = searchFile(fullPath, filename);
if (result) break;
} else if (file === filename) {
console.log('File found: '+fullPath)
result = fullPath;
break;
}
}
//console.log('Result: '+result)
return result;
}
// Search for the file starting from the current directory
var filename = flaw.finding_details.file_path
const currentDir = process.cwd();
console.log('Current Directory: ' + currentDir);
console.log('Filename: ' + filename);
const foundFilePath = searchFile(currentDir, path.basename(filename));
if (foundFilePath) {
//filepath = foundFilePath;
filepath = foundFilePath.replace(process.cwd(), '')
console.log('Adjusted Filepath: ' + filepath);
} else {
filepath = filename;
console.log('File not found in the current directory or its subdirectories.');
}
/* old rewrite path
//rewrite path
function replacePath (rewrite, path){
replaceValues = rewrite.split(":")
//console.log('Value 1:'+replaceValues[0]+' Value 2: '+replaceValues[1]+' old path: '+path)
newPath = path.replace(replaceValues[0],replaceValues[1])
//console.log('new Path:'+newPath)
return newPath
}
filename = flaw.finding_details.file_path
let filepath
console.log('File Path: '+filename+' before rewrite')
if (options.source_base_path_1 || options.source_base_path_2 || options.source_base_path_3){
orgPath1 = options.source_base_path_1.split(":")
orgPath2 = options.source_base_path_2.split(":")
orgPath3 = options.source_base_path_3.split(":")
console.log('path1: '+orgPath1[0]+' path2: '+orgPath2[0]+' path3: '+orgPath3[0])
if( filename.includes(orgPath1[0])) {
//console.log('file path1: '+filename)
filepath = replacePath(options.source_base_path_1, filename)
//console.log('Filepath rewrtie 1: '+filepath);
}
else if (filename.includes(orgPath2[0])){
//console.log('file path2: '+filename)
filepath = replacePath(options.source_base_path_2, filename)
//console.log('Filepath rewrite 2: '+filepath);
}
else if (filename.includes(orgPath3[0])){
//console.log('file path3: '+filename)
filepath = replacePath(options.source_base_path_3, filename)
//console.log('Filepath rewrite 3: '+filepath);
}
//console.log('Filepath end: '+filepath);
}
if ( filepath == undefined ){
filepath = filename
}
if ( filepath == "" ){
filepath = filename
}
old rewrite path */
linestart = eval(flaw.finding_details.file_line_number-5)
linened = eval(flaw.finding_details.file_line_number+5)
let commit_path = "https://github.com/"+options.githubOwner+"/"+options.githubRepo+"/blob/"+options.commit_hash+"/"+filepath+"#L"+linestart+"-L"+linened
//console.log('Full Path:'+commit_path)
// add to repo's Issues
// (in theory, we could do this w/o await-ing, but GitHub has rate throttling, so single-threading this helps)
let title = `${flaw.finding_details.cwe.name} ('${flaw.finding_details.finding_category.name}') ` + createVeracodeFlawID(flaw);
let lableBase = label.otherLabels.find( val => val.id === 'policy').name;
let severity = flaw.finding_details.severity;
if ( options.debug == "true" ){
core.info('#### DEBUG START ####')
core.info("policy.js")
console.log('isPr?: '+options.isPR)
core.info('#### DEBUG END ####')
}
if ( options.isPR >= 1 ){
pr_link = `Veracode issue link to PR: https://github.com/`+options.githubOwner+`/`+options.githubRepo+`/pull/`+options.pr_commentID
}
let bodyText = `${commit_path}`;
bodyText += `\n\n**Filename:** ${flaw.finding_details.file_name}`;
bodyText += `\n\n**Line:** ${flaw.finding_details.file_line_number}`;
bodyText += `\n\n**CWE:** ${flaw.finding_details.cwe.id} (${flaw.finding_details.cwe.name} ('${flaw.finding_details.finding_category.name}'))`;
bodyText += '\n\n' + decodeURI(flaw.description);
//console.log('bodyText: '+bodyText)
let issue = {
'flaw': {
'cwe': {
'id': flaw.finding_details.cwe.id,
'name': flaw.finding_details.cwe.name
},
'lineNumber': flaw.finding_details.file_line_number,
'file': flaw.finding_details.file_name
},
'title': title,
'label': lableBase,
'severity': severity,
'body': bodyText,
'pr_link': pr_link
};
console.log('Issue: '+JSON.stringify(issue))
await addVeracodeIssue(options, issue)
.catch( error => {
if(error instanceof util.ApiError) {
// TODO: fall back, retry this same issue, continue process
// for now, only 1 case - rate limit tripped
//console.warn('Rate limiter tripped. 30 second delay and time between issues increased by 2 seconds.');
// await sleep(30000);
// waitTime += 2;
// // retry this same issue again, bail out if this fails
// await addVeracodeIssue(options, flaw)
// .catch( error => {
// throw new Error(`Issue retry failed ${error.message}`);
// })
throw error;
} else {
//console.error(error.message);
throw error;
}
})
console.log('My Issue Nmbuer: '+addVeracodeIssue.issue_numnber)
// progress counter for large flaw counts
if( (index > 0) && (index % 25 == 0) )
console.log(`Processed ${index} flaws`)
// rate limiter, per GitHub: https://docs.github.com/en/rest/guides/best-practices-for-integrators
if(waitTime > 0)
await util.sleep(waitTime * 1000);
}
return index;
}
module.exports = { processPolicyFlaws }