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

fix: Logs print different messages for each the disruptive actions #827

Merged
merged 11 commits into from
Aug 22, 2023

Conversation

M4tteoP
Copy link
Member

@M4tteoP M4tteoP commented Jun 26, 2023

Initial report: corazawaf/coraza-proxy-wasm#209

This PR proposes to add different messages for each disruptive action, following the same logic used by ModSecurity v2 ( See here).

Currently, we have two Error log prefixes based on the Disruptive_ field of the matched rules that we are logging:

	if mr.Disruptive_ {
		fmt.Fprintf(log, "Coraza: Access denied (phase %d). ", mr.Rule_.Phase())
	} else {
		log.WriteString("Coraza: Warning. ")
	}

Considering that the Disruptive_ field itself is kind of misleading (actions like deny, allow, and pass are all considered disruptive actions), some logs ends up being misleading.
For example, a log from the 980170 rule, which has a pass action, starts with Access denied:

Coraza: Access denied (phase 5). Anomaly Scores: (Inbound Scores: blocking=3, detection=3, per_pl=3-0-0-0, threshold=5) - (Outbound Scores: blocking=0, detection=0, per_pl=0-0-0-0, threshold=4) - (SQLI=0, XSS=0, RFI=0, LFI=0, RCE=0,  [file "@owasp_crs/RESPONSE-980-CORRELATION.conf"] [line "12628"] [id "980170"] [rev ""] [msg "Anomaly Scores: (Inbound Scores: blocking=3, detection=3, per_pl=3-0-0-0, threshold=5) - (Outbound Scores: blocking=0, detection=0, per_pl=0-0-0-0, threshold=4) - (SQLI=0, XSS=0, RFI=0, LFI=0, RCE=0, "] [data ""] [severity "emergency"] [ver "OWASP_CRS/4.0.0-rc1"] [maturity "0"] [accuracy "0"] [tag "reporting"] [hostname "10.110.32.70"] [uri "/metrics"] [unique_id "KjkRGNlRpxeKJftwrWP"]

@codecov
Copy link

codecov bot commented Jun 26, 2023

Codecov Report

Patch coverage: 92.00% and project coverage change: +0.04% 🎉

Comparison is base (5d3b707) 81.51% compared to head (f848261) 81.56%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #827      +/-   ##
==========================================
+ Coverage   81.51%   81.56%   +0.04%     
==========================================
  Files         160      160              
  Lines        9033     9051      +18     
==========================================
+ Hits         7363     7382      +19     
+ Misses       1418     1417       -1     
  Partials      252      252              
Flag Coverage Δ
default 76.65% <91.66%> (+0.05%) ⬆️
examples 25.51% <40.00%> (+0.01%) ⬆️
ftw 46.67% <4.16%> (-0.12%) ⬇️
ftw-multiphase 48.78% <4.16%> (-0.12%) ⬇️
tinygo 74.83% <91.66%> (+0.06%) ⬆️

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

Files Changed Coverage Δ
internal/corazawaf/body_buffer.go 74.52% <0.00%> (ø)
internal/corazarules/rule_match.go 65.44% <93.75%> (+4.15%) ⬆️
examples/http-server/main.go 70.27% <100.00%> (ø)
internal/corazawaf/transaction.go 78.61% <100.00%> (+0.08%) ⬆️

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@M4tteoP M4tteoP marked this pull request as ready for review July 7, 2023 10:53
@M4tteoP M4tteoP requested a review from a team as a code owner July 7, 2023 10:53
@jcchavezs
Copy link
Member

What is missing here?

@M4tteoP
Copy link
Member Author

M4tteoP commented Jul 11, 2023

What is missing here?

I moved it to ready for review after adding a proper PR description. There is nothing missing from my side

@@ -229,6 +231,22 @@ func (mr MatchedRule) ErrorLog() string {
}
}

log.WriteString("\n")
Copy link
Member Author

Choose a reason for hiding this comment

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

Removing this line should fix corazawaf/coraza-caddy#87 and supersede corazawaf/coraza-caddy#93. Now writing the new line is delegated to the ErrorLog() caller.

return log.String()
}

func writeDisruptiveActionSpecificLog(log *strings.Builder, mr MatchedRule) {
switch mr.DisruptiveActionName_ {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe for another PR but we should migrate this to an enum instead of string for faster matching

Copy link
Member

Choose a reason for hiding this comment

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

Could you please take care of that before merge, @M4tteoP?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks Rag, I tried to address it. A better way might be propagating the Action enum from the parser mapping into Action functions, but it requires quite a broad refactor. Did you have in mind something like what I just implemented?

@jcchavezs
Copy link
Member

Are you happy with the change @anuraaga ?

@jcchavezs jcchavezs merged commit 9104d7c into corazawaf:main Aug 22, 2023
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants