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

experimental: UseRequest/ResponseBody methods #1212

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

M4tteoP
Copy link
Member

@M4tteoP M4tteoP commented Nov 12, 2024

Exposing as experimental two new methods to provide the bodies to Coraza. They are meant to be used when the entire request body is available, as it avoids the need for an extra copy into the body buffers.

Benchmark:

goos: darwin
goarch: arm64
pkg: github.com/corazawaf/coraza/v3/internal/corazawaf
BenchmarkUseRequestBody/WriteRequestBody-10         	    1383	    912582 ns/op	10544513 B/op	     214 allocs/op
BenchmarkUseRequestBody/UseRequestBody-10           	   63420	     17178 ns/op	   50075 B/op	     212 allocs/op
PASS
ok  	github.com/corazawaf/coraza/v3/internal/corazawaf	3.559s

Usage:

tx := waf.NewTransaction().(corazaexperimental.Transaction)
body := []byte("body") // Retrieved from the connector or somewhere
tx.UseResponseBody(body)

Usage:

@M4tteoP M4tteoP marked this pull request as ready for review November 13, 2024 00:12
@M4tteoP M4tteoP requested a review from a team as a code owner November 13, 2024 00:12
@jptosso
Copy link
Member

jptosso commented Nov 13, 2024

My only concern is maintaining a pointer that can be flushed at any point.
I'm not sure if telling the user to avoid using b slice is enough

@M4tteoP
Copy link
Member Author

M4tteoP commented Nov 13, 2024

My only concern is maintaining a pointer that can be flushed at any point.
I'm not sure if telling the user to avoid using b slice is enough

Hey, I elaborated on the description a bit more. However, this feature fundamentally relies on this approach, so I would assume that anyone using it is intentionally willing to let Coraza the ownership of that pointer

@jcchavezs
Copy link
Member

jcchavezs commented Nov 14, 2024

I am missing the use cases where this can be used. In regular go code I think it is risky as mutations can happen, in other environments I think is more acceptable. I would not merge this until we show solid code using it.

Copy link

codecov bot commented Dec 3, 2024

Codecov Report

Attention: Patch coverage is 85.36585% with 12 lines in your changes missing coverage. Please review.

Project coverage is 81.69%. Comparing base (70f1746) to head (4bcf23c).

Files with missing lines Patch % Lines
internal/corazawaf/body_buffer.go 45.45% 4 Missing and 2 partials ⚠️
internal/corazawaf/transaction.go 91.54% 4 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1212      +/-   ##
==========================================
+ Coverage   81.66%   81.69%   +0.03%     
==========================================
  Files         168      168              
  Lines        9655     9737      +82     
==========================================
+ Hits         7885     7955      +70     
- Misses       1519     1527       +8     
- Partials      251      255       +4     
Flag Coverage Δ
coraza.rule.case_sensitive_args_keys 81.65% <85.36%> (+0.03%) ⬆️
coraza.rule.multiphase_valuation 81.69% <85.36%> (+0.03%) ⬆️
coraza.rule.no_regex_multiline 81.63% <85.36%> (+0.03%) ⬆️
default 81.69% <85.36%> (+0.03%) ⬆️
examples+ 16.30% <0.00%> (-0.16%) ⬇️
examples+coraza.rule.case_sensitive_args_keys 81.65% <85.36%> (+0.03%) ⬆️
examples+coraza.rule.multiphase_valuation 81.53% <85.36%> (+0.03%) ⬆️
examples+coraza.rule.no_regex_multiline 81.55% <85.36%> (+0.03%) ⬆️
examples+memoize_builders 81.65% <85.36%> (+0.03%) ⬆️
examples+no_fs_access 80.97% <85.36%> (+0.03%) ⬆️
ftw 81.69% <85.36%> (+0.03%) ⬆️
memoize_builders 81.79% <85.36%> (+0.03%) ⬆️
no_fs_access 81.14% <85.36%> (+0.03%) ⬆️
tinygo 81.66% <85.36%> (+0.03%) ⬆️

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

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

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.

3 participants