Skip to content

Commit

Permalink
csgrep --limit-msg-len: limit length of event messages
Browse files Browse the repository at this point in the history
  • Loading branch information
lbossis committed Aug 16, 2023
1 parent f600411 commit 2d8f403
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/csgrep.cc
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ int main(int argc, char *argv[])
("embed-context,U", po::value<int>(), "embed a number of lines of context from the source file for the key event")
("prune-events", po::value<int>(), "event is preserved if its verbosity level is below the given number")
("warning-rate-limit", po::value<int>(), "stop processing a warning if the count of its occurrences exceeds the specified limit")
("limit-msg-len", po::value<int>(), "limit message length by a number provided")
("remove-duplicates,u", "remove defects that are not unique by their key event")
("set-scan-prop", po::value<TStringList>(), "NAME:VALUE pair to override the specified scan property")
("strip-path-prefix", po::value<string>(), "string prefix to strip from path (applied after all filters)")
Expand Down Expand Up @@ -661,6 +662,7 @@ int main(int argc, char *argv[])

if (!chainDecoratorIntArg<EventPrunner>(&eng, vm, "prune-events")
|| !chainDecoratorIntArg<RateLimitter>(&eng, vm, "warning-rate-limit")
|| !chainDecoratorIntArg<MsgTrimmer>(&eng, vm, "limit-msg-len")
|| !chainDecoratorIntArg<CtxEmbedder>(&eng, vm, "embed-context"))
// error message already printed, eng already feeed
return 1;
Expand Down
40 changes: 40 additions & 0 deletions src/lib/filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,43 @@ void RateLimitter::flush()
// forward the call through the chain of writers
AbstractFilter::flush();
}

// /////////////////////////////////////////////////////////////////////////////
// implementation of MsgTrimmer

void MsgTrimmer::handleDef(const Defect &defOrig)
{
// create a copy so that we can write to it
Defect def = defOrig;
unsigned cntTrimmed = 0;

// iterate over events
for (DefEvent &evt : def.events) {
if (evt.msg.size() <= maxMsgLen_)
// no trimming needed
continue;

// trim this message as requested
evt.msg.resize(maxMsgLen_);
evt.msg += " [...]";
++cntTrimmed;
}

if (cntTrimmed) {
// format a message about the message trimming
std::ostringstream noteMsg;
noteMsg << "trimmed " << cntTrimmed
<< " message(s) with length over " << maxMsgLen_;

// take location from the key event and construct a note message
DefEvent note = def.events[def.keyEventIdx];
note.event = "note";
note.verbosityLevel = /* note */ 1;
note.msg = noteMsg.str();

// append the note about message trimming
def.events.push_back(std::move(note));
}

agent_->handleDef(def);
}
15 changes: 15 additions & 0 deletions src/lib/filter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -177,4 +177,19 @@ class RateLimitter: public AbstractFilter {
std::unique_ptr<Private> d;
};

/// Trim messages to avoid excessively long output
class MsgTrimmer: public GenericAbstractFilter {
private:
size_t maxMsgLen_;

public:
MsgTrimmer(AbstractWriter *agent, const size_t maxMsgLen):
GenericAbstractFilter(agent),
maxMsgLen_(maxMsgLen)
{
}

void handleDef(const Defect &defOrig) override;
};

#endif /* H_GUARD_FILTER_H */
1 change: 1 addition & 0 deletions tests/csgrep/0113-gitleaks-limit-msg-len-args.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--mode=json --limit-msg-len=31
79 changes: 79 additions & 0 deletions tests/csgrep/0113-gitleaks-limit-msg-len-stdin.txt

Large diffs are not rendered by default.

119 changes: 119 additions & 0 deletions tests/csgrep/0113-gitleaks-limit-msg-len-stdout.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"defects": [
{
"checker": "GITLEAKS_WARNING",
"tool": "gitleaks",
"key_event_idx": 0,
"events": [
{
"file_name": "./contrib/guide/get_started/00-cleanup.sh",
"line": 6,
"column": 1,
"event": "warning[SC2164]",
"message": "-----BEGIN RSA PRIVATE KEY----- [...]",
"verbosity_level": 0
},
{
"file_name": "./contrib/guide/get_started/00-cleanup.sh",
"line": 6,
"column": 1,
"event": "note",
"message": "trimmed 1 message(s) with length over 31",
"verbosity_level": 1
}
]
},
{
"checker": "GITLEAKS_WARNING",
"tool": "gitleaks",
"key_event_idx": 0,
"events": [
{
"file_name": "./contrib/guide/get_started/12-datadir-create.sh",
"line": 4,
"column": 7,
"event": "warning[SC2174]",
"message": "-----BEGIN RSA PRIVATE KEY----- [...]",
"verbosity_level": 0
},
{
"file_name": "./contrib/guide/get_started/12-datadir-create.sh",
"line": 4,
"column": 7,
"event": "note",
"message": "trimmed 1 message(s) with length over 31",
"verbosity_level": 1
}
]
},
{
"checker": "GITLEAKS_WARNING",
"tool": "gitleaks",
"key_event_idx": 0,
"events": [
{
"file_name": "./contrib/scripts/resetgnome",
"line": 14,
"column": 13,
"event": "note[SC2006]",
"message": "-----BEGIN RSA PRIVATE KEY----- [...]",
"verbosity_level": 0
},
{
"file_name": "./contrib/scripts/resetgnome",
"line": 14,
"column": 13,
"event": "note",
"message": "trimmed 1 message(s) with length over 31",
"verbosity_level": 1
}
]
},
{
"checker": "GITLEAKS_WARNING",
"tool": "gitleaks",
"key_event_idx": 0,
"events": [
{
"file_name": "./contrib/scripts/resetgnome",
"line": 15,
"column": 25,
"event": "note[SC2086]",
"message": "-----BEGIN RSA PRIVATE KEY----- [...]",
"verbosity_level": 0
},
{
"file_name": "./contrib/scripts/resetgnome",
"line": 15,
"column": 25,
"event": "note",
"message": "trimmed 1 message(s) with length over 31",
"verbosity_level": 1
}
]
},
{
"checker": "GITLEAKS_WARNING",
"tool": "gitleaks",
"key_event_idx": 0,
"events": [
{
"file_name": "./contrib/scripts/resetgnome",
"line": 17,
"column": 10,
"event": "note[SC2006]",
"message": "-----BEGIN RSA PRIVATE KEY----- [...]",
"verbosity_level": 0
},
{
"file_name": "./contrib/scripts/resetgnome",
"line": 17,
"column": 10,
"event": "note",
"message": "trimmed 1 message(s) with length over 31",
"verbosity_level": 1
}
]
}
]
}
1 change: 1 addition & 0 deletions tests/csgrep/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,4 @@ test_csgrep("0109-shellcheck-sarif-cwe" )
test_csgrep("0110-warning-rate-limit" )
test_csgrep("0111-gcc-parser-ubsan-simple" )
test_csgrep("0112-gcc-parser-ubsan-bt" )
test_csgrep("0113-gitleaks-limit-msg-len" )

0 comments on commit 2d8f403

Please sign in to comment.