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

feat: ✨ Add caddy client_ip variable parsing #184

Merged
merged 8 commits into from
Jan 9, 2025

Conversation

adrienyhuel
Copy link
Contributor

Resolve #150

Copy link
Member

@fzipi fzipi left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution!

Is there a simple way to add tests on this change?

@adrienyhuel
Copy link
Contributor Author

@fzipi
I don't know how we could test that, it's only in the logs

fzipi
fzipi previously requested changes Dec 18, 2024
Copy link
Member

@fzipi fzipi left a comment

Choose a reason for hiding this comment

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

Indentation on this code is bad. Maybe try to run go fmt?

Also, this might just deserve a new function call instead of adding it here?

@adrienyhuel
Copy link
Contributor Author

@fzipi
A function that might include the whole address parsing (from req.RemoteAddr and caddy client_ip variable) ?

@adrienyhuel adrienyhuel force-pushed the fix-client-ip branch 3 times, most recently from c95c1bc to 2fe46bc Compare December 18, 2024 21:24
@adrienyhuel adrienyhuel requested a review from fzipi December 18, 2024 21:27
@fzipi
Copy link
Member

fzipi commented Dec 19, 2024

I think it is way more clear now. @M4tteoP can you help me with the review?

@jcchavezs
Copy link
Member

Thanks for this @adrienyhuel. I think the unit tests here aren't enough to cover this. Any chance you can create an integration test using caddytest package as in https://github.com/dunglas/frankenphp/blob/2b7b3d1e4b0bb2634936ce463ffe7cf10c5978e3/caddy/caddy_test.go#L18. It can live in coraza_test.go file.

@adrienyhuel
Copy link
Contributor Author

@jcchavezs I added a simple integration test, is it sufficient ?

@jcchavezs
Copy link
Member

Please fix the lint and we can merge it.

@adrienyhuel
Copy link
Contributor Author

@jcchavezs my bad, I always forget to lint as my IDE always replace tabs with 4 spaces...

@fzipi
Copy link
Member

fzipi commented Jan 8, 2025

Maybe adding an .editorconfig could help in this repo for that case?

M4tteoP
M4tteoP previously approved these changes Jan 8, 2025
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.

Sorry, I'm a bit late, I'm finally taking a look. Overall looks good to me!

@@ -3,6 +3,9 @@
debug
auto_https off
order coraza_waf first
servers {
trusted_proxies static private_ranges
Copy link
Member

Choose a reason for hiding this comment

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

Would it be possible to add a comment on why this line is needed? Also, is it then something that is strictly required to have the client_ip properly working? If so, should we document it in the readme: https://github.com/corazawaf/coraza-caddy?tab=readme-ov-file#plugin-syntax?

Copy link
Contributor Author

@adrienyhuel adrienyhuel Jan 8, 2025

Choose a reason for hiding this comment

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

@M4tteoP
This line is needed is you want Caddy to populate client_ip from X-Forwarded-For header, in the case Caddy is behind a reverse proxy.
If you don't provide this config, Caddy will populate client_ip from request.RemoteAddr :

  • If client connect directly, it will be the real IP
  • if client connect through another proxy (like Cloudflare, or an Ingress in K8s), it will the proxy IP

See : https://caddyserver.com/docs/caddyfile/options#trusted-proxies
I think all users who want real client IP in their access logs know that config.

Copy link
Member

Choose a reason for hiding this comment

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

Awesome, thanks for the explanation!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Btw, with this pull request, coraza-caddy will use client_ip variable provided by caddy to use in WAF rules and in WAF logs, regardless of how Caddy populate this variable.

Copy link
Member

Choose a reason for hiding this comment

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

@adrienyhuel please add this explanation as a comment in the config.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jcchavezs done :)

Copy link

sonarqubecloud bot commented Jan 8, 2025

@jcchavezs jcchavezs merged commit 0959a59 into corazawaf:main Jan 9, 2025
6 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.

wrong client ip when using with cloudflared
5 participants