fix: preserve global flags when sudo is sudo-rs

`sudo-rs`, the default sudo on Ubuntu 25.10 and later, does not support preserving the entire environment, so `--quiet`, `--trace`, `--force` and `--app` stopped taking effect once `dokku` re-executed itself as the `dokku` user, and a warning was printed on every invocation. Variables outside the set dokku forwards no longer cross that boundary and can be named in `DOKKU_PRESERVE_ENV` instead.
This commit is contained in:
Jose Diaz-Gonzalez committed 2026-09-20 04:11:15 -04:00
1 parent c04e74c668
commit 6b1b7bc6f6
14 files changed
+403 -10

No files matched your search

+112
View File
@@ -0,0 +1,112 @@
#!/usr/bin/env bash
# Checks that the environment dokku forwards across its privilege drop survives the
# local sudo implementation. sudo-rs, the default sudo on ubuntu 25.10 and later,
# ignores -E and bare --preserve-env, so dokku names every variable it needs.
set -eo pipefail
PLUGIN_CORE_AVAILABLE_PATH="${PLUGIN_CORE_AVAILABLE_PATH:-$(dirname "$0")/../../plugins}"
export PLUGIN_CORE_AVAILABLE_PATH
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
FAILURES=0
fail() {
declare desc="report a failed check"
echo "not ok - $1" 1>&2
FAILURES=$((FAILURES + 1))
}
pass() {
declare desc="report a passing check"
echo "ok - $1"
}
report-whole-environment-support() {
declare desc="record whether this sudo honours -E, for the log"
local warning
warning="$(DOKKU_TEST_CROSSING=1 sudo -E -u nobody true 2>&1 >/dev/null || true)"
if [[ -n "$warning" ]]; then
echo "# sudo -E is not supported here: $warning"
else
echo "# sudo -E is supported here"
fi
}
check-named-list-crosses() {
declare desc="the named variables reach the target user"
local flag output
flag="$(DOKKU_QUIET_OUTPUT=1 DOKKU_TRACE=1 SSH_USER=ci fn-sudo-preserve-env-flag DOKKU_QUIET_OUTPUT DOKKU_TRACE SSH_USER)"
output="$(DOKKU_QUIET_OUTPUT=1 DOKKU_TRACE=1 SSH_USER=ci sudo "$flag" -u nobody /usr/bin/env)"
local name
for name in DOKKU_QUIET_OUTPUT=1 DOKKU_TRACE=1 SSH_USER=ci; do
if grep -qxF "$name" <<<"$output"; then
pass "$name crossed the privilege drop"
else
fail "$name did not cross the privilege drop"
fi
done
}
check-named-list-is-silent() {
declare desc="no warning is written to stderr"
local flag stderr
flag="$(SSH_USER=ci fn-sudo-preserve-env-flag SSH_USER)"
stderr="$(SSH_USER=ci sudo "$flag" -u nobody true 2>&1 >/dev/null || true)"
if [[ -z "$stderr" ]]; then
pass "no warning on stderr"
else
fail "unexpected stderr: $stderr"
fi
}
check-absent-names-are-tolerated() {
declare desc="naming a variable that is not set is not an error"
local flag
flag="$(fn-sudo-preserve-env-flag DOKKU_DEFINITELY_NOT_SET_HERE SSH_USER)"
if sudo "$flag" -u nobody true; then
pass "unset names are ignored"
else
fail "sudo rejected a list naming an unset variable"
fi
}
check-home-still-wins() {
declare desc="-H is not defeated by the preserved list"
local flag output
local names=()
read -ra names <<<"$(fn-cli-preserved-env-names)"
flag="$(fn-sudo-preserve-env-flag "${names[@]}")"
output="$(sudo "$flag" -H -u nobody /usr/bin/env)"
if grep -qxF "HOME=$(getent passwd nobody | cut -d: -f6)" <<<"$output"; then
pass "-H still sets HOME for the target user"
else
fail "HOME was not reset for the target user"
fi
}
main() {
declare desc="run every check"
echo "# sudo version:"
sudo --version | head -1 | sed -e 's/^/# /'
report-whole-environment-support
check-named-list-crosses
check-named-list-is-silent
check-absent-names-are-tolerated
check-home-still-wins
if [[ "$FAILURES" -gt 0 ]]; then
echo "$FAILURES check(s) failed" 1>&2
exit 1
fi
echo "all checks passed"
}
main "$@"
+51
View File
@@ -120,3 +120,54 @@ teardown() {
assert_line 0 "--label"
assert_line 1 "a b"
}
@test "(common) [preserve-env] fn-sudo-preserve-env-flag emits only the requested names" {
run /bin/bash -c "source '$PLUGIN_CORE_AVAILABLE_PATH/common/functions'; fn-sudo-preserve-env-flag DOKKU_TRACE DOKKU_APP_NAME"
echo "output: $output"
echo "status: $status"
assert_success
assert_output "--preserve-env=DOKKU_TRACE,DOKKU_APP_NAME"
}
@test "(common) [preserve-env] fn-sudo-preserve-env-flag omits sudo managed names" {
run /bin/bash -c "source '$PLUGIN_CORE_AVAILABLE_PATH/common/functions'; export DOKKU_TEST_CROSSING=1; fn-sudo-preserve-env-flag | sed 's/^--preserve-env=//' | tr ',' '\\n' | grep -cx DOKKU_TEST_CROSSING"
echo "output: $output"
echo "status: $status"
assert_success
assert_output "1"
run /bin/bash -c "source '$PLUGIN_CORE_AVAILABLE_PATH/common/functions'; fn-sudo-preserve-env-flag | sed 's/^--preserve-env=//' | tr ',' '\\n' | grep -cxE 'HOME|LOGNAME|MAIL|PATH|SHELL|USER|_|SUDO_.*'"
echo "output: $output"
echo "status: $status"
assert_output "0"
}
@test "(common) [preserve-env] fn-sudo-preserve-env-flag honors DOKKU_PRESERVE_ENV" {
run /bin/bash -c "source '$PLUGIN_CORE_AVAILABLE_PATH/common/functions'; DOKKU_PRESERVE_ENV=ALPHA,BETA fn-sudo-preserve-env-flag DOKKU_TRACE"
echo "output: $output"
echo "status: $status"
assert_success
assert_output "--preserve-env=DOKKU_TRACE,ALPHA,BETA"
run /bin/bash -c "source '$PLUGIN_CORE_AVAILABLE_PATH/common/functions'; DOKKU_PRESERVE_ENV=ALPHA fn-sudo-preserve-env-flag | sed 's/^--preserve-env=//' | tr ',' '\\n' | grep -cx ALPHA"
echo "output: $output"
echo "status: $status"
assert_success
assert_output "1"
}
@test "(common) [preserve-env] fn-cli-preserved-env-names covers every parse_args export" {
run /bin/bash -c "source '$PLUGIN_CORE_AVAILABLE_PATH/common/functions'; fn-cli-preserved-env-names | tr ' ' '\\n' | grep -cxE 'DOKKU_APP_NAME|DOKKU_APPS_FORCE_DELETE|DOKKU_GLOBAL_FLAGS|DOKKU_PRESERVE_ENV|DOKKU_QUIET_OUTPUT|DOKKU_TRACE|SSH_USER'"
echo "output: $output"
echo "status: $status"
assert_success
assert_output "7"
}
@test "(common) [preserve-env] parse_args records global flags for replay" {
run /bin/bash -c "source '$PLUGIN_CORE_AVAILABLE_PATH/common/functions'; parse_args --label=a=b run foo env; echo \"\$DOKKU_GLOBAL_FLAGS\""
echo "output: $output"
echo "status: $status"
assert_success
assert_output "--label=a=b"
}
+105
View File
@@ -0,0 +1,105 @@
#!/usr/bin/env bats
load test_helper
setup() {
global_setup
create_app
}
teardown() {
destroy_app
global_teardown
}
@test "(core) [global-flags] --quiet crosses the privilege drop" {
run /bin/bash -c "dokku apps:list"
echo "output: $output"
echo "status: $status"
assert_success
assert_output_contains "My Apps"
run /bin/bash -c "dokku --quiet apps:list"
echo "output: $output"
echo "status: $status"
assert_success
assert_output_not_contains "My Apps"
assert_output_contains "$TEST_APP"
}
@test "(core) [global-flags] --trace crosses the privilege drop" {
run /bin/bash -c "dokku --trace apps:list 2>&1 >/dev/null"
echo "output: $output"
echo "status: $status"
assert_success
assert_output_contains "execute_dokku_cmd"
}
@test "(core) [global-flags] --force crosses the privilege drop" {
local force_app="${TEST_APP}-force"
run /bin/bash -c "dokku apps:create $force_app"
echo "output: $output"
echo "status: $status"
assert_success
run /bin/bash -c "dokku --force apps:destroy $force_app </dev/null"
echo "output: $output"
echo "status: $status"
assert_success
assert_output_not_contains "To proceed, type"
}
@test "(core) [global-flags] --app crosses the privilege drop" {
run /bin/bash -c "dokku --app $TEST_APP config:set --no-restart TEST_PRESERVED=crossed"
echo "output: $output"
echo "status: $status"
assert_success
run /bin/bash -c "dokku config:get $TEST_APP TEST_PRESERVED"
echo "output: $output"
echo "status: $status"
assert_success
assert_output "crossed"
}
@test "(core) [global-flags] a clean invocation writes nothing to stderr" {
run /bin/bash -c "dokku apps:list 2>&1 >/dev/null"
echo "output: $output"
echo "status: $status"
assert_success
assert_output ""
}
@test "(core) [global-flags] the caller environment crosses the privilege drop" {
run /bin/bash -c "DOKKU_QUIET_OUTPUT=1 dokku apps:list"
echo "output: $output"
echo "status: $status"
assert_success
assert_output_not_contains "My Apps"
assert_output_contains "$TEST_APP"
}
@test "(core) [global-flags] an unlisted variable only crosses when DOKKU_PRESERVE_ENV names it" {
# DOKKU_IMAGE is not carried across the re-exec and is re-derived by the child, so the
# traced value shows whether the caller's value survived. The flag form of --trace is
# used deliberately: DOKKU_TRACE=1 would trace the parent as well as the child.
run /bin/bash -c "DOKKU_IMAGE=probe/image dokku --trace apps:list 2>&1 >/dev/null | grep -m1 'export DOKKU_IMAGE'"
echo "output: $output"
echo "status: $status"
assert_success
assert_output_not_contains "probe/image"
run /bin/bash -c "DOKKU_PRESERVE_ENV=DOKKU_IMAGE DOKKU_IMAGE=probe/image dokku --trace apps:list 2>&1 >/dev/null | grep -m1 'export DOKKU_IMAGE'"
echo "output: $output"
echo "status: $status"
assert_success
assert_output_contains "probe/image"
}
@test "(core) [global-flags] the system sudo supports a named preserve-env list" {
run /bin/bash -c "DOKKU_TEST_CROSSING=1 sudo -u dokku --preserve-env=DOKKU_TEST_CROSSING -H /usr/bin/env | grep -cxE 'DOKKU_TEST_CROSSING=1|HOME=/home/dokku'"
echo "output: $output"
echo "status: $status"
assert_success
assert_output "2"
}
+11 -4
View File
@@ -116,7 +116,7 @@ teardown() {
echo "status: $status"
assert_success
run /bin/bash -c "sudo -E -u nobody dokku plugin:uninstall $TEST_PLUGIN_NAME"
run /bin/bash -c "sudo -u nobody dokku plugin:uninstall $TEST_PLUGIN_NAME"
echo "output: $output"
echo "status: $status"
assert_failure
@@ -158,7 +158,7 @@ teardown() {
echo "status: $status"
assert_success
run /bin/bash -c "sudo -E -u nobody dokku plugin:uninstall $TEST_PLUGIN_NAME"
run /bin/bash -c "sudo -u nobody dokku plugin:uninstall $TEST_PLUGIN_NAME"
echo "output: $output"
echo "status: $status"
assert_failure
@@ -212,7 +212,7 @@ teardown() {
}
@test "(plugin) plugin:install, plugin:disable, plugin:uninstall as non-root user failure" {
run /bin/bash -c "sudo -E -u nobody dokku plugin:install $TEST_PLUGIN_GIT_REPO"
run /bin/bash -c "sudo -u nobody dokku plugin:install $TEST_PLUGIN_GIT_REPO"
echo "output: $output"
echo "status: $status"
assert_failure
@@ -227,7 +227,7 @@ teardown() {
echo "status: $status"
assert_success
run /bin/bash -c "sudo -E -u nobody dokku plugin:disable $TEST_PLUGIN_NAME"
run /bin/bash -c "sudo -u nobody dokku plugin:disable $TEST_PLUGIN_NAME"
echo "output: $output"
echo "status: $status"
assert_failure
@@ -372,3 +372,10 @@ teardown() {
assert_output_contains "root" "0"
assert_output_contains "dokku" "3"
}
@test "(plugin:trigger) forwards the caller environment across the privilege drop" {
run /bin/bash -c "DOKKU_TEST_CROSSING=1 DOKKU_TRACE=1 dokku plugin:trigger app-list 2>&1 >/dev/null | grep -q -- '--preserve-env=[^ ]*DOKKU_TEST_CROSSING'"
echo "output: $output"
echo "status: $status"
assert_success
}
+2
View File
@@ -101,6 +101,8 @@ teardown() {
echo "status: $status"
assert_success
# DOKKU_OMIT_RESOURCE_ARGS has to survive the privilege drop in
# plugins/plugin/subcommands/trigger, so this also covers that forwarding
run_plugn_trigger DOKKU_OMIT_RESOURCE_ARGS=1 docker-args-process-build "$TEST_APP" herokuish
echo "output: $output"
echo "status: $status"
+3
View File
@@ -35,6 +35,9 @@ teardown() {
echo "status: $status"
assert_success
# the leading --label is replayed out of DOKKU_GLOBAL_FLAGS by
# plugins/scheduler-docker-local/scheduler-run, so this also covers that variable
# surviving the re-exec as the dokku user
RANDOM_RUN_CID="$(dokku --label=com.dokku.test-label=value run:detached $TEST_APP sleep 300)"
run /bin/bash -c "docker inspect -f '{{ .State.Status }}' $RANDOM_RUN_CID"
echo "output: $output"