2026-11-23 06:29:48 -08:00
#!/usr/bin/env bash
## whp-symlink-module.test.sh - live probes for src/mod_whp_symlink against a built
## shared-httpd image: no .htaccess Options form may re-enable un-owner-checked
## symlinks, exec-SSI or CGI; quoted/escaped Options lines are refused; stock CMS
## shapes (`Options +FollowSymlinks`) no longer answer 500; the owner guard refuses
## a swapped public_html.
## whp-symlink-module.test.sh [image] (default shared-httpd:latest)
2026-11-23 07:33:47 -08:00
## Locally it SKIPs (exit 0) without docker or the image. With WHP_SYMTEST_REQUIRE=1
## (CI) a missing docker/image or a container that does not start FAILS instead,
## so the job can never go green without having run the probes.
## Build the image first: docker build -f Dockerfile.shared-httpd -t shared-httpd:symtest .
2026-11-23 06:29:48 -08:00
set -u
IMAGE = " ${ 1 :- shared -httpd: latest } "
2026-11-23 07:33:47 -08:00
REQUIRE = " ${ WHP_SYMTEST_REQUIRE :- 0 } "
skip() { if [ " $REQUIRE " = 1 ] ; then echo "FAIL (required): $* " ; exit 1; fi ; echo "SKIP: $* " ; exit 0; }
2026-11-23 06:29:48 -08:00
NAME = "whp-symtest- $$ " ; T = " $( mktemp -d) "
trap 'docker rm -f "$NAME" >/dev/null 2>&1; rm -rf "$T"' EXIT
2026-11-23 07:33:47 -08:00
command -v docker >/dev/null 2>& 1 || skip "no docker"
docker image inspect " $IMAGE " >/dev/null 2>& 1 || skip "no image $IMAGE "
2026-11-23 06:29:48 -08:00
fail = 0; pass = 0
ok() { pass = $(( pass+1)) ; echo "ok $* " ; }
bad() { fail = $(( fail+1)) ; echo "FAIL $* " ; }
# A per-site vhost shaped like WHP's shared-vhost-template.tpl (2026-11-22).
cat > " $T /site.conf" <<'EOF'
<Directory "/mnt/users">
Options SymLinksIfOwnerMatch
</Directory>
<Directory "/mnt/users/a/a.test/public_html">
Options Indexes SymLinksIfOwnerMatch MultiViews
AllowOverride AuthConfig FileInfo Indexes Limit Options=Indexes,MultiViews,SymLinksIfOwnerMatch,ExecCGI
Require all granted
</Directory>
<Directory "/mnt/users/a/a2.test/public_html">
Options Indexes SymLinksIfOwnerMatch MultiViews
Require all granted
</Directory>
<VirtualHost *:80>
ServerName a.test
DocumentRoot "/mnt/users/a/a.test/public_html"
</VirtualHost>
<VirtualHost *:80>
ServerName a2.test
DocumentRoot "/mnt/users/a/a2.test/public_html"
</VirtualHost>
<IfModule whp_symlink_module>
WHPOwnerGuard On
</IfModule>
EOF
cat > " $T /lines" <<'EOF'
+All
-All
All
+all
All +Indexes
All Indexes
None +FollowSymLinks
-None
FollowSymLinks
FOLLOWSYMLINKS
+FollowSymlinks
+followSYMlinks
+FollowSymLinks -SymLinksIfOwnerMatch
-SymLinksIfOwnerMatch +FollowSymLinks
Indexes +FollowSymLinks
+FollowSymLinks\t+Indexes
"FollowSymLinks Indexes"
"+FollowSymLinks"
'FollowSymLinks'
Follow\\SymLinks
"Follow"SymLinks
+FollowSymLinks "x y"
+Includes
Includes
+RunScripts
+ExecCGI
ExecCGI
+Bogus +FollowSymLinks
+FollowSymLinks +ExecCGI +Includes
EOF
2026-11-23 07:13:13 -08:00
# Whole .htaccess bodies (\n = newline): Options inside every container a
# tenant may use, mod_macro expansion, line continuation; the module rewrites at
# parse time, so each must behave like a top-level line. Directory must work (200).
cat > " $T /blocks" <<'EOF'
<If "true">\n<If "true">\nOptions FollowSymLinks\n</If>\n</If>
<If "false">\n</If>\n<ElseIf "true">\nOptions +FollowSymLinks\n</ElseIf>
<If "false">\n</If>\n<Else>\nOptions All\n</Else>
<Files *>\n<If "true">\nOptions +FollowSymLinks -SymLinksIfOwnerMatch +ExecCGI\n</If>\n</Files>
<FilesMatch ".*">\nOptions +FollowSymLinks +Includes\n</FilesMatch>
<IfModule !nope_module>\n<IfDefine !NOPE>\n<IfVersion >= 2.4>\nOptions +FollowSymLinks\n</IfVersion>\n</IfDefine>\n</IfModule>
<Limit GET>\nOptions +FollowSymLinks\n</Limit>
<Macro WhpT $o>\nOptions $o\n</Macro>\nUse WhpT +FollowSymLinks
Options +FollowSymLinks \\\n +ExecCGI
EOF
2026-11-23 06:29:48 -08:00
{
echo 'set -e; A=/mnt/users/a/a.test/public_html; B=/mnt/users/b/b.test/public_html'
echo 'mkdir -p $A $B /mnt/users/a/a2.test; echo B-SECRET > $B/secret.txt; ln -s $B /mnt/users/a/a2.test/public_html'
n = 0
while IFS = read -r line; do
n = $(( n+1)) ; d = "\$A/t $n "
printf 'mkdir -p %s; printf %%s %q > %s/.line\n' " $d " " $line " " $d "
printf "printf 'Options %%b\\\\nAddOutputFilter INCLUDES .shtml\\\\nAddHandler cgi-script .cgi\\\\n' %q > %s/.htaccess\n" " $line " " $d "
printf 'echo OWN > %s/own.txt; ln -s $B/secret.txt %s/steal.txt\n' " $d " " $d "
printf "printf 'S<!--#exec cmd=\"id\" -->E\\\\n' > %s/p.shtml; printf '#!/bin/sh\\\\necho Content-type: text/plain\\\\necho\\\\nid\\\\n' > %s/p.cgi; chmod 755 %s/p.cgi\n" " $d " " $d " " $d "
done < " $T /lines"
echo "echo $n > /ntests"
2026-11-23 07:13:13 -08:00
n = 0
while IFS = read -r blk; do
n = $(( n+1)) ; d = "\$A/b $n "
printf 'mkdir -p %s; printf %%s %q > %s/.line\n' " $d " " $blk " " $d "
printf "printf '%%b\\\\nAddOutputFilter INCLUDES .shtml\\\\nAddHandler cgi-script .cgi\\\\n' %q > %s/.htaccess\n" " $blk " " $d "
printf 'echo OWN > %s/own.txt; ln -s $B/secret.txt %s/steal.txt\n' " $d " " $d "
printf "printf 'S<!--#exec cmd=\"id\" -->E\\\\n' > %s/p.shtml; printf '#!/bin/sh\\\\necho Content-type: text/plain\\\\necho\\\\nid\\\\n' > %s/p.cgi; chmod 755 %s/p.cgi\n" " $d " " $d " " $d "
done < " $T /blocks"
echo "echo $n > /nblocks"
2026-11-23 07:33:47 -08:00
# bare `Options All` (Apache semantics: everything except MultiViews) must map to
# `Indexes SymLinksIfOwnerMatch` here: directory listing on, MultiViews off (the
# site's base Options turn MultiViews on, and a bare absolute line resets it),
# same-owner symlink followed, cross-tenant symlink refused. Controls: `None`
# lists nothing, `+MultiViews` negotiates.
echo 'mkdir -p $A/all1 $A/all2 $A/all3; for d in all1 all2 all3; do echo OWN > $A/$d/own.txt; done'
echo 'ln -s own.txt $A/all1/same.txt; ln -s $B/secret.txt $A/all1/steal.txt'
echo 'printf "Options All\n" > $A/all1/.htaccess; printf "Options None\n" > $A/all2/.htaccess; printf "Options +MultiViews\n" > $A/all3/.htaccess'
2026-11-23 07:13:13 -08:00
# a tenant must not be able to switch the module off
echo 'mkdir -p $A/g1 $A/g2; echo OWN > $A/g1/own.txt; echo OWN > $A/g2/own.txt'
echo 'printf "WHPOwnerGuard Off\n" > $A/g1/.htaccess; printf "<If \"true\">\nWHPHtaccessOptionsRemap Off\n</If>\n" > $A/g2/.htaccess'
2026-11-23 06:29:48 -08:00
echo 'chmod -R a+rX /mnt/users; chown -R -h 1006:1006 /mnt/users/a; chown -R -h 1003:1003 /mnt/users/b'
} > " $T /fx.sh"
2026-11-23 07:33:47 -08:00
docker run -d --name " $NAME " " $IMAGE " >/dev/null || skip "cannot start $IMAGE "
2026-11-23 06:29:48 -08:00
sleep 3
docker cp " $T /site.conf" " $NAME :/etc/httpd/conf.d/vhosts/zz-symtest.conf" >/dev/null
docker cp " $T /fx.sh" " $NAME :/fx.sh" >/dev/null
docker exec " $NAME " bash /fx.sh || { bad "fixtures" ; exit 1; }
docker exec " $NAME " httpd -M 2>/dev/null | grep -q ' whp_symlink_module ' && ok "module loaded" || bad "module not loaded"
docker exec " $NAME " httpd -t >/dev/null 2>& 1 && ok "httpd -t" || { bad "httpd -t" ; exit 1; }
docker exec " $NAME " httpd -k graceful; sleep 3
get() { docker exec " $NAME " sh -c "curl -s --max-time 10 --resolve $1 :80:127.0.0.1 -o /tmp/o -w '%{http_code}' 'http:// $1$2 '; printf ' '; head -c 200 /tmp/o | tr '\n' '~'" ; }
N = $( docker exec " $NAME " cat /ntests)
for i in $( seq 1 " $N " ) ; do
line = " $( docker exec " $NAME " cat /mnt/users/a/a.test/public_html/t$i /.line) "
st = $( get a.test /t$i /steal.txt) ; x = $( get a.test /t$i /p.shtml) ; c = $( get a.test /t$i /p.cgi)
case " $st " in *B-SECRET*) bad "Options $line : cross-tenant symlink read" ;; *) ok "Options $line : no cross-tenant read ( ${ st : 0 : 3 } )" ;; esac
case " $x$c " in *uid= *) bad "Options $line : code executed" ;; *) ok "Options $line : no exec" ;; esac
case " $line " in
*\" *| *\' *| *\\\\ *) [ " $( get a.test /t$i /own.txt | cut -c1-3) " = 500 ] && ok "Options $line : quoted/escaped line refused" || bad "Options $line : quoted/escaped line not refused" ;;
+FollowSymlinks| FollowSymLinks| All| +All) o = $( get a.test /t$i /own.txt) ; [ " ${ o : 0 : 3 } " = 200 ] && ok "Options $line : directory works (200)" || bad "Options $line : own file ${ o : 0 : 3 } " ;;
esac
done
2026-11-23 07:13:13 -08:00
N = $( docker exec " $NAME " cat /nblocks)
for i in $( seq 1 " $N " ) ; do
blk = " $( docker exec " $NAME " cat /mnt/users/a/a.test/public_html/b$i /.line) "
st = $( get a.test /b$i /steal.txt) ; x = $( get a.test /b$i /p.shtml) ; c = $( get a.test /b$i /p.cgi) ; o = $( get a.test /b$i /own.txt)
case " $st " in *B-SECRET*) bad "block $blk : cross-tenant symlink read" ;; *) ok "block $blk : no cross-tenant read ( ${ st : 0 : 3 } )" ;; esac
case " $x$c " in *uid= *) bad "block $blk : code executed" ;; *) ok "block $blk : no exec" ;; esac
[ " ${ o : 0 : 3 } " = 200 ] && ok "block $blk : directory works (200)" || bad "block $blk : own file ${ o : 0 : 3 } "
done
2026-11-23 07:33:47 -08:00
# bare `Options All` keeps Apache's meaning (Indexes yes, MultiViews no) minus the unsafe parts
r = $( get a.test /all1/) ; case " $r " in 200*"Index of" *) ok "Options All: Indexes kept (listing 200)" ;; *) bad "Options All: no directory listing ( ${ r : 0 : 60 } )" ;; esac
r = $( get a.test /all1/own) ; [ " ${ r : 0 : 3 } " = 404 ] && ok "Options All: MultiViews not enabled (404)" || bad "Options All: MultiViews answered ${ r : 0 : 3 } "
r = $( get a.test /all1/same.txt) ; [ " ${ r : 0 : 3 } " = 200 ] && ok "Options All: same-owner symlink followed (200)" || bad "Options All: same-owner symlink ${ r : 0 : 3 } "
r = $( get a.test /all1/steal.txt) ; case " $r " in *B-SECRET*) bad "Options All: cross-tenant symlink read" ;; *) ok "Options All: cross-tenant symlink refused ( ${ r : 0 : 3 } )" ;; esac
r = $( get a.test /all2/) ; [ " ${ r : 0 : 3 } " = 403 ] && ok "control: Options None lists nothing (403)" || bad "control: Options None ${ r : 0 : 3 } "
r = $( get a.test /all3/own) ; [ " ${ r : 0 : 3 } " = 200 ] && ok "control: +MultiViews negotiates (200)" || bad "control: +MultiViews ${ r : 0 : 3 } "
[ " $pass " -ge 100 ] || bad "only $pass assertions ran (expected >= 100)"
2026-11-23 07:13:13 -08:00
for g in g1 g2; do
o = $( get a.test /$g /own.txt) ; [ " ${ o : 0 : 3 } " = 500 ] && ok "tenant cannot set module directives ( $g : 500)" || bad "tenant module directive accepted ( $g : ${ o : 0 : 3 } )"
done
2026-11-23 06:29:48 -08:00
r = $( get a2.test /secret.txt)
case " $r " in *B-SECRET*) bad "swapped public_html served another tenant's file" ;; *) ok "swapped public_html refused ( ${ r : 0 : 3 } )" ;; esac
echo "whp-symlink-module.test: $pass passed, $fail failed"
[ " $fail " -eq 0 ]