Skip to content

Commit 2d8f403

Browse files
committed
csgrep --limit-msg-len: limit length of event messages
Fixes: #133 Closes: #134 Related: https://issues.redhat.com/browse/OSH-67
1 parent f600411 commit 2d8f403

7 files changed

+257
-0
lines changed

Diff for: src/csgrep.cc

+2
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ int main(int argc, char *argv[])
556556
("embed-context,U", po::value<int>(), "embed a number of lines of context from the source file for the key event")
557557
("prune-events", po::value<int>(), "event is preserved if its verbosity level is below the given number")
558558
("warning-rate-limit", po::value<int>(), "stop processing a warning if the count of its occurrences exceeds the specified limit")
559+
("limit-msg-len", po::value<int>(), "limit message length by a number provided")
559560
("remove-duplicates,u", "remove defects that are not unique by their key event")
560561
("set-scan-prop", po::value<TStringList>(), "NAME:VALUE pair to override the specified scan property")
561562
("strip-path-prefix", po::value<string>(), "string prefix to strip from path (applied after all filters)")
@@ -661,6 +662,7 @@ int main(int argc, char *argv[])
661662

662663
if (!chainDecoratorIntArg<EventPrunner>(&eng, vm, "prune-events")
663664
|| !chainDecoratorIntArg<RateLimitter>(&eng, vm, "warning-rate-limit")
665+
|| !chainDecoratorIntArg<MsgTrimmer>(&eng, vm, "limit-msg-len")
664666
|| !chainDecoratorIntArg<CtxEmbedder>(&eng, vm, "embed-context"))
665667
// error message already printed, eng already feeed
666668
return 1;

Diff for: src/lib/filter.cc

+40
Original file line numberDiff line numberDiff line change
@@ -296,3 +296,43 @@ void RateLimitter::flush()
296296
// forward the call through the chain of writers
297297
AbstractFilter::flush();
298298
}
299+
300+
// /////////////////////////////////////////////////////////////////////////////
301+
// implementation of MsgTrimmer
302+
303+
void MsgTrimmer::handleDef(const Defect &defOrig)
304+
{
305+
// create a copy so that we can write to it
306+
Defect def = defOrig;
307+
unsigned cntTrimmed = 0;
308+
309+
// iterate over events
310+
for (DefEvent &evt : def.events) {
311+
if (evt.msg.size() <= maxMsgLen_)
312+
// no trimming needed
313+
continue;
314+
315+
// trim this message as requested
316+
evt.msg.resize(maxMsgLen_);
317+
evt.msg += " [...]";
318+
++cntTrimmed;
319+
}
320+
321+
if (cntTrimmed) {
322+
// format a message about the message trimming
323+
std::ostringstream noteMsg;
324+
noteMsg << "trimmed " << cntTrimmed
325+
<< " message(s) with length over " << maxMsgLen_;
326+
327+
// take location from the key event and construct a note message
328+
DefEvent note = def.events[def.keyEventIdx];
329+
note.event = "note";
330+
note.verbosityLevel = /* note */ 1;
331+
note.msg = noteMsg.str();
332+
333+
// append the note about message trimming
334+
def.events.push_back(std::move(note));
335+
}
336+
337+
agent_->handleDef(def);
338+
}

Diff for: src/lib/filter.hh

+15
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,19 @@ class RateLimitter: public AbstractFilter {
177177
std::unique_ptr<Private> d;
178178
};
179179

180+
/// Trim messages to avoid excessively long output
181+
class MsgTrimmer: public GenericAbstractFilter {
182+
private:
183+
size_t maxMsgLen_;
184+
185+
public:
186+
MsgTrimmer(AbstractWriter *agent, const size_t maxMsgLen):
187+
GenericAbstractFilter(agent),
188+
maxMsgLen_(maxMsgLen)
189+
{
190+
}
191+
192+
void handleDef(const Defect &defOrig) override;
193+
};
194+
180195
#endif /* H_GUARD_FILTER_H */

Diff for: tests/csgrep/0113-gitleaks-limit-msg-len-args.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--mode=json --limit-msg-len=31

Diff for: tests/csgrep/0113-gitleaks-limit-msg-len-stdin.txt

+79
Large diffs are not rendered by default.

