Repository navigation
Keep app code read-only for www-data in Dockerfile examples - #758
Closed
Abdulmajeed-Jamaan wants to merge 2 commits into
Closed
Abdulmajeed-Jamaan wants to merge 2 commits into
Abdulmajeed-Jamaan wants to merge 2 commits into
Conversation
The docs told users to COPY the whole app with --chown=www-data:www-data, which lets the PHP process overwrite its own code, including public/. Copy code as root instead and give www-data only the directories the app writes to (storage/ and bootstrap/cache/ for Laravel).
storage/app holds uploads and storage/framework/views holds compiled Blade, including inline Blade::render() templates. Everything else stays read-only, so document how Laravel's logs, file cache, sessions, and bootstrap/cache writes move elsewhere.
Contributor
Author
|
Closing in favor of #759, which covers this along with the related image defaults so the approach can be agreed on first. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The production Dockerfile examples copy the whole app with
COPY --chown=www-data:www-data . /var/www/html. That makes all the code writable bywww-data, the user PHP runs as:app/,vendor/,config/,routes/, andpublic/.The images run unprivileged, but this setup undoes most of the benefit. Any bug that lets an attacker write a file, such as an unsafe upload or path traversal, becomes code execution. The attacker can overwrite an existing PHP file, or drop a new
.phpintopublic/where the web server runs it.Fix
Copy the code as
root, and givewww-dataonly the two directories Laravel must write to at runtime:storage/app/public/storagelinks tostorage/app/public)storage/framework/views/Blade::render()at runtimeEverything else Laravel writes by default moves elsewhere, and the packaging page documents this in a table:
LOG_CHANNEL=stderrinstead ofstorage/logs/.databaseorredisdriver instead offile.bootstrap/cache: runroute:cacheandevent:cachein the Dockerfile, and turn off the Laravel automations' optimize and cache steps.config:cachestays out of the build because it would save secrets into the image.Notes on the Dockerfile:
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.composer dump-autoloadnow runs asroot(COMPOSER_ALLOW_SUPERUSER=1is already set in every variation), and thechownruns after it.Changes
4.deployment-and-production/1.packaging-your-app-for-deployment.md: both examples, a caution against--chownon the whole app, and a section on what Laravel needs with this setup5.customizing-the-image/5.understanding-file-permissions.md: the production stage1.getting-started/7.migrating-from-official-php-images.md: the before/after example and the "Givewww-datayour files" step6.upgrading/4.upgrade-to-v3.mdstill shows the old line. I left that historical page unchanged.Testing
I built a test image from
serversideup/php:8.4-frankenphp-trixiewith the new pattern. Aswww-data:storage/app,storage/app/public, andstorage/framework/views./var/www/html,public/,storage/,storage/framework/cache,bootstrap/cache, and the app's PHP files.