-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
183 lines (150 loc) · 4.1 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env node
import axios from 'axios'
import minimist from 'minimist'
import { v4 as uuidv4 } from 'uuid'
const BB_API_URL = 'https://api.bitbucket.org/2.0/repositories'
const argv = minimist(process.argv.slice(2));
const BB_USER = argv['user']
const BB_APP_PASSWORD = argv['password']
const REPO = argv['repo']
const COMMIT = argv['commit']
const WORKSPACE = argv['workspace']
const paramsAreValid = () => {
if (BB_USER == null) {
console.log('Error: specify user')
return false
}
if (BB_APP_PASSWORD == null) {
console.log('Error: specify password')
return false
}
if (REPO == null) {
console.log('Error: specify repo')
return false
}
if (COMMIT == null) {
console.log('Error: specify commit')
return false
}
if (WORKSPACE == null) {
console.log('Error: specify workspace')
return false
}
return true
}
const rulesAsMap = (sarifRules) => {
return sarifRules.reduce((map, rule) => ({ ...map, [rule['id']]: rule}), {})
}
const getPath = (sarifResult) => {
return sarifResult['locations'][0]['physicalLocation']['artifactLocation']['uri']
}
const getLine = (sarifResult) => {
const region = sarifResult['locations'][0]['physicalLocation']['region']
if (region['endLine'] != null) {
return region['endLine']
}
return region['startLine']
}
const getSummary = (sarifResult, rulesMap) => {
const ruleId = sarifResult['ruleId']
const rule = rulesMap[ruleId]
if (rule['fullDescription'] != null) {
return rule['fullDescription']['text']
}
if (rule['shortDescription'] != null) {
return rule['shortDescription']['text']
}
}
const mapSarif = (sarif) => {
const severityMap = {
'note': 'LOW',
'warning': 'MEDIUM',
'error': 'HIGH'
}
const rulesMap = rulesAsMap(sarif['runs'][0]['tool']['driver']['rules'])
return sarif['runs'][0]['results']
.map(result => {
return {
external_id: uuidv4(),
annotation_type: "VULNERABILITY",
severity: severityMap[result['level']],
path: getPath(result),
line: getLine(result),
summary: getSummary(result, rulesMap),
details: result['message']['text']
}
})
}
const getScanType = (sarif) => {
const scanName = sarif['runs'][0]['tool']['driver']['name']
return {
id: scanName.replace(/\s+/g, "").toLowerCase(),
title: scanName,
name: scanName,
mapper: mapSarif,
count: sarif['runs'][0]['results'].length
}
}
const sarifToBitBucket = async (sarifRawOutput) => {
const sarifResult = JSON.parse(sarifRawOutput);
const scanType = getScanType(sarifResult);
let vulns = scanType.mapper(sarifResult)
let details = `This repository contains ${scanType['count']} ${scanType['name']} vulnerabilities`
if (vulns.length > 100) {
vulns = vulns.slice(0, 100)
details = `${details} (first 100 vulnerabilities shown)`
}
// 1. Delete Existing Report
await axios.delete(`${BB_API_URL}/${WORKSPACE}/${REPO}/commit/${COMMIT}/reports/${scanType['id']}`,
{
auth: {
username: BB_USER,
password: BB_APP_PASSWORD
}
}
)
// 2. Create Report
await axios.put(
`${BB_API_URL}/${WORKSPACE}/${REPO}/commit/${COMMIT}/reports/${scanType['id']}`,
{
title: scanType['title'],
details: details,
report_type: "SECURITY",
reporter: "sarif-to-bitbucket",
result: "PASSED"
},
{
auth: {
username: BB_USER,
password: BB_APP_PASSWORD
}
}
)
// 3. Upload Annotations (Vulnerabilities)
await axios.post(`${BB_API_URL}/${WORKSPACE}/${REPO}/commit/${COMMIT}/reports/${scanType['id']}/annotations`,
vulns,
{
auth: {
username: BB_USER,
password: BB_APP_PASSWORD
}
}
)
}
const getInput = () => {
return new Promise((resolve, reject) => {
const stdin = process.stdin;
let data = '';
stdin.setEncoding('utf8');
stdin.on('data', function (chunk) {
data += chunk;
});
stdin.on('end', function () {
resolve(data);
});
stdin.on('error', reject);
});
}
if (paramsAreValid()) {
getInput().then(sarifToBitBucket).catch(console.error)
}