diff --git a/functions b/functions index 2298efd..ba94242 100644 --- a/functions +++ b/functions @@ -411,8 +411,18 @@ function setup_php_extensions() { fi } -# Functions ee calls unguarded that hardened php.ini files (e.g. HestiaCP's CLI one) often list in disable_functions. -EE_PHP_REQUIRED_FUNCTIONS="curl_exec disk_free_space escapeshellarg exec getmypid ini_set pcntl_signal pcntl_signal_dispatch php_uname posix_getegid posix_geteuid posix_getpwuid proc_close proc_open" +# The functions ee refuses to run without, and the ones it only warns about (core's EE\Requirements FUNCTIONS and OPTIONAL_FUNCTIONS). +EE_PHP_REQUIRED_FUNCTIONS="chgrp chown curl_close curl_errno curl_error curl_exec curl_getinfo curl_init curl_setopt curl_version disk_free_space escapeshellarg exec gethostname getmypid gzclose gzeof gzgets gzopen gzwrite openssl_pkey_get_private openssl_x509_check_private_key openssl_x509_parse parse_ini_string php_uname posix_getegid posix_geteuid posix_getpwuid proc_close proc_open stream_select stream_set_blocking symlink" +EE_PHP_OPTIONAL_FUNCTIONS="pcntl_signal pcntl_signal_dispatch shell_exec" + +# What ee loses without an optional function, worded as core's debug message. +function ee_php_optional_impact() { + case "$1" in + pcntl_signal) echo "site commands interrupted by a signal (e.g. Ctrl+C) are not rolled back" ;; + pcntl_signal_dispatch) echo "Ctrl+C while waiting for another SSL operation takes effect only after the wait" ;; + shell_exec) echo "\`ee site share\` does not work" ;; + esac +} function check_php_functions() { ee_log_info1 "Checking required PHP functions" @@ -420,15 +430,20 @@ function check_php_functions() { return 0 fi - local report kind name disabled="" missing="" degraded="" ini_files="" scan_dir="" where - # PHP 7 only warns when a disabled function is called, so disabled pcntl_* just loses rollback on signals there. + local report kind name disabled="" missing="" optional="" ini_files="" scan_dir="" where + # Required functions come before the "--", optional ones after it. # shellcheck disable=SC2016,SC2086 # PHP code, not shell; one argument per function if ! report="$(php -r ' $off = function_exists("ini_get") ? preg_split("/[\s,]+/", (string) ini_get("disable_functions")) : array(); + $is_optional = false; foreach (array_slice($argv, 1) as $f) { + if ($f === "--") { $is_optional = true; continue; } if (function_exists($f)) { continue; } - if (!in_array($f, $off, true)) { echo "missing ", $f, "\n"; } - elseif (PHP_MAJOR_VERSION < 8 && strpos($f, "pcntl_") === 0) { echo "degraded ", $f, "\n"; } + if ($is_optional) { + // As in core: without pcntl_signal() there is no handler to dispatch. + if ($f !== "pcntl_signal_dispatch" || function_exists("pcntl_signal")) { echo "optional ", $f, "\n"; } + } + elseif (!in_array($f, $off, true)) { echo "missing ", $f, "\n"; } else { echo "disabled ", $f, "\n"; } } if (function_exists("php_ini_loaded_file") && function_exists("php_ini_scanned_files") && function_exists("file_get_contents")) { @@ -437,7 +452,7 @@ function check_php_functions() { if (preg_match("/^[ \t]*disable_functions[ \t]*=[ \t]*[\"\x27]?[ \t]*[A-Za-z_]/m", (string) @file_get_contents($file))) { echo "ini ", $file, "\n"; } } } - echo "scandir ", PHP_CONFIG_FILE_SCAN_DIR, "\n";' -- $EE_PHP_REQUIRED_FUNCTIONS)"; then + echo "scandir ", PHP_CONFIG_FILE_SCAN_DIR, "\n";' -- $EE_PHP_REQUIRED_FUNCTIONS -- $EE_PHP_OPTIONAL_FUNCTIONS)"; then ee_log_fail "ERROR: Could not run the PHP CLI ($(command -v php)) to check the functions EasyEngine needs." fi @@ -445,15 +460,15 @@ function check_php_functions() { case "$kind" in disabled) disabled+="${disabled:+, }$name" ;; missing) missing+="${missing:+, }$name" ;; - degraded) degraded+="${degraded:+, }$name" ;; + optional) optional+="${optional:+ }$name" ;; ini) ini_files+="${ini_files:+, }$name" ;; scandir) scan_dir="$name" ;; esac done <<< "$report" - if [ -n "$degraded" ]; then - ee_log_warn "The PHP CLI disables ${degraded}; EasyEngine can't roll back a site operation interrupted by a signal (on PHP 8 this is a fatal error)." - fi + for name in $optional; do + ee_log_warn "PHP function ${name}() is unavailable, so $(ee_php_optional_impact "$name")." + done if [ -z "$disabled" ] && [ -z "$missing" ]; then return 0 fi @@ -478,7 +493,7 @@ function check_php_functions() { fi if [ -n "$missing" ]; then echo "ERROR: The PHP CLI ($(command -v php)) lacks functions EasyEngine needs: ${missing}." >&2 - ee_log_verbose "To fix it, install or enable the PHP extensions that provide them (e.g. pcntl, posix, curl) for the PHP CLI. Then re-run the installer." >&2 + ee_log_verbose "To fix it, install or enable the PHP extensions that provide them (e.g. posix, curl, openssl) for the PHP CLI. Then re-run the installer." >&2 fi exit 1 } diff --git a/tests/README.md b/tests/README.md index 29766d3..4b206a0 100644 --- a/tests/README.md +++ b/tests/README.md @@ -74,8 +74,8 @@ functions to drive the algorithm through every branch deterministically. - `ee_select_and_install_php` — native vs PPA, version-first ordering, no-candidate. - `download_and_install_easyengine` — checksum match / mismatch / empty phar / empty checksum / download failure. -- `check_php_functions` — functions disabled via `disable_functions` (comma- or space-separated) or missing from the build, PHP 7's warn-only pcntl case, the messages, and its place in `check_dependencies` (after PHP setup, before Docker). -- Host-prep integration — php + the three required extensions install across Ubuntu 22.04/24.04/26.04/rolling/devel and Debian 12/13, the functions ee calls are usable, and `check_php_functions` refuses a HestiaCP-style CLI `disable_functions`. +- `check_php_functions` — required functions disabled via `disable_functions` (comma- or space-separated) or missing from the build, the optional ones (`pcntl_signal`, `pcntl_signal_dispatch`, `shell_exec`) that only warn, the messages, and its place in `check_dependencies` (after PHP setup, before Docker). +- Host-prep integration — php + the three required extensions install across Ubuntu 22.04/24.04/26.04/rolling/devel and Debian 12/13, the functions ee calls are usable, and `check_php_functions` only warns about a HestiaCP-style CLI `disable_functions` (pcntl_*) but refuses one that also disables a required function. ## Not yet covered (candidate follow-ups) diff --git a/tests/integration/host-prep.sh b/tests/integration/host-prep.sh index 7b4aeb9..e5e82b0 100755 --- a/tests/integration/host-prep.sh +++ b/tests/integration/host-prep.sh @@ -78,16 +78,23 @@ done in_subshell() { ( "$@" ); } check "required PHP functions are available" in_subshell check_php_functions -# A HestiaCP-style CLI disable_functions must be refused with the file named; a scan-dir file overrides php.ini. +# A HestiaCP-style CLI disable_functions (pcntl_*) only warns; a required function disabled next to them is refused with the file named. scan_dir="$(php -r 'echo PHP_CONFIG_FILE_SCAN_DIR;')" if [ -n "$scan_dir" ] && [ -d "$scan_dir" ]; then hardened_ini="$scan_dir/99-ee-test-hardening.ini" echo "disable_functions = pcntl_alarm,pcntl_fork,pcntl_signal,pcntl_signal_dispatch" > "$hardened_ini" + pcntl_out="$( (check_php_functions) 2>&1 )" + pcntl_rc=$? + echo "disable_functions = pcntl_alarm,pcntl_fork,pcntl_signal,pcntl_signal_dispatch,exec" > "$hardened_ini" hardened_out="$( (check_php_functions) 2>&1 )" hardened_rc=$? rm -f "$hardened_ini" - check "disabled PHP functions are refused" test "$hardened_rc" -ne 0 - check "the error names the disabled functions" grep -qF "disables functions EasyEngine needs: pcntl_signal, pcntl_signal_dispatch." <<< "$hardened_out" + check "disabled pcntl functions are accepted" test "$pcntl_rc" -eq 0 + check "disabled pcntl functions get a warning" grep -qF "PHP function pcntl_signal() is unavailable" <<< "$pcntl_out" + no_error() { ! grep -q ERROR <<< "$1"; } + check "disabled pcntl functions are no error" no_error "$pcntl_out" + check "disabled required functions are refused" test "$hardened_rc" -ne 0 + check "the error names the disabled functions" grep -qF "disables functions EasyEngine needs: exec." <<< "$hardened_out" check "the error names the ini file" grep -qF "$hardened_ini" <<< "$hardened_out" check "the error says to edit that file" grep -qF "remove them from disable_functions in the file(s) above." <<< "$hardened_out" check "the error is at most 4 lines" test "$(sed -n '/^ERROR: /,$p' <<< "$hardened_out" | wc -l)" -le 4 diff --git a/tests/unit/php_functions.bats b/tests/unit/php_functions.bats index a203e3d..59d78d2 100644 --- a/tests/unit/php_functions.bats +++ b/tests/unit/php_functions.bats @@ -1,6 +1,6 @@ #!/usr/bin/env bats # -# check_php_functions() — refuses a PHP CLI that disables or lacks functions ee calls (e.g. HestiaCP's pcntl_* list), which otherwise only fails on the first `ee site` command. +# check_php_functions() — refuses a PHP CLI that disables or lacks functions ee needs, which otherwise only fails on the first `ee` command, and only warns about the optional ones (e.g. HestiaCP's pcntl_* list). setup() { source "${BATS_TEST_DIRNAME}/../helpers/common.bash" @@ -47,9 +47,9 @@ assert_error_lines() { @test "fails and names disabled functions, the ini file and a conf.d override" { stub_php_report - PHP_REPORT='disabled pcntl_signal\ndisabled pcntl_signal_dispatch\nini /etc/php/8.3/cli/php.ini\nscandir /etc/php/8.3/cli/conf.d\n' run check_php_functions + PHP_REPORT='disabled exec\ndisabled proc_open\nini /etc/php/8.3/cli/php.ini\nscandir /etc/php/8.3/cli/conf.d\n' run check_php_functions [ "$status" -eq 1 ] - [[ "$output" == *"ERROR: The PHP CLI ("*") disables functions EasyEngine needs: pcntl_signal, pcntl_signal_dispatch."* ]] + [[ "$output" == *"ERROR: The PHP CLI ("*") disables functions EasyEngine needs: exec, proc_open."* ]] [[ "$output" == *" disable_functions is set in: /etc/php/8.3/cli/php.ini"* ]] [[ "$output" == *"create /etc/php/8.3/cli/conf.d/99-easyengine.ini that sets disable_functions to the same list without these functions, or edit php.ini directly."* ]] [[ "$output" == *"Only the PHP CLI configuration needs the change, not PHP-FPM. Then re-run the installer."* ]] @@ -66,14 +66,32 @@ assert_error_lines() { assert_error_lines 4 } -@test "only warns about pcntl functions PHP 7 disables" { +@test "only warns when optional functions are unavailable" { stub_php_report - PHP_REPORT='degraded pcntl_signal\n' run check_php_functions + PHP_REPORT='optional pcntl_signal\noptional pcntl_signal_dispatch\noptional shell_exec\nini /etc/php/8.3/cli/php.ini\n' run check_php_functions [ "$status" -eq 0 ] - [[ "$output" == *"disables pcntl_signal; EasyEngine can't roll back"* ]] + [[ "$output" == *"PHP function pcntl_signal() is unavailable, so site commands interrupted by a signal (e.g. Ctrl+C) are not rolled back."* ]] + [[ "$output" == *"PHP function pcntl_signal_dispatch() is unavailable, so Ctrl+C while waiting for another SSL operation takes effect only after the wait."* ]] + [[ "$output" == *"PHP function shell_exec() is unavailable, so \`ee site share\` does not work."* ]] [[ "$output" != *ERROR* ]] } +@test "warns about optional functions and still fails on a required one" { + stub_php_report + PHP_REPORT='optional pcntl_signal\ndisabled exec\nini /etc/php/8.3/cli/php.ini\nscandir /etc/php/8.3/cli/conf.d\n' run check_php_functions + [ "$status" -eq 1 ] + [[ "$output" == *"PHP function pcntl_signal() is unavailable"* ]] + [[ "$output" == *"disables functions EasyEngine needs: exec."* ]] + assert_error_lines 4 +} + +@test "pcntl_signal, pcntl_signal_dispatch and shell_exec are optional, not required" { + for f in pcntl_signal pcntl_signal_dispatch shell_exec; do + [[ " $EE_PHP_REQUIRED_FUNCTIONS " != *" $f "* ]] + [[ " $EE_PHP_OPTIONAL_FUNCTIONS " == *" $f "* ]] + done +} + @test "points at php --ini when no file could be identified" { stub_php_report PHP_REPORT='disabled exec\n' run check_php_functions @@ -85,9 +103,9 @@ assert_error_lines() { @test "fails and names functions missing from the PHP build" { stub_php_report - PHP_REPORT='missing pcntl_signal\n' run check_php_functions + PHP_REPORT='missing posix_getpwuid\n' run check_php_functions [ "$status" -eq 1 ] - [[ "$output" == *"lacks functions EasyEngine needs: pcntl_signal."* ]] + [[ "$output" == *"lacks functions EasyEngine needs: posix_getpwuid."* ]] [[ "$output" == *"To fix it, install or enable the PHP extensions that provide them"* ]] [[ "$output" == *"Then re-run the installer."* ]] [[ "$output" != *"disables functions"* ]] @@ -122,14 +140,54 @@ stub_real_php() { # stub_real_php [[ "$output" == *"disables functions EasyEngine needs: curl_exec, exec."* ]] } +# Skips when the real php lacks an extension ee needs (e.g. curl, posix), so the disable list isn't all that's missing. +skip_unless_required_available() { + # shellcheck disable=SC2016,SC2086 + if ! "$REAL_PHP" -r 'foreach (array_slice($argv, 1) as $f) { if (!function_exists($f)) { exit(1); } }' -- $EE_PHP_REQUIRED_FUNCTIONS; then + skip "this php lacks an extension ee needs (e.g. curl, posix)" + fi +} + @test "real php: passes when nothing required is disabled" { stub_real_php "" - # shellcheck disable=SC2016,SC2086 - if ! php -r 'foreach (array_slice($argv, 1) as $f) { if (!function_exists($f)) { exit(1); } }' -- $EE_PHP_REQUIRED_FUNCTIONS; then - skip "this php lacks an extension ee needs (e.g. curl, pcntl, posix)" + skip_unless_required_available + run check_php_functions + [ "$status" -eq 0 ] +} + +@test "real php: warns and continues when pcntl_signal and pcntl_signal_dispatch are disabled" { + # HestiaCP's CLI php.ini disables these; ee 4.14.0+ works without them. + stub_real_php "pcntl_alarm,pcntl_fork,pcntl_signal,pcntl_signal_dispatch" + skip_unless_required_available + run check_php_functions + [ "$status" -eq 0 ] + [[ "$output" == *"PHP function pcntl_signal() is unavailable, so site commands interrupted by a signal (e.g. Ctrl+C) are not rolled back."* ]] + # Without pcntl_signal() there is no handler to dispatch, so ee doesn't miss pcntl_signal_dispatch(). + [[ "$output" != *"pcntl_signal_dispatch() is unavailable"* ]] + [[ "$output" != *ERROR* ]] +} + +@test "real php: still fails when a required function is disabled next to the pcntl ones" { + stub_real_php "pcntl_signal,pcntl_signal_dispatch,proc_open" + skip_unless_required_available + run check_php_functions + [ "$status" -eq 1 ] + [[ "$output" == *"PHP function pcntl_signal() is unavailable"* ]] + [[ "$output" == *"disables functions EasyEngine needs: proc_open."* ]] + assert_error_lines 4 +} + +@test "real php: warns about pcntl_signal_dispatch alone and about shell_exec" { + stub_real_php "pcntl_signal_dispatch shell_exec" + skip_unless_required_available + if ! "$REAL_PHP" -r 'exit(function_exists("pcntl_signal") ? 0 : 1);'; then + skip "this php has no pcntl extension" fi run check_php_functions [ "$status" -eq 0 ] + [[ "$output" == *"PHP function pcntl_signal_dispatch() is unavailable, so Ctrl+C while waiting for another SSL operation takes effect only after the wait."* ]] + [[ "$output" == *"PHP function shell_exec() is unavailable, so \`ee site share\` does not work."* ]] + [[ "$output" != *ERROR* ]] } @test "check_dependencies checks PHP functions after PHP setup and before Docker" {