Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

fix(install): the firewall bootstrap never ran on a stock Ubuntu box

Juan Denis27dec7ddb811
diff --git a/install.sh b/install.sh
index cc442279..27010afa 100644
--- a/install.sh
+++ b/install.sh
@@ -1518,7 +1518,17 @@ bootstrap_firewall() {
if [ "${OS_FAMILY:-}" != "debian" ]; then
return 0
fi
- if [ "$(firewall_detect)" != "none" ]; then
+ # "Is this box already protected", NOT "does a firewall binary exist".
+ #
+ # This used to ask firewall_detect() != none, which was wrong in the way
+ # that matters: firewall_detect returns `nftables` when the nft binary is
+ # merely present and can list an empty ruleset, so on a stock Ubuntu cloud
+ # image — precisely the box this function exists for — the bootstrap
+ # returned here silently and never installed anything. Caught on a real
+ # 24.04 VM: the install logged "Opening HTTP/HTTPS (80, 443) via nftables"
+ # while every line below went unprinted.
+ if firewall_inbound_default_deny; then
+ good "Inbound traffic is already filtered by default — leaving the firewall alone."
return 0
fi
diff --git a/scripts/lib/firewall.sh b/scripts/lib/firewall.sh
index 3d0427cc..aeb1432b 100644
--- a/scripts/lib/firewall.sh
+++ b/scripts/lib/firewall.sh
@@ -36,9 +36,79 @@ _fw_run() {
"$@" >/dev/null 2>&1
}
+# Is inbound traffic already filtered by default on this host?
+#
+# Distinct from firewall_detect() on purpose, and the difference is load-bearing.
+# firewall_detect answers "which backend do I open a port THROUGH" — it happily
+# returns `nftables` when the nft binary merely exists and can list an EMPTY
+# ruleset, which is true of a stock Ubuntu/Debian cloud image. Using it as an
+# "is a firewall already protecting this box" test therefore reports every
+# modern Linux as protected, and a default-deny bootstrap gated on it never
+# runs (found on a real Ubuntu 24.04 VM, where install logged "via nftables"
+# and the bootstrap said nothing at all).
+#
+# This asks the question that actually matters, and the only one the bootstrap
+# cares about: does the host already DENY inbound by default? Presence of rules
+# is not the test — Docker installs plenty of nft/iptables chains and protects
+# nothing — a default-deny policy on the input hook is.
+#
+# Returns 0 when inbound is default-denied, 1 otherwise (including when it
+# cannot tell: an unprotected box is the safer assumption for a caller whose
+# response is to *offer* protection, and every caller re-checks before acting).
+firewall_inbound_default_deny() {
+ # firewalld: running means a zone policy is in force; its default zones all
+ # reject unsolicited inbound.
+ if command -v firewall-cmd >/dev/null 2>&1; then
+ case "$(firewall-cmd --state 2>/dev/null || true)" in
+ *running*) return 0 ;;
+ esac
+ fi
+
+ # ufw: active AND its default incoming policy is deny/reject. `ufw status
+ # verbose` prints e.g. "Default: deny (incoming), allow (outgoing)".
+ if command -v ufw >/dev/null 2>&1; then
+ local ufw_out
+ ufw_out="$(ufw status verbose 2>/dev/null || true)"
+ case "$ufw_out" in
+ *[Ss]tatus:*[Ii]nactive*) : ;;
+ *[Ss]tatus:*[Aa]ctive*)
+ case "$ufw_out" in
+ *[Dd]efault:*deny\ \(incoming\)*|*[Dd]efault:*reject\ \(incoming\)*)
+ return 0 ;;
+ esac
+ ;;
+ esac
+ fi
+
+ # nftables: a base chain hooked to input whose policy is drop or reject.
+ # An empty ruleset, or Docker's nat/filter chains, match neither.
+ if command -v nft >/dev/null 2>&1; then
+ local nft_out
+ nft_out="$(nft -a list ruleset 2>/dev/null || true)"
+ if printf '%s' "$nft_out" \
+ | tr '\n' ' ' \
+ | grep -qE 'hook[[:space:]]+input[^}]*policy[[:space:]]+(drop|reject)'; then
+ return 0
+ fi
+ fi
+
+ # iptables/ip6tables: an INPUT policy that is not ACCEPT.
+ if command -v iptables >/dev/null 2>&1; then
+ case "$(iptables -S INPUT 2>/dev/null | head -1 || true)" in
+ *'-P INPUT DROP'*|*'-P INPUT REJECT'*) return 0 ;;
+ esac
+ fi
+
+ return 1
+}
+
# Detect the active firewall. Order matters: a box can have several of these
# binaries installed but only one actually managing the input chain, so we pick
# the highest-level manager that reports itself active.
+#
+# NOTE: this answers "which backend should I open a port through", NOT "is this
+# box protected" — it returns a backend for a binary that exists even with no
+# rules at all. For the latter use firewall_inbound_default_deny() above.
firewall_detect() {
if [ -n "${FIREWALL_BACKEND:-}" ]; then
printf '%s\n' "$FIREWALL_BACKEND"
diff --git a/scripts/test/stubs.sh b/scripts/test/stubs.sh
index e48e1a66..6a0e98d6 100644
--- a/scripts/test/stubs.sh
+++ b/scripts/test/stubs.sh
@@ -45,6 +45,24 @@ make_stub_exit() {
done
}
+# make_stub_stdout <bindir> <name> [exit-code] — a stub whose stdout is read
+# from THIS function's stdin, ignoring whatever arguments it is called with.
+#
+# Firewall detection reads the *text* of `ufw status verbose` and
+# `nft list ruleset` — "Status: active", "Default: deny (incoming)", a chain
+# policy — so a canned transcript is the only faithful fixture for it. An
+# exit-code stub cannot express "the tool ran fine and reported nothing", which
+# is exactly the stock-cloud-image case that matters.
+make_stub_stdout() {
+ local dir="$1" name="$2" code="${3:-0}"
+ {
+ printf '#!/usr/bin/env bash\ncat <<%s\n' "'STUB_EOF'"
+ cat
+ printf 'STUB_EOF\nexit %s\n' "$code"
+ } > "$dir/$name"
+ chmod +x "$dir/$name"
+}
+
# make_stub_ok <bindir> <name...> — happy no-op stubs (exit 0).
make_stub_ok() {
local dir="$1"
diff --git a/scripts/test/test_install.sh b/scripts/test/test_install.sh
index c0e53a71..b93da492 100644
--- a/scripts/test/test_install.sh
+++ b/scripts/test/test_install.sh
@@ -1610,21 +1610,129 @@ else
bad "bootstrap_firewall would lock the panel out on PANEL_PORT=8443: [$(printf '%s' "$out" | tr '\n' ' ')]"
fi
-# -- bootstrap: an ALREADY-ACTIVE firewall is the operator's. Open ports on it
-# (configure_firewall's job), never rewrite its default policy.
-out="$( set -Eeuo pipefail; PATH="$t42/bin:$PATH"
- OS_FAMILY=debian; FW_DRY_RUN=1; FIREWALL_BACKEND=firewalld
- SSHD_CONFIG_ROOT="$t42/ssh"
- detect_container() { printf ''; }
- pkg_add() { :; }
+# --------------------------------------------------------------------------
+# "Is this box already protected?" — the question the bootstrap's skip-guard
+# has to ask. It originally asked firewall_detect() != none, which answers a
+# DIFFERENT question ("which backend do I open a port through") and returns
+# `nftables` for a bare nft binary with an empty ruleset. Every case below was
+# written after a real Ubuntu 24.04 VM install logged "via nftables" while the
+# bootstrap printed nothing at all.
+# --------------------------------------------------------------------------
+fw_probe_dir() { # fw_probe_dir <name> -> fresh bin dir on stdout
+ local d="$t42/fw-$1"; rm -rf "$d"; mkdir -p "$d"; printf '%s' "$d"
+}
+run_bootstrap() { # run_bootstrap <bindir>
+ ( set -Eeuo pipefail; PATH="$1:$PATH"
+ OS_FAMILY=debian; FW_DRY_RUN=1
+ SSHD_CONFIG_ROOT="$t42/ssh"
+ detect_container() { printf ''; }
+ pkg_add() { :; }
+ source "$LIB_DIR/firewall.sh"
+ bootstrap_firewall 2>&1 )
+}
+
+# -- The two predicates answer DIFFERENT questions, and conflating them is what
+# broke this. Pinned at the predicate level so the distinction survives any
+# future refactor of either one.
+d="$(fw_probe_dir predicates)"
+make_stub_stdout "$d" nft 0 </dev/null
+make_stub_stdout "$d" ufw 0 <<'EOF'
+Status: inactive
+EOF
+res="$( set -Eeuo pipefail; PATH="$d:$PATH"
source "$LIB_DIR/firewall.sh"
- bootstrap_firewall 2>&1 )"
-if [ -z "$out" ]; then
+ printf '%s/' "$(firewall_detect)"
+ firewall_inbound_default_deny && printf 'protected' || printf 'unprotected' )"
+if [ "$res" = "nftables/unprotected" ]; then
+ ok "firewall_detect names a backend where inbound_default_deny sees no protection"
+else
+ bad "expected [nftables/unprotected], got [$res]"
+fi
+
+# -- THE REGRESSION: stock cloud image — nft binary present, ruleset EMPTY, ufw
+# installed but inactive. Nothing is filtering, so the bootstrap MUST run.
+d="$(fw_probe_dir empty)"
+make_stub_stdout "$d" nft 0 </dev/null
+make_stub_stdout "$d" ufw 0 <<'EOF'
+Status: inactive
+EOF
+out="$(run_bootstrap "$d")"
+if printf '%s' "$out" | grep -q 'ufw allow'; then
+ ok "bootstrap_firewall runs on a box whose nft ruleset is empty (stock-image regression)"
+else
+ bad "bootstrap_firewall skipped an UNPROTECTED box: [$(printf '%s' "$out" | tr '\n' ' ')]"
+fi
+
+# -- Docker's chains are not a host firewall. A box running Docker has plenty of
+# nft rules and still accepts every inbound port.
+d="$(fw_probe_dir docker)"
+make_stub_stdout "$d" nft 0 <<'EOF'
+table ip nat {
+ chain DOCKER {
+ iifname "docker0" counter packets 0 bytes 0 return
+ }
+}
+table ip filter {
+ chain FORWARD {
+ type filter hook forward priority filter; policy drop;
+ }
+}
+EOF
+make_stub_stdout "$d" ufw 0 <<'EOF'
+Status: inactive
+EOF
+out="$(run_bootstrap "$d")"
+if printf '%s' "$out" | grep -q 'ufw allow'; then
+ ok "bootstrap_firewall is not fooled by Docker's nftables chains"
+else
+ bad "Docker's rules read as a protected box: [$(printf '%s' "$out" | tr '\n' ' ')]"
+fi
+
+# -- A real default-deny nft policy on the INPUT hook IS protection: hands off.
+d="$(fw_probe_dir nftdeny)"
+make_stub_stdout "$d" nft 0 <<'EOF'
+table inet filter {
+ chain input {
+ type filter hook input priority filter; policy drop;
+ tcp dport 22 accept
+ }
+}
+EOF
+out="$(run_bootstrap "$d")"
+if printf '%s' "$out" | grep -qi 'already filtered'; then
+ ok "bootstrap_firewall leaves a real nftables default-deny alone"
+else
+ bad "bootstrap_firewall did not respect an nft input policy drop: [$(printf '%s' "$out" | tr '\n' ' ')]"
+fi
+
+# -- An ALREADY-ACTIVE ufw is the operator's. Open ports on it
+# (configure_firewall's job), never rewrite its default policy.
+d="$(fw_probe_dir ufwactive)"
+make_stub_stdout "$d" ufw 0 <<'EOF'
+Status: active
+Default: deny (incoming), allow (outgoing), disabled (routed)
+EOF
+out="$(run_bootstrap "$d")"
+if printf '%s' "$out" | grep -qi 'already filtered' \
+ && ! printf '%s' "$out" | grep -q 'ufw default deny'; then
ok "bootstrap_firewall leaves an already-active firewall's policy alone"
else
bad "bootstrap_firewall touched an existing firewall: [$(printf '%s' "$out" | tr '\n' ' ')]"
fi
+# -- ufw installed and active but ALLOWING inbound is not protection.
+d="$(fw_probe_dir ufwallow)"
+make_stub_stdout "$d" ufw 0 <<'EOF'
+Status: active
+Default: allow (incoming), allow (outgoing), disabled (routed)
+EOF
+out="$(run_bootstrap "$d")"
+if printf '%s' "$out" | grep -q 'ufw default deny'; then
+ ok "bootstrap_firewall fixes an active ufw that still allows inbound"
+else
+ bad "an allow-inbound ufw read as protected: [$(printf '%s' "$out" | tr '\n' ' ')]"
+fi
+
# -- bootstrap: non-Debian is a no-op (UFW is Debian/Ubuntu; RHEL boots with
# firewalld active and is handled by configure_firewall's open step).
out="$( set -Eeuo pipefail; PATH="$t42/bin:$PATH"