envoy icon indicating copy to clipboard operation
envoy copied to clipboard

xff: add support for configuring a list of trusted CIDRs

Open jamesog opened this issue 1 year ago • 26 comments

Commit Message: xff: add support for configuring a list of trusted CIDRs

The original client IP address can be determined from the x-forwarded-for header either by a fixed number of trusted hops, or by evaluating the client IP address against a list of trusted addresses.

This adds support for configuring a list of CIDRs in the xff original IP detection extension. The remote IP address is evaluated against these, and optionally recurses through XFF to find the last non-trusted address.

Additional Description: This feature is generally used by people with a CDN in front of their edge proxy to ensure that XFF is only parsed when the remote connection comes from a CDN server.

The behaviour of the new functionality should be the same as Nginx's realip module.

Disclaimer: This is my first time writing C++ so I'm not certain my changes are completely idiomatic, but I've tried to stick with existing style in the codebase. Feedback very welcome!

Risk Level: Medium Testing: Unit tests, manual tests Docs Changes: Updates to HTTP Connection Manager header manipulation docs, and proto docs. Release Notes: Added to changelogs/current.yaml Platform Specific Features: None Fixes #21639 Relates to #31296

jamesog avatar Jan 15 '24 19:01 jamesog

Hi @jamesog, welcome and thank you for your contribution.

We will try to review your Pull Request as quickly as possible.

In the meantime, please take a look at the contribution guidelines if you have not done so already.

:cat:

Caused by: https://github.com/envoyproxy/envoy/pull/31831 was opened by jamesog.

see: more, trace.

CC @envoyproxy/api-shepherds: Your approval is needed for changes made to (api/envoy/|docs/root/api-docs/). envoyproxy/api-shepherds assignee is @markdroth CC @envoyproxy/api-watchers: FYI only for changes made to (api/envoy/|docs/root/api-docs/).

:cat:

Caused by: https://github.com/envoyproxy/envoy/pull/31831 was opened by jamesog.

see: more, trace.

/lgtm api

markdroth avatar Jan 17 '24 17:01 markdroth

Apologies for the delay in pushing updates. I got caught in the weeds a bit trying to understand some of the internals.

I think I've addressed all of Alyssa's feedback, and I added more documentation and examples to headers.rst.

The one change I'm not sure about is a587a23d55ca8d042250c87b33d1d7db3af4f138 - I've tried to understand what "internal" means to Envoy and I still can't say I'm 100% sure. PR #2232 added some comments about it needing to be a single IP in XFF, so I've made a similar change here but I don't feel quite right about it.

jamesog avatar Feb 13 '24 18:02 jamesog

Can you merge main?

/wait

mattklein123 avatar Feb 29 '24 16:02 mattklein123

Can you merge main?

Done.

jamesog avatar Feb 29 '24 19:02 jamesog

/retest

mattklein123 avatar Mar 03 '24 19:03 mattklein123

Argh sorry I think the CI issue is real since some of this code goes in the mobile build which doesn't use exceptions. Can you take a look? Hopefully it will be an easy fix.

/wait

mattklein123 avatar Mar 03 '24 19:03 mattklein123

Argh sorry I think the CI issue is real since some of this code goes in the mobile build which doesn't use exceptions. Can you take a look? Hopefully it will be an easy fix.

Ah, I meant to follow up on this one, I noticed that failure too. I could use some advice here actually. In an earlier change I had the constructor for the xff extension forcibly setting xff_num_trusted_hops to 0 when xff_trusted_cidrs was set, but Alyssa recommended logging an exception instead.

What's the best way to fix this to work for mobile, do you think? Should I set it back to overwriting the value and logging it, or is there another way to make it fail that works for Mobile?

jamesog avatar Mar 03 '24 19:03 jamesog

What's the best way to fix this to work for mobile, do you think? Should I set it back to overwriting the value and logging it, or is there another way to make it fail that works for Mobile?

Probably the best way would be to somehow make this code return an error and then somehow fail with an exception outside in the server (non mobile code) but we probably will need to ask @alyssawilk about this tomorrow when she is around.

mattklein123 avatar Mar 03 '24 20:03 mattklein123

Digging through the codebase a bit, I think I could either:

  • ENVOY_LOG(warn, "setting both xff_num_trusted_hops and xff_trusted_cidrs is not recommended"); or
  • Revert some of d976354a8906b186d4b01f2ab995594b2cca5933 and:
    • Force set xff_num_trusted_hops_ to 0 when has_xff_trusted_cidrs()
    • ENVOY_LOG(error, "cannot set both xff_num_trusted_hops and xff_trusted_cidrs; xff_num_trusted_hops is ignored")

jamesog avatar Mar 03 '24 21:03 jamesog

Hey folks, any feedback to which approach I should take here? Thanks!

jamesog avatar Mar 14 '24 17:03 jamesog

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 7 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

github-actions[bot] avatar Apr 13 '24 20:04 github-actions[bot]

Not stale, waiting for feedback! But I'm also trying to find other examples in the codebase that do similar things, but there's a lot of code to examine...

jamesog avatar Apr 13 '24 20:04 jamesog

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 7 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

github-actions[bot] avatar May 14 '24 04:05 github-actions[bot]

Can we keep this open and get it reviewed? @alyssawilk @mattklein123

robd003 avatar Jun 03 '24 18:06 robd003

I put it back into draft because I need to make some more changes but I haven't had the time. I'm hoping to get back to it soon.

jamesog avatar Jun 03 '24 18:06 jamesog

This pull request has been automatically marked as stale because it has not had activity in the last 30 days. It will be closed in 7 days if no further activity occurs. Please feel free to give a status update now, ping for review, or re-open when it's ready. Thank you for your contributions!

github-actions[bot] avatar Jul 03 '24 20:07 github-actions[bot]

Please keep this open, also Happy 4th of July! 🇺🇸

robd003 avatar Jul 04 '24 07:07 robd003

Sorry for the long delay here. Life kind of got in the way and it took me some time to trawl through existing code to see if there was an existing pattern for handling the case on mobile to remove the throw. All tests are passing now, hopefully this covers the last of the feedback.

jamesog avatar Jul 13 '24 20:07 jamesog

hm, CI looks wedged. can you do a main merge - maybe sorting current.yaml will fix?

alyssawilk avatar Aug 12 '24 13:08 alyssawilk

/retest

jamesog avatar Aug 15 '24 17:08 jamesog

Maybe a maintainer needs to run that? :-) Seems like CI got wedged.

jamesog avatar Aug 15 '24 17:08 jamesog

/retest

adisuissa avatar Aug 20 '24 20:08 adisuissa

/wait

wbpcode avatar Aug 21 '24 12:08 wbpcode