Skip to content

Commit daf2af1

Browse files
committed
json-writer: read tool, tool-version, and tool-url from scan props
... and fallback to csdiff as the tool only if the scan properties are not available.
1 parent 391dada commit daf2af1

File tree

2 files changed

+55
-9
lines changed

2 files changed

+55
-9
lines changed

src/json-writer.cc

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ void SimpleTreeEncoder::writeTo(std::ostream &str)
140140
// validation: https://sarifweb.azurewebsites.net/Validation
141141
class SarifTreeEncoder: public AbstractTreeEncoder {
142142
public:
143-
SarifTreeEncoder();
143+
SarifTreeEncoder() = default;
144144

145145
/// import supported scan properties
146146
void importScanProps(const TScanProps &) override;
@@ -152,6 +152,7 @@ class SarifTreeEncoder: public AbstractTreeEncoder {
152152
void writeTo(std::ostream &) override;
153153

154154
private:
155+
void initToolVersion();
155156
void serializeCweMap();
156157

157158
typedef std::map<std::string, int> TCweMap;
@@ -161,13 +162,57 @@ class SarifTreeEncoder: public AbstractTreeEncoder {
161162
PTree results_;
162163
};
163164

164-
SarifTreeEncoder::SarifTreeEncoder()
165+
void SarifTreeEncoder::initToolVersion()
165166
{
166-
// mandatory: tool/driver
167-
driver_.put<std::string>("name", "csdiff");
168-
driver_.put<std::string>("version", CS_VERSION);
169-
driver_.put<std::string>("informationUri",
170-
"https://github.com/csutils/csdiff");
167+
std::string tool;
168+
auto it = scanProps_.find("tool");
169+
if (scanProps_.end() != it)
170+
// read "tool" scan property
171+
tool = it->second;
172+
173+
std::string ver;
174+
it = scanProps_.find("tool-version");
175+
if (scanProps_.end() != it) {
176+
// read "tool-version" scan property
177+
ver = it->second;
178+
179+
if (tool.empty()) {
180+
// try to split the "{tool}-{version}" string by the last '-'
181+
const size_t lastDashAt = ver.rfind('-');
182+
if (std::string::npos != lastDashAt) {
183+
// read tool from the "{tool}-{version}" string
184+
tool = ver.substr(0, lastDashAt);
185+
186+
// remove "{tool}-" from "{tool}-{version}"
187+
ver.erase(0U, lastDashAt);
188+
}
189+
}
190+
else {
191+
// try to find "{tool}-" prefix in the "tool-version" scan property
192+
const std::string prefix = tool + "-";
193+
if (0U == ver.find(prefix))
194+
ver.erase(0U, prefix.size());
195+
}
196+
}
197+
198+
std::string uri;
199+
if (tool.empty()) {
200+
// unable to read tool name --> fallback to csdiff as the tool
201+
tool = "csdiff";
202+
ver = CS_VERSION;
203+
uri = "https://github.com/csutils/csdiff";
204+
}
205+
else if (scanProps_.end() != (it = scanProps_.find("tool-url")))
206+
// read "tool-url" scan property
207+
uri = it->second;
208+
209+
driver_.put<std::string>("name", tool);
210+
211+
if (!ver.empty())
212+
driver_.put<std::string>("version", ver);
213+
214+
if (!uri.empty())
215+
driver_.put<std::string>("informationUri", uri);
171216
}
172217

173218
void SarifTreeEncoder::serializeCweMap()
@@ -376,6 +421,8 @@ void SarifTreeEncoder::writeTo(std::ostream &str)
376421
root.put_child("inlineExternalProperties", propsList);
377422
}
378423

424+
this->initToolVersion();
425+
379426
if (!cweMap_.empty())
380427
// needs to run before we pick driver_
381428
this->serializeCweMap();

tests/csgrep/85-sarif-writer-stdout.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@
3333
{
3434
"tool": {
3535
"driver": {
36-
"name": "csdiff",
36+
"name": "csmock",
3737
"version": "",
38-
"informationUri": "https://github.com/csutils/csdiff",
3938
"rules": [
4039
{
4140
"id": "UNCAUGHT_EXCEPT: root_function",

0 commit comments

Comments
 (0)