diff --git a/.github/scripts/smoke-test.sh b/.github/scripts/smoke-test.sh index 6654728..53ed2f4 100755 --- a/.github/scripts/smoke-test.sh +++ b/.github/scripts/smoke-test.sh @@ -68,7 +68,12 @@ trap cleanup EXIT # A page that reports what the server's own PHP looks like. The container user # is not the runner's, so the fixture must be world-readable. -mkdir -p "$fixture/public" +mkdir -p "$fixture/public/storage/idx" +# A script an attacker could drop under /storage/ (Livewire GHSA-29cq-5w36-x7w3), +# and an ordinary upload next to it. +echo ' "$fixture/public/storage/shell.php" +echo ' "$fixture/public/storage/idx/index.php" +echo 'upload' > "$fixture/public/storage/hello.txt" cat > "$fixture/public/index.php" <<'PHP' temporaryUrl()` links. + +The rule covers only the URL path `/storage/`. For other directories the app can write to, `PHP_NO_EXEC_PATHS` goes further: nothing under those prefixes can ever reach the PHP handler, so a request for a path with no file behind it gets a 404 rather than falling through to the app (which is why it is not the default for `/storage/`). ```sh -docker run -e PHP_NO_EXEC_PATHS="/storage/*" \ +docker run -e PHP_NO_EXEC_PATHS="/uploads/*" \ -e CADDY_IMMUTABLE_ASSET_PATHS="/build/" \ -e CADDY_MEDIA_PATHS="/storage/" \ ghcr.io/aliasproject/php:8.4-frankenphp ``` -Requests for PHP source under that path (any case, e.g. `shell.PHP`, or with trailing path info, e.g. `shell.php/foo`) never execute: they return 404 or 400, or (with a trailing slash) redirect to a URL that returns 404. Images and other files in the directory are still served, so `public/storage` uploads keep working. The rule covers only the URL path `/storage/`; if the app serves uploads or other writable files from another public path, add it too, space-separated (`PHP_NO_EXEC_PATHS="/storage/* /uploads/*"`). `PHP_NO_EXEC_PATHS` replaces the default rather than appending, so a derived image or `docker run` that sets it must list every path it wants protected. +Requests for PHP source under a `PHP_NO_EXEC_PATHS` prefix (any case, or with trailing path info) never execute: they return 404 or 400, or (with a trailing slash) redirect to a URL that returns 404. Images and other files in the directory are still served. Add more prefixes space-separated (`PHP_NO_EXEC_PATHS="/uploads/* /files/*"`). `PHP_NO_EXEC_PATHS` replaces the default rather than appending, so a derived image or `docker run` that sets it must list every path it wants protected. ## WordPress hardening diff --git a/php/8.2/frankenphp/Dockerfile b/php/8.2/frankenphp/Dockerfile index 6242ebc..534c32c 100644 --- a/php/8.2/frankenphp/Dockerfile +++ b/php/8.2/frankenphp/Dockerfile @@ -32,6 +32,7 @@ ENV PHP_MEMORY_LIMIT=256M \ PHP_NO_EXEC_HIDE_EXTENSIONS="php phtml phar phps php3 php4 php5 php6 php7 php8 inc" \ PHP_DENY_PATHS= \ PHP_DENY_EXTENSIONS= \ + PHP_DENY_STORAGE_PHP=on \ CADDY_ACCESS_LOG=on \ CADDY_ACCESS_LOG_PATH=/data/caddy/access.log \ MSMTP_RELAY_SERVER_HOST=mailhog \ diff --git a/php/8.2/frankenphp/docker-configure b/php/8.2/frankenphp/docker-configure index ba99f08..407d255 100644 --- a/php/8.2/frankenphp/docker-configure +++ b/php/8.2/frankenphp/docker-configure @@ -110,6 +110,32 @@ ext_alternation() { echo "}" fi + # Laravel's storage directory holds user-controlled files, and the Livewire + # upload RCE (GHSA-29cq-5w36-x7w3) was exploited in the wild by dropping a + # .php file there and requesting it. Nothing legitimately executes PHP from + # a /storage/ URL, so refuse two kinds of URL under it: + # - one that names a .php file: any case, any depth, with or without + # trailing path info (which is how php_server splits it); + # - a directory URL (trailing slash, or /storage/ itself): php_server + # answers those by running the directory's index.php, without the URL + # ever naming a .php file. A request for the directory without the + # slash is redirected to the slash form, so it lands here too. + # Deliberately NOT `handle /storage/*` + file_server like the no-exec paths + # below: that also 404s requests with no file behind them, which breaks + # Laravel 11+'s local-disk serve route (GET /storage/{path}, answered by + # PHP). Everything else under /storage/ still reaches php_server and the + # static file handler. Only an explicit off/0/false/no turns this off, so a + # mistyped value cannot silently disable the protection. + case "${PHP_DENY_STORAGE_PHP:-on}" in + off | OFF | Off | 0 | false | FALSE | False | no | NO | No) ;; + *) + echo "@storage_php path_regexp (?i)^/storage/(.*\\.php(/|\$)|(.*/)?\$)" + echo "handle @storage_php {" + echo " respond 404" + echo "}" + ;; + esac + # Paths the application can WRITE to must never EXECUTE. `handle` is # terminal and contains only file_server, so requests matching these # prefixes can never reach the PHP handler -- regardless of extension diff --git a/php/8.3/frankenphp/Dockerfile b/php/8.3/frankenphp/Dockerfile index 578c9af..49e1029 100644 --- a/php/8.3/frankenphp/Dockerfile +++ b/php/8.3/frankenphp/Dockerfile @@ -32,6 +32,7 @@ ENV PHP_MEMORY_LIMIT=256M \ PHP_NO_EXEC_HIDE_EXTENSIONS="php phtml phar phps php3 php4 php5 php6 php7 php8 inc" \ PHP_DENY_PATHS= \ PHP_DENY_EXTENSIONS= \ + PHP_DENY_STORAGE_PHP=on \ CADDY_ACCESS_LOG=on \ CADDY_ACCESS_LOG_PATH=/data/caddy/access.log \ MSMTP_RELAY_SERVER_HOST=mailhog \ diff --git a/php/8.3/frankenphp/docker-configure b/php/8.3/frankenphp/docker-configure index ba99f08..407d255 100644 --- a/php/8.3/frankenphp/docker-configure +++ b/php/8.3/frankenphp/docker-configure @@ -110,6 +110,32 @@ ext_alternation() { echo "}" fi + # Laravel's storage directory holds user-controlled files, and the Livewire + # upload RCE (GHSA-29cq-5w36-x7w3) was exploited in the wild by dropping a + # .php file there and requesting it. Nothing legitimately executes PHP from + # a /storage/ URL, so refuse two kinds of URL under it: + # - one that names a .php file: any case, any depth, with or without + # trailing path info (which is how php_server splits it); + # - a directory URL (trailing slash, or /storage/ itself): php_server + # answers those by running the directory's index.php, without the URL + # ever naming a .php file. A request for the directory without the + # slash is redirected to the slash form, so it lands here too. + # Deliberately NOT `handle /storage/*` + file_server like the no-exec paths + # below: that also 404s requests with no file behind them, which breaks + # Laravel 11+'s local-disk serve route (GET /storage/{path}, answered by + # PHP). Everything else under /storage/ still reaches php_server and the + # static file handler. Only an explicit off/0/false/no turns this off, so a + # mistyped value cannot silently disable the protection. + case "${PHP_DENY_STORAGE_PHP:-on}" in + off | OFF | Off | 0 | false | FALSE | False | no | NO | No) ;; + *) + echo "@storage_php path_regexp (?i)^/storage/(.*\\.php(/|\$)|(.*/)?\$)" + echo "handle @storage_php {" + echo " respond 404" + echo "}" + ;; + esac + # Paths the application can WRITE to must never EXECUTE. `handle` is # terminal and contains only file_server, so requests matching these # prefixes can never reach the PHP handler -- regardless of extension diff --git a/php/8.4/frankenphp/Dockerfile b/php/8.4/frankenphp/Dockerfile index f844453..d335357 100644 --- a/php/8.4/frankenphp/Dockerfile +++ b/php/8.4/frankenphp/Dockerfile @@ -32,6 +32,7 @@ ENV PHP_MEMORY_LIMIT=256M \ PHP_NO_EXEC_HIDE_EXTENSIONS="php phtml phar phps php3 php4 php5 php6 php7 php8 inc" \ PHP_DENY_PATHS= \ PHP_DENY_EXTENSIONS= \ + PHP_DENY_STORAGE_PHP=on \ CADDY_ACCESS_LOG=on \ CADDY_ACCESS_LOG_PATH=/data/caddy/access.log \ MSMTP_RELAY_SERVER_HOST=mailhog \ diff --git a/php/8.4/frankenphp/docker-configure b/php/8.4/frankenphp/docker-configure index ba99f08..407d255 100644 --- a/php/8.4/frankenphp/docker-configure +++ b/php/8.4/frankenphp/docker-configure @@ -110,6 +110,32 @@ ext_alternation() { echo "}" fi + # Laravel's storage directory holds user-controlled files, and the Livewire + # upload RCE (GHSA-29cq-5w36-x7w3) was exploited in the wild by dropping a + # .php file there and requesting it. Nothing legitimately executes PHP from + # a /storage/ URL, so refuse two kinds of URL under it: + # - one that names a .php file: any case, any depth, with or without + # trailing path info (which is how php_server splits it); + # - a directory URL (trailing slash, or /storage/ itself): php_server + # answers those by running the directory's index.php, without the URL + # ever naming a .php file. A request for the directory without the + # slash is redirected to the slash form, so it lands here too. + # Deliberately NOT `handle /storage/*` + file_server like the no-exec paths + # below: that also 404s requests with no file behind them, which breaks + # Laravel 11+'s local-disk serve route (GET /storage/{path}, answered by + # PHP). Everything else under /storage/ still reaches php_server and the + # static file handler. Only an explicit off/0/false/no turns this off, so a + # mistyped value cannot silently disable the protection. + case "${PHP_DENY_STORAGE_PHP:-on}" in + off | OFF | Off | 0 | false | FALSE | False | no | NO | No) ;; + *) + echo "@storage_php path_regexp (?i)^/storage/(.*\\.php(/|\$)|(.*/)?\$)" + echo "handle @storage_php {" + echo " respond 404" + echo "}" + ;; + esac + # Paths the application can WRITE to must never EXECUTE. `handle` is # terminal and contains only file_server, so requests matching these # prefixes can never reach the PHP handler -- regardless of extension diff --git a/php/8.5/frankenphp/Dockerfile b/php/8.5/frankenphp/Dockerfile index 3438d80..a484eb8 100644 --- a/php/8.5/frankenphp/Dockerfile +++ b/php/8.5/frankenphp/Dockerfile @@ -32,6 +32,7 @@ ENV PHP_MEMORY_LIMIT=256M \ PHP_NO_EXEC_HIDE_EXTENSIONS="php phtml phar phps php3 php4 php5 php6 php7 php8 inc" \ PHP_DENY_PATHS= \ PHP_DENY_EXTENSIONS= \ + PHP_DENY_STORAGE_PHP=on \ CADDY_ACCESS_LOG=on \ CADDY_ACCESS_LOG_PATH=/data/caddy/access.log \ MSMTP_RELAY_SERVER_HOST=mailhog \ diff --git a/php/8.5/frankenphp/docker-configure b/php/8.5/frankenphp/docker-configure index ba99f08..407d255 100644 --- a/php/8.5/frankenphp/docker-configure +++ b/php/8.5/frankenphp/docker-configure @@ -110,6 +110,32 @@ ext_alternation() { echo "}" fi + # Laravel's storage directory holds user-controlled files, and the Livewire + # upload RCE (GHSA-29cq-5w36-x7w3) was exploited in the wild by dropping a + # .php file there and requesting it. Nothing legitimately executes PHP from + # a /storage/ URL, so refuse two kinds of URL under it: + # - one that names a .php file: any case, any depth, with or without + # trailing path info (which is how php_server splits it); + # - a directory URL (trailing slash, or /storage/ itself): php_server + # answers those by running the directory's index.php, without the URL + # ever naming a .php file. A request for the directory without the + # slash is redirected to the slash form, so it lands here too. + # Deliberately NOT `handle /storage/*` + file_server like the no-exec paths + # below: that also 404s requests with no file behind them, which breaks + # Laravel 11+'s local-disk serve route (GET /storage/{path}, answered by + # PHP). Everything else under /storage/ still reaches php_server and the + # static file handler. Only an explicit off/0/false/no turns this off, so a + # mistyped value cannot silently disable the protection. + case "${PHP_DENY_STORAGE_PHP:-on}" in + off | OFF | Off | 0 | false | FALSE | False | no | NO | No) ;; + *) + echo "@storage_php path_regexp (?i)^/storage/(.*\\.php(/|\$)|(.*/)?\$)" + echo "handle @storage_php {" + echo " respond 404" + echo "}" + ;; + esac + # Paths the application can WRITE to must never EXECUTE. `handle` is # terminal and contains only file_server, so requests matching these # prefixes can never reach the PHP handler -- regardless of extension