Diff for: tests/csgrep/0113-gitleaks-limit-msg-len-stdout.txt

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
{
2+
"defects": [
3+
{
4+
"checker": "GITLEAKS_WARNING",
5+
"tool": "gitleaks",
6+
"key_event_idx": 0,
7+
"events": [
8+
{
9+
"file_name": "./contrib/guide/get_started/00-cleanup.sh",
10+
"line": 6,
11+
"column": 1,
12+
"event": "warning[SC2164]",
13+
"message": "-----BEGIN RSA PRIVATE KEY----- [...]",
14+
"verbosity_level": 0
15+
},
16+
{
17+
"file_name": "./contrib/guide/get_started/00-cleanup.sh",
18+
"line": 6,
19+
"column": 1,
20+
"event": "note",
21+
"message": "trimmed 1 message(s) with length over 31",
22+
"verbosity_level": 1
23+
}
24+
]
25+
},
26+
{
27+
"checker": "GITLEAKS_WARNING",
28+
"tool": "gitleaks",
29+
"key_event_idx": 0,
30+
"events": [
31+
{
32+
"file_name": "./contrib/guide/get_started/12-datadir-create.sh",
33+
"line": 4,
34+
"column": 7,
35+
"event": "warning[SC2174]",
36+
"message": "-----BEGIN RSA PRIVATE KEY----- [...]",
37+
"verbosity_level": 0
38+
},
39+
{
40+
"file_name": "./contrib/guide/get_started/12-datadir-create.sh",
41+
"line": 4,
42+
"column": 7,
43+
"event": "note",
44+
"message": "trimmed 1 message(s) with length over 31",
45+
"verbosity_level": 1
46+
}
47+
]
48+
},
49+
{
50+
"checker": "GITLEAKS_WARNING",
51+
"tool": "gitleaks",
52+
"key_event_idx": 0,
53+
"events": [
54+
{
55+
"file_name": "./contrib/scripts/resetgnome",
56+
"line": 14,
57+
"column": 13,
58+
"event": "note[SC2006]",
59+
"message": "-----BEGIN RSA PRIVATE KEY----- [...]",
60+
"verbosity_level": 0
61+
},
62+
{
63+
"file_name": "./contrib/scripts/resetgnome",
64+
"line": 14,
65+
"column": 13,
66+
"event": "note",
67+
"message": "trimmed 1 message(s) with length over 31",
68+
"verbosity_level": 1
69+
}
70+
]
71+
},
72+
{
73+
"checker": "GITLEAKS_WARNING",
74+
"tool": "gitleaks",
75+
"key_event_idx": 0,
76+
"events": [
77+
{
78+
"file_name": "./contrib/scripts/resetgnome",
79+
"line": 15,
80+
"column": 25,
81+
"event": "note[SC2086]",
82+
"message": "-----BEGIN RSA PRIVATE KEY----- [...]",
83+
"verbosity_level": 0
84+
},
85+
{
86+
"file_name": "./contrib/scripts/resetgnome",
87+
"line": 15,
88+
"column": 25,
89+
"event": "note",
90+
"message": "trimmed 1 message(s) with length over 31",
91+
"verbosity_level": 1
92+
}
93+
]
94+
},
95+
{
96+
"checker": "GITLEAKS_WARNING",
97+
"tool": "gitleaks",
98+
"key_event_idx": 0,
99+
"events": [
100+
{
101+
"file_name": "./contrib/scripts/resetgnome",
102+
"line": 17,
103+
"column": 10,
104+
"event": "note[SC2006]",
105+
"message": "-----BEGIN RSA PRIVATE KEY----- [...]",
106+
"verbosity_level": 0
107+
},
108+
{
109+
"file_name": "./contrib/scripts/resetgnome",
110+
"line": 17,
111+
"column": 10,
112+
"event": "note",
113+
"message": "trimmed 1 message(s) with length over 31",
114+
"verbosity_level": 1
115+
}
116+
]
117+
}
118+
]
119+
}

Diff for: tests/csgrep/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,4 @@ test_csgrep("0109-shellcheck-sarif-cwe" )
156156
test_csgrep("0110-warning-rate-limit" )
157157
test_csgrep("0111-gcc-parser-ubsan-simple" )
158158
test_csgrep("0112-gcc-parser-ubsan-bt" )
159+
test_csgrep("0113-gitleaks-limit-msg-len" )

0 commit comments

Comments
 (0)