diff --git a/configs/whp-proxy-guard.conf b/configs/whp-proxy-guard.conf index bc46ec5..ac3cba5 100644 --- a/configs/whp-proxy-guard.conf +++ b/configs/whp-proxy-guard.conf @@ -23,6 +23,22 @@ WHPProxyAllow fcgi://*:9000 WHPProxyGuard On + # No proxying for a request that did not arrive over TLS. The edge + # (HAProxy) reaches shared-httpd on :443 only, and the per-site :80 + # vhosts only redirect; but :80 is reachable from the shared docker + # network, and a :80 vhost rendered before WHP made its redirect + # unconditional served alias hosts with the tenant .htaccess evaluated + # and none of the :443 vhost's FastCGI pins (measured: [P] to another + # tenant's backend ran its script). + WHPProxyRequireTLS On + + # Fail-closed for vhosts WITHOUT their own WHPProxyAllow (the inherited + # list above would then not count). Off here on purpose: a host still + # carries vhosts rendered before WHP renders a list per vhost, and those + # would lose PHP. WHP switches it On in its drop-in once every vhost on + # the fleet carries its own list. + WHPProxyRequireVhostList Off + # Handlers no tenant may reach, whatever selects them (nested # SetHandler, RewriteRule [H=...]). WHPDenyHandler balancer-manager server-status application/x-httpd-status server-info lua-script cgi-script application/x-httpd-cgi diff --git a/scripts/tests/whp-proxy-guard-module.test.sh b/scripts/tests/whp-proxy-guard-module.test.sh index 2349c71..1de5e20 100755 --- a/scripts/tests/whp-proxy-guard-module.test.sh +++ b/scripts/tests/whp-proxy-guard-module.test.sh @@ -56,13 +56,26 @@ vhost l.test l ' WHPProxyGuard Log WHPProxyAllow fcgi://l.test-01:9000 ' > "$T/v/l.conf" +vhost r.test r ' + WHPProxyRequireVhostList On + ' > "$T/v/r.conf" # no own list: inherited default must not count +vhost s.test s ' + WHPProxyRequireVhostList On + WHPProxyAllow fcgi://s.test-01:9000 + ' > "$T/v/s.conf" +cat > "$T/v/p80.conf" <<'EOF2' + + ServerName p.test + DocumentRoot "/srv/a" + +EOF2 vhost n.test n ' WHPProxyAllow None ' > "$T/v/n.conf" cat > "$T/fx.sh" <<'EOF' set -e -A=/srv/a; mkdir -p $A /srv/b /srv/l /srv/n; : > /srv/n/index.php +A=/srv/a; mkdir -p $A /srv/b /srv/l /srv/n /srv/r /srv/s; : > /srv/n/index.php; : > /srv/r/index.php; : > /srv/s/index.php echo A-OWN > $A/own.txt; : > $A/index.php mk() { mkdir -p "$A/$1"; printf "$2" > "$A/$1/.htaccess"; echo F > "$A/$1/f.txt"; } SF='ProxyFCGISetEnvIf "true" SCRIPT_FILENAME "/home/b/admin.php"\n' @@ -152,11 +165,17 @@ docker exec "$H" sh -c 'mkdir -p /srv/b/t; printf "RewriteEngine on\nRewriteRule code " ... b.test [P] http:// (default list is fcgi on :9000 only)" b.test /t/go 403 notcode " ... b.test [P] fcgi://b.test-01:9000 (default list)" b.test /t/gf 403 r="$(get l.test /lg/go)"; [ "${r%% *}" != 403 ] && ok "WHPProxyGuard Log does not refuse (${r%% *})" || bad "Log mode refused" -docker exec "$H" sh -c 'grep -q "whp_proxy_guard: proxy target http://target.test:80 is not in the allow list of virtual host l.test .*(log only)" /var/log/httpd/*error*log' \ +docker exec "$H" sh -c 'grep -q "whp_proxy_guard: proxy target http://target.test:80, virtual host l.test (uri /lg/go): not in the allow list (log only)" /var/log/httpd/*error*log' \ && ok "Log mode logs the target" || bad "Log mode did not log" code "WHPProxyAllow None: even the vhost's own .php handler is refused" n.test /index.php 403 +r="$(docker exec "$H" sh -c "curl -s --http1.1 --max-time 15 --resolve p.test:80:127.0.0.1 -o /dev/null -w '%{http_code}' http://p.test/t1/go")" +[ "$r" = 403 ] && ok "plain-HTTP (:80) [P] to a fcgi:9000 backend refused (WHPProxyRequireTLS) -> 403" || bad "plain-HTTP [P] -> $r, want 403" +docker exec "$H" sh -c 'grep -q "did not arrive over TLS (WHPProxyRequireTLS) (refused)" /var/log/httpd/*error*log' && ok "RequireTLS refusal logged" || bad "RequireTLS refusal not logged" +code "WHPProxyRequireVhostList On, no own list: own .php refused" r.test /index.php 403 +notcode "WHPProxyRequireVhostList On, own list: own .php allowed" s.test /index.php 403 + echo "== denied handlers (WHPDenyHandler)" code " SetHandler balancer-manager" a.test /h1/f.txt 403 code "RewriteRule [H=server-status]" a.test /h2/f.txt 403 diff --git a/src/mod_whp_proxy_guard/mod_whp_proxy_guard.c b/src/mod_whp_proxy_guard/mod_whp_proxy_guard.c index b6bec95..87a10a0 100644 --- a/src/mod_whp_proxy_guard/mod_whp_proxy_guard.c +++ b/src/mod_whp_proxy_guard/mod_whp_proxy_guard.c @@ -74,6 +74,15 @@ * the handler is chosen after every section a server-scope pin can live * in. Compared case-insensitively. * + * 5. Fail-closed switches (server config only) + * WHPProxyRequireTLS On: a request that did not arrive over TLS may not + * proxy at all. On the shared tier the edge reaches Apache on :443 only + * and the per-site :80 vhosts only redirect, so a plain-HTTP proxy is + * never legitimate (it was reachable from the shared docker network). + * WHPProxyRequireVhostList On: a virtual host whose WHPProxyAllow was not + * set inside its own (inherited lists do not count) may not + * proxy at all. For once every vhost renders its own list. + * * Nothing here changes a request whose virtual host has none of these * directives in effect. */ @@ -85,6 +94,7 @@ #include "http_request.h" #include "apr_strings.h" #include "apr_uri.h" +#include "http_ssl.h" #include "mod_proxy.h" module AP_MODULE_DECLARE_DATA whp_proxy_guard_module; @@ -107,6 +117,9 @@ typedef struct { apr_array_header_t *scrub; /* const char *, NULL = not set here */ apr_array_header_t *deny_handler;/* const char *, NULL = not set here */ int htaccess_fcgi; /* MODE_UNSET / MODE_OFF (allow) / MODE_ON (deny) */ + int allow_own; /* the list was set in THIS */ + int require_vhost_list; /* MODE_UNSET / OFF / ON */ + int require_tls; /* MODE_UNSET / OFF / ON */ } guard_srv; static void *create_srv(apr_pool_t *p, server_rec *s) @@ -114,6 +127,8 @@ static void *create_srv(apr_pool_t *p, server_rec *s) guard_srv *c = apr_pcalloc(p, sizeof(*c)); c->mode = MODE_UNSET; c->htaccess_fcgi = MODE_UNSET; + c->require_vhost_list = MODE_UNSET; + c->require_tls = MODE_UNSET; return c; } @@ -124,6 +139,11 @@ static void *merge_srv(apr_pool_t *p, void *b_, void *a_) c->allow = a->allow ? a->allow : b->allow; c->scrub = a->scrub ? a->scrub : b->scrub; c->deny_handler = a->deny_handler ? a->deny_handler : b->deny_handler; + c->allow_own = a->allow_own; /* never inherited */ + c->require_vhost_list = a->require_vhost_list != MODE_UNSET + ? a->require_vhost_list : b->require_vhost_list; + c->require_tls = a->require_tls != MODE_UNSET ? a->require_tls + : b->require_tls; c->htaccess_fcgi = a->htaccess_fcgi != MODE_UNSET ? a->htaccess_fcgi : b->htaccess_fcgi; return c; @@ -133,8 +153,10 @@ static int effective_mode(const guard_srv *c) { if (c->mode != MODE_UNSET) return c->mode; - /* A list that is set (even `WHPProxyAllow None`, i.e. empty) enforces. */ - return c->allow ? MODE_ON : MODE_OFF; + /* A list that is set (even `WHPProxyAllow None`, i.e. empty), or either + * fail-closed switch, enforces. */ + return (c->allow || c->require_vhost_list == MODE_ON + || c->require_tls == MODE_ON) ? MODE_ON : MODE_OFF; } /* ------------------------------------------------------------------ */ @@ -164,8 +186,12 @@ static const char *cmd_allow(cmd_parms *cmd, void *d, const char *arg) if (!c->allow) c->allow = apr_array_make(cmd->pool, 4, sizeof(allow_entry)); - if (!strcasecmp(arg, "none")) /* explicit empty list: deny every target */ + if (cmd->server->is_virtual) + c->allow_own = 1; + if (!strcasecmp(arg, "none")) { /* explicit empty list: deny every target */ + apr_array_clear(c->allow); return NULL; + } e = apr_array_push(c->allow); memset(e, 0, sizeof(*e)); @@ -245,6 +271,15 @@ static const char *cmd_deny_handler(cmd_parms *cmd, void *d, const char *arg) return NULL; } +static const char *cmd_flag(cmd_parms *cmd, void *d, int on) +{ + guard_srv *c = ap_get_module_config(cmd->server->module_config, + &whp_proxy_guard_module); + int *f = cmd->info == (void *)1 ? &c->require_vhost_list : &c->require_tls; + *f = on ? MODE_ON : MODE_OFF; + return NULL; +} + static const char *cmd_htaccess_fcgi(cmd_parms *cmd, void *d, const char *arg) { guard_srv *c = ap_get_module_config(cmd->server->module_config, @@ -331,6 +366,7 @@ static int guard_scheme_handler(request_rec *r, proxy_worker *worker, guard_srv *c = ap_get_module_config(r->server->module_config, &whp_proxy_guard_module); const char *desc = NULL; + const char *why = "not in the allow list"; int mode; if (c->scrub) { @@ -343,14 +379,23 @@ static int guard_scheme_handler(request_rec *r, proxy_worker *worker, mode = effective_mode(c); if (mode == MODE_OFF) return DECLINED; - if (target_allowed(r, c, worker, url, &desc)) + if (c->require_tls == MODE_ON && !ap_ssl_conn_is_ssl(r->connection)) { + target_allowed(r, c, worker, url, &desc); + why = "the request did not arrive over TLS (WHPProxyRequireTLS)"; + } + else if (c->require_vhost_list == MODE_ON && !c->allow_own) { + target_allowed(r, c, worker, url, &desc); + why = "the virtual host has no WHPProxyAllow of its own " + "(WHPProxyRequireVhostList)"; + } + else if (target_allowed(r, c, worker, url, &desc)) return DECLINED; ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, - "whp_proxy_guard: proxy target %s is not in the allow list " - "of virtual host %s (uri %s)%s", desc, + "whp_proxy_guard: proxy target %s, virtual host " + "%s (uri %s): %s%s", desc, r->server->server_hostname ? r->server->server_hostname : "-", - r->uri, mode == MODE_ON ? " (refused)" : " (log only)"); + r->uri, why, mode == MODE_ON ? " (refused)" : " (log only)"); return mode == MODE_ON ? HTTP_FORBIDDEN : DECLINED; } @@ -387,6 +432,11 @@ static const command_rec cmds[] = { "environment variables removed before any proxy scheme handler runs"), AP_INIT_ITERATE("WHPDenyHandler", cmd_deny_handler, NULL, RSRC_CONF, "handler names answered 403 in this virtual host"), + AP_INIT_FLAG("WHPProxyRequireVhostList", cmd_flag, (void *)1, RSRC_CONF, + "On: a virtual host without its OWN WHPProxyAllow may not proxy " + "at all (an inherited list does not count)"), + AP_INIT_FLAG("WHPProxyRequireTLS", cmd_flag, (void *)2, RSRC_CONF, + "On: no proxying for requests that did not arrive over TLS"), AP_INIT_TAKE1("WHPHtaccessProxyFCGI", cmd_htaccess_fcgi, NULL, RSRC_CONF, "Allow | Deny: ProxyFCGISetEnvIf / ProxyFCGIBackendType in .htaccess"), /* Same names and override class as mod_proxy_fcgi; invoked first. */