Skip to content

Commit 8ebf66d

Browse files
committed
json-writer: write CWE IDs to SARIF if available
1 parent 3628f2e commit 8ebf66d

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed

src/json-writer.cc

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,22 +136,46 @@ class SarifTreeEncoder: public AbstractTreeEncoder {
136136
void writeTo(std::ostream &) override;
137137

138138
private:
139+
void serializeCweMap();
140+
141+
typedef std::map<std::string, int> TCweMap;
142+
TCweMap cweMap_;
139143
TScanProps scanProps_;
140-
PTree run0_;
144+
PTree driver_;
141145
PTree results_;
142146
};
143147

144148
SarifTreeEncoder::SarifTreeEncoder()
145149
{
146150
// mandatory: tool/driver
147-
PTree driver;
148-
driver.put<std::string>("name", "csdiff");
149-
driver.put<std::string>("version", CS_VERSION);
150-
driver.put<std::string>("informationUri",
151+
driver_.put<std::string>("name", "csdiff");
152+
driver_.put<std::string>("version", CS_VERSION);
153+
driver_.put<std::string>("informationUri",
151154
"https://github.com/csutils/csdiff");
152-
PTree tool;
153-
tool.put_child("driver", driver);
154-
run0_.put_child("tool", tool);
155+
}
156+
157+
void SarifTreeEncoder::serializeCweMap()
158+
{
159+
PTree ruleList;
160+
161+
for (const auto &item : cweMap_) {
162+
PTree rule;
163+
const auto &id = item.first;
164+
rule.put<std::string>("id", id);
165+
166+
PTree cweList;
167+
const auto cwe = item.second;
168+
const auto cweStr = "CWE-" + std::to_string(cwe);
169+
appendNode(&cweList, PTree(cweStr));
170+
171+
PTree props;
172+
props.put_child("cwe", cweList);
173+
rule.put_child("properties", props);
174+
175+
appendNode(&ruleList, rule);
176+
}
177+
178+
driver_.put_child("rules", ruleList);
155179
}
156180

157181
void SarifTreeEncoder::importScanProps(const TScanProps &scanProps)
@@ -228,6 +252,9 @@ void SarifTreeEncoder::appendDef(const Defect &def)
228252
// checker (FIXME: suboptimal mapping to SARIF)
229253
const std::string ruleId = def.checker + ": " + keyEvt.event;
230254
result.put<std::string>("ruleId", ruleId);
255+
if (def.cwe)
256+
// update CWE map
257+
cweMap_[ruleId] = def.cwe;
231258

232259
// key event location
233260
PTree loc;
@@ -285,13 +312,23 @@ void SarifTreeEncoder::writeTo(std::ostream &str)
285312
root.put_child("inlineExternalProperties", propsList);
286313
}
287314

315+
if (!cweMap_.empty())
316+
// needs to run before we pick driver_
317+
this->serializeCweMap();
318+
319+
PTree tool;
320+
tool.put_child("driver", driver_);
321+
322+
PTree run0;
323+
run0.put_child("tool", tool);
324+
288325
if (!results_.empty())
289326
// results
290-
run0_.put_child("results", results_);
327+
run0.put_child("results", results_);
291328

292329
// mandatory: runs
293330
PTree runs;
294-
appendNode(&runs, run0_);
331+
appendNode(&runs, run0);
295332
root.put_child("runs", runs);
296333

297334
// encode as JSON

0 commit comments

Comments
 (0)