-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathparser-json-sarif.cc
450 lines (373 loc) · 13.3 KB
/
parser-json-sarif.cc
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
* Copyright (C) 2012-2022 Red Hat, Inc.
*
* This file is part of csdiff.
*
* csdiff is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* csdiff is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with csdiff. If not, see <http://www.gnu.org/licenses/>.
*/
#include "parser-json-sarif.hh"
#include "parser-common.hh" // for ImpliedAttrDigger
#include "regex.hh"
#include <boost/algorithm/string/predicate.hpp>
struct SarifTreeDecoder::Private {
void updateCweMap(const pt::ptree *driverNode);
void readToolInfo(TScanProps *pScanProps, const pt::ptree *toolNode);
std::string singleChecker = "UNKNOWN_SARIF_WARNING";
std::string pwd;
const RE reFileUrl = RE("^file://");
const RE reCwe = RE("^CWE-([0-9]+)$");
const RE reVersion = RE("^([0-9][0-9.]+).*$");
const RE reRuleId =
RE("(" RE_CHECKER_NAME "): (" RE_EVENT ")");
typedef std::map<std::string, int> TCweMap;
TCweMap cweMap;
ImpliedAttrDigger digger;
};
SarifTreeDecoder::SarifTreeDecoder():
d(new Private)
{
}
SarifTreeDecoder::~SarifTreeDecoder() = default;
void SarifTreeDecoder::Private::updateCweMap(const pt::ptree *driverNode)
{
const pt::ptree *rules;
if (!findChildOf(&rules, *driverNode, "rules"))
return;
for (const auto &item : *rules) {
const pt::ptree &rule = item.second;
const auto id = valueOf<std::string>(rule, "id");
if (id.empty())
// rule ID missing
continue;
const pt::ptree *props;
if (!findChildOf(&props, rule, "properties"))
// properties missing
continue;
const pt::ptree *cweList;
if (!findChildOf(&cweList, *props, "cwe") || cweList->empty())
// cwe list missing
continue;
const std::string cweStr = cweList->begin()->second.data();
boost::smatch sm;
if (!boost::regex_match(cweStr, sm, this->reCwe))
// unable to parse cwe
continue;
const int cwe = std::stoi(sm[/* cwe */ 1]);
this->cweMap[id] = cwe;
}
}
void SarifTreeDecoder::Private::readToolInfo(
TScanProps *pScanProps,
const pt::ptree *toolNode)
{
const pt::ptree *driverNode;
if (!findChildOf(&driverNode, *toolNode, "driver"))
return;
this->updateCweMap(driverNode);
const auto name = valueOf<std::string>(*driverNode, "name");
auto version = valueOf<std::string>(*driverNode, "version");
if (version.empty())
version = valueOf<std::string>(*driverNode, "semanticVersion");
if (name == "SnykCode") {
// Snyk Code detected!
this->singleChecker = "SNYK_CODE_WARNING";
if (!version.empty())
// record tool version of Snyk Code
(*pScanProps)["analyzer-version-snyk-code"] = std::move(version);
}
else if (name == "gitleaks") {
// gitleaks
this->singleChecker = "GITLEAKS_WARNING";
if (!version.empty())
(*pScanProps)["analyzer-version-gitleaks"] = std::move(version);
}
else if (name == "Semgrep OSS") {
// semgrep
this->singleChecker = "SEMGREP_WARNING";
if (!version.empty())
(*pScanProps)["analyzer-version-semgrep"] = std::move(version);
}
else if (boost::starts_with(name, "GNU C")) {
// GCC
this->singleChecker = "COMPILER_WARNING";
boost::smatch sm;
if (boost::regex_match(version, sm, this->reVersion))
(*pScanProps)["analyzer-version-gcc"] = sm[/* version */ 1];
}
}
void SarifTreeDecoder::readScanProps(
TScanProps *pDst,
const pt::ptree *root)
{
// read external properties if available
const pt::ptree *iep;
if (findChildOf(&iep, *root, "inlineExternalProperties")
&& (1U == iep->size()))
{
const pt::ptree *props;
if (findChildOf(&props, iep->begin()->second, "externalizedProperties"))
for (const pt::ptree::value_type &item : *props)
(*pDst)[item.first] = item.second.data();
}
// check that we have exactly one run
const pt::ptree *runs;
if (!findChildOf(&runs, *root, "runs")
|| /* TODO: warn bout unsupported format */ (1U != runs->size()))
return;
// jump to the only run
const pt::ptree &run0 = runs->begin()->second;
// check which tool was used for the run
const pt::ptree *toolNode;
if (findChildOf(&toolNode, run0, "tool"))
d->readToolInfo(pDst, toolNode);
// read PWD so that we can reconstruct absolute paths later on
const pt::ptree *uriBase, *pwdNode, *uriNode;
if (findChildOf(&uriBase, run0, "originalUriBaseIds")
&& findChildOf(&pwdNode, *uriBase, "PWD")
&& findChildOf(&uriNode, *pwdNode, "uri"))
{
// remove the "file://" prefix
const auto &pwd = uriNode->data();
d->pwd = boost::regex_replace(pwd, d->reFileUrl, "");
// FIXME: Should we check whether d->pwd begins with '/'?
// make sure that d->pwd ends with '/'
if (!d->pwd.empty() && *d->pwd.rbegin() != '/')
d->pwd += '/';
}
}
void SarifTreeDecoder::readRoot(const pt::ptree *runs)
{
if (1U != runs->size())
// exactly one run expected
return;
if (!findChildOf(&defList_, runs->begin()->second, "results"))
// no results found
return;
// initialize the iteration over "results"
defIter_ = defList_->begin();
}
static void sarifReadLocation(DefEvent *pEvt, const pt::ptree &loc)
{
const pt::ptree *pl;
if (!findChildOf(&pl, loc, "physicalLocation"))
// unknown location info format
return;
const pt::ptree *al;
if (findChildOf(&al, *pl, "artifactLocation")) {
const auto uri = valueOf<std::string>(*al, "uri");
if (!uri.empty())
// read file name
pEvt->fileName = std::move(uri);
}
const pt::ptree *reg;
if (findChildOf(®, *pl, "region")) {
// read line
if ((pEvt->line = valueOf<int>(*reg, "startLine"))) {
const int endLine = valueOf<int>(*reg, "endLine");
pEvt->vSize = diffNums(pEvt->line, endLine);
}
// read column
if ((pEvt->column = valueOf<int>(*reg, "startColumn"))) {
const int endColumn = valueOf<int>(*reg, "endColumn");
pEvt->hSize = diffNums(pEvt->column, endColumn);
}
}
}
static bool sarifReadMsg(std::string *pDst, const pt::ptree &node)
{
const pt::ptree *msgNode;
if (!findChildOf(&msgNode, node, "message"))
return false;
*pDst = valueOf<std::string>(*msgNode, "text", "<unknown>");
return true;
}
static void sarifReadComments(Defect *pDef, const pt::ptree &relatedLocs)
{
for (const auto &item : relatedLocs) {
const pt::ptree &loc = item.second;
DefEvent tmp;
sarifReadLocation(&tmp, loc);
if (!tmp.fileName.empty())
// location info available --> not a csdiff-encoded comment
continue;
DefEvent evt("#");
if (!sarifReadMsg(&evt.msg, loc))
continue;
evt.verbosityLevel = 1;
pDef->events.push_back(std::move(evt));
}
}
static void sarifReadCodeFlow(Defect *pDef, const pt::ptree &cf)
{
const pt::ptree *tf;
if ((1U != cf.size())
|| !findChildOf(&tf, cf.begin()->second, "threadFlows"))
return;
const pt::ptree *locs;
if (1U != tf->size()
|| !findChildOf(&locs, tf->begin()->second, "locations"))
return;
TEvtList events;
int keyEventIdx = -1;
// read the full list of events
for (const auto &item : *locs) {
const pt::ptree &tfLoc = item.second;
std::string evtName;
const pt::ptree *kindList;
if (findChildOf(&kindList, tfLoc, "kinds")) {
// calculate event name from the `kinds` list
for (const auto &kindItem : *kindList) {
const pt::ptree &kind = kindItem.second;
if (!evtName.empty())
evtName += "_";
evtName += kind.data();
}
}
// append a new event of the specified kind
events.push_back(DefEvent(evtName));
DefEvent &evt = events.back();
// read/infer verbosity level
evt.verbosityLevel = valueOf<int>(tfLoc, "nestingLevel",
(evt.event.empty())
? /* trace */ 2
: /* info */ 1);
if (!evt.verbosityLevel)
// update key event
keyEventIdx = events.size() - 1U;
const pt::ptree *loc;
if (!findChildOf(&loc, tfLoc, "location"))
// location info missing
return;
sarifReadLocation(&evt, *loc);
sarifReadMsg(&evt.msg, *loc);
if (evt.event.empty()) {
// if no `kind` is given, assume a generic trace event
evt.event = "path";
if (evt.msg.empty())
evt.msg = "generic trace event";
}
}
if (events.size() <= 1U)
// we failed to read more than one event
return;
if (-1 == keyEventIdx) {
// no key event in threadFlows
std::copy(events.begin(), events.end(),
std::back_inserter(pDef->events));
}
else {
// use only the events from threadFlows
events.swap(pDef->events);
pDef->keyEventIdx = keyEventIdx;
}
}
static int sarifCweFromDefNode(const pt::ptree &defNode)
{
const pt::ptree *taxa;
if (!findChildOf(&taxa, defNode, "taxa"))
return 0;
for (const auto &item : *taxa) {
const pt::ptree &t = item.second;
const pt::ptree *tc;
if (!findChildOf(&tc, t, "toolComponent"))
continue;
if (valueOf<std::string>(*tc, "name") == "cwe")
return valueOf<int>(t, "id");
}
// not found
return 0;
}
static void expandRelativePaths(Defect *pDef, const std::string &pwd)
{
if (pwd.empty())
// no PWD info provided
return;
// go through all events
for (DefEvent &evt : pDef->events) {
std::string &fileName = evt.fileName;
if (fileName.empty())
// no file path to expand
continue;
const unsigned char beginsWith = *fileName.begin();
switch (beginsWith) {
case '/': // absolute path
case '<': // <unknown> and the like
continue;
default:
// prepend `pwd` to relative path
fileName = pwd + fileName;
}
}
}
bool SarifTreeDecoder::readNode(Defect *def)
{
// move the iterator after we get the current position
const pt::ptree *pNode = this->nextNode();
if (!pNode)
// failed initialization or EOF
return false;
const pt::ptree &defNode = *pNode;
// initialize the defect structure
*def = Defect(d->singleChecker);
// read "level" if available and propagate "error" to the "imp" flag
const auto level = valueOf<std::string>(defNode, "level", "warning");
if (level == "error")
def->imp = 1;
// initialize the key event
def->events.push_back(DefEvent(level));
DefEvent &keyEvent = def->events.back();
// read "rule" that triggered the report
const auto rule = valueOf<std::string>(defNode, "ruleId");
if (!rule.empty()) {
boost::smatch sm;
if (boost::regex_match(rule, sm, d->reRuleId)) {
// csdiff format
def->checker = sm[/* checker */ 1];
keyEvent.event = sm[/* keyEvent */ 2];
}
else {
// output of a single tool
keyEvent.event += "[" + rule + "]";
// distinguish GCC compiler/analyzer
if (def->checker == "COMPILER_WARNING"
&& boost::starts_with(rule, "-Wanalyzer-"))
def->checker = "GCC_ANALYZER_WARNING";
}
}
def->cwe = sarifCweFromDefNode(defNode);
if (!def->cwe) {
// lookup cwe
const auto it = d->cweMap.find(rule);
if (d->cweMap.end() != it)
def->cwe = it->second;
}
// read location and diagnostic message
keyEvent.fileName = "<unknown>";
const pt::ptree *locs;
if (findChildOf(&locs, defNode, "locations") && !locs->empty())
sarifReadLocation(&keyEvent, locs->begin()->second);
sarifReadMsg(&keyEvent.msg, defNode);
// read code flow if available
const pt::ptree *cf;
if (findChildOf(&cf, defNode, "codeFlows"))
sarifReadCodeFlow(def, *cf);
// read comments if available
const pt::ptree *relatedLocs;
if (findChildOf(&relatedLocs, defNode, "relatedLocations"))
sarifReadComments(def, *relatedLocs);
expandRelativePaths(def, d->pwd);
d->digger.inferLangFromChecker(def);
d->digger.inferToolFromChecker(def);
return true;
}