#!/usr/bin/env bash ## httpd-exec-surface-gate.test.sh - proves scripts/httpd-exec-surface-gate.sh can FAIL. ## ## A gate that always says "OK" is worse than none. This runs the gate inside a ## throwaway container on stock AlmaLinux 10 + httpd + mod_ssl + mod_lua (the exact ## starting point of Dockerfile.shared-httpd) and checks: ## - `check` FAILS on the unhardened image (negative control) ## - `remove` then `check` PASSES, and `httpd -M` has none of the banned modules ## - each regression is caught: lua / include / cgid LoadModule back, the global ## INCLUDES filter, a cgi-bin ScriptAlias, `Options +ExecCGI`, and a REQUIRED ## module going missing (positive control) ## Needs docker and network (dnf). Exits 0 only if every case behaves. ## Env: BASE (default almalinux/10-base). set -u HERE=$(cd "$(dirname "$0")/.." && pwd) GATE="$HERE/httpd-exec-surface-gate.sh" BASE=${BASE:-almalinux/10-base} N="exec-gate-test-$$" fail=0; pass=0 ok() { pass=$((pass + 1)); echo " ok $*"; } bad() { fail=$((fail + 1)); echo " FAIL $*"; } command -v docker >/dev/null 2>&1 || { echo "SKIP-FAIL: docker not installed" >&2; exit 1; } trap 'docker rm -f "$N" >/dev/null 2>&1' EXIT docker run -d --name "$N" "$BASE" sleep 3600 >/dev/null || { echo "cannot start $BASE" >&2; exit 1; } docker exec "$N" sh -c 'dnf install -y httpd mod_ssl mod_lua openssl >/dev/null 2>&1' \ || { echo "dnf install failed (network?)" >&2; exit 1; } docker exec "$N" sh -c 'mkdir -p /scripts; [ -e /etc/pki/tls/certs/localhost.crt ] || openssl req -newkey rsa:2048 -nodes -keyout /etc/pki/tls/private/localhost.key -x509 -days 2 -subj /CN=localhost -out /etc/pki/tls/certs/localhost.crt 2>/dev/null' docker cp "$GATE" "$N:/scripts/gate.sh" g() { docker exec "$N" /scripts/gate.sh "$@" 2>&1; } # Snapshot of the hardened state so each mutation starts clean. snap() { docker exec "$N" sh -c 'rm -rf /tmp/snap; mkdir /tmp/snap; cp -a /etc/httpd /tmp/snap/etc; cp -a /usr/lib64/httpd /tmp/snap/lib'; } restore() { docker exec "$N" sh -c 'rm -rf /etc/httpd /usr/lib64/httpd; cp -a /tmp/snap/etc /etc/httpd; cp -a /tmp/snap/lib /usr/lib64/httpd'; } echo "== negative control: unhardened image" if out=$(g check); then bad "check passed on an unhardened image: $out"; else ok "check FAILS on the unhardened image"; fi echo "== remove + check" g remove >/dev/null || bad "remove failed" if out=$(g check); then ok "remove then check passes"; else bad "check after remove: $out"; fi if docker exec "$N" sh -c "httpd -M 2>/dev/null | grep -E '^ (cgid|cgi|lua|include|actions|dav|dav_fs|dav_lock|userdir)_module '"; then bad "banned module in httpd -M"; else ok "httpd -M has none of cgid/cgi/lua/include/actions/dav*/userdir"; fi if docker exec "$N" rpm -q mod_lua >/dev/null 2>&1; then bad "mod_lua RPM still installed"; else ok "mod_lua RPM gone"; fi if out=$(g remove) && out2=$(g check); then ok "remove is idempotent"; else bad "second remove/check: $out $out2"; fi snap expect_fail() { # label, shell snippet run in the container to introduce the regression restore docker exec "$N" sh -c "$2" >/dev/null 2>&1 || { bad "$1: could not set up the mutation"; return; } if g check >/dev/null; then bad "$1: gate did NOT fail"; else ok "$1: gate fails"; fi } echo "== regressions must be caught" expect_fail "lua_module LoadModule back (RPM)" 'dnf install -y mod_lua' expect_fail "include_module LoadModule line back" 'echo "LoadModule include_module modules/mod_include.so" >> /etc/httpd/conf.modules.d/00-base.conf' expect_fail "cgid LoadModule line back" 'echo "LoadModule cgid_module modules/mod_cgid.so" >> /etc/httpd/conf.modules.d/01-cgi.conf' expect_fail "global AddOutputFilter INCLUDES .shtml" 'echo "AddOutputFilter INCLUDES .shtml" >> /etc/httpd/conf/httpd.conf' expect_fail "ScriptAlias /cgi-bin/" 'echo "ScriptAlias /cgi-bin/ /var/www/cgi-bin/" >> /etc/httpd/conf/httpd.conf' expect_fail "Options +ExecCGI in conf.d" 'printf "\nOptions +ExecCGI\n\n" > /etc/httpd/conf.d/x.conf' expect_fail "leftover mod_userdir.so on disk" 'touch /usr/lib64/httpd/modules/mod_userdir.so' expect_fail "required module missing (rewrite)" 'sed -i "/rewrite_module/d" /etc/httpd/conf.modules.d/00-base.conf' expect_fail "required module missing (proxy_fcgi)" 'sed -i "/proxy_fcgi_module/d" /etc/httpd/conf.modules.d/00-proxy.conf' expect_fail "httpd -t failure" 'echo "BogusDirective on" >> /etc/httpd/conf/httpd.conf' echo "$pass passed, $fail failed" [ "$fail" -eq 0 ]