Blame SOURCES/0001-Modify-am_handler-setup-to-run-before-mod_proxy.patch

182345
From e09a28a30e13e5c22b481010f26b4a7743a09280 Mon Sep 17 00:00:00 2001
182345
From: John Dennis <jdennis@redhat.com>
182345
Date: Tue, 5 Mar 2019 10:15:48 +0100
182345
Subject: [PATCH] Modify am_handler setup to run before mod_proxy
182345
182345
The way the ECP flow works is that when a client initiates the flow, the
182345
SP's response is HTTP 200, but not the requested content, but a signed XML
182345
document that contains the "samlp:AuthnRequest" element. The idea is that
182345
the ECP client would then determine the IDP and send the document to the
182345
IDP, get a samlp:Response and convey that to the SP to get access to the
182345
protected resource.
182345
182345
Internally, the auth check which is normally done with am_check_uid() set to
182345
apache's ap_hook_check_user_id() hook, just responds with OK, so it pretends
182345
to authenticate the user. Then in the usual flow, the request reaches the
182345
ap_hook_handler which handles the request. There in the pipeline, mellon
182345
registers functions am_handler() which should run first (APR_HOOK_FIRST),
182345
determine that this request is an ECP one and return the ECP AuthnRequest
182345
document. But in case the proxy module is also in the picture, the proxy
182345
module "races" for who gets to be the first to handle the request in the
182345
pipeline and wins. Therefore, the request reaches the protected resource
182345
via mod_proxy and returns it.
182345
182345
This fix modifies the ap_hook_handler() call to explicitly run before
182345
handlers from mod_proxy.c
182345
182345
To reproduce the bug:
182345
0) Have a SP with mellon connected to a Keycloak IDP (or any other IDP I
182345
   guess). In the example below, my SAML SP is saml.federation.test
182345
1) Set a Location protected by mellon that proxies requests to another
182345
   URL. For example:
182345
182345
    ProxyPass         /sp-proxy  http://app.federation.test/example_app/
182345
    <Location /sp-proxy>
182345
        AuthType Mellon
182345
        MellonEnable auth
182345
        Require valid-user
182345
    </Location>
182345
182345
2) call:
182345
 curl -L -H "Accept: application/vnd.paos+xml" \
182345
         -H 'PAOS: ver="urn:liberty:paos:2003-08";"urn:oasis:names:tc:SAML:2.0:profiles:SSO:ecp"' \
182345
          http://saml.federation.test/sp-proxy
182345
182345
Before the patch, you would see whatever is served from the proxied
182345
page. With the patch, you should get back a XML document with a
182345
samlp:AuthnRequest.
182345
---
182345
 mod_auth_mellon.c | 8 +++++++-
182345
 1 file changed, 7 insertions(+), 1 deletion(-)
182345
182345
diff --git a/mod_auth_mellon.c b/mod_auth_mellon.c
182345
index 74bd328..5330f48 100644
182345
--- a/mod_auth_mellon.c
182345
+++ b/mod_auth_mellon.c
182345
@@ -207,6 +207,12 @@ static int am_create_request(request_rec *r)
182345
 
182345
 static void register_hooks(apr_pool_t *p)
182345
 {
182345
+    /* Our handler needs to run before mod_proxy so that it can properly
182345
+     * return ECP AuthnRequest messages when running as a reverse proxy.
182345
+     * See: https://github.com/Uninett/mod_auth_mellon/pull/196
182345
+     */
182345
+    static const char * const run_handler_before[]={ "mod_proxy.c", NULL };
182345
+
182345
     ap_hook_access_checker(am_auth_mellon_user, NULL, NULL, APR_HOOK_MIDDLE);
182345
     ap_hook_check_user_id(am_check_uid, NULL, NULL, APR_HOOK_MIDDLE);
182345
     ap_hook_post_config(am_global_init, NULL, NULL, APR_HOOK_MIDDLE);
182345
@@ -222,7 +228,7 @@ static void register_hooks(apr_pool_t *p)
182345
      * Therefore this hook must run before any handler that may check
182345
      * r->handler and decide that it is the only handler for this URL.
182345
      */
182345
-    ap_hook_handler(am_handler, NULL, NULL, APR_HOOK_FIRST);
182345
+    ap_hook_handler(am_handler, NULL, run_handler_before, APR_HOOK_FIRST);
182345
 
182345
 #ifdef ENABLE_DIAGNOSTICS
182345
     ap_hook_open_logs(am_diag_log_init,NULL,NULL,APR_HOOK_MIDDLE);
182345
-- 
182345
2.19.2
182345