Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove wrong loop in matchData #785

Merged
merged 4 commits into from May 9, 2023
Merged

remove wrong loop in matchData #785

merged 4 commits into from May 9, 2023

Conversation

Hayak3
Copy link
Contributor

@Hayak3 Hayak3 commented May 4, 2023

the matchData is called from loop in AuditLog

@Hayak3 Hayak3 requested a review from a team as a code owner May 4, 2023 11:19
@codecov
Copy link

codecov bot commented May 4, 2023

Codecov Report

Patch coverage has no change and project coverage change: +0.03 🎉

Comparison is base (21dfb41) 81.78% compared to head (52bc889) 81.82%.

Additional details and impacted files
@@            Coverage Diff             @@
##           v3/dev     #785      +/-   ##
==========================================
+ Coverage   81.78%   81.82%   +0.03%     
==========================================
  Files         153      153              
  Lines        8204     8255      +51     
==========================================
+ Hits         6710     6755      +45     
- Misses       1274     1280       +6     
  Partials      220      220              
Flag Coverage Δ
default 78.07% <0.00%> (+0.03%) ⬆️
examples 25.82% <0.00%> (-0.13%) ⬇️
ftw 49.18% <0.00%> (+0.12%) ⬆️
ftw-multiphase 49.29% <0.00%> (+0.12%) ⬆️
tinygo 77.22% <0.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
internal/corazarules/rule_match.go 50.48% <0.00%> (-1.52%) ⬇️

... and 20 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Member

@M4tteoP M4tteoP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically, the input was not used and matchData iteration was performed again inside matchData() 🧐. Looks good to me, thank you!

I don't know the feasibility but would be great to add some tests with some "expected output" for audit logs

value = value[:200]
}
if mr.Rule_.Operator() != "" {
log.WriteString(fmt.Sprintf("Matched \"Operator %s matched %s at %s.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we need the first interpolation here @jptosso

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a bug, let's fix it by interpolating the operator

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW I'm wondering whether \" is needed here and below, it seems to be an unterminated quote

@jcchavezs jcchavezs requested review from anuraaga and jptosso May 5, 2023 11:37
@jcchavezs jcchavezs added the v3 label May 6, 2023
@@ -130,21 +130,19 @@ func (mr MatchedRule) details(matchData types.MatchData) string {

func (mr MatchedRule) matchData(matchData types.MatchData) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's a bit unrelated, but while we're here, can we have this function accept log *strings.Builder and pass it in from the caller instead of creating a new one?

value = value[:200]
}
if mr.Rule_.Operator() != "" {
log.WriteString(fmt.Sprintf("Matched \"Operator %s matched %s at %s.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a bug, let's fix it by interpolating the operator

@@ -130,21 +130,19 @@ func (mr MatchedRule) details(matchData types.MatchData) string {

func (mr MatchedRule) matchData(matchData types.MatchData) string {
log := &strings.Builder{}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's restructure this function to something like

op := mr.Rule_.Operator()
if op == "" {
  log.WriteString("Matched.")
  return
}
// Rest of logic

}
v := matchData.Variable().Name()
if matchData.Key() != "" {
v += fmt.Sprintf(":%s", matchData.Key())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to remove the intermediate strings, while it's more lines I guess it's still not so bad. We're basically expanding printf into code, which should be worth it for this very hot path function

log.WriteString("Matched Operator ")
log.WriteString(op)
log.WriteString(" matched ")
log.WriteString(value)
log.WriteString(" at ")
log.WriteString(matchData.Variable().Name())
if matchData.Key() != "" {
  log.WriteString(":")
  log.WriteString(matchData.Key())
}
log.WriteString(".")

value = value[:200]
}
if mr.Rule_.Operator() != "" {
log.WriteString(fmt.Sprintf("Matched \"Operator %s matched %s at %s.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW I'm wondering whether \" is needed here and below, it seems to be an unterminated quote

@anuraaga
Copy link
Contributor

anuraaga commented May 8, 2023

BTW if the improvements are too much we can merge the fix first and do them in a separate PR

@Hayak3 Hayak3 requested a review from anuraaga May 8, 2023 07:53
internal/corazarules/rule_match.go Show resolved Hide resolved
@jcchavezs jcchavezs merged commit dac4d8a into corazawaf:v3/dev May 9, 2023
8 of 9 checks passed
@jcchavezs
Copy link
Member

Thanks @Violet-E !

@Hayak3 Hayak3 deleted the Violet-E-patch-1 branch May 9, 2023 08:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants