Files
cloud-apache-container/scripts/tests/httpd-exec-surface-gate.test.sh
T
shadowdaoandClaude Opus 5.5 0f8407c239 httpd-exec-surface-gate test: stop the mod_lua case masking every later case; assert each failure reason
The first regression case runs `dnf install -y mod_lua`, and restore() only
copied back /etc/httpd and /usr/lib64/httpd, not the RPM database. Every
later case therefore failed on "RPM mod_lua is installed" and passed whether
or not the check it targets worked: with the LoadModule scan, the leftover-.so
check, the RPM check, or the loaded-module check disabled in the gate, all 15
cases still passed (measured by mutating the gate).

restore() now uninstalls mod_lua, and expect_fail asserts the gate's own
failure message for each case. The include case puts the real .so back so the
module actually loads; a new case uses the stock prefork-guarded `LoadModule
cgi_module` shape (never loaded under event MPM), which only the LoadModule
scan can see; a new positive-control case removes proxy_balancer. Re-run:
17 passed; disabling the LoadModule scan, the .so check, the config grep or
the required-module check now each fail the suite.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6
2026-11-22 22:09:09 -08:00

82 lines
6.2 KiB
Bash
Executable File

#!/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 FOR ITS OWN REASON (the failure message is
## asserted): mod_lua back, include loaded again, a prefork-guarded cgi
## LoadModule line, the global INCLUDES filter, a cgi-bin ScriptAlias,
## `Options +ExecCGI`, a leftover .so, and a REQUIRED module going missing
## (positive control, incl. the balancer used by multi-FPM sites)
## 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. The RPM database
# is not in the snapshot, so restore also uninstalls mod_lua: a case that
# reinstalls it must not leave every later case failing on "RPM mod_lua is
# installed" instead of on the check it is meant to exercise.
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 'if rpm -q mod_lua >/dev/null 2>&1; then rpm -e mod_lua; fi; rm -rf /etc/httpd /usr/lib64/httpd; cp -a /tmp/snap/etc /etc/httpd; cp -a /tmp/snap/lib /usr/lib64/httpd'; }
# Unhardened module set, so a case can put a real banned .so back.
docker exec "$N" sh -c 'cp -a /usr/lib64/httpd/modules /tmp/pristine-mods'
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, ERE the gate output must match (the reason), shell snippet
# run in the container to introduce the regression. The reason is asserted so a
# case cannot pass on some other, unrelated failure.
local out
restore
docker exec "$N" sh -c "$3" >/dev/null 2>&1 || { bad "$1: could not set up the mutation"; return; }
if out=$(g check); then bad "$1: gate did NOT fail"; return; fi
if printf '%s\n' "$out" | grep -qE "$2"; then ok "$1: gate fails ($2)"; else bad "$1: gate failed for another reason: $out"; fi
}
echo "== regressions must be caught, each for its own reason"
expect_fail "mod_lua RPM back" 'RPM mod_lua is installed' 'dnf install -y mod_lua'
expect_fail "include .so + LoadModule back (loads)" 'exec-capable module is still loaded' \
'cp /tmp/pristine-mods/mod_include.so /usr/lib64/httpd/modules/ && echo "LoadModule include_module modules/mod_include.so" >> /etc/httpd/conf.modules.d/00-base.conf'
# Stock 01-cgi.conf shape: under event MPM this line never loads, so only the
# LoadModule scan can see it.
expect_fail "cgi LoadModule line (prefork-guarded)" 'LoadModule line for a banned module' \
'printf "<IfModule mpm_prefork_module>\n LoadModule cgi_module modules/mod_cgi.so\n</IfModule>\n" > /etc/httpd/conf.modules.d/01-cgi.conf'
expect_fail "cgid LoadModule line, .so gone" 'httpd -M failed' 'echo "LoadModule cgid_module modules/mod_cgid.so" >> /etc/httpd/conf.modules.d/01-cgi.conf'
expect_fail "global AddOutputFilter INCLUDES .shtml" 'global INCLUDES filter' 'echo "AddOutputFilter INCLUDES .shtml" >> /etc/httpd/conf/httpd.conf'
expect_fail "ScriptAlias /cgi-bin/" 'global INCLUDES filter' 'echo "ScriptAlias /cgi-bin/ /var/www/cgi-bin/" >> /etc/httpd/conf/httpd.conf'
expect_fail "Options +ExecCGI in conf.d" 'global INCLUDES filter' 'printf "<Directory /var/www>\nOptions +ExecCGI\n</Directory>\n" > /etc/httpd/conf.d/x.conf'
expect_fail "leftover mod_userdir.so on disk" 'mod_userdir.so still exists' 'touch /usr/lib64/httpd/modules/mod_userdir.so'
expect_fail "required module missing (rewrite)" 'required module rewrite_module' 'sed -i "/rewrite_module/d" /etc/httpd/conf.modules.d/00-base.conf'
expect_fail "required module missing (proxy_fcgi)" 'required module proxy_fcgi_module' 'sed -i "/proxy_fcgi_module/d" /etc/httpd/conf.modules.d/00-proxy.conf'
expect_fail "required module missing (balancer)" 'required module proxy_balancer_module' 'sed -i "/proxy_balancer_module/d" /etc/httpd/conf.modules.d/00-proxy.conf'
expect_fail "config does not parse" 'httpd -M failed' 'echo "BogusDirective on" >> /etc/httpd/conf/httpd.conf'
echo "$pass passed, $fail failed"
[ "$fail" -eq 0 ]