Extended Remote IP functionality: Host, Port, "Protocol" (emulating HTTPS presence), etc. for running behind balancers/frontends
This is related to #68 and is the reimplementation of it to 2.4.48, removing all the hacky parts. Please tell if anything else needs to be adjusted. If someone is using my older variant (from #68), please test in production to make sure I did not introduce any new bugs (I did the initial testing myself though).
The feature description is: mod_remoteip is extended by multiple options handy for frontend proxies moving traffic to Apache backends
- RemoteAllowOnlyInternalProxies - only allows 'internal' trusted proxies to connect to the Apache server, so no untrusted requests can be done (such are refused with Forbidden)
- RemoteHostHeader - accepted only from 'internal' trusted proxies, designates name of HTTP header to use as the supplementary Host header used to re-select virtual host inside request processing - keeping the original supplied Host header (if any) normally visible to i.e. user scripts so they don't know internal virtual host ID is different
- RemotePortHeader - accepted only from 'internal' trusted proxies, designates name of HTTP header to use as the port number originally used to connect which is used as 'default port' in the request and its subrequests
- RemoteProtoHeader - accepted only from 'internal' trusted proxies, designates name of HTTP header to supply for HTTPS 'simulation' (only sets the HTTPS used flag internally in the request and its subrequests, so i.e. rewrite conditions work like HTTPS is enabled when the header is present, even if the access to Apache from the proxy is done via HTTP, PHP shows HTTPS enabled, and so on, request HTTP schema is also adjusted)
- RemoteHTTPSEnableProto - accepted only from 'internal' trusted proxies, supplements RemoteProtoHeader, if set, additionally compares RemoteProtoHeader header contents to the value set and only sets HTTPS emulation if header contents match
Changes to 2.4.41-46 version in #68:
- No more obscure hacks of SSL module and internal metadata for HTTPS simulation handling and port alteration, rely fully on existing Apache hooks (including new 2.4.48 ssl_conn_is_ssl() hook)
- Use apr_table_nset() for connection annotation and header changes, we strdup() anyways
- RemoteHTTPSEnableProto option omitted now means setting HTTPS mode simulation on any RemoteProtoHeader header presence, without verifying header contents, for compatibility with the previous default "RemoteHTTPSEnableProto https" needs to be used
Added 'static' declaration missing from the initial commit, build passes now.
I think this also needs some justification about how it's usable. This one was made with shared hosting clusters and frontend-to-backend splits in mind.
- RemoteAllowOnlyInternalProxies is rather trivial, it allows only your trusted frontends to connect, denying all direct requests to backend servers, a safety measure.
- RemoteHostHeader and RemotePortHeader apply respective header contents after virtual host selection and allow to define a single virtual host for backend, performing real hostname and port mapping on frontends. All .htaccess and other configuration subrequests like rewrite are done to the original virtual host defined in the backend, while i.e. backend scripts see the original Host/Port that were accessed, thinking they are actually running with the specified hostname/port and generating proper URLs from this data. So you can i.e. call your virtual host 'cmr1234.mydomain.com' and map 'yourcustomerhost.com' to 'cmr1234.mydomain.com' on the proxying frontend, sending Host of 'cmr1234.mydomain.com' in the request to backend, and specific headers for the original host/port accessed.
- RemoteProtoHeader / RemoteHTTPSEnableProto is a widely used setup supplement. It allows to perform all SSL on frontend balancers, while running backend over HTTP only in a trusted network, removing SSL overhead from backends. This allows to create a header to inform backend scripts and other dynamic stuff the original request came via SSL to make it generate HTTPS URLs while the backend will always serve unencrypted. This also affects rewrite conditions so i.e. SSL=on/off dependent rules may be used. This feature removes the need of tricky SSL/non-SSL state handling on backends in such setups, faking SSL state and allowing third party stuff to run unmodified in SSL proxy setups.
Should the options not be prefixed RemoteIP*? So RemoteIPHostHeader etc.
Would love to see this added to master 👍
Should the options not be prefixed RemoteIP*? So
RemoteIPHostHeaderetc.
Well, they are technically part of remoteip module, but as they do not exactly affect/specify remote IP but remote Host and Port, remote protocol flag or everything at once (in case of RemoteAllowOnlyInternalProxies), I've named them accordingly.
I tried this patch. It works, but there is something with RemoteAllowOnlyInternalProxies On, it returns 403 for requests that run through .htaccess - i can't really figure out any other pattern of why it happens.
But excellent patch otherwise, thank you :)
I tried this patch. It works, but there is something with
RemoteAllowOnlyInternalProxies On, it returns 403 for requests that run through .htaccess - i can't really figure out any other pattern of why it happens.
If you please can share a bit of configuration that allows to reproduce the issue, I'll definitely look into it as I want this to be as clean as possible.
If it's not exactly publicly sharable but possible to send in private, I can be reached by mail at alex [meow] alex-at.net
It's really hard to debug, but after some time (many reloads) it's like things leak from one request to another. Suddenly %HTTPS in mod_rewrite doesn't return the correct value, where on the previous request it did. After a httpd restart it works again for a little while.
I'm using
RemoteIPInternalProxy 127.0.0.1/32
RemoteIPHeader X-Forwarded-For
RemoteHostHeader X-Forwarded-Host
RemotePortHeader X-Forwarded-Port
RemoteProtoHeader X-Forwarded-Proto
RemoteHTTPSEnableProto https
RemoteAllowOnlyInternalProxies Off
Hello, @tomsommer
I have been using this on my massive shared hostings for years, especially with mod_rewrite for customers, and am not seeing this issue. HTTPS state adjustment relies on the internal Apache hook, so I wonder.
Nevertheless, if there is any possibility of this happening, I want to investigate it as it potentially may shoot us in the leg as well. First things first, please state the MPM and list of loaded modules you are using, also the nature of the request (to static files, to dynamic code). Having full mod_rewrite rules (without sensitive data) you are able to reproduce this on also would be very helpful. That's because I suspect this could be mpm_event or other threaded MPM, then I would have to thoroughly doublecheck the local flags scope in the code and dig in that direction (we are using mpm_event at places for it as well, but not for shared hostings, still had never seen the issue, but yes, anyways I want to thoroughly check that).
@tomsommer
Okay, knowing it's mpm_event already points me to the test set creation. Not promising it would be fast to diagnose, but I'll surely post if I find (or do not) something.
There is definitely some leakage between requests on mpm_event, the HTTPS variable is switching between on and off when hitting the same non-https page, even though HTTP_X_FORWARDED_PROTO=http and HTTP_X_FORWARDED_PORT=80 remain.
Don't know if https://github.com/gnif/mod_rpaf/issues/42 is relevant
I turned off keep-alive on the upstream in nginx and that solved the problem, but of course I would like to use keep-alive :)
@tomsommer
Yes, I see where it may happen now. It is always spurious 'https on' state that should not be there, am I correct? Not related to MPM, but to keepalive connections indeed. This appeared due to RemoteHTTPSEnableProto handling changes. Thanks!!!
@tomsommer
Please test with the latest commit added. Should fix the leakage of HTTPS flag under the conditions stated in the commit description.
@AlexAT Seems to be fixed now 👍
Please test with the latest commit added. Should fix the leakage of HTTPS flag under the conditions stated in the commit description.
IIUC the scenario fixed here is httpd with Keepalive on (listening on an http or https port), a first request arrives at the frontend on an https port so it sets e.g. X-Forwarded-Proto: https which causes HTTPS=on in httpd, then a second request arrives at the frontend on an http port so it now sets X-Forwarded-Proto: http and forwards the request on the same httpd kept alive connection leading to HTTPS=off (or unset) in httpd.
So RemoteHTTPSEnableProto is supposed to force the HTTPS env var according to what the frontend says, regardless of whether httpd is listening on https or http, right?
I suppose that there are cases where the communications between the frontend and httpd can be considered secure/private even if it's not TLS (or the other way around), and that controlling HTTPS at the frontend can be useful for e.g. generating the right URL schemes in scripts/redirects, but that'll probably need a big warning in the docs stating that HTTPS=on can be set for non-TLS httpd connections (or the other way around, though maybe less critical).
Btw, it seems that HTTPS=off (or unsetting) is missing in remoteip_hook_fixups(), and that remoteip_hook_http_scheme() is registered APR_HOOK_LAST thus runs after mod_ssl's ssl_hook_http_scheme() at APR_HOOK_MIDDLE (so if SSLEngine is on, the scheme will always be "https" regardless of the frontend, which may not be consistent with the above?).
Hello, @ylavic
Thanks for the review, I'll do the changes accordingly today or tomorrow.
For the hook order and emulating off mode when it's SSL module handling requests, I assume I will have to use APR_HOOK_REALLY_FIRST given the nature of the function then if and then only if config->proto_header_name is set, return DONE instead of DECLINED so the process is complete, but the result is not OK (as it's compared with OK in ap_ssl_conn_is_ssl() @ ssl.c).
Will first check what adverse effects it may have. [glancing through the code only shows mod_nw_ssl may be not compatible] There's a fallback to ssl_is_https() inside ap_ssl_conn_is_ssl() and so it will probably not bode well with mod_nw_ssl.
For the hook order and emulating off mode...
I was talking about the remoteip_hook_http_scheme() actually, I don't think mod_remote_ip should hook into ssl_conn_is_ssl, that one should stay local IMO (we need a way in httpd to know for sure whether the local connection is TLS or not).
I didn't look closely at all the ap_ssl_conn_is_ssl() usages, but the two local and remote indicators shouldn't be mixed..
This may mean adding a new ssl_conn_remote_is_ssl hook and using ap_ssl_conn_remote_is_ssl() where ap_ssl_conn_is_ssl() is currently misused if/when these changes to mod_remote_ip are in.
It could be an APR_OPTIONAL_FN too, provided by mod_remote_ip and uses by e.g. ap_conn_is_remote_ssl() if it exists.
Just to add, that hooking into ap_hook_ssl_conn_is_ssl is the big feature of this module/patch, that you would otherwise need mod_rpaf for. There is no other way to let mod_rewrite think that the connection is secure and thus make rewrite rules act correctly as the connection to the visitor is indeed https but HTTPD doesn't know this otherwise.
Also the HTTPS=off environment variable would indeed be nice, but I solved that with a SetEnvIf for now.
I do agree that perhaps RemoteHTTPSEnableProto should default to empty/unset.