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

Chore: updates Middleware adding synthesis of Transfer-Encoding header #55

Merged
merged 2 commits into from
May 2, 2023

Conversation

M4tteoP
Copy link
Member

@M4tteoP M4tteoP commented Apr 16, 2023

Aligns the coraza-caddy middleware with the Coraza one porting corazawaf/coraza#768.
Closes #53.

@@ -50,6 +50,12 @@ func processRequest(tx types.Transaction, req *http.Request) (*types.Interruptio
tx.SetServerName(serverName)
}

// Transfer-Encoding header is removed by go/http
Copy link
Member

Choose a reason for hiding this comment

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

Does this remain true for caddy aswell? Shall we provide some refs to this assertion?

Copy link
Member Author

Choose a reason for hiding this comment

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

I can't find the precise call from the caddy codebase, but before ServeHTTP which is the caddy entry point for all HTTP requests, the go/http transfer encoding handling is already called. Dumping the headers from the Coraza filter we experience the same behavior of our Coraza middleware: with the Transfer-Encoding header stripped off.

I added a reference to the Transfer-Encoding handling go/http side.

@sonarcloud
Copy link

sonarcloud bot commented Apr 26, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@M4tteoP
Copy link
Member Author

M4tteoP commented Apr 26, 2023

I did some more manual tests with the help of the example. With this fix caddy manages to detect the Transfer-Encoding header and triggers the interruption (if it is the case).

One corner case I noticed with a different behavior compared to the plain Go HTTP server: Just sending the chunked header without a body, the WAF detects it and triggers the interruption, but then the request remains hanging. 🤔
Request: curl -H "Transfer-Encoding:chunked" 'localhost:8080' -v
Logs: {"level":"debug","ts":1682501883.4048684,"logger":"http.handlers.waf","msg":"Transaction finished","tx_id":"DDBoQbOLaWYqpUZR","is_interrupted":true,"status":403,"rule_id":111}
I can reproduce this behavior also before this PR, I think it is rather a Caddy behavior, of which I am not an expert

@jcchavezs
Copy link
Member

jcchavezs commented Apr 26, 2023 via email

@mholt
Copy link

mholt commented Apr 26, 2023

I'm not sure, but possibly caddyserver/caddy#5420 ? (Recently fixed, will be tagged in 2.7 beta 1 soon-ish) -- would be curious if it still hangs if you try the latest caddy on master.

@M4tteoP
Copy link
Member Author

M4tteoP commented May 1, 2023

Sorry @mholt, I missed the edit. I gave a go with caddy 1af419e7eca0c7b8559ec2f3b397f948f1ef13c2 (currently the latest commit on master), and I'm experiencing the same behavior. Basically, even if the module performs a WriteHeader and returns from the function that implements caddyhttp.MiddlewareHandler, that response code is not returned to the client. Caddy keeps hanging (presumably waiting for the chunked part of the request (that will never come)?)

@mholt
Copy link

mholt commented May 1, 2023

Thanks for trying it out. I guess my next question is why say the transfer-encoding is chunked when there's actually no body?

@M4tteoP
Copy link
Member Author

M4tteoP commented May 2, 2023

Good question 😅, it definitely is a corner case, made of a malformed request. Being the module a WAF integration, I considered the possibility that the server could enforce the WAF action and interrupt the request as soon as it was detected as malicious. During this "hanging status" we already know that it is a bad request, but we can't act accordingly.

One significant note: I have been wrong during the first message, the caddy behavior seems consistent with the plain Go HTTP server (They are sharing the same Middleware); also the latter triggers the interruption, but remains hanging.

@jcchavezs
Copy link
Member

@mholt so this PR aimed to test a rule that mitigated an attack. The transfer encoding header gets removed as a header because if becomes a first class field but coraza needs to be aware of it so we needed to somehow propagate it to coraza, that is fine.

On the other hand, sending a transfer encoding without a body is exactly a way of attacking the middlewares/proxies because some of them will send the headers on beforehand before receiving the body (triggering a header based attack).

@M4tteoP the behaviour of hanging response waiting for body seems fine as of course the server ia waiting for a body to be sent. If it is consistent with Go we should just accept it.

@mholt
Copy link

mholt commented May 2, 2023

Gotcha. Sounds like you've decided on a good solution then.

I also generally recommend setting I/O timeouts for stuff like that.

@M4tteoP M4tteoP merged commit 94eb7b0 into corazawaf:master May 2, 2023
@M4tteoP M4tteoP deleted the middleware_trasnferencoding branch May 3, 2023 15:45
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.

[Bug] CRS rule 920171 is not applied on chunked-encoded requests
3 participants