diff --git a/scripts/tests/whp-symlink-module.test.sh b/scripts/tests/whp-symlink-module.test.sh new file mode 100755 index 0000000..c7e4189 --- /dev/null +++ b/scripts/tests/whp-symlink-module.test.sh @@ -0,0 +1,116 @@ +#!/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) +## SKIPs (exit 0) without docker or the image. +set -u +IMAGE="${1:-shared-httpd:latest}" +NAME="whp-symtest-$$"; T="$(mktemp -d)" +trap 'docker rm -f "$NAME" >/dev/null 2>&1; rm -rf "$T"' EXIT +command -v docker >/dev/null 2>&1 || { echo "SKIP: no docker"; exit 0; } +docker image inspect "$IMAGE" >/dev/null 2>&1 || { echo "SKIP: no image $IMAGE"; exit 0; } +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' + + Options SymLinksIfOwnerMatch + + + Options Indexes SymLinksIfOwnerMatch MultiViews + AllowOverride AuthConfig FileInfo Indexes Limit Options=Indexes,MultiViews,SymLinksIfOwnerMatch,ExecCGI + Require all granted + + + Options Indexes SymLinksIfOwnerMatch MultiViews + Require all granted + + + ServerName a.test + DocumentRoot "/mnt/users/a/a.test/public_html" + + + ServerName a2.test + DocumentRoot "/mnt/users/a/a2.test/public_html" + + + WHPOwnerGuard On + +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 +{ + 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 'SE\\\\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" + 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" + +docker run -d --name "$NAME" "$IMAGE" >/dev/null || { echo "SKIP: cannot start $IMAGE"; exit 0; } +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 +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 ] diff --git a/src/mod_whp_symlink/mod_whp_symlink.c b/src/mod_whp_symlink/mod_whp_symlink.c index 76beeba..065bdeb 100644 --- a/src/mod_whp_symlink/mod_whp_symlink.c +++ b/src/mod_whp_symlink/mod_whp_symlink.c @@ -120,11 +120,25 @@ static void push(apr_array_header_t *out, char action, const char *w) action ? apr_pstrcat(out->pool, buf, w, NULL) : w; } +/* Push `name` with prefix `action` only when AllowOverride Options= permits + * the option bits `bits`; used for every name this module synthesises. */ +static void push_if(apr_array_header_t *out, cmd_parms *cmd, char action, + const char *name, int bits) +{ + if ((cmd->override_opts & bits) == bits) + push(out, action, name); +} + +/* Deny by default: every input word is matched against the complete list of + * core Options keywords (server/core.c set_options); only known-safe names + * are ever emitted, unknown words are dropped, and a line containing quote + * or escape characters is refused outright, because ap_getword_conf() + * unquotes and a re-joined line could tokenize differently in the core. */ static const char *options_remap(cmd_parms *cmd, void *dummy, const char *l) { whp_srv_conf *sc; apr_array_header_t *words, *out; - int i, follow_on = 0, merge = 0, changed = 0, owner_ok, noexec_ok; + int i, follow_on = 0, changed = 0, any_merge = 0, absolute = 0; const char *orig = l; char *res; @@ -134,24 +148,31 @@ static const char *options_remap(cmd_parms *cmd, void *dummy, const char *l) if (!sc || sc->remap != MODE_ON) return NULL; + if (strpbrk(l, "\"'\\")) { + return "Options: quoted or escaped option names are not supported " + "on this server (whp_symlink)"; + } + words = apr_array_make(cmd->temp_pool, 8, sizeof(char *)); while (*l) { char *w = ap_getword_conf(cmd->temp_pool, &l); + const char *n = w; if (!*w) continue; + if (*n == '+' || *n == '-') + n++; + if (*n == '\0' || strpbrk(n, " \t\r\n\"'\\")) + return "Options: malformed option word (whp_symlink)"; + if (words->nelts == 0 && n == w) + absolute = 1; + if (n != w) + any_merge = 1; *(char **)apr_array_push(words) = w; - if ((w[0] == '+' && !ap_cstr_casecmp(w + 1, "FollowSymLinks")) - || !ap_cstr_casecmp(w, "FollowSymLinks")) + if (*w != '-' && (!ap_cstr_casecmp(n, "FollowSymLinks") + || !ap_cstr_casecmp(n, "All"))) follow_on = 1; - if (w[0] == '+' || w[0] == '-') - merge = 1; } - /* Synthesised names are only emitted when AllowOverride Options= - * permits them, so the rewritten line never fails the core's check. */ - owner_ok = (cmd->override_opts & OPT_SYM_OWNER) != 0; - noexec_ok = (cmd->override_opts & OPT_INCLUDES) != 0; - out = apr_array_make(cmd->temp_pool, 8, sizeof(char *)); for (i = 0; i < words->nelts; i++) { const char *w = ((char **)words->elts)[i]; @@ -159,55 +180,93 @@ static const char *options_remap(cmd_parms *cmd, void *dummy, const char *l) if (*w == '+' || *w == '-') action = *w++; - if (!ap_cstr_casecmp(w, "FollowSymLinks")) { - changed = 1; - if (action != '-' && owner_ok) + if (!ap_cstr_casecmp(w, "Indexes")) { + push(out, action, "Indexes"); + } + else if (!ap_cstr_casecmp(w, "MultiViews")) { + push(out, action, "MultiViews"); + } + else if (!ap_cstr_casecmp(w, "IncludesNOEXEC")) { + push(out, action, "IncludesNOEXEC"); + } + else if (!ap_cstr_casecmp(w, "SymLinksIfOwnerMatch")) { + if (action == '-' && follow_on) + changed = 1; /* "-SymLinksIfOwnerMatch +FollowSymLinks" */ + else push(out, action, "SymLinksIfOwnerMatch"); } - else if (!ap_cstr_casecmp(w, "SymLinksIfOwnerMatch") - && action == '-' && follow_on) { + else if (!ap_cstr_casecmp(w, "FollowSymLinks")) { changed = 1; + if (action != '-') + push_if(out, cmd, action, "SymLinksIfOwnerMatch", OPT_SYM_OWNER); } else if (!ap_cstr_casecmp(w, "Includes")) { - changed = 1; - if (noexec_ok) - push(out, action, "IncludesNOEXEC"); + changed = 1; /* exec-SSI never; NOEXEC if allowed */ + push_if(out, cmd, action, "IncludesNOEXEC", OPT_INCLUDES); } else if (!ap_cstr_casecmp(w, "ExecCGI")) { if (action == '-') - push(out, action, w); + push(out, action, "ExecCGI"); else - changed = 1; + changed = 1; /* tier policy: never enable CGI */ } else if (!ap_cstr_casecmp(w, "RunScripts")) { - changed = 1; + changed = 1; /* = MultiViews + ExecCGI */ push(out, action, "MultiViews"); + if (action == '-') + push_if(out, cmd, '-', "ExecCGI", OPT_EXECCGI); } - else if (!ap_cstr_casecmp(w, "All") && !action) { + else if (!ap_cstr_casecmp(w, "None")) { + if (action || i != 0) + changed = 1; /* invalid in the core; drop */ + else + push(out, 0, "None"); + } + else if (!ap_cstr_casecmp(w, "All")) { changed = 1; - if (cmd->override_opts & OPT_INDEXES) - push(out, 0, "Indexes"); - if (owner_ok) - push(out, 0, "SymLinksIfOwnerMatch"); - if (noexec_ok) - push(out, 0, "IncludesNOEXEC"); + if (!action && i != 0) { + /* the core rejects this; drop it */ + } + else if (action == '-') { + push_if(out, cmd, '-', "Indexes", OPT_INDEXES); + push_if(out, cmd, '-', "SymLinksIfOwnerMatch", OPT_SYM_OWNER); + push_if(out, cmd, '-', "IncludesNOEXEC", OPT_INCLUDES); + push_if(out, cmd, '-', "ExecCGI", OPT_EXECCGI); + } + else { + /* bare All (first) or +All: the safe subset of All. A bare + * All followed by +/- words becomes "None +x +y" (the core + * accepts +/- after None); otherwise plain bare names. */ + char a = action; + if (!action && any_merge) { + push(out, 0, "None"); + a = '+'; + } + push_if(out, cmd, a, "Indexes", OPT_INDEXES); + push_if(out, cmd, a, "SymLinksIfOwnerMatch", OPT_SYM_OWNER); + push_if(out, cmd, a, "IncludesNOEXEC", OPT_INCLUDES); + } } else { - push(out, action, w); + changed = 1; /* unknown word: deny by default */ + ap_log_error(APLOG_MARK, APLOG_INFO, 0, cmd->server, + "whp_symlink: %s:%d: dropped unknown Options word %s", + cmd->directive->filename, cmd->directive->line_num, w); } } - if (!changed) - return NULL; + /* Always replace the line with the normalised, known-keyword-only form + * (also when nothing was remapped), so the core never sees input words. */ if (out->nelts == 0) - res = merge ? "" : "None"; /* "" = no-op in core set_options */ + res = absolute ? "None" : ""; /* "" = no-op in core set_options */ else res = apr_array_pstrcat(cmd->temp_pool, out, ' '); - ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, cmd->server, - "whp_symlink: %s:%d: Options \"%s\" -> \"%s\"", - cmd->directive->filename, cmd->directive->line_num, - orig, res); + if (changed) + ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, cmd->server, + "whp_symlink: %s:%d: Options \"%s\" -> \"%s\"", + cmd->directive->filename, cmd->directive->line_num, + orig, res); /* The core reads current->args again for its own invocation. */ ((ap_directive_t *)cmd->directive)->args = res; return NULL;