Skip to content

Commit bb1634e

Browse files
committed
writer-json-sarif: add tool to rule properties
... to ease SARIF import into Defect Dojo Resolves: https://issues.redhat.com/browse/OSH-355 Closes: #147
1 parent 0bfa251 commit bb1634e

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

src/lib/writer-json-sarif.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ using namespace boost::json;
2828
struct RuleProps {
2929
int cweId;
3030
std::string scRuleId;
31+
std::string tool;
3132
};
3233

3334
struct SarifTreeEncoder::Private {
@@ -164,6 +165,12 @@ void SarifTreeEncoder::Private::serializeRules()
164165
const bool haveScRule = !rp.scRuleId.empty();
165166
if (haveScRule)
166167
sarifEncodeShellCheckRule(&rule, rp.scRuleId);
168+
else if (!rp.tool.empty()) {
169+
// encode tool tag
170+
array tags = { rp.tool };
171+
object &props = rule["properties"].as_object();
172+
props["tags"] = std::move(tags);
173+
}
167174

168175
if (rp.cweId)
169176
sarifEncodeCweRule(&rule, rp.cweId, /*append =*/ haveScRule);
@@ -316,6 +323,10 @@ void SarifTreeEncoder::appendDef(const Defect &def)
316323
result["properties"] = std::move(cweProp);
317324
}
318325

326+
if (!def.tool.empty())
327+
// update tool for this rule
328+
d->ruleMap[ruleId].tool = def.tool;
329+
319330
// key event severity level
320331
sarifEncodeLevel(&result, keyEvt.event);
321332

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
{
1313
"id": "ALLOC_FREE_MISMATCH: free",
1414
"properties": {
15+
"tags": [
16+
"coverity"
17+
],
1518
"cwe": [
1619
"CWE-762"
1720
]
@@ -23,6 +26,9 @@
2326
{
2427
"id": "CHECKED_RETURN: check_return",
2528
"properties": {
29+
"tags": [
30+
"coverity"
31+
],
2632
"cwe": [
2733
"CWE-252"
2834
]
@@ -34,6 +40,9 @@
3440
{
3541
"id": "CONSTANT_EXPRESSION_RESULT: pointless_expression",
3642
"properties": {
43+
"tags": [
44+
"coverity"
45+
],
3746
"cwe": [
3847
"CWE-569"
3948
]
@@ -45,6 +54,9 @@
4554
{
4655
"id": "CONSTANT_EXPRESSION_RESULT: result_independent_of_operands",
4756
"properties": {
57+
"tags": [
58+
"coverity"
59+
],
4860
"cwe": [
4961
"CWE-569"
5062
]
@@ -56,6 +68,9 @@
5668
{
5769
"id": "DEADCODE: dead_error_begin",
5870
"properties": {
71+
"tags": [
72+
"coverity"
73+
],
5974
"cwe": [
6075
"CWE-561"
6176
]
@@ -67,6 +82,9 @@
6782
{
6883
"id": "DEADCODE: dead_error_line",
6984
"properties": {
85+
"tags": [
86+
"coverity"
87+
],
7088
"cwe": [
7189
"CWE-561"
7290
]
@@ -78,6 +96,9 @@
7896
{
7997
"id": "DEADCODE: effectively_constant",
8098
"properties": {
99+
"tags": [
100+
"coverity"
101+
],
81102
"cwe": [
82103
"CWE-561"
83104
]
@@ -89,6 +110,9 @@
89110
{
90111
"id": "FORWARD_NULL: var_deref_model",
91112
"properties": {
113+
"tags": [
114+
"coverity"
115+
],
92116
"cwe": [
93117
"CWE-476"
94118
]
@@ -100,6 +124,9 @@
100124
{
101125
"id": "NEGATIVE_RETURNS: negative_returns",
102126
"properties": {
127+
"tags": [
128+
"coverity"
129+
],
103130
"cwe": [
104131
"CWE-687"
105132
]
@@ -111,6 +138,9 @@
111138
{
112139
"id": "OVERRUN: illegal_address",
113140
"properties": {
141+
"tags": [
142+
"coverity"
143+
],
114144
"cwe": [
115145
"CWE-119"
116146
]
@@ -122,6 +152,9 @@
122152
{
123153
"id": "OVERRUN: overrun-buffer-arg",
124154
"properties": {
155+
"tags": [
156+
"coverity"
157+
],
125158
"cwe": [
126159
"CWE-119"
127160
]
@@ -133,6 +166,9 @@
133166
{
134167
"id": "OVERRUN: overrun-local",
135168
"properties": {
169+
"tags": [
170+
"coverity"
171+
],
136172
"cwe": [
137173
"CWE-125"
138174
]
@@ -144,6 +180,9 @@
144180
{
145181
"id": "PRINTF_ARGS: invalid_type",
146182
"properties": {
183+
"tags": [
184+
"coverity"
185+
],
147186
"cwe": [
148187
"CWE-686"
149188
]
@@ -155,6 +194,9 @@
155194
{
156195
"id": "RESOURCE_LEAK: leaked_storage",
157196
"properties": {
197+
"tags": [
198+
"coverity"
199+
],
158200
"cwe": [
159201
"CWE-404"
160202
]
@@ -166,6 +208,9 @@
166208
{
167209
"id": "RESOURCE_LEAK: overwrite_var",
168210
"properties": {
211+
"tags": [
212+
"coverity"
213+
],
169214
"cwe": [
170215
"CWE-404"
171216
]
@@ -177,6 +222,9 @@
177222
{
178223
"id": "STRING_NULL: string_null",
179224
"properties": {
225+
"tags": [
226+
"coverity"
227+
],
180228
"cwe": [
181229
"CWE-170"
182230
]
@@ -188,6 +236,9 @@
188236
{
189237
"id": "STRING_OVERFLOW: fixed_size_dest",
190238
"properties": {
239+
"tags": [
240+
"coverity"
241+
],
191242
"cwe": [
192243
"CWE-120"
193244
]
@@ -199,6 +250,9 @@
199250
{
200251
"id": "TOCTOU: toctou",
201252
"properties": {
253+
"tags": [
254+
"coverity"
255+
],
202256
"cwe": [
203257
"CWE-367"
204258
]

tests/csgrep/0090-sarif-writer-illegal-utf8-sequence-stdout.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,17 @@
77
"driver": {
88
"name": "csdiff",
99
"version": "",
10-
"informationUri": "https://github.com/csutils/csdiff"
10+
"informationUri": "https://github.com/csutils/csdiff",
11+
"rules": [
12+
{
13+
"id": "COMPILER_WARNING: warning",
14+
"properties": {
15+
"tags": [
16+
"gcc"
17+
]
18+
}
19+
}
20+
]
1121
}
1222
},
1323
"results": [

0 commit comments

Comments
 (0)