Repository navigation
Description
Concern
The images run as the unprivileged www-data user. That stops a compromised app from becoming root, but www-data can still write more than it needs to. Any bug that lets an attacker write a file, such as an unsafe upload or a path traversal, can then become code execution that stays in place until the container is recreated.
1. The docs make the whole app writable
The production examples use COPY --chown=www-data:www-data . /var/www/html, on these pages:
- packaging your app for deployment
- understanding file permissions
- migrating from the official images
That gives the PHP process write access to its own code: app/, vendor/, config/, routes/, and public/. An attacker can overwrite an existing PHP file, or drop a new .php into public/ where the web server runs it.
2. The image leaves files writable by www-data that aren't needed at runtime
From a stock 8.4-frankenphp-trixie image, running as www-data:
| Path | Needed at runtime? |
|---|---|
/var/www, /var/www/html, /var/www/html/public, public/index.php, /app |
No. Left over from chown -R www-data:www-data /var/www in every variation. |
$PHP_INI_DIR/conf.d/zzz-serversideup-docker-php-debug.ini |
Only written when LOG_OUTPUT_LEVEL=debug. A writable file in conf.d/ lets the app add auto_prepend_file or an extension that every later PHP process loads, including artisan, queue workers, and the scheduler. |
/usr/local/etc/php-fpm.conf, php-fpm.d/zzz-docker-php-serversideup-fpm-debug.conf (FPM variations) |
Rewritten at startup by 1-log-output-level.sh. Same risk as the PHP ini file. |
/composer |
Only if Composer runs as www-data. Low risk, because vendor/bin isn't on PATH. |
/config, /data, /etc/ssl/private, /etc/ssl/healthcheck |
Yes: Caddy state, and certificates when SSL_MODE isn't off. |
Suggested solutions
Docs: copy code as root, and give www-data only the directories the app writes to
COPY . /var/www/html
USER root
RUN chown root:root /var/www/html \
&& chown -R www-data:www-data storage/app storage/framework/views
USER www-data-
COPYwithout--chownleaves files owned byroot, so PHP can read them but not change them. -
chown root:root /var/www/htmlis needed because the image creates that directory owned bywww-data, andCOPYdoesn't change ownership of an existing directory. -
COPY --chown=www-data:www-data <dir>doesn't work as a shortcut. It changes ownership of what's inside the directory, not the directory itself. -
For Laravel,
storage/appandstorage/framework/viewsare the minimum. The second holds compiled Blade, including inlineBlade::render()templates. Everything else then needs these settings:LOG_CHANNEL=stderr- a
databaseorrediscache and session driver route:cacheandevent:cacherun at build time, notconfig:cache, because that would save secrets into the image- the
AUTORUN_LARAVEL_*cache steps turned off, since they write tobootstrap/cache
A less strict version also makes
storage/andbootstrap/cachewritable and needs no settings changed. With either version, the code andpublic/stay read-only.
I tested this pattern against a built image. Only the listed directories were writable by www-data.
Image: no PHP or FPM configuration writable at runtime (non-breaking)
- PHP: in debug mode, export
PHP_DISPLAY_ERRORS=On,PHP_DISPLAY_STARTUP_ERRORS=On, andPHP_ERROR_REPORTING=32767fromdocker-php-serversideup-entrypoint, instead of appending tozzz-serversideup-docker-php-debug.ini.serversideup-docker-php.inialready reads these variables. Then remove the placeholder file and its entries indocker-php-serversideup-set-file-permissions. One trade-off:docker exec ... phpno longer gets debug settings, because it doesn't go through the entrypoint. - FPM: set
log_leveland the access log through environment variables, which FPM config files can read. That wayphp-fpm.confand the debug pool file can be root-owned. - Test: add an image test that fails when
www-datacan write anything in$PHP_INI_DIRor/usr/local/etc/php-fpm*.
I started a branch for the PHP ini part, but it isn't verified yet. The new debug test failed on my local builds and I didn't track down why, so treat this as a direction, not a finished fix.
Image: root-owned /var/www (breaking, next major version)
Stop running chown -R www-data:www-data /var/www in the variation Dockerfiles. This breaks Dockerfiles that run composer install as www-data into /var/www/html, including the multi-stage example in the docs. That's why it would need a major version.
Docs: a hardened runtime example
These are worth documenting, because two of them fail in ways that aren't obvious:
read_only: true, with tmpfs mounts for/tmp,/config, and/data. Each mount needsuid=33,gid=33, or82on Alpine. A plain tmpfs is owned by root, and Caddy logsmkdir /config/caddy: permission denied.cap_drop: [ALL]stops FrankenPHP from starting, withexec: frankenphp: Operation not permitted(exit 126). The binary has thecap_net_bind_servicefile capability, so addcap_add: [NET_BIND_SERVICE].security_opt: [no-new-privileges:true]works as is.- With
SSL_MODE=mixedorfullon a read-only filesystem,/etc/ssl/privateand/etc/ssl/healthcheckmust be writable mounts. Otherwise the container exits during startup.
I'm happy to open PRs for any of these once you've decided which direction you want.
Activity