Skip to content

Commit e2b7099

Browse files
tchapitchapi
andauthored
Strenghten prod build on Docker (#284)
Co-authored-by: tchapi <regbasket@gmail.com>
1 parent 02847b8 commit e2b7099

10 files changed

Lines changed: 56 additions & 7 deletions

File tree

.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ phpunit.xml.dist
1313
.dockerignore
1414
var/cache/*
1515
var/log/*
16+
var/*.db
17+
var/webdav
18+
var/tmp

.github/workflows/ci.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,28 @@ jobs:
289289
- name: Install dependencies with Composer
290290
run: composer install --no-progress --no-interaction --ansi --optimize-autoloader
291291

292+
# Regression check for #282: a `framework.profiler` block placed outside `when@dev` / `when@test`
293+
# silently enables the Symfony profiler in *every* environment (any child key flips `enabled` to true).
294+
# In production that writes a profile to var/cache/prod on every request and, when that directory
295+
# is not writable (root-owned var/ in a Docker image), turns every request into a 500 with no log.
296+
# We boot the real prod configuration and assert that neither the config nor the compiled container
297+
# carries the profiler. The profiler must only ever be configured in config/packages/{dev,test}/.
298+
- name: Check that the profiler is disabled in production
299+
env:
300+
APP_ENV: prod
301+
APP_DEBUG: "0"
302+
run: |
303+
php bin/console debug:config framework profiler | tee /tmp/profiler-config.txt
304+
if ! grep -q '^enabled: false' /tmp/profiler-config.txt; then
305+
echo "❌ framework.profiler is enabled in the prod environment (see config/packages/framework.yaml)"
306+
exit 1
307+
fi
308+
if php bin/console debug:container --tag=kernel.event_subscriber | grep -qi 'ProfilerListener'; then
309+
echo "❌ The ProfilerListener is registered in the prod container"
310+
exit 1
311+
fi
312+
echo "✅ Profiler disabled in production"
313+
292314
- name: Prepare application
293315
env:
294316
DATABASE_URL: mysql://davis:davis@mysql:3306/davis_test

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ In a shell, if you run Davis locally:
635635
636636
### I have a 500 and a log about `Uncaught Error: Class "Symfony\Bundle\WebProfilerBundle\WebProfilerBundle" not found`
637637
638-
You are running the app in dev mode, but you haven't installed the dev dependencies. Either:
638+
You are running the app in dev mode, but you haven't installed the dev dependencies (the Docker images default to `APP_ENV=prod`, so this only happens if you override it). Either:
639639
640640
a. Set `APP_ENV=prod` in your local env file (See configuration above)
641641

config/packages/dev/web_profiler.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ web_profiler:
33
intercept_redirects: false
44

55
framework:
6-
profiler: { only_exceptions: false }
6+
profiler:
7+
only_exceptions: false
8+
collect_serializer_data: true

config/packages/framework.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ framework:
1414
name: 'DAVIS_SESSION'
1515
storage_factory_id: session.storage.factory.native
1616

17-
profiler:
18-
collect_serializer_data: true
19-
2017
property_info:
2118
with_constructor_extractor: false
2219

config/packages/test/web_profiler.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ web_profiler:
33
intercept_redirects: false
44

55
framework:
6-
profiler: { collect: false }
6+
profiler:
7+
collect: false
8+
collect_serializer_data: true

docker/Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ FROM base-image
7070
ARG fpm_user=82:82
7171
ENV FPM_USER=${fpm_user}
7272

73+
# Production by default: the dev bundles are not installed in the image (composer --no-dev)
74+
ENV APP_ENV=prod
75+
7376
ENV PHP_OPCACHE_MEMORY_CONSUMPTION="256" \
7477
PHP_OPCACHE_MAX_WASTED_PERCENTAGE="10"
7578

@@ -100,6 +103,10 @@ RUN APP_ENV=prod COMPOSER_ALLOW_SUPERUSER=1 composer install --no-ansi --no-dev
100103
&& php ./vendor/symfony/intl/Resources/bin/compress \
101104
&& rm -rf /var/www/davis/docker
102105

106+
# composer install (and its cache warmup) ran as root: give the runtime directories back to the FPM user,
107+
# otherwise PHP-FPM can neither write its cache nor its logs (500 on every request, and no log to tell)
108+
RUN mkdir -p var/log var/cache && chown -R ${FPM_USER} var
109+
103110
USER $FPM_USER
104111

105112
HEALTHCHECK --interval=30s --timeout=1s CMD php-fpm-healthcheck || exit 1

docker/Dockerfile-standalone

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ COPY ./docker/configurations/opcache.ini /usr/local/etc/php/conf.d/opcache.ini
6666
# Final image ————————————————————————————————————————————————————————————————————
6767
FROM base-image
6868

69+
# Production by default: the dev bundles are not installed in the image (composer --no-dev)
70+
ENV APP_ENV=prod
71+
6972
ENV PHP_OPCACHE_MEMORY_CONSUMPTION="256" \
7073
PHP_OPCACHE_MAX_WASTED_PERCENTAGE="10"
7174

docker/configurations/Caddyfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@
2929
-X-Powered-By
3030

3131
# keep referrer data off of HTTP connections
32-
Referrer-Policy no-referrer-when-downgrade
32+
Referrer-Policy strict-origin-when-cross-origin
3333

3434
# disable clients from sniffing the media type
3535
X-Content-Type-Options nosniff
36+
37+
# the admin UI is never meant to be framed
38+
X-Frame-Options DENY
39+
40+
# no browser feature is needed
41+
Permissions-Policy "camera=(), microphone=(), geolocation=()"
3642
}
3743

3844
}

docker/configurations/nginx.conf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ server {
1919

2020
charset utf-8;
2121

22+
# Security headers (add `Strict-Transport-Security` once TLS is terminated in front of nginx)
23+
add_header X-Content-Type-Options nosniff always;
24+
add_header X-Frame-Options DENY always;
25+
add_header Referrer-Policy strict-origin-when-cross-origin always;
26+
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
27+
server_tokens off;
28+
2229
location ~ /(\.ht) {
2330
deny all;
2431
return 404;

0 commit comments

Comments
 (0)