Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion .github/scripts/smoke-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<?php echo "STORAGE-PHP-EXECUTED";' > "$fixture/public/storage/shell.php"
echo '<?php echo "STORAGE-PHP-EXECUTED";' > "$fixture/public/storage/idx/index.php"
echo 'upload' > "$fixture/public/storage/hello.txt"
cat > "$fixture/public/index.php" <<'PHP'
<?php
echo "VERSION=", PHP_VERSION, "\n";
Expand Down Expand Up @@ -112,6 +117,24 @@ check_php "$version" "$loaded"
uid=$(docker exec "$name" id -u)
[ "$uid" != 0 ] || fail "runs as root"

# PHP under /storage/ never executes, but ordinary files there are still served
# and other /storage/ URLs still reach the app (Laravel's local-disk serve route).
code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "http://127.0.0.1:$port/storage/shell.php") || true
[ "$code" = 404 ] || fail "GET /storage/shell.php returned '$code', expected 404"
code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "http://127.0.0.1:$port/storage/shell.php/foo") || true
[ "$code" = 404 ] || fail "GET /storage/shell.php/foo returned '$code', expected 404"
# A directory URL makes the server run that directory's index.php.
for path in /storage/ /storage/idx/; do
code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 10 "http://127.0.0.1:$port$path") || true
[ "$code" = 404 ] || fail "GET $path returned '$code', expected 404 (directory index.php must not run)"
done
body=$(curl -fsS --max-time 10 "http://127.0.0.1:$port/storage/hello.txt") \
|| fail "GET /storage/hello.txt did not return 2xx"
[ "$body" = upload ] || fail "GET /storage/hello.txt returned '$body', expected the file"
body=$(curl -fsS --max-time 10 "http://127.0.0.1:$port/storage/private/report.pdf") \
|| fail "GET /storage/private/report.pdf (no such file) did not reach the app"
printf '%s\n' "$body" | grep -q '^VERSION=' || fail "a non-file /storage/ URL did not reach the app"

# docker-configure ran at boot and rendered its snippets.
docker exec "$name" test -f /config/caddy/site.d/00-hardening.caddyfile \
|| fail "docker-configure did not render /config/caddy/site.d/00-hardening.caddyfile"
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,20 @@ The `wordpress` variant sets both lists out of the box (see below).

## Laravel hardening

The base image ships no no-exec policy, so a `.php` file that lands in a directory the app can write to is executed when requested. For Laravel that directory is `storage`: the Livewire upload vulnerability ([GHSA-29cq-5w36-x7w3](https://github.com/livewire/livewire/security/advisories/GHSA-29cq-5w36-x7w3)) was exploited in the wild by dropping a script there and then requesting `/storage/....php`. Set `PHP_NO_EXEC_PATHS` so nothing under `/storage/` can ever reach the PHP handler:
Laravel's `storage` directory holds user-controlled files, and the Livewire upload vulnerability ([GHSA-29cq-5w36-x7w3](https://github.com/livewire/livewire/security/advisories/GHSA-29cq-5w36-x7w3)) was exploited in the wild by dropping a `.php` file there and requesting `/storage/....php`. Every frankenphp image therefore refuses, with a 404, any URL under `/storage/` that names a `.php` file (any case such as `shell.PHP`, any depth, with or without trailing path info such as `shell.php/foo`, however the path is encoded) and any directory URL (`/storage/` or `/storage/dir/`), because the server would answer a directory by running its `index.php`. This is on by default; set `PHP_DENY_STORAGE_PHP=off` for an app that really does serve PHP from a `/storage/` URL. The accepted spellings are `off`, `false` and `no` in lower case, upper case or capitalised (`off`, `OFF`, `Off`), and `0`; any other value, including `true`, `1` or a typo, leaves the rule on.

Only those URLs are refused. Everything else under `/storage/` is handled as usual: uploads in `public/storage` are served as static files, and a PHP route at `/storage/{path}` still works, such as Laravel 11+'s local-disk `serve` route for signed `Storage::disk('local')->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

Expand Down
1 change: 1 addition & 0 deletions php/8.2/frankenphp/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
26 changes: 26 additions & 0 deletions php/8.2/frankenphp/docker-configure
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions php/8.3/frankenphp/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
26 changes: 26 additions & 0 deletions php/8.3/frankenphp/docker-configure
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions php/8.4/frankenphp/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
26 changes: 26 additions & 0 deletions php/8.4/frankenphp/docker-configure
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions php/8.5/frankenphp/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
26 changes: 26 additions & 0 deletions php/8.5/frankenphp/docker-configure
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading