#!/usr/bin/env bash # whp-proxy-guard-module.test.sh - live probe of mod_whp_proxy_guard in a built # shared-httpd image (src/mod_whp_proxy_guard, configs/whp-proxy-guard.conf). # # Usage: whp-proxy-guard-module.test.sh [image] (default shared-httpd:latest) # Env: WHP_FPM_IMAGE a cac-fpm image for the FastCGI environment section # (default repo.anhonesthost.net/cloud-hosting-platform/cac-fpm:php83; # that section is SKIPPED, not failed, when it is absent) # SKIPs (exit 0) without docker or without the image. # # Tenant vhosts a.test / b.test / l.test on a private docker network, an HTTP # "target" (this image's httpd, counted in its access log) and, with # WHP_FPM_IMAGE, a php-fpm container answering as a.test-01 and b.test-01. set -u IMAGE="${1:-shared-httpd:latest}" FPM_IMAGE="${WHP_FPM_IMAGE:-repo.anhonesthost.net/cloud-hosting-platform/cac-fpm:php83}" ID="whp-pg-$$"; NET="$ID-net"; H="$ID-httpd"; TG="$ID-tgt"; FP="$ID-fpm" pass=0; fail=0 ok() { pass=$((pass + 1)); printf ' ok %s\n' "$*"; } bad() { fail=$((fail + 1)); printf ' FAIL %s\n' "$*"; } skip() { printf 'SKIP whp-proxy-guard-module: %s\n' "$*"; exit 0; } command -v docker >/dev/null 2>&1 || skip "no docker" docker image inspect "$IMAGE" >/dev/null 2>&1 || skip "image $IMAGE not available" T="$(mktemp -d)" trap 'docker rm -f "$H" "$TG" "$FP" >/dev/null 2>&1; docker network rm "$NET" >/dev/null 2>&1; rm -rf "$T"' EXIT vhost() { # name user allow-line cat < Options SymLinksIfOwnerMatch AllowOverride AuthConfig FileInfo Indexes Limit Options=Indexes,MultiViews,SymLinksIfOwnerMatch Require all granted ServerName $1 DocumentRoot "/srv/$2" SSLEngine on SSLCertificateFile /etc/pki/tls/certs/localhost.crt SSLCertificateKeyFile /etc/pki/tls/private/localhost.key SetHandler "proxy:fcgi://$2.test-01:9000" $3 ProxyFCGISetEnvIf "true" SCRIPT_FILENAME "/home/$2%{reqenv:SCRIPT_NAME}" EOF } mkdir -p "$T/v" vhost a.test a ' WHPProxyAllow "fcgi://A.TEST-01:9000" ' > "$T/v/a.conf" vhost b.test b '' > "$T/v/b.conf" # inherits the image default list vhost l.test l ' WHPProxyGuard Log WHPProxyAllow fcgi://l.test-01:9000 ' > "$T/v/l.conf" 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 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' mk e0 "$SF" mk e1 "\n$SF\n" mk e2 "\n\n$SF\n\n" mk e3 "\n\n\n$SF\n" mk e4 "\n\n\n$SF\n\n\n" mk e5 'ProxyFCGIBackendType GENERIC\n' p() { mk "$1" "RewriteEngine on\nRewriteRule ^go\$ $2 [P]\n"; } p t1 'fcgi://b.test-01:9000/home/b/admin.php' # another tenant's backend p t2 'fcgi://a.test-01:9000/home/a/index.php' # own backend (case differs from the list) p t3 'fcgi://a.test-01:9001/home/a/index.php' # own host, other port p t4 'http://target.test/' # SSRF: http p t5 'ws://target.test/' p t6 'ajp://target.test:80/' p t7 'fcgi://TGTIP:9000/home/b/admin.php' # another backend by IP mk t8 '\n\nSetHandler "proxy:http://target.test/"\n\n\n' mk t9 '\nSetHandler "proxy:unix:/run/httpd/cgisock|http://localhost/"\n\n' mk h1 '\n\nSetHandler balancer-manager\n\n\n' mk h2 'RewriteEngine on\nRewriteRule ^ - [H=server-status]\n' mk h3 'SetHandler lua-script\n' mk h4 '\n\nSetHandler application/x-httpd-cgi\n\n\n' mk h5 'RewriteEngine on\nRewriteRule ^ - [H=application/x-httpd-status]\n' mk c1 '\nSetHandler none\nSetHandler default-handler\n\n' mk c2 'RewriteEngine on\nRewriteRule ^pretty$ f.txt [L]\nHeader set X-T 1\nSetEnvIf User-Agent . X=1\n' mkdir -p /srv/l/lg; printf 'RewriteEngine on\nRewriteRule ^go$ http://target.test/ [P]\n' > /srv/l/lg/.htaccess chmod -R a+rX /srv EOF docker network create "$NET" >/dev/null || skip "could not create a docker network" docker run -d --name "$TG" --network "$NET" --network-alias target.test --entrypoint sh "$IMAGE" \ -c 'echo TARGET > /var/www/html/index.html; exec httpd -DFOREGROUND' >/dev/null || skip "could not start the target" fpm=0; BK="$TG" if docker image inspect "$FPM_IMAGE" >/dev/null 2>&1 && docker run -d --name "$FP" --network "$NET" --network-alias a.test-01 --network-alias b.test-01 \ --entrypoint sleep "$FPM_IMAGE" infinity >/dev/null 2>&1; then fpm=1; BK="$FP" docker exec "$FP" sh -c ' mkdir -p /home/a /home/b /run/php-fpm echo A-INDEX > /home/a/index.php; echo A-OTHER > /home/a/other.php; echo PREPENDED > /home/a/pre.txt echo B-ADMIN > /home/b/admin.php; chmod -R a+rX /home printf "[w]\nuser=nobody\ngroup=nobody\nlisten=0.0.0.0:9000\npm=static\npm.max_children=1\n" > /etc/php-fpm.d/www.conf php-fpm -D' >/dev/null 2>&1 fi TGTIP="$(docker inspect -f "{{(index .NetworkSettings.Networks \"$NET\").IPAddress}}" "$BK")" sed -i "s/TGTIP/$TGTIP/" "$T/fx.sh" docker run -d --name "$H" --network "$NET" "$IMAGE" >/dev/null || skip "could not start $IMAGE" sleep 3 docker cp "$T/v/." "$H:/etc/httpd/conf.d/vhosts/" >/dev/null docker exec "$H" sh -c 'grep -q "conf.d/vhosts" /etc/httpd/conf/httpd.conf /etc/httpd/conf.d/*.conf 2>/dev/null || echo "IncludeOptional conf.d/vhosts/*.conf" > /etc/httpd/conf.d/zz-vhosts.conf' docker cp "$T/fx.sh" "$H:/fx.sh" >/dev/null docker exec "$H" bash /fx.sh || { bad "fixtures"; exit 1; } docker exec "$H" sh -c 'httpd -M 2>/dev/null' | grep -q ' whp_proxy_guard_module ' && ok "module loaded" || bad "module not loaded" docker exec "$H" httpd -t >/dev/null 2>&1 && ok "httpd -t" || { bad "httpd -t"; docker exec "$H" httpd -t 2>&1 | tail -3; exit 1; } docker exec "$H" httpd -k graceful >/dev/null 2>&1; sleep 3 get() { # host path -> " " docker exec "$H" sh -c "curl -sk --http1.1 --max-time 15 --resolve $1:443:127.0.0.1 -o /tmp/o -w '%{http_code} ' 'https://$1$2'; head -c 60 /tmp/o | tr '\n' '~'" 2>/dev/null } code() { local r; r="$(get "$2" "$3")"; [ "${r%% *}" = "$4" ] && ok "$1 -> $4" || bad "$1 -> got '${r:0:50}', want $4"; } notcode() { local r; r="$(get "$2" "$3")"; [ "${r%% *}" != "$4" ] && ok "$1 -> ${r%% *} (not $4)" || bad "$1 -> $4"; } hits() { docker exec "$TG" sh -c 'cat /var/log/httpd/access_log 2>/dev/null | wc -l' 2>/dev/null || echo 0; } echo "== .htaccess ProxyFCGISetEnvIf / ProxyFCGIBackendType refused in every nesting (WHPHtaccessProxyFCGI Deny)" for c in "e0|plain" "e1|" "e2|" "e3|" "e4|" "e5|ProxyFCGIBackendType"; do code "${c#*|}" a.test "/${c%%|*}/f.txt" 500 done docker exec "$H" sh -c 'grep -q "ProxyFCGISetEnvIf is not allowed in .htaccess on this server (whp_proxy_guard)" /var/log/httpd/*error*log' \ && ok "refusal is logged with the module name" || bad "refusal not logged" echo "== proxy allow list (scheme_handler hook)" code "[P] another tenant's backend (not on a.test's list)" a.test /t1/go 403 notcode "[P] own backend (host case differs from the list)" a.test /t2/go 403 code "[P] own host, other port" a.test /t3/go 403 h0=$(hits) code "[P] http:// SSRF" a.test /t4/go 403 code "[P] ws:// SSRF" a.test /t5/go 403 code "[P] ajp:// SSRF" a.test /t6/go 403 code "[P] backend by IP address" a.test /t7/go 403 code " SetHandler proxy:http://" a.test /t8/f.txt 403 [ "$(hits)" = "$h0" ] && ok "the SSRF target saw no request" || bad "the SSRF target was reached" r="$(get a.test /t9/f.txt)"; case "$r" in 403*|"200 F"*) ok "SetHandler proxy:unix: -> ${r:0:3} (refused or not proxied)" ;; *) bad "proxy:unix: -> ${r:0:40}" ;; esac code "vhost without a list inherits the image default: [P] http://" b.test /t4/go 404 # no /t4 under /srv/b docker exec "$H" sh -c 'mkdir -p /srv/b/t; printf "RewriteEngine on\nRewriteRule ^go\$ http://target.test/ [P]\nRewriteRule ^gf\$ fcgi://b.test-01:9000/home/b/admin.php [P]\n" > /srv/b/t/.htaccess; chmod -R a+rX /srv/b' 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' \ && 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 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 code "SetHandler lua-script" a.test /h3/f.txt 403 code " SetHandler application/x-httpd-cgi (mod_cgid alias)" a.test /h4/f.txt 403 code "[H=application/x-httpd-status] (mod_status alias)" a.test /h5/f.txt 403 echo "== compat" code "static file" a.test /own.txt 200 code "uploads idiom SetHandler none/default-handler" a.test /c1/f.txt 200 code "RewriteRule / Header / SetEnvIf" a.test /c2/pretty 200 if [ "$fpm" = 1 ]; then echo "== FastCGI environment (WHPProxyScrubEnv), own backend" docker exec "$H" sh -c 'mkdir -p /srv/a/s1 /srv/a/s2; : > /srv/a/s1/index.php; : > /srv/a/s2/index.php printf "SetEnv PHP_VALUE \"auto_prepend_file=/home/a/pre.txt\"\nRewriteEngine on\nRewriteRule ^go\$ index.php [E=PHP_ADMIN_VALUE:auto_prepend_file=/home/a/pre.txt]\n" > /srv/a/s1/.htaccess printf "SetEnv PATH_TRANSLATED /home/a/other.php\nRewriteEngine on\nRewriteRule ^go\$ index.php\n" > /srv/a/s2/.htaccess chmod -R a+rX /srv/a' docker exec "$FP" sh -c 'mkdir -p /home/a/s1 /home/a/s2; echo S1-INDEX > /home/a/s1/index.php; echo S2-INDEX > /home/a/s2/index.php; chmod -R a+rX /home/a' code "own .php reaches its backend" a.test /index.php 200 r="$(get a.test /s1/go)"; case "$r" in *PREPENDED*) bad "PHP_VALUE / PHP_ADMIN_VALUE reached php-fpm -> ${r:0:40}" ;; "200 S1-INDEX"*) ok "SetEnv PHP_VALUE / [E=PHP_ADMIN_VALUE] scrubbed" ;; *) bad "s1 -> ${r:0:40}" ;; esac r="$(get a.test /s2/go)"; case "$r" in *A-OTHER*) bad "tenant PATH_TRANSLATED chose the script -> ${r:0:40}" ;; "200 S2-INDEX"*) ok "SetEnv PATH_TRANSLATED + REDIRECT_URL scrubbed" ;; *) bad "s2 -> ${r:0:40}" ;; esac else echo " skip FastCGI environment section: $FPM_IMAGE not available" fi if [ "$fail" -eq 0 ]; then echo "PASS whp-proxy-guard-module ($pass assertions)"; exit 0; fi echo "FAIL whp-proxy-guard-module: $fail failed, $pass passed"; exit 1