From 4b0aedf5734cf5d46d6d0de9c349389264fd2a9c Mon Sep 17 00:00:00 2001 From: Omarchybot <317366263+omarchybot@users.noreply.github.com> Date: Fri, 9 Oct 2026 04:44:39 +0200 Subject: [PATCH 1/3] Start sshd and open its port only once a key is authorized The setup command enabled sshd and opened port 22 before asking for a key, so a cancelled prompt, a rejected key or a GitHub user with no keys left a reachable server accepting passwords. It now installs openssh and generates host keys, authorizes the key, writes and verifies the hardening drop-in, and only then enables sshd and opens the firewall. Host keys are generated up front because sshd -t refuses to validate without them, and on a fresh machine they would otherwise first appear when the server starts. Co-Authored-By: Codex Medium Co-Authored-By: Claude Opus 5.5 --- bin/omarchy-setup-security-sshd | 28 +++++++---- test/shell.d/setup-security-sshd-test.sh | 59 ++++++++++++++++++++---- 2 files changed, 70 insertions(+), 17 deletions(-) diff --git a/bin/omarchy-setup-security-sshd b/bin/omarchy-setup-security-sshd index 7b93936ff637..ce4ac56a7dda 100755 --- a/bin/omarchy-setup-security-sshd +++ b/bin/omarchy-setup-security-sshd @@ -57,10 +57,21 @@ if [[ -n $KEY && -n $GITHUB_USER ]]; then exit 2 fi -setup_sshd() { - echo "Installing and starting the OpenSSH server..." +install_openssh() { + echo "Installing the OpenSSH server..." omarchy-pkg-add openssh - sudo systemctl enable --now sshd.service + + # sshd validates no config without host keys, and the server is not started + # until its config has been validated. + sudo ssh-keygen -A >/dev/null +} + +# Reload rather than restart a server that is already running, so an +# administrator already connected keeps their session. +start_sshd() { + echo "Starting the OpenSSH server..." + sudo systemctl enable sshd.service + sudo systemctl reload-or-restart sshd.service } open_firewall() { @@ -181,16 +192,13 @@ CONF sudo rm -f "$config" return 1 fi - - # Reload rather than restart so an administrator already connected keeps - # their session. - sudo systemctl reload sshd.service } echo -e "\e[32mSetting up SSH server access with key-based authentication.\n\e[0m" -setup_sshd -open_firewall +# Nothing listens and no port opens until a key is authorized and passwords are +# off, so a cancelled prompt or a rejected key leaves the machine as it was. +install_openssh echo if [[ -n $KEY ]]; then @@ -206,6 +214,8 @@ else fi disable_password_auth +start_sshd +open_firewall echo -e "\e[32m\nPerfect! The SSH server is running and your key is authorized.\e[0m" echo "Password logins are off; this machine now accepts authorized keys only." diff --git a/test/shell.d/setup-security-sshd-test.sh b/test/shell.d/setup-security-sshd-test.sh index b060c5ecbc05..8fa0fa368ca0 100755 --- a/test/shell.d/setup-security-sshd-test.sh +++ b/test/shell.d/setup-security-sshd-test.sh @@ -16,11 +16,22 @@ printf 'pkg %s\n' "$*" >>"${CALL_LOG:?}" STUB cat >"$stub_bin/omarchy-cmd-missing" <<'STUB' #!/bin/bash -exit 0 +exit 1 +STUB +cat >"$stub_bin/ufw" <<'STUB' +#!/bin/bash +printf 'ufw %s\n' "$*" >>"${CALL_LOG:?}" +[[ -e ${TEST_ROOT:?}/etc/ssh/sshd_config.d/10-omarchy-hardening.conf ]] || echo "exposed before hardening" >>"$CALL_LOG" STUB cat >"$stub_bin/systemctl" <<'STUB' #!/bin/bash printf 'systemctl %s\n' "$*" >>"${CALL_LOG:?}" +[[ -e ${TEST_ROOT:?}/etc/ssh/sshd_config.d/10-omarchy-hardening.conf ]] || echo "exposed before hardening" >>"$CALL_LOG" +STUB +cat >"$stub_bin/gum" <<'STUB' +#!/bin/bash +# Esc at the first prompt. +exit 130 STUB cat >"$stub_bin/sshd" <<'STUB' #!/bin/bash @@ -54,6 +65,9 @@ install) rm) /usr/bin/rm -f "${TEST_ROOT:?}${3:?}" ;; +ssh-keygen) + printf 'sudo %s\n' "$*" >>"${CALL_LOG:?}" + ;; *) exec "$@" ;; @@ -65,7 +79,7 @@ ssh-keygen -q -t ed25519 -N "" -f "$test_dir/key" public_key=$(<"$test_dir/key.pub") run_setup() { - local scenario="$1" + local scenario="$1" key="${2:-$public_key}" local home="$test_dir/$scenario/home" local root="$test_dir/$scenario/root" @@ -77,14 +91,28 @@ run_setup() { SSHD_PASSWORD_AUTH="${SSHD_PASSWORD_AUTH:-no}" \ SSHD_KBD_AUTH="${SSHD_KBD_AUTH:-no}" \ PATH="$stub_bin:$PATH" \ - bash "$ROOT/bin/omarchy-setup-security-sshd" --key="$public_key" + bash "$ROOT/bin/omarchy-setup-security-sshd" --key="$key" +} + +# Nothing may listen or be opened while passwords could still be accepted. +assert_nothing_exposed() { + local scenario="$1" + + ! grep -qE "^systemctl (enable|start|restart|reload)" "$test_dir/$scenario.calls" || + fail "SSH setup must not start sshd when $scenario" + ! grep -q "^ufw " "$test_dir/$scenario.calls" || + fail "SSH setup must not open the firewall when $scenario" } output=$(run_setup success) config="$test_dir/success/root/etc/ssh/sshd_config.d/10-omarchy-hardening.conf" grep -qxF "PasswordAuthentication no" "$config" || fail "SSH setup disables password authentication" grep -qxF "KbdInteractiveAuthentication no" "$config" || fail "SSH setup disables keyboard-interactive authentication" -grep -qxF "systemctl reload sshd.service" "$test_dir/success.calls" || fail "SSH setup reloads the validated config" +grep -qxF "sudo ssh-keygen -A" "$test_dir/success.calls" || fail "SSH setup generates host keys before validating" +grep -qxF "systemctl reload-or-restart sshd.service" "$test_dir/success.calls" || fail "SSH setup starts sshd with the validated config" +grep -qxF "ufw limit 22/tcp comment omarchy-sshd" "$test_dir/success.calls" || fail "SSH setup opens the SSH port" +! grep -qxF "exposed before hardening" "$test_dir/success.calls" || fail "SSH setup starts sshd and opens the port only after passwords are off" +grep -qxF "$public_key" "$test_dir/success/home/.ssh/authorized_keys" || fail "SSH setup writes the authorized key" grep -q "Password logins are off" <<<"$output" || fail "SSH setup reports hardening after it succeeds" pass "SSH setup authorizes a key and disables password logins" @@ -99,8 +127,7 @@ if SSHD_PASSWORD_AUTH=yes run_setup ineffective >"$test_dir/ineffective.output" fi [[ ! -e $test_dir/ineffective/root/etc/ssh/sshd_config.d/10-omarchy-hardening.conf ]] || fail "SSH setup removes an ineffective hardening config" -! grep -qF "systemctl reload sshd.service" "$test_dir/ineffective.calls" || - fail "SSH setup must not reload ineffective hardening" +assert_nothing_exposed ineffective ! grep -q "Password logins are off" "$test_dir/ineffective.output" || fail "SSH setup must not claim ineffective hardening succeeded" pass "SSH setup verifies the effective daemon settings" @@ -110,8 +137,24 @@ if SSHD_SYNTAX_VALID=0 run_setup invalid >"$test_dir/invalid.output" 2>&1; then fi [[ ! -e $test_dir/invalid/root/etc/ssh/sshd_config.d/10-omarchy-hardening.conf ]] || fail "SSH setup removes a rejected hardening config" -! grep -qF "systemctl reload sshd.service" "$test_dir/invalid.calls" || - fail "SSH setup must not reload a rejected config" +assert_nothing_exposed invalid ! grep -q "Password logins are off" "$test_dir/invalid.output" || fail "SSH setup must not claim rejected hardening succeeded" pass "SSH setup fails safely when sshd rejects the config" + +if run_setup rejected-key "not-a-key" >"$test_dir/rejected-key.output" 2>&1; then + fail "SSH setup must fail when the key is rejected" +fi +assert_nothing_exposed rejected-key +[[ ! -e $test_dir/rejected-key/root/etc/ssh/sshd_config.d/10-omarchy-hardening.conf ]] || + fail "SSH setup writes no hardening config without a key" +pass "SSH setup starts nothing and opens nothing when no key is authorized" + +mkdir -p "$test_dir/cancelled/home" "$test_dir/cancelled/root" +: >"$test_dir/cancelled.calls" +if HOME="$test_dir/cancelled/home" TEST_ROOT="$test_dir/cancelled/root" CALL_LOG="$test_dir/cancelled.calls" \ + PATH="$stub_bin:$PATH" bash "$ROOT/bin/omarchy-setup-security-sshd" >/dev/null 2>&1; then + fail "SSH setup must fail when the key prompt is cancelled" +fi +assert_nothing_exposed cancelled +pass "SSH setup starts nothing and opens nothing when the key prompt is cancelled" From 725c53375d889902d7a94e8a51e86ac97f8ec6cb Mon Sep 17 00:00:00 2001 From: Ryan Hughes Date: Sun, 1 Nov 2026 01:38:57 -0500 Subject: [PATCH 2/3] Fail the sshd test when host keys come after validation sshd -t and sshd -T exit without host keys, so on a fresh install setup only works if ssh-keygen -A runs first. The test only checked that ssh-keygen ran at all. The sshd stub now refuses to run until it has. Co-authored-by: Donald Dunsmore <215750096+thebytorsnowdog@users.noreply.github.com> --- test/shell.d/setup-security-sshd-test.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/shell.d/setup-security-sshd-test.sh b/test/shell.d/setup-security-sshd-test.sh index 8fa0fa368ca0..60e6b5bd8f58 100755 --- a/test/shell.d/setup-security-sshd-test.sh +++ b/test/shell.d/setup-security-sshd-test.sh @@ -35,6 +35,9 @@ exit 130 STUB cat >"$stub_bin/sshd" <<'STUB' #!/bin/bash +# A fresh install has no host keys until ssh-keygen -A runs, and sshd -t and +# sshd -T refuse to run without them. +[[ -e ${TEST_ROOT:?}/host-keys ]] || exit 1 case $1 in -t) [[ ${SSHD_SYNTAX_VALID:-1} == 1 ]] @@ -66,7 +69,7 @@ rm) /usr/bin/rm -f "${TEST_ROOT:?}${3:?}" ;; ssh-keygen) - printf 'sudo %s\n' "$*" >>"${CALL_LOG:?}" + : >"${TEST_ROOT:?}/host-keys" ;; *) exec "$@" @@ -108,7 +111,6 @@ output=$(run_setup success) config="$test_dir/success/root/etc/ssh/sshd_config.d/10-omarchy-hardening.conf" grep -qxF "PasswordAuthentication no" "$config" || fail "SSH setup disables password authentication" grep -qxF "KbdInteractiveAuthentication no" "$config" || fail "SSH setup disables keyboard-interactive authentication" -grep -qxF "sudo ssh-keygen -A" "$test_dir/success.calls" || fail "SSH setup generates host keys before validating" grep -qxF "systemctl reload-or-restart sshd.service" "$test_dir/success.calls" || fail "SSH setup starts sshd with the validated config" grep -qxF "ufw limit 22/tcp comment omarchy-sshd" "$test_dir/success.calls" || fail "SSH setup opens the SSH port" ! grep -qxF "exposed before hardening" "$test_dir/success.calls" || fail "SSH setup starts sshd and opens the port only after passwords are off" From a405dfd87d79eabb05b1444b30cbd4c247fd160f Mon Sep 17 00:00:00 2001 From: Ryan Hughes Date: Sun, 1 Nov 2026 01:39:17 -0500 Subject: [PATCH 3/3] Make the cancelled sshd test prove it reached the prompt On quattro the prompt labels come from omarchy-i18n, and the test only found it through an installed Omarchy. Without one, setup exited before gum and the test still passed. Put the checkout's bin on PATH and check that gum was called. Co-Authored-By: Codex gpt-6.1-sol --- test/shell.d/setup-security-sshd-test.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/shell.d/setup-security-sshd-test.sh b/test/shell.d/setup-security-sshd-test.sh index 60e6b5bd8f58..230916e39bbc 100755 --- a/test/shell.d/setup-security-sshd-test.sh +++ b/test/shell.d/setup-security-sshd-test.sh @@ -30,6 +30,7 @@ printf 'systemctl %s\n' "$*" >>"${CALL_LOG:?}" STUB cat >"$stub_bin/gum" <<'STUB' #!/bin/bash +printf 'gum %s\n' "$1" >>"${CALL_LOG:?}" # Esc at the first prompt. exit 130 STUB @@ -155,8 +156,9 @@ pass "SSH setup starts nothing and opens nothing when no key is authorized" mkdir -p "$test_dir/cancelled/home" "$test_dir/cancelled/root" : >"$test_dir/cancelled.calls" if HOME="$test_dir/cancelled/home" TEST_ROOT="$test_dir/cancelled/root" CALL_LOG="$test_dir/cancelled.calls" \ - PATH="$stub_bin:$PATH" bash "$ROOT/bin/omarchy-setup-security-sshd" >/dev/null 2>&1; then + PATH="$stub_bin:$ROOT/bin:$PATH" bash "$ROOT/bin/omarchy-setup-security-sshd" >/dev/null 2>&1; then fail "SSH setup must fail when the key prompt is cancelled" fi +grep -q "^gum choose" "$test_dir/cancelled.calls" || fail "SSH setup reaches the key prompt" assert_nothing_exposed cancelled pass "SSH setup starts nothing and opens nothing when the key prompt is cancelled"