From 0d9e57e012be157d5e6cd7d4dcb9d4d8a490c20b Mon Sep 17 00:00:00 2001 From: jknapp Date: Mon, 23 Nov 2026 07:33:47 -0800 Subject: [PATCH] ci+chore(mod_whp_symlink): remove racy WHPSymlinkWalk prototype; run the probe test in CI; assert Options All mapping - Remove the WHPSymlinkWalk post-walk lstat prototype (check-then-use race by construction; WHPOwnerGuard is the race-free mechanism): hook, directive, per-dir member, header docs. The directive is now an unknown command. - Probe test: WHP_SYMTEST_REQUIRE=1 turns every SKIP into a FAIL (no docker, no image, container does not start) and a minimum assertion count is enforced. New assertions: bare `Options All` keeps Apache semantics (Indexes on, MultiViews off, same-owner symlink followed, cross-tenant refused) plus None/+MultiViews controls (106 assertions). - CI: new pull_request workflow shared-httpd-symlink-pr.yaml (builds amd64 locally, no push/secrets, runs the probe test); build-push.yaml Build-Shared-httpd runs the probe test against the amd64 digest it just pushed, before the arm64 build and tag assembly, so a failure publishes no tag. Test script added to the shellcheck set; wiring tests added. - docs/shared-httpd-symlink-module.md: behaviour, local run, CI. Co-Authored-By: Claude Sonnet 5.5 Claude-Session: https://claude.ai/code/session_0185ukXi4YX5FxJGqhi6qab6 --- .gitea/workflows/build-push.yaml | 16 +++++ .gitea/workflows/shared-httpd-symlink-pr.yaml | 44 ++++++++++++++ configs/whp-symlink.conf | 5 +- docs/shared-httpd-symlink-module.md | 46 +++++++++++++++ scripts/tests/multiarch-workflow.test.py | 25 ++++++++ scripts/tests/whp-symlink-module.test.sh | 29 ++++++++-- src/mod_whp_symlink/mod_whp_symlink.c | 58 +------------------ 7 files changed, 161 insertions(+), 62 deletions(-) create mode 100644 .gitea/workflows/shared-httpd-symlink-pr.yaml create mode 100644 docs/shared-httpd-symlink-module.md diff --git a/.gitea/workflows/build-push.yaml b/.gitea/workflows/build-push.yaml index d5fedd1..d127a8c 100644 --- a/.gitea/workflows/build-push.yaml +++ b/.gitea/workflows/build-push.yaml @@ -91,6 +91,7 @@ jobs: scripts/tests/index-platforms.test.sh \ scripts/tests/lsphp-abi-gate.test.sh \ scripts/tests/lsphp-src-upload-progress.test.sh \ + scripts/tests/whp-symlink-module.test.sh \ .gitea/scripts/resolve-base-image.sh \ .gitea/scripts/plan-legs.sh @@ -960,6 +961,21 @@ jobs: PKG_CACHE=${{ steps.pkgcache.outputs.url }} outputs: type=image,name=repo.anhonesthost.net/cloud-hosting-platform/shared-httpd,push-by-digest=true,name-canonical=true,push=true + # Publish gate for mod_whp_symlink: run the live probe test against the very + # amd64 image that was just pushed (pulled by digest). The step is fatal and + # sits before the arm64 build and the tag-assembly step, so a failed + # assertion (or a missing docker/image: WHP_SYMTEST_REQUIRE=1 turns the + # local SKIP into a FAIL) publishes no tag. Only the untagged by-digest + # blob exists at this point, as after any other failed job. + - name: Probe test (mod_whp_symlink, amd64 image by digest) + env: + AMD64_DIGEST: ${{ steps.amd64.outputs.digest }} + run: | + set -euo pipefail + ref="repo.anhonesthost.net/cloud-hosting-platform/shared-httpd@${AMD64_DIGEST}" + docker pull "$ref" + WHP_SYMTEST_REQUIRE=1 ./scripts/tests/whp-symlink-module.test.sh "$ref" + # arm64: best effort, built under QEMU on this same trusted x86 runner (no # secret ever leaves it). Same Dockerfile, same build-args (incl. the php.net # EXPECT_PHP_MIN gate, which runs `php -r` on the arm64 binary). Must never diff --git a/.gitea/workflows/shared-httpd-symlink-pr.yaml b/.gitea/workflows/shared-httpd-symlink-pr.yaml new file mode 100644 index 0000000..e995208 --- /dev/null +++ b/.gitea/workflows/shared-httpd-symlink-pr.yaml @@ -0,0 +1,44 @@ +name: shared-httpd mod_whp_symlink PR check +run-name: mod_whp_symlink probe test for ${{ gitea.actor }} + +## Pull-request gate for the shared-httpd tenant-symlink module. build-push.yaml +## only runs on trunk (and publishes); this builds the amd64 shared-httpd image +## locally (nothing is pushed, no secrets) and runs the live probe test against +## it. WHP_SYMTEST_REQUIRE=1: a missing docker/image or a failed assertion +## fails the job. Run the same thing locally: docs/shared-httpd-symlink-module.md. +on: + pull_request: + paths: + - 'src/mod_whp_symlink/**' + - 'configs/whp-symlink.conf' + - 'Dockerfile.shared-httpd' + - 'scripts/tests/whp-symlink-module.test.sh' + - '.gitea/workflows/shared-httpd-symlink-pr.yaml' + +jobs: + Symlink-Module-Probe: + runs-on: ubuntu-latest + timeout-minutes: 40 + steps: + - name: Checkout + uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + + # Runner images differ on whether they are root and whether sudo exists. + - name: Install shellcheck + run: | + if ! command -v shellcheck >/dev/null 2>&1; then + (apt-get update && apt-get install -y shellcheck) || + (sudo apt-get update && sudo apt-get install -y shellcheck) + fi + shellcheck --version + + - name: Lint the probe test + run: | + bash -n scripts/tests/whp-symlink-module.test.sh + shellcheck -S warning scripts/tests/whp-symlink-module.test.sh + + - name: Build shared-httpd (amd64, local only, nothing pushed) + run: docker build -f Dockerfile.shared-httpd -t shared-httpd:pr-probe . + + - name: Probe test (fails the job on any failed assertion) + run: WHP_SYMTEST_REQUIRE=1 ./scripts/tests/whp-symlink-module.test.sh shared-httpd:pr-probe diff --git a/configs/whp-symlink.conf b/configs/whp-symlink.conf index fb46557..612f466 100644 --- a/configs/whp-symlink.conf +++ b/configs/whp-symlink.conf @@ -6,7 +6,10 @@ # rewritten before the core parses it (FollowSymLinks -> SymLinksIfOwnerMatch, # Includes -> IncludesNOEXEC, ExecCGI dropped), so stock CMS files # (CodeIgniter 4, Joomla, OpenCart, Magento) stop answering 500 and no - # tenant can switch owner-unchecked symlinks back on. + # tenant can switch owner-unchecked symlinks back on. Bare `Options All` + # keeps Apache's meaning minus the unsafe parts: it becomes + # `Indexes SymLinksIfOwnerMatch` (All includes Indexes and excludes + # MultiViews, so Indexes stays on and MultiViews stays off). WHPHtaccessOptionsRemap On # Owner check on the file descriptor actually served (the Bluehost/cPanel diff --git a/docs/shared-httpd-symlink-module.md b/docs/shared-httpd-symlink-module.md new file mode 100644 index 0000000..064d29f --- /dev/null +++ b/docs/shared-httpd-symlink-module.md @@ -0,0 +1,46 @@ +# shared-httpd: mod_whp_symlink (tenant symlink safety) + +Source: `src/mod_whp_symlink/mod_whp_symlink.c`, built in a separate stage of +`Dockerfile.shared-httpd` against the distribution `httpd-devel` (the runtime keeps the +stock AlmaLinux httpd RPMs). Defaults: `configs/whp-symlink.conf`. + +## What it does + +- `WHPHtaccessOptionsRemap On`: rewrites a tenant `.htaccess` `Options` line before the + core parses it, deny-by-default (only known-safe keywords are emitted, a name is only + emitted when `AllowOverride Options=` permits it): + - any FollowSymLinks form becomes `SymLinksIfOwnerMatch`; + - any `Includes` form becomes `IncludesNOEXEC`, positive `ExecCGI` is dropped; + - bare `All` keeps Apache semantics (All includes Indexes and excludes MultiViews), so + it becomes `Indexes SymLinksIfOwnerMatch` under WHP's `Options=` list; + - quoted/escaped lines are refused (config error, 500, nothing enabled). +- `WHPOwnerGuard Off|Log|On` (+ `WHPOwnerGuardRoot`): fstat() owner check on the file + descriptor actually served; closes the SymLinksIfOwnerMatch check-then-open race and + the swapped-`public_html` case. Ships in Log mode. + +The earlier `WHPSymlinkWalk` prototype (post-walk lstat check) was removed: it is racy by +construction, the owner guard is the race-free mechanism. + +## Probe test + +`scripts/tests/whp-symlink-module.test.sh [image]` starts the image, creates two tenants and +probes every `Options` form, nested containers, the module directives, the swapped docroot +and bare `Options All` (Indexes on, MultiViews off, same-owner symlink followed, +cross-tenant refused). + +Run locally (needs docker, about 5 minutes for the build): + + docker build -f Dockerfile.shared-httpd -t shared-httpd:symtest . + WHP_SYMTEST_REQUIRE=1 ./scripts/tests/whp-symlink-module.test.sh shared-httpd:symtest + +Without `WHP_SYMTEST_REQUIRE=1` a missing docker or image is a SKIP (exit 0); with it, a +FAIL. Any failed assertion exits 1. + +## CI + +- Pull requests touching the module, its config, `Dockerfile.shared-httpd` or the test: + `.gitea/workflows/shared-httpd-symlink-pr.yaml` builds the amd64 image locally (no push, no + secrets) and runs the test with `WHP_SYMTEST_REQUIRE=1`. +- Publish: `Build-Shared-httpd` in `build-push.yaml` runs the same test against the amd64 image + it just pushed by digest, before the arm64 build and the tag-assembly step. A failure + publishes no tag. (amd64 only; the arm64 image is not probed.) diff --git a/scripts/tests/multiarch-workflow.test.py b/scripts/tests/multiarch-workflow.test.py index 865f58a..0ed8088 100755 --- a/scripts/tests/multiarch-workflow.test.py +++ b/scripts/tests/multiarch-workflow.test.py @@ -177,6 +177,31 @@ class Wiring(unittest.TestCase): self.assertIn("default: 'false'", m.group(1)) self.assertIn('type: string', m.group(1)) + def test_symlink_probe_gates_shared_httpd_publish(self): + job = self.jobs['Build-Shared-httpd'] + names = [n for n, _ in steps(job)] + g = [i for i, n in enumerate(names) if n.startswith('Probe test (mod_whp_symlink')] + self.assertEqual(len(g), 1) + self.assertLess(names.index([n for n in names if n.startswith('Build amd64')][0]), g[0]) + self.assertLess(g[0], names.index([n for n in names if n.startswith('Build arm64')][0])) + self.assertLess(g[0], names.index([n for n in names if n.startswith('Assemble')][0])) + st = step(job, 'Probe test (mod_whp_symlink') + self.assertNotIn('continue-on-error', st) + self.assertNotIn('if:', st) + self.assertIn('${{ steps.amd64.outputs.digest }}', st) + self.assertIn('set -euo pipefail', st) + self.assertIn('WHP_SYMTEST_REQUIRE=1 ./scripts/tests/whp-symlink-module.test.sh', st) + + def test_symlink_pr_workflow(self): + with open(os.path.join(ROOT, '.gitea', 'workflows', 'shared-httpd-symlink-pr.yaml')) as f: + t = f.read() + self.assertRegex(t, r'(?m)^on:\n pull_request:\n paths:') + self.assertNotIn('docker push', t) + self.assertNotIn('secrets.', t) + self.assertIn('WHP_SYMTEST_REQUIRE=1 ./scripts/tests/whp-symlink-module.test.sh', t) + self.assertLess(t.index('apt-get update && apt-get install'), t.index('shellcheck -S warning')) + self.assertNotIn('continue-on-error', t) + def test_tag_scheme_unchanged(self): """Run the assemble step's shell with a recording stub: exact tags per version.""" for j, (repo, _, matrix) in SLICE.items(): diff --git a/scripts/tests/whp-symlink-module.test.sh b/scripts/tests/whp-symlink-module.test.sh index 2f2ce2a..d7a26cd 100755 --- a/scripts/tests/whp-symlink-module.test.sh +++ b/scripts/tests/whp-symlink-module.test.sh @@ -5,13 +5,18 @@ ## 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. +## 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 . set -u IMAGE="${1:-shared-httpd:latest}" +REQUIRE="${WHP_SYMTEST_REQUIRE:-0}" +skip() { if [ "$REQUIRE" = 1 ]; then echo "FAIL (required): $*"; exit 1; fi; echo "SKIP: $*"; exit 0; } 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; } +command -v docker >/dev/null 2>&1 || skip "no docker" +docker image inspect "$IMAGE" >/dev/null 2>&1 || skip "no image $IMAGE" fail=0; pass=0 ok() { pass=$((pass+1)); echo "ok $*"; } bad() { fail=$((fail+1)); echo "FAIL $*"; } @@ -108,13 +113,21 @@ EOF 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/blocks" echo "echo $n > /nblocks" + # 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' # 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 "\nWHPHtaccessOptionsRemap Off\n\n" > $A/g2/.htaccess' 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; } +docker run -d --name "$NAME" "$IMAGE" >/dev/null || skip "cannot start $IMAGE" 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 @@ -143,6 +156,14 @@ for i in $(seq 1 "$N"); do 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 +# 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)" 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 diff --git a/src/mod_whp_symlink/mod_whp_symlink.c b/src/mod_whp_symlink/mod_whp_symlink.c index 607bf58..92dca51 100644 --- a/src/mod_whp_symlink/mod_whp_symlink.c +++ b/src/mod_whp_symlink/mod_whp_symlink.c @@ -44,12 +44,6 @@ * listings (names only), and PHP (read by the tenant's own FPM). The * swapped-public_html listing needs WHP's * Options SymLinksIfOwnerMatch; the guard alone does not close it. - * - * 3. WHPSymlinkWalk Off|Log|On (+ WHPOwnerGuardRoot ) - * Prototype of "re-implement SymLinksIfOwnerMatch after the walk": a - * fixups hook lstat()s every component of r->filename below the root and - * refuses (403) when a symlink's owner differs from its target's owner. - * Kept for comparison; see the recommendation document. */ #include "httpd.h" #include "http_config.h" @@ -76,7 +70,6 @@ typedef struct { typedef struct { int guard; - int walk; const char *root; } whp_dir_conf; @@ -98,7 +91,6 @@ static void *create_dir(apr_pool_t *p, char *d) { whp_dir_conf *c = apr_pcalloc(p, sizeof(*c)); c->guard = MODE_UNSET; - c->walk = MODE_UNSET; return c; } @@ -106,7 +98,6 @@ static void *merge_dir(apr_pool_t *p, void *b_, void *a_) { whp_dir_conf *b = b_, *a = a_, *c = apr_pcalloc(p, sizeof(*c)); c->guard = a->guard != MODE_UNSET ? a->guard : b->guard; - c->walk = a->walk != MODE_UNSET ? a->walk : b->walk; c->root = a->root ? a->root : b->root; return c; } @@ -301,11 +292,6 @@ static const char *cmd_guard(cmd_parms *cmd, void *d, const char *arg) return set_mode(arg, &((whp_dir_conf *)d)->guard, 1); } -static const char *cmd_walk(cmd_parms *cmd, void *d, const char *arg) -{ - return set_mode(arg, &((whp_dir_conf *)d)->walk, 1); -} - static const char *cmd_root(cmd_parms *cmd, void *d, const char *arg) { ((whp_dir_conf *)d)->root = arg; @@ -384,45 +370,6 @@ static apr_status_t guard_filter(ap_filter_t *f, apr_bucket_brigade *bb) return ap_pass_brigade(f->next, bb); } -/* ------------------------------------------------------------------ */ -/* 3. Post-walk lstat check (prototype of option b) */ - -static int walk_fixup(request_rec *r) -{ - whp_dir_conf *dc = ap_get_module_config(r->per_dir_config, - &whp_symlink_module); - char *path, *p; - size_t rl; - - if (dc->walk < MODE_LOG || !under_root(dc, r->filename)) - return DECLINED; - path = apr_pstrdup(r->pool, r->filename); - rl = strlen(dc->root); - /* walk every component strictly below the root */ - for (p = path + rl; ; p++) { - if (*p == '/' || *p == '\0') { - char save = *p; - struct stat ls, ts; - *p = '\0'; - if (p > path + rl && lstat(path, &ls) == 0 && S_ISLNK(ls.st_mode)) { - if (stat(path, &ts) != 0 || ts.st_uid != ls.st_uid) { - ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, - "whp_symlink: walk: symlink %s (owner %ld) " - "-> target owner %ld%s", path, - (long)ls.st_uid, (long)ts.st_uid, - dc->walk == MODE_ON ? " (refused)" : " (log only)"); - if (dc->walk == MODE_ON) - return HTTP_FORBIDDEN; - } - } - *p = save; - if (!save) - break; - } - } - return DECLINED; -} - static const command_rec cmds[] = { AP_INIT_RAW_ARGS("Options", options_remap, NULL, OR_OPTIONS, "(whp_symlink) .htaccess Options remap; the core " @@ -431,10 +378,8 @@ static const command_rec cmds[] = { "On|Off: remap FollowSymLinks/Includes/ExecCGI/All in .htaccess Options"), AP_INIT_TAKE1("WHPOwnerGuard", cmd_guard, NULL, RSRC_CONF | ACCESS_CONF, "On|Log|Off: served file owner must equal DocumentRoot owner"), - AP_INIT_TAKE1("WHPSymlinkWalk", cmd_walk, NULL, RSRC_CONF | ACCESS_CONF, - "On|Log|Off: post-walk symlink owner check"), AP_INIT_TAKE1("WHPOwnerGuardRoot", cmd_root, NULL, RSRC_CONF | ACCESS_CONF, - "path prefix the guard/walk apply to, e.g. /mnt/users/"), + "path prefix the guard applies to, e.g. /mnt/users/"), { NULL } }; @@ -443,7 +388,6 @@ static void register_hooks(apr_pool_t *p) ap_register_output_filter("WHP_OWNER_GUARD", guard_filter, NULL, AP_FTYPE_RESOURCE - 1); ap_hook_insert_filter(guard_insert, NULL, NULL, APR_HOOK_LAST); - ap_hook_fixups(walk_fixup, NULL, NULL, APR_HOOK_FIRST); } AP_DECLARE_MODULE(whp_symlink) = {