Stop --help from running site commands

`serverkit add-site --help` wrote a vhost for a site named
"--help" and `setup-proxy --help` reran the proxy setup, which
reset the panel's certificate paths. Subcommands that take plain
positionals now print their usage for --help/-h and stop before
any command code runs, and add-site/remove-site refuse a name
that is not a hostname.

Part of #269.
This commit is contained in:
Juan Denis committed 2026-11-22 01:50:18 -05:00
1 parent f68819da02
commit 73e2251e28
2 files changed
+144

No files matched your search

+80
View File
@@ -835,6 +835,86 @@ else
bad "flags lost between wrapper and cli.py:$t20_fail"
fi
# --------------------------------------------------------------------------
# T21 — a help flag on a plain-positional subcommand must print usage and
# change nothing (issue #269). `add-site --help` used to write a vhost for a
# site named "--help"; `setup-proxy --help` reran the proxy setup and reset
# the panel's certificate paths. Runs through the real executed-mode dispatch
# with the root check out of the way, so "it stopped early" can only mean the
# help guard and not a missing-root refusal.
t="$WORK/t21"; mkdir -p "$t/install/scripts" "$t/nginx/sites-available" "$t/nginx/sites-enabled"
cat > "$t/install/scripts/setup-nginx-proxy.sh" <<EOF
#!/usr/bin/env bash
echo ran > "$t/proxy-setup-ran"
EOF
chmod +x "$t/install/scripts/setup-nginx-proxy.sh"
# The dispatch runs with the script's own functions, so the root check is
# neutralised by a wrapper that sources the CLI's functions and replays the
# dispatch block verbatim.
cat > "$t/run.sh" <<EOF
#!/usr/bin/env bash
source "$CLI"
check_root() { :; }
eval "\$(sed -n '/^if \[ \$# -gt 1 \] && \[\[ " \$PLAIN_COMMANDS "/,\$p' "$CLI")"
EOF
run_plain() {
SERVERKIT_DIR="$t/install" SERVERKIT_NGINX_DIR="$t/nginx" bash "$t/run.sh" "$@" 2>&1 </dev/null
}
t21_fail=""
for args in "add-site --help" "add-site -h" "add-site example.com --help" \
"remove-site --help" "setup-proxy --help" "setup-proxy -h" \
"doctor --help" "restore-db --help" "factory-reset --help"; do
# shellcheck disable=SC2086
out="$(run_plain $args)"; rc=$?
cmd="${args%% *}"
if [ "$rc" -ne 0 ] || ! printf '%s' "$out" | grep -q "^Usage: serverkit $cmd"; then
t21_fail="$t21_fail [$args → rc=$rc: $(printf '%s' "$out" | head -n 2 | tr '\n' ' ')]"
fi
done
written="$(find "$t/nginx" -mindepth 2 2>/dev/null | tr '
' ' ')"
if [ -n "$written" ]; then
t21_fail="$t21_fail [a help flag wrote nginx config: $written]"
fi
[ ! -e "$t/proxy-setup-ran" ] || t21_fail="$t21_fail [setup-proxy --help ran the proxy setup]"
if [ -z "$t21_fail" ]; then
ok "--help on a plain subcommand prints its usage and touches nothing"
else
bad "--help had side effects or no usage:$t21_fail"
fi
# T21b — the guard must not swallow the real command: the same wrapper still
# writes the vhost and runs the proxy setup when no help flag is present.
out="$(run_plain add-site t21.example.com 3210)"
run_plain setup-proxy >/dev/null
if [ -f "$t/nginx/sites-available/t21.example.com.conf" ] \
&& grep -q 'proxy_pass http://127.0.0.1:3210;' "$t/nginx/sites-available/t21.example.com.conf" \
&& [ -e "$t/proxy-setup-ran" ]; then
ok "add-site and setup-proxy still do their work without a help flag"
else
bad "the help guard broke the real commands: [$out]"
fi
# T21c — a site name that is not a hostname (a stray flag, a path) is refused
# before anything is written or removed.
t21c_fail=""
for name in "--frobnicate" "../etc/passwd" "a b" ".hidden"; do
out="$(run_plain add-site "$name")"; rc=$?
[ "$rc" -ne 0 ] || t21c_fail="$t21c_fail [add-site '$name' exited 0]"
out="$(run_plain remove-site "$name")"; rc=$?
[ "$rc" -ne 0 ] || t21c_fail="$t21c_fail [remove-site '$name' exited 0]"
done
out="$(run_plain add-site t21c.example.com notaport)"; rc=$?
[ "$rc" -ne 0 ] || t21c_fail="$t21c_fail [add-site with a non-numeric port exited 0]"
if [ "$(ls "$t/nginx/sites-available" | tr '\n' ' ')" != "t21.example.com.conf " ]; then
t21c_fail="$t21c_fail [unexpected vhosts: $(ls "$t/nginx/sites-available" | tr '\n' ' ')]"
fi
if [ -z "$t21c_fail" ]; then
ok "add-site/remove-site refuse a name that is not a hostname"
else
bad "invalid site names got through:$t21c_fail"
fi
# --------------------------------------------------------------------------
printf '\n%d passed, %d failed, %d skipped\n\n' "$PASS" "$FAIL" "$SKIP"
[ "$FAIL" -eq 0 ]