diff --git a/scripts/test/test_cli.sh b/scripts/test/test_cli.sh index ab5995ff..80d999c3 100644 --- a/scripts/test/test_cli.sh +++ b/scripts/test/test_cli.sh @@ -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" < "$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" <&1 /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 ] diff --git a/serverkit b/serverkit index ade58df7..723768e8 100644 --- a/serverkit +++ b/serverkit @@ -1084,6 +1084,13 @@ reload_nginx_or_warn() { print_warning "Could not reload nginx — apply the change manually: systemctl reload nginx (or nginx -s reload)" } +# A site name becomes a file under the nginx dir and a server_name, so it has +# to look like a hostname. Anything else — a stray flag above all — is +# refused before a single file is written or removed. +valid_site_domain() { + [[ "$1" =~ ^[A-Za-z0-9]([A-Za-z0-9.-]*[A-Za-z0-9])?$ ]] +} + cmd_add_site() { check_root @@ -1099,6 +1106,18 @@ cmd_add_site() { exit 1 fi + if ! valid_site_domain "$DOMAIN"; then + print_error "Not a valid domain: $DOMAIN" + echo "Usage: serverkit add-site [port]" + exit 1 + fi + + if ! [[ "$PORT" =~ ^[0-9]+$ ]]; then + print_error "Not a valid port: $PORT" + echo "Usage: serverkit add-site [port]" + exit 1 + fi + resolve_site_paths "$DOMAIN" if [ -f "$SITE_FILE" ]; then @@ -1554,6 +1573,12 @@ cmd_remove_site() { exit 1 fi + if ! valid_site_domain "$DOMAIN"; then + print_error "Not a valid domain: $DOMAIN" + echo "Usage: serverkit remove-site " + exit 1 + fi + resolve_site_paths "$DOMAIN" if [ ! -f "$SITE_FILE" ]; then @@ -1575,6 +1600,40 @@ cmd_remove_site() { fi } +# Subcommands that take plain positional arguments (or none) and parse no +# flags of their own. A help flag used to fall through to them as a +# positional: `add-site --help` wrote a vhost for a site named "--help", and +# `setup-proxy --help` reran the proxy setup, resetting the panel's +# certificate paths. For these, --help/-h anywhere prints usage and stops +# before any command code runs. Commands missing from this list parse their +# own flags (update, uninstall, cleanup-apps) or hand them to cli.py. +PLAIN_COMMANDS="start stop restart status logs branch check-update setup-code init-db migrate-db factory-reset backup-db restore-db generate-keys config setup-proxy create-app start-app stop-app app-logs add-site list-sites remove-site doctor completion" + +wants_help() { + local arg + for arg in "$@"; do + case "$arg" in + --help|-h) return 0 ;; + esac + done + return 1 +} + +command_usage() { + local cmd="$1" args="" + case "$cmd" in + logs) args=" [backend|frontend|nginx|all]" ;; + restore-db) args=" " ;; + start-app|stop-app|app-logs) args=" " ;; + add-site) args=" [port]" ;; + remove-site) args=" " ;; + doctor) args=" [--fix]" ;; + esac + echo "Usage: serverkit ${cmd}${args}" + # The command's one-line description from the main help, when it has one. + cmd_help 2>/dev/null | grep -m1 -E "^ ${cmd}( |\$)" | sed -E "s/^ ${cmd} +/ /" || true +} + # Main # # Sourcing this file (e.g. from scripts/test/test_cli.sh) exposes every @@ -1582,6 +1641,11 @@ cmd_remove_site() { # pattern as scripts/update.sh. Only direct execution runs the dispatch below. [ "${BASH_SOURCE[0]}" = "${0}" ] || return 0 +if [ $# -gt 1 ] && [[ " $PLAIN_COMMANDS " == *" $1 "* ]] && wants_help "${@:2}"; then + command_usage "$1" + exit 0 +fi + case "${1:-help}" in help|--help|-h) cmd_help