diff --git a/.github/scripts/smoke-test.sh b/.github/scripts/smoke-test.sh index 53ed2f4..314c9d3 100755 --- a/.github/scripts/smoke-test.sh +++ b/.github/scripts/smoke-test.sh @@ -54,14 +54,37 @@ if [ "$variant" = cli ]; then check_php "$version" "$loaded" uid=$(docker run --rm "$image" id -u) || fail "id did not run" [ "$uid" != 0 ] || fail "runs as root" + # Only cli/Dockerfile.wordpress carries the WORDPRESS_VCS_CHECKOUT prepend + # file (see the frankenphp checks below). CI doesn't build that image, so + # this runs only by hand. A docker run that fails fails the check, rather + # than reading as "no file". + has_vcs=$(docker run --rm "$image" sh -c \ + 'if test -f /usr/local/share/php-prepend/wordpress-vcs-checkout.php; then echo yes; else echo no; fi') \ + || fail "the WORDPRESS_VCS_CHECKOUT file check did not run" + case "$has_vcs" in + yes | no) ;; + *) fail "the WORDPRESS_VCS_CHECKOUT file check printed '$has_vcs'" ;; + esac + if [ "$has_vcs" = yes ]; then + seeded='{"automatic_updates_is_vcs_checkout":{"10":[{"function":"__return_true","accepted_args":1}]}}' + check='echo " /tmp/vcs.php && php /tmp/vcs.php' + out=$(docker run --rm "$image" sh -c "$check") || fail "vcs check did not run" + [ "$out" = none ] || fail "WORDPRESS_VCS_CHECKOUT unset, but the filter was seeded: $out" + out=$(docker run --rm -e WORDPRESS_VCS_CHECKOUT=1 "$image" sh -c "$check") || fail "vcs check did not run" + [ "$out" = "$seeded" ] || fail "WORDPRESS_VCS_CHECKOUT=1 did not seed the filter: $out" + fi echo "ok: $image (PHP $version, uid $uid)" exit 0 fi fixture=$(mktemp -d) name="smoke-$$" +containers=() cleanup() { - docker rm -f "$name" >/dev/null 2>&1 || true + local c + for c in "${containers[@]}"; do + docker rm -f "$c" >/dev/null 2>&1 || true + done rm -rf "$fixture" } trap cleanup EXIT @@ -80,31 +103,44 @@ echo "VERSION=", PHP_VERSION, "\n"; echo "SAPI=", php_sapi_name(), "\n"; echo "EXTENSIONS=", implode(' ', get_loaded_extensions()), "\n"; PHP +# What the WORDPRESS_VCS_CHECKOUT prepend file did to this request. +cat > "$fixture/public/vcs.php" <<'PHP' +/dev/null - -health=starting -for _ in $(seq 1 60); do - state=$(docker inspect -f '{{.State.Status}}' "$name") - if [ "$state" != running ]; then - docker logs "$name" >&2 || true - fail "container is '$state', not running" +# Boots the image as container $1 (extra docker run arguments after it) with +# the fixture at /app, waits for it to be healthy and sets $port. +boot() { + local cname=$1 state health=starting + shift + containers+=("$cname") + # Short health interval: the image's own HEALTHCHECK (30s) would make the + # wait below much longer than it needs to be. + docker run -d --name "$cname" -p 127.0.0.1::80 -v "$fixture:/app:ro" \ + --health-interval 2s --health-timeout 5s --health-retries 5 --health-start-period 30s \ + "$@" "$image" >/dev/null + + for _ in $(seq 1 60); do + state=$(docker inspect -f '{{.State.Status}}' "$cname") + if [ "$state" != running ]; then + docker logs "$cname" >&2 || true + fail "container is '$state', not running (${*:-no extra arguments})" + fi + health=$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$cname") + [ "$health" = healthy ] && break + sleep 2 + done + if [ "$health" != healthy ]; then + docker logs "$cname" >&2 || true + fail "not healthy after 120s (health: $health; ${*:-no extra arguments})" fi - health=$(docker inspect -f '{{if .State.Health}}{{.State.Health.Status}}{{else}}none{{end}}' "$name") - [ "$health" = healthy ] && break - sleep 2 -done -if [ "$health" != healthy ]; then - docker logs "$name" >&2 || true - fail "not healthy after 120s (health: $health)" -fi -port=$(docker port "$name" 80/tcp | head -n1 | sed 's/.*://') + port=$(docker port "$cname" 80/tcp | head -n1 | sed 's/.*://') +} + +boot "$name" body=$(curl -fsS --max-time 10 "http://127.0.0.1:$port/") \ || fail "GET / did not return 2xx" @@ -144,4 +180,33 @@ docker exec "$name" test -s /usr/local/etc/php/conf.d/app.ini \ # Composer is baked into every frankenphp image. docker exec "$name" composer --version >/dev/null 2>&1 || fail "composer is not runnable" -echo "ok: $image (PHP $version, uid $uid, healthy)" +# WORDPRESS_VCS_CHECKOUT (see the frankenphp Dockerfile). Unset, the prepend +# file does nothing; the exact value 1 seeds WordPress's +# automatic_updates_is_vcs_checkout filter; with PHP_OPEN_BASEDIR set, +# docker-configure lets the prepend file in, or every request would fail; +# with getenv disabled the file must not fatal. +seeded='{"automatic_updates_is_vcs_checkout":{"10":[{"function":"__return_true","accepted_args":1}]}}' +docker exec "$name" test -f /usr/local/share/php-prepend/wordpress-vcs-checkout.php \ + || fail "the WORDPRESS_VCS_CHECKOUT prepend file is missing" +body=$(curl -fsS --max-time 10 "http://127.0.0.1:$port/vcs.php") \ + || fail "GET /vcs.php did not return 2xx" +[ "$body" = none ] || fail "WORDPRESS_VCS_CHECKOUT unset, but the filter was seeded: $body" +docker rm -f "$name" >/dev/null + +vcs_case() { # expected, then extra docker run arguments + local expected=$1 cname="$name-vcs-${#containers[@]}" + shift + boot "$cname" "$@" + body=$(curl -fsS --max-time 10 "http://127.0.0.1:$port/vcs.php") \ + || fail "GET /vcs.php did not return 2xx ($*)" + [ "$body" = "$expected" ] || fail "GET /vcs.php returned '$body', expected '$expected' ($*)" + body=$(curl -fsS --max-time 10 "http://127.0.0.1:$port/") \ + || fail "GET / did not return 2xx ($*)" + printf '%s\n' "$body" | grep -q '^VERSION=' || fail "GET / did not run the app ($*)" + docker rm -f "$cname" >/dev/null +} +vcs_case "$seeded" -e WORDPRESS_VCS_CHECKOUT=1 +vcs_case "$seeded" -e WORDPRESS_VCS_CHECKOUT=1 -e PHP_OPEN_BASEDIR=/app:/tmp +vcs_case none -e WORDPRESS_VCS_CHECKOUT=1 -e PHP_DISABLE_FUNCTIONS=getenv + +echo "ok: $image (PHP $version, uid $uid, healthy, WORDPRESS_VCS_CHECKOUT)" diff --git a/README.md b/README.md index be1bd80..ed5b340 100644 --- a/README.md +++ b/README.md @@ -74,3 +74,15 @@ The `frankenphp:wordpress` variant renders a Caddy hardening snippet at containe All of the above are overridable via environment variables if a site needs different values. The `wordpress` variant also ships defaults for the [static asset caching](#static-asset-caching) snippet covering both the Bedrock (`/app/...`, `/wp/...`) and stock (`/wp-content/...`, `/wp-includes/...`) layouts, including cache-plugin output and Avada's compiled `fusion-styles` / `fusion-scripts` directories. + +### Auto-updates on sites deployed from git + +WordPress skips its background auto-updates (core, plugins, themes and translations) when it finds a `.git`, `.svn`, `.hg` or `.bzr` directory at or above the code it would update, so code deployed from a repository isn't rewritten underneath it. A deploy that keeps the repository outside the folder mounted at `/app` loses that, and WordPress starts updating itself. Set `WORDPRESS_VCS_CHECKOUT=1` to keep it off: + +```sh +docker run -e WORDPRESS_VCS_CHECKOUT=1 ghcr.io/aliasproject/php:8.4-frankenphp-wordpress +``` + +It is unset by default, and only the exact value `1` turns it on. Every frankenphp image (base, `dev`, `wordpress` and `wordpress-dev`, so a WordPress site on the base image is covered too) sets PHP's `auto_prepend_file` to `/usr/local/share/php-prepend/wordpress-vcs-checkout.php`, in `php.ini`. The file is root-owned, and adds `__return_true` to WordPress's `automatic_updates_is_vcs_checkout` filter before WordPress loads (WordPress 4.7 or newer). That covers web requests, WP-Cron when a web request runs it, and WP-CLI run with `docker exec` in the frankenphp container (a system cron calling `docker exec php wp-cli.phar cron event run`, for example; the images don't ship WP-CLI itself). Only WP-CLI in a separate CLI container needs the image built from `php/8.4/cli/Dockerfile.wordpress`, which carries the same file; CI doesn't build or publish that image. + +With the variable unset the file does nothing. With it set, the only effect on code that isn't WordPress is a global `$wp_filter` array with that one entry. Manual updates from wp-admin or `wp core update` still work. If `getenv` is in `PHP_DISABLE_FUNCTIONS`, the file can't read the variable and does nothing. A derived image that sets its own `auto_prepend_file` (a Wordfence WAF, say) replaces this one, whether it edits the `auto_prepend_file` line in `php.ini`, appends its own line there or adds any `conf.d` file: PHP reads `conf.d` after `php.ini`, and the last setting wins. When `PHP_OPEN_BASEDIR` is set, the frankenphp images add `/usr/local/share/php-prepend/` to it, because PHP refuses every request whose prepend file it can't open. An image that sets `open_basedir` some other way (its own `conf.d` file) has to add that directory itself. diff --git a/php/8.2/frankenphp/Dockerfile b/php/8.2/frankenphp/Dockerfile index b8f0f14..1856d97 100644 --- a/php/8.2/frankenphp/Dockerfile +++ b/php/8.2/frankenphp/Dockerfile @@ -1,3 +1,4 @@ +# syntax=docker/dockerfile:1 ARG FRANKENPHP_VERSION=1.14.0 ARG PHP_VERSION=8.2 FROM dunglas/frankenphp:${FRANKENPHP_VERSION}-php${PHP_VERSION}-alpine @@ -128,6 +129,83 @@ COPY --chmod=755 docker-configure /usr/local/bin/docker-configure # source is deployed without its own Dockerfile. COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer +# --------------------------------------------------------------------------- +# WORDPRESS_VCS_CHECKOUT (opt-in, unset by default) +# +# WordPress skips background auto-updates (core, plugins, themes, +# translations) when it finds a .git, .svn, .hg or .bzr directory at or above +# the code it would update, so files deployed from a repository aren't +# rewritten underneath it. A deploy that keeps the repository outside the +# served folder loses that. WORDPRESS_VCS_CHECKOUT=1 restores it: the prepend +# file below seeds WordPress's automatic_updates_is_vcs_checkout filter with +# __return_true before WordPress loads, and WordPress (4.7+) turns pre-seeded +# $wp_filter entries into real hooks. Any other value, or unset, does nothing. +# +# It lives here rather than in Dockerfile.wordpress so a WordPress site on +# this image (framework not flagged) is covered too; every variant inherits +# it. auto_prepend_file runs before every PHP script, WordPress or not, so +# the file only touches $GLOBALS['wp_filter'] and only when the variable is +# "1". If getenv is in PHP_DISABLE_FUNCTIONS it can't read the variable and +# does nothing. It lives outside /app, and docker-configure adds its +# directory to PHP_OPEN_BASEDIR. +# +# The directive goes in php.ini itself, replacing its "auto_prepend_file =" +# line in place, not in a conf.d file: PHP reads conf.d after php.ini, so a +# derived image that sets its own auto_prepend_file (a Wordfence WAF, say), +# by editing that line, appending one or adding any conf.d file, wins. The +# prepend file and php.ini are root-owned and read-only to appuser. appuser +# (group 0) can write conf.d, so it can override this directive there, as it +# can any other PHP setting. +# +# php/8.{2,3,4,5}/frankenphp/Dockerfile and php/8.4/cli/Dockerfile.wordpress +# carry this same block; keep them in sync. +# --------------------------------------------------------------------------- +COPY <<'PHP' /usr/local/share/php-prepend/wordpress-vcs-checkout.php + '__return_true', + 'accepted_args' => 1, + ]; +} +PHP + +# There is only one auto_prepend_file. Fail the build if php.ini is missing or +# the upstream image already sets one, rather than silently replacing it +# (chain to it from the file above if that happens). Then set it in php.ini +# and run a real script both ways: the filter is seeded only with the exact +# value "1". +RUN ini="$PHP_INI_DIR/php.ini" && \ + prepend=/usr/local/share/php-prepend/wordpress-vcs-checkout.php && \ + { test -f "$ini" || { echo "ERROR: $ini is missing" >&2; exit 1; }; } && \ + for bin in "frankenphp php-cli" php; do \ + $bin -r 'exit(ini_get("auto_prepend_file") === "" ? 0 : 1);' || \ + { echo "ERROR: upstream image already sets auto_prepend_file ($bin)" >&2; exit 1; }; \ + done && \ + if grep -q '^auto_prepend_file[[:space:]]*=' "$ini"; then \ + sed -i "s|^auto_prepend_file[[:space:]]*=.*|auto_prepend_file = $prepend|" "$ini"; \ + else \ + printf '\nauto_prepend_file = %s\n' "$prepend" >> "$ini"; \ + fi && \ + { test "$(grep -c '^auto_prepend_file[[:space:]]*=' "$ini")" = 1 || \ + { echo "ERROR: $ini should have exactly one auto_prepend_file line" >&2; exit 1; }; } && \ + echo ' /tmp/vcs-check.php && \ + seeded='{"automatic_updates_is_vcs_checkout":{"10":[{"function":"__return_true","accepted_args":1}]}}' && \ + for bin in "frankenphp php-cli" php; do \ + test "$($bin -r 'echo ini_get("auto_prepend_file");')" = "$prepend" || \ + { echo "ERROR: auto_prepend_file is not $prepend ($bin)" >&2; exit 1; }; \ + test "$(WORDPRESS_VCS_CHECKOUT=1 $bin /tmp/vcs-check.php)" = "$seeded" || \ + { echo "ERROR: WORDPRESS_VCS_CHECKOUT=1 did not seed the filter ($bin)" >&2; exit 1; }; \ + test "$(WORDPRESS_VCS_CHECKOUT=true $bin /tmp/vcs-check.php)" = none || \ + { echo "ERROR: WORDPRESS_VCS_CHECKOUT=true seeded the filter ($bin)" >&2; exit 1; }; \ + test "$(env -u WORDPRESS_VCS_CHECKOUT $bin /tmp/vcs-check.php)" = none || \ + { echo "ERROR: unset WORDPRESS_VCS_CHECKOUT seeded the filter ($bin)" >&2; exit 1; }; \ + done && \ + rm /tmp/vcs-check.php + EXPOSE 80 443 443/udp HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \ diff --git a/php/8.2/frankenphp/docker-configure b/php/8.2/frankenphp/docker-configure index 407d255..bb793ee 100644 --- a/php/8.2/frankenphp/docker-configure +++ b/php/8.2/frankenphp/docker-configure @@ -44,7 +44,11 @@ CADDY_SITE_D=/config/caddy/site.d fi if [ -n "${PHP_OPEN_BASEDIR}" ]; then - echo "open_basedir = ${PHP_OPEN_BASEDIR}" + # The Dockerfile keeps the WORDPRESS_VCS_CHECKOUT auto_prepend_file + # here. PHP refuses to run any script whose prepend file is outside + # open_basedir, so let it in. The directory is root-owned and holds + # only that file. + echo "open_basedir = ${PHP_OPEN_BASEDIR}:/usr/local/share/php-prepend/" fi } > "${PHP_INI_DIR}/conf.d/app.ini" diff --git a/php/8.3/frankenphp/Dockerfile b/php/8.3/frankenphp/Dockerfile index 2f3b782..838af00 100644 --- a/php/8.3/frankenphp/Dockerfile +++ b/php/8.3/frankenphp/Dockerfile @@ -1,3 +1,4 @@ +# syntax=docker/dockerfile:1 ARG FRANKENPHP_VERSION=1.14.0 ARG PHP_VERSION=8.3 FROM dunglas/frankenphp:${FRANKENPHP_VERSION}-php${PHP_VERSION}-alpine @@ -128,6 +129,83 @@ COPY --chmod=755 docker-configure /usr/local/bin/docker-configure # source is deployed without its own Dockerfile. COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer +# --------------------------------------------------------------------------- +# WORDPRESS_VCS_CHECKOUT (opt-in, unset by default) +# +# WordPress skips background auto-updates (core, plugins, themes, +# translations) when it finds a .git, .svn, .hg or .bzr directory at or above +# the code it would update, so files deployed from a repository aren't +# rewritten underneath it. A deploy that keeps the repository outside the +# served folder loses that. WORDPRESS_VCS_CHECKOUT=1 restores it: the prepend +# file below seeds WordPress's automatic_updates_is_vcs_checkout filter with +# __return_true before WordPress loads, and WordPress (4.7+) turns pre-seeded +# $wp_filter entries into real hooks. Any other value, or unset, does nothing. +# +# It lives here rather than in Dockerfile.wordpress so a WordPress site on +# this image (framework not flagged) is covered too; every variant inherits +# it. auto_prepend_file runs before every PHP script, WordPress or not, so +# the file only touches $GLOBALS['wp_filter'] and only when the variable is +# "1". If getenv is in PHP_DISABLE_FUNCTIONS it can't read the variable and +# does nothing. It lives outside /app, and docker-configure adds its +# directory to PHP_OPEN_BASEDIR. +# +# The directive goes in php.ini itself, replacing its "auto_prepend_file =" +# line in place, not in a conf.d file: PHP reads conf.d after php.ini, so a +# derived image that sets its own auto_prepend_file (a Wordfence WAF, say), +# by editing that line, appending one or adding any conf.d file, wins. The +# prepend file and php.ini are root-owned and read-only to appuser. appuser +# (group 0) can write conf.d, so it can override this directive there, as it +# can any other PHP setting. +# +# php/8.{2,3,4,5}/frankenphp/Dockerfile and php/8.4/cli/Dockerfile.wordpress +# carry this same block; keep them in sync. +# --------------------------------------------------------------------------- +COPY <<'PHP' /usr/local/share/php-prepend/wordpress-vcs-checkout.php + '__return_true', + 'accepted_args' => 1, + ]; +} +PHP + +# There is only one auto_prepend_file. Fail the build if php.ini is missing or +# the upstream image already sets one, rather than silently replacing it +# (chain to it from the file above if that happens). Then set it in php.ini +# and run a real script both ways: the filter is seeded only with the exact +# value "1". +RUN ini="$PHP_INI_DIR/php.ini" && \ + prepend=/usr/local/share/php-prepend/wordpress-vcs-checkout.php && \ + { test -f "$ini" || { echo "ERROR: $ini is missing" >&2; exit 1; }; } && \ + for bin in "frankenphp php-cli" php; do \ + $bin -r 'exit(ini_get("auto_prepend_file") === "" ? 0 : 1);' || \ + { echo "ERROR: upstream image already sets auto_prepend_file ($bin)" >&2; exit 1; }; \ + done && \ + if grep -q '^auto_prepend_file[[:space:]]*=' "$ini"; then \ + sed -i "s|^auto_prepend_file[[:space:]]*=.*|auto_prepend_file = $prepend|" "$ini"; \ + else \ + printf '\nauto_prepend_file = %s\n' "$prepend" >> "$ini"; \ + fi && \ + { test "$(grep -c '^auto_prepend_file[[:space:]]*=' "$ini")" = 1 || \ + { echo "ERROR: $ini should have exactly one auto_prepend_file line" >&2; exit 1; }; } && \ + echo ' /tmp/vcs-check.php && \ + seeded='{"automatic_updates_is_vcs_checkout":{"10":[{"function":"__return_true","accepted_args":1}]}}' && \ + for bin in "frankenphp php-cli" php; do \ + test "$($bin -r 'echo ini_get("auto_prepend_file");')" = "$prepend" || \ + { echo "ERROR: auto_prepend_file is not $prepend ($bin)" >&2; exit 1; }; \ + test "$(WORDPRESS_VCS_CHECKOUT=1 $bin /tmp/vcs-check.php)" = "$seeded" || \ + { echo "ERROR: WORDPRESS_VCS_CHECKOUT=1 did not seed the filter ($bin)" >&2; exit 1; }; \ + test "$(WORDPRESS_VCS_CHECKOUT=true $bin /tmp/vcs-check.php)" = none || \ + { echo "ERROR: WORDPRESS_VCS_CHECKOUT=true seeded the filter ($bin)" >&2; exit 1; }; \ + test "$(env -u WORDPRESS_VCS_CHECKOUT $bin /tmp/vcs-check.php)" = none || \ + { echo "ERROR: unset WORDPRESS_VCS_CHECKOUT seeded the filter ($bin)" >&2; exit 1; }; \ + done && \ + rm /tmp/vcs-check.php + EXPOSE 80 443 443/udp HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \ diff --git a/php/8.3/frankenphp/docker-configure b/php/8.3/frankenphp/docker-configure index 407d255..bb793ee 100644 --- a/php/8.3/frankenphp/docker-configure +++ b/php/8.3/frankenphp/docker-configure @@ -44,7 +44,11 @@ CADDY_SITE_D=/config/caddy/site.d fi if [ -n "${PHP_OPEN_BASEDIR}" ]; then - echo "open_basedir = ${PHP_OPEN_BASEDIR}" + # The Dockerfile keeps the WORDPRESS_VCS_CHECKOUT auto_prepend_file + # here. PHP refuses to run any script whose prepend file is outside + # open_basedir, so let it in. The directory is root-owned and holds + # only that file. + echo "open_basedir = ${PHP_OPEN_BASEDIR}:/usr/local/share/php-prepend/" fi } > "${PHP_INI_DIR}/conf.d/app.ini" diff --git a/php/8.4/cli/Dockerfile.wordpress b/php/8.4/cli/Dockerfile.wordpress index 2287bf7..f587a76 100644 --- a/php/8.4/cli/Dockerfile.wordpress +++ b/php/8.4/cli/Dockerfile.wordpress @@ -1,3 +1,4 @@ +# syntax=docker/dockerfile:1 FROM ghcr.io/aliasproject/php:8.4-cli LABEL maintainer="Michael Aguiar " @@ -34,4 +35,81 @@ RUN install-php-extensions \ imagick \ soap +# --------------------------------------------------------------------------- +# WORDPRESS_VCS_CHECKOUT (opt-in, unset by default) +# +# WordPress skips background auto-updates (core, plugins, themes, +# translations) when it finds a .git, .svn, .hg or .bzr directory at or above +# the code it would update, so files deployed from a repository aren't +# rewritten underneath it. A deploy that keeps the repository outside the +# served folder loses that. WORDPRESS_VCS_CHECKOUT=1 restores it: the prepend +# file below seeds WordPress's automatic_updates_is_vcs_checkout filter with +# __return_true before WordPress loads, and WordPress (4.7+) turns pre-seeded +# $wp_filter entries into real hooks. Any other value, or unset, does nothing. +# +# WP-CLI runs (cron among them) can trigger auto-updates too, so a separate +# CLI container running wp needs it as well as the web one (wp run with +# docker exec in a frankenphp container already has it). auto_prepend_file +# runs before every PHP script, WordPress or not, so the file only touches +# $GLOBALS['wp_filter'] and only when the variable is "1". If getenv is +# disabled it can't read the variable and does nothing. It lives outside +# /app. This image isn't built by build-and-push.yml. +# +# The directive goes in php.ini itself, replacing its "auto_prepend_file =" +# line in place, not in a conf.d file: PHP reads conf.d after php.ini, so a +# derived image that sets its own auto_prepend_file (a Wordfence WAF, say), +# by editing that line, appending one or adding any conf.d file, wins. The +# prepend file and php.ini are root-owned and read-only to appuser. appuser +# (group 0) can write conf.d, so it can override this directive there, as it +# can any other PHP setting. +# +# php/8.{2,3,4,5}/frankenphp/Dockerfile and php/8.4/cli/Dockerfile.wordpress +# carry this same block; keep them in sync. +# --------------------------------------------------------------------------- +COPY <<'PHP' /usr/local/share/php-prepend/wordpress-vcs-checkout.php + '__return_true', + 'accepted_args' => 1, + ]; +} +PHP + +# There is only one auto_prepend_file. Fail the build if php.ini is missing or +# the upstream image already sets one, rather than silently replacing it +# (chain to it from the file above if that happens). Then set it in php.ini +# and run a real script both ways: the filter is seeded only with the exact +# value "1". +RUN ini="$PHP_INI_DIR/php.ini" && \ + prepend=/usr/local/share/php-prepend/wordpress-vcs-checkout.php && \ + { test -f "$ini" || { echo "ERROR: $ini is missing" >&2; exit 1; }; } && \ + for bin in php; do \ + $bin -r 'exit(ini_get("auto_prepend_file") === "" ? 0 : 1);' || \ + { echo "ERROR: upstream image already sets auto_prepend_file ($bin)" >&2; exit 1; }; \ + done && \ + if grep -q '^auto_prepend_file[[:space:]]*=' "$ini"; then \ + sed -i "s|^auto_prepend_file[[:space:]]*=.*|auto_prepend_file = $prepend|" "$ini"; \ + else \ + printf '\nauto_prepend_file = %s\n' "$prepend" >> "$ini"; \ + fi && \ + { test "$(grep -c '^auto_prepend_file[[:space:]]*=' "$ini")" = 1 || \ + { echo "ERROR: $ini should have exactly one auto_prepend_file line" >&2; exit 1; }; } && \ + echo ' /tmp/vcs-check.php && \ + seeded='{"automatic_updates_is_vcs_checkout":{"10":[{"function":"__return_true","accepted_args":1}]}}' && \ + for bin in php; do \ + test "$($bin -r 'echo ini_get("auto_prepend_file");')" = "$prepend" || \ + { echo "ERROR: auto_prepend_file is not $prepend ($bin)" >&2; exit 1; }; \ + test "$(WORDPRESS_VCS_CHECKOUT=1 $bin /tmp/vcs-check.php)" = "$seeded" || \ + { echo "ERROR: WORDPRESS_VCS_CHECKOUT=1 did not seed the filter ($bin)" >&2; exit 1; }; \ + test "$(WORDPRESS_VCS_CHECKOUT=true $bin /tmp/vcs-check.php)" = none || \ + { echo "ERROR: WORDPRESS_VCS_CHECKOUT=true seeded the filter ($bin)" >&2; exit 1; }; \ + test "$(env -u WORDPRESS_VCS_CHECKOUT $bin /tmp/vcs-check.php)" = none || \ + { echo "ERROR: unset WORDPRESS_VCS_CHECKOUT seeded the filter ($bin)" >&2; exit 1; }; \ + done && \ + rm /tmp/vcs-check.php + USER appuser diff --git a/php/8.4/frankenphp/Dockerfile b/php/8.4/frankenphp/Dockerfile index ba01b51..f21b673 100644 --- a/php/8.4/frankenphp/Dockerfile +++ b/php/8.4/frankenphp/Dockerfile @@ -1,3 +1,4 @@ +# syntax=docker/dockerfile:1 ARG FRANKENPHP_VERSION=1.14.0 ARG PHP_VERSION=8.4 FROM dunglas/frankenphp:${FRANKENPHP_VERSION}-php${PHP_VERSION}-alpine @@ -133,6 +134,83 @@ COPY --chmod=755 docker-configure /usr/local/bin/docker-configure # source is deployed without its own Dockerfile. COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer +# --------------------------------------------------------------------------- +# WORDPRESS_VCS_CHECKOUT (opt-in, unset by default) +# +# WordPress skips background auto-updates (core, plugins, themes, +# translations) when it finds a .git, .svn, .hg or .bzr directory at or above +# the code it would update, so files deployed from a repository aren't +# rewritten underneath it. A deploy that keeps the repository outside the +# served folder loses that. WORDPRESS_VCS_CHECKOUT=1 restores it: the prepend +# file below seeds WordPress's automatic_updates_is_vcs_checkout filter with +# __return_true before WordPress loads, and WordPress (4.7+) turns pre-seeded +# $wp_filter entries into real hooks. Any other value, or unset, does nothing. +# +# It lives here rather than in Dockerfile.wordpress so a WordPress site on +# this image (framework not flagged) is covered too; every variant inherits +# it. auto_prepend_file runs before every PHP script, WordPress or not, so +# the file only touches $GLOBALS['wp_filter'] and only when the variable is +# "1". If getenv is in PHP_DISABLE_FUNCTIONS it can't read the variable and +# does nothing. It lives outside /app, and docker-configure adds its +# directory to PHP_OPEN_BASEDIR. +# +# The directive goes in php.ini itself, replacing its "auto_prepend_file =" +# line in place, not in a conf.d file: PHP reads conf.d after php.ini, so a +# derived image that sets its own auto_prepend_file (a Wordfence WAF, say), +# by editing that line, appending one or adding any conf.d file, wins. The +# prepend file and php.ini are root-owned and read-only to appuser. appuser +# (group 0) can write conf.d, so it can override this directive there, as it +# can any other PHP setting. +# +# php/8.{2,3,4,5}/frankenphp/Dockerfile and php/8.4/cli/Dockerfile.wordpress +# carry this same block; keep them in sync. +# --------------------------------------------------------------------------- +COPY <<'PHP' /usr/local/share/php-prepend/wordpress-vcs-checkout.php + '__return_true', + 'accepted_args' => 1, + ]; +} +PHP + +# There is only one auto_prepend_file. Fail the build if php.ini is missing or +# the upstream image already sets one, rather than silently replacing it +# (chain to it from the file above if that happens). Then set it in php.ini +# and run a real script both ways: the filter is seeded only with the exact +# value "1". +RUN ini="$PHP_INI_DIR/php.ini" && \ + prepend=/usr/local/share/php-prepend/wordpress-vcs-checkout.php && \ + { test -f "$ini" || { echo "ERROR: $ini is missing" >&2; exit 1; }; } && \ + for bin in "frankenphp php-cli" php; do \ + $bin -r 'exit(ini_get("auto_prepend_file") === "" ? 0 : 1);' || \ + { echo "ERROR: upstream image already sets auto_prepend_file ($bin)" >&2; exit 1; }; \ + done && \ + if grep -q '^auto_prepend_file[[:space:]]*=' "$ini"; then \ + sed -i "s|^auto_prepend_file[[:space:]]*=.*|auto_prepend_file = $prepend|" "$ini"; \ + else \ + printf '\nauto_prepend_file = %s\n' "$prepend" >> "$ini"; \ + fi && \ + { test "$(grep -c '^auto_prepend_file[[:space:]]*=' "$ini")" = 1 || \ + { echo "ERROR: $ini should have exactly one auto_prepend_file line" >&2; exit 1; }; } && \ + echo ' /tmp/vcs-check.php && \ + seeded='{"automatic_updates_is_vcs_checkout":{"10":[{"function":"__return_true","accepted_args":1}]}}' && \ + for bin in "frankenphp php-cli" php; do \ + test "$($bin -r 'echo ini_get("auto_prepend_file");')" = "$prepend" || \ + { echo "ERROR: auto_prepend_file is not $prepend ($bin)" >&2; exit 1; }; \ + test "$(WORDPRESS_VCS_CHECKOUT=1 $bin /tmp/vcs-check.php)" = "$seeded" || \ + { echo "ERROR: WORDPRESS_VCS_CHECKOUT=1 did not seed the filter ($bin)" >&2; exit 1; }; \ + test "$(WORDPRESS_VCS_CHECKOUT=true $bin /tmp/vcs-check.php)" = none || \ + { echo "ERROR: WORDPRESS_VCS_CHECKOUT=true seeded the filter ($bin)" >&2; exit 1; }; \ + test "$(env -u WORDPRESS_VCS_CHECKOUT $bin /tmp/vcs-check.php)" = none || \ + { echo "ERROR: unset WORDPRESS_VCS_CHECKOUT seeded the filter ($bin)" >&2; exit 1; }; \ + done && \ + rm /tmp/vcs-check.php + EXPOSE 80 443 443/udp HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \ diff --git a/php/8.4/frankenphp/docker-configure b/php/8.4/frankenphp/docker-configure index 407d255..bb793ee 100644 --- a/php/8.4/frankenphp/docker-configure +++ b/php/8.4/frankenphp/docker-configure @@ -44,7 +44,11 @@ CADDY_SITE_D=/config/caddy/site.d fi if [ -n "${PHP_OPEN_BASEDIR}" ]; then - echo "open_basedir = ${PHP_OPEN_BASEDIR}" + # The Dockerfile keeps the WORDPRESS_VCS_CHECKOUT auto_prepend_file + # here. PHP refuses to run any script whose prepend file is outside + # open_basedir, so let it in. The directory is root-owned and holds + # only that file. + echo "open_basedir = ${PHP_OPEN_BASEDIR}:/usr/local/share/php-prepend/" fi } > "${PHP_INI_DIR}/conf.d/app.ini" diff --git a/php/8.5/frankenphp/Dockerfile b/php/8.5/frankenphp/Dockerfile index 0105920..d059963 100644 --- a/php/8.5/frankenphp/Dockerfile +++ b/php/8.5/frankenphp/Dockerfile @@ -1,3 +1,4 @@ +# syntax=docker/dockerfile:1 ARG FRANKENPHP_VERSION=1.14.0 ARG PHP_VERSION=8.5 FROM dunglas/frankenphp:${FRANKENPHP_VERSION}-php${PHP_VERSION}-alpine @@ -128,6 +129,83 @@ COPY --chmod=755 docker-configure /usr/local/bin/docker-configure # source is deployed without its own Dockerfile. COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer +# --------------------------------------------------------------------------- +# WORDPRESS_VCS_CHECKOUT (opt-in, unset by default) +# +# WordPress skips background auto-updates (core, plugins, themes, +# translations) when it finds a .git, .svn, .hg or .bzr directory at or above +# the code it would update, so files deployed from a repository aren't +# rewritten underneath it. A deploy that keeps the repository outside the +# served folder loses that. WORDPRESS_VCS_CHECKOUT=1 restores it: the prepend +# file below seeds WordPress's automatic_updates_is_vcs_checkout filter with +# __return_true before WordPress loads, and WordPress (4.7+) turns pre-seeded +# $wp_filter entries into real hooks. Any other value, or unset, does nothing. +# +# It lives here rather than in Dockerfile.wordpress so a WordPress site on +# this image (framework not flagged) is covered too; every variant inherits +# it. auto_prepend_file runs before every PHP script, WordPress or not, so +# the file only touches $GLOBALS['wp_filter'] and only when the variable is +# "1". If getenv is in PHP_DISABLE_FUNCTIONS it can't read the variable and +# does nothing. It lives outside /app, and docker-configure adds its +# directory to PHP_OPEN_BASEDIR. +# +# The directive goes in php.ini itself, replacing its "auto_prepend_file =" +# line in place, not in a conf.d file: PHP reads conf.d after php.ini, so a +# derived image that sets its own auto_prepend_file (a Wordfence WAF, say), +# by editing that line, appending one or adding any conf.d file, wins. The +# prepend file and php.ini are root-owned and read-only to appuser. appuser +# (group 0) can write conf.d, so it can override this directive there, as it +# can any other PHP setting. +# +# php/8.{2,3,4,5}/frankenphp/Dockerfile and php/8.4/cli/Dockerfile.wordpress +# carry this same block; keep them in sync. +# --------------------------------------------------------------------------- +COPY <<'PHP' /usr/local/share/php-prepend/wordpress-vcs-checkout.php + '__return_true', + 'accepted_args' => 1, + ]; +} +PHP + +# There is only one auto_prepend_file. Fail the build if php.ini is missing or +# the upstream image already sets one, rather than silently replacing it +# (chain to it from the file above if that happens). Then set it in php.ini +# and run a real script both ways: the filter is seeded only with the exact +# value "1". +RUN ini="$PHP_INI_DIR/php.ini" && \ + prepend=/usr/local/share/php-prepend/wordpress-vcs-checkout.php && \ + { test -f "$ini" || { echo "ERROR: $ini is missing" >&2; exit 1; }; } && \ + for bin in "frankenphp php-cli" php; do \ + $bin -r 'exit(ini_get("auto_prepend_file") === "" ? 0 : 1);' || \ + { echo "ERROR: upstream image already sets auto_prepend_file ($bin)" >&2; exit 1; }; \ + done && \ + if grep -q '^auto_prepend_file[[:space:]]*=' "$ini"; then \ + sed -i "s|^auto_prepend_file[[:space:]]*=.*|auto_prepend_file = $prepend|" "$ini"; \ + else \ + printf '\nauto_prepend_file = %s\n' "$prepend" >> "$ini"; \ + fi && \ + { test "$(grep -c '^auto_prepend_file[[:space:]]*=' "$ini")" = 1 || \ + { echo "ERROR: $ini should have exactly one auto_prepend_file line" >&2; exit 1; }; } && \ + echo ' /tmp/vcs-check.php && \ + seeded='{"automatic_updates_is_vcs_checkout":{"10":[{"function":"__return_true","accepted_args":1}]}}' && \ + for bin in "frankenphp php-cli" php; do \ + test "$($bin -r 'echo ini_get("auto_prepend_file");')" = "$prepend" || \ + { echo "ERROR: auto_prepend_file is not $prepend ($bin)" >&2; exit 1; }; \ + test "$(WORDPRESS_VCS_CHECKOUT=1 $bin /tmp/vcs-check.php)" = "$seeded" || \ + { echo "ERROR: WORDPRESS_VCS_CHECKOUT=1 did not seed the filter ($bin)" >&2; exit 1; }; \ + test "$(WORDPRESS_VCS_CHECKOUT=true $bin /tmp/vcs-check.php)" = none || \ + { echo "ERROR: WORDPRESS_VCS_CHECKOUT=true seeded the filter ($bin)" >&2; exit 1; }; \ + test "$(env -u WORDPRESS_VCS_CHECKOUT $bin /tmp/vcs-check.php)" = none || \ + { echo "ERROR: unset WORDPRESS_VCS_CHECKOUT seeded the filter ($bin)" >&2; exit 1; }; \ + done && \ + rm /tmp/vcs-check.php + EXPOSE 80 443 443/udp HEALTHCHECK --interval=30s --timeout=5s --start-period=15s --retries=3 \ diff --git a/php/8.5/frankenphp/docker-configure b/php/8.5/frankenphp/docker-configure index 407d255..bb793ee 100644 --- a/php/8.5/frankenphp/docker-configure +++ b/php/8.5/frankenphp/docker-configure @@ -44,7 +44,11 @@ CADDY_SITE_D=/config/caddy/site.d fi if [ -n "${PHP_OPEN_BASEDIR}" ]; then - echo "open_basedir = ${PHP_OPEN_BASEDIR}" + # The Dockerfile keeps the WORDPRESS_VCS_CHECKOUT auto_prepend_file + # here. PHP refuses to run any script whose prepend file is outside + # open_basedir, so let it in. The directory is root-owned and holds + # only that file. + echo "open_basedir = ${PHP_OPEN_BASEDIR}:/usr/local/share/php-prepend/" fi } > "${PHP_INI_DIR}/conf.d/app.ini"