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
23 changes: 23 additions & 0 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,29 @@ jobs:
# include hook and the Statamic static-caching preset (#40).
tests/e2e/run-e2e-tests.sh "$IMAGE" nginx-template

- name: Run SSL/TLS E2E test
env:
IMAGE: test-image:local
VERBOSE: "true"
run: |
# Boots with SSL_MODE=on and =full and fetches over TLS (#55).
# Nothing covered TLS before, which is how a standalone `http2 on;` -
# a directive nginx only learned in 1.25.1, against bookworm's
# 1.22.1 - shipped and made the container refuse to start at all.
tests/e2e/run-e2e-tests.sh "$IMAGE" ssl

- name: Run Laravel symlinked-storage E2E test
env:
IMAGE: test-image:local
VERBOSE: "true"
run: |
# storage as a symlink to shared storage (#54). chown -R does not
# traverse a command-line symlink while chmod -R does, so the
# scaffold stayed root-owned while every file in the shared tree was
# rewritten to 775 on each start. Every other Laravel test uses a
# plain directory, which is why this was invisible.
tests/e2e/run-e2e-tests.sh "$IMAGE" laravel-storage

- name: Run security E2E test
env:
IMAGE: test-image:local
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ All notable changes to Cbox PHP Base Images.
- **php-base installs every extension through PIE instead of `pecl`** ([#23](https://github.com/cboxdk/php-baseimages/issues/23)) - the `pecl` binary does not exist in `php:8.6` images (`--with-pear` dropped, PECL formally deprecated, docker-library/php#1682) and the PECL network service has a stated shelf life, so this had to happen whether or not 8.6 is added. Package names are not mechanical - pecl `redis` is PIE `phpredis/phpredis` - and each was verified against Packagist before the switch. PIE is delivered as a pinned, sha256-verified phar through `scripts/download-pie.sh`, the same pattern as cbox-init and fpm-exporter, rather than bind-mounted from a floating registry tag as upstream's examples do: a floating tag is neither reproducible nor dependably available (ghcr's blob CDN returned 502 for both `php/pie` tags for twenty minutes while this was written). The phar is bind-mounted from the build context, so none of its 8.6 MB lands in a layer. All three tiers verified by build: slim, standard and dev carry the same extensions at the same pinned versions as before, opentelemetry still lands in `conf.d-otel/` rather than the scan dir, and the dev tier's own xdebug/spx/pcov ini files still overwrite what PIE wrote - PIE uses the same `docker-php-ext-<name>.ini` filename `docker-php-ext-enable` did, which was measured before anything was migrated

### Fixed
- **`SSL_MODE=on` made the container fail to start at all** ([#55](https://github.com/cboxdk/php-baseimages/issues/55)) - the generated HTTPS server used the standalone `http2 on;` directive, which nginx only learned in 1.25.1, while the image ships Debian bookworm's nginx 1.22.1. The result was not a degraded TLS setup but `[emerg] unknown directive "http2"` and a container that never booted, on every tier, whenever TLS was enabled. Nothing caught it because every other e2e scenario runs plain HTTP - TLS had no test at all. The entrypoint now reads `nginx -v` and picks the form that binary understands: `listen ... ssl http2` below 1.25.1, `http2 on;` from 1.25.1 up. Deliberately version-detected rather than reverted to the old form, because the trixie move ([#52](https://github.com/cboxdk/php-baseimages/issues/52)) brings nginx 1.26+ where the listen parameter is deprecated and the directive is correct - this way that migration needs no second edit here. `tests/e2e/scenarios/test-ssl.sh` boots with `SSL_MODE=on` and `=full`, fetches a page over TLS, asserts the HTTP-to-HTTPS redirect, and checks that the rendered config matches the running nginx in both directions; it also asserts that no TLS server is rendered when `SSL_MODE` is unset. That test immediately found a second, latent bug: `SSL_MODE=full` writes an HTTP-to-HTTPS redirect server that nginx never reaches, because `conf.d` loads alphabetically and `default.conf`'s HTTP server is the first-defined default server for the port. Filed as [#58](https://github.com/cboxdk/php-baseimages/issues/58) rather than fixed here, because the obvious remedies all shadow `/healthz`, which the container healthcheck reaches over plaintext on loopback - that needs a decision, not a patch at the end of an unrelated PR. Nobody can have depended on the redirect: until this release `SSL_MODE=on` and `=full` both aborted startup, so no bookworm container ever ran with TLS at all

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changelog says test-ssl.sh asserts the HTTP-to-HTTPS redirect, but the test explicitly does not assert it (#⁠58).

Remove 'asserts the HTTP-to-HTTPS redirect,' from the #⁠55 entry and say that the redirect is left to #⁠58.

- **Laravel storage on a symlinked shared directory: the scaffold stayed root-owned while shared files were rewritten on every start** ([#54](https://github.com/cboxdk/php-baseimages/issues/54)) - on a release layout, `releases/<id>/storage` is a symlink to `shared/storage`, and GNU `chown -R` and `chmod -R` disagree about a symlink named on the command line. Measured in bookworm rather than assumed: `chown -R www-data release/storage` left a just-scaffolded `storage/app/public` as `root:root`, because `chown -R` defaults to `-P` and will not traverse it, so the app user could not write there; `chmod -R 775 release/storage` *did* follow the symlink and took an existing 644 file in shared storage to 775, on every single container start - sessions, logs, caches and uploads included. One operation did nothing and the other did far too much, and both were invisible without a symlink in the picture, which is precisely why every other Laravel test uses a plain directory. The startup now resolves the path with `realpath` once so the scaffold, the chown and the mode fix all act on the same real directory; skips the recursive walk when the tree is already owned by the app user, the same shortcut `setup_user_permissions` uses for mounted volumes; and applies 775 to directories and 664 to files via `find` instead of marking every log file executable. `tests/e2e/scenarios/test-laravel-storage.sh` builds a real symlinked release tree and asserts the scaffolded directory's owner through the symlink, that a pre-existing 644 file keeps its mode, that directories are 775, that php-fpm's user can write what the scaffold created, and that a second start skips the walk. `docs/guides/symlink-releases.md` documents the resolved behaviour next to the layout that triggers it
- **`cgi.fix_pathinfo=0` made every PHP request 403 on the default template** ([#49](https://github.com/cboxdk/php-baseimages/issues/49)) - the template sent `PATH_TRANSLATED` unconditionally as the script root plus `$fastcgi_path_info`. That path info is empty on every request here, because the PHP location is `~ \.php$` and so cannot match a URI with anything after the `.php` - frameworks are reached through `try_files` as `/index.php` with a query string instead. The value was therefore the web root DIRECTORY, and with `cgi.fix_pathinfo=0` PHP-FPM takes the script from `PATH_TRANSLATED` rather than `SCRIPT_FILENAME`, finds a directory, and `security.limit_extensions` refuses it: `Access to the script '<webroot>' has been denied`, 403 on everything. `cgi.fix_pathinfo=0` is a common hardening and is not set by the image's own ini, which is why nothing caught it; it predates the `$realpath_root` change and was equally broken with `$document_root`. The param is now built through a map and sent `if_not_empty`, so it is omitted when there is no path info and still correct if that location pattern is ever widened. Fixed in the template and in both baked fallback configs; `test-nginx-template.sh` grew a case that mounts `cgi.fix_pathinfo=0` and asserts a 200, no denial in the php-fpm log, and that the param is absent rather than empty - 3 failures on the published image, 0 after
- **The PIE phar download was the one release fetch without `--retry`, and a 40-second GitHub blip proved it** ([run 39595819843](https://github.com/cboxdk/php-baseimages/actions/runs/39595819843)) - at 12:05 UTC on 2026-10-21, minutes after the PIE migration landed on `main`, GitHub's release endpoint returned `500` for a few seconds. Two build jobs died, both at the same line: `curl: (22) The requested URL returned error: 500` fetching `pie.phar`. The cbox-init and fpm-exporter downloads sitting immediately above it in the same step went through the same blip untouched, checksums verified - because they carry `--retry 5 --retry-delay 5 --retry-all-errors` and the phar fetch, added later, did not. That is as clean a controlled experiment as CI ever hands you: same step, same seconds, same endpoint, one flag different. A lost build job is not a lost build - its `merge` job skips and that tier's tag is never promoted, which is the same mechanism as the October publishing outage. Every GitHub-release download in the repository now retries: the 7 `pie.phar` fetches across `_build-image.yml`, `e2e-tests.yml` and `integration-tests.yml`, and all four `scripts/download-*.sh` helpers that local builds use. The sweep also found that `e2e-tests.yml` and `integration-tests.yml` had **no** retries on any of their cbox-init, fpm-exporter or checksums fetches - 20 unprotected downloads, which is the likeliest reason those two workflows have been the flakiest in the repository - so those were fixed in the same pass. Verified that the flags do what is claimed rather than assuming it: a local server returning a hard `500` gets 1 + N requests with the delay applied, so curl does treat an HTTP 500 as retryable
- **A rate-limited Trivy database made the pre-promotion gate fail, which promotes no tag** - on run 39588448211 the `merge` job for slim 8.5 died at the SARIF scan with `failed to download vulnerability DB ... from mirror.gcr.io/aquasec/trivy-db:2: unexpected status code 429 Too Many Requests`. Note where it died: the *scanner*, not the gate - the gate step was skipped and never got to judge anything, so this was never a CVE finding. But a failed `merge` job moves no tags at all, which is the exact mechanism that kept the 8-20 October publishing outage invisible, so a scanner that cannot fetch its database is a publishing outage wearing a different hat. trivy-action does not set `TRIVY_DB_REPOSITORY`, so Trivy used its own default list - `[mirror.gcr.io/..., ghcr.io/...]` - and still aborted on the first entry's 429 rather than trying the second, so the built-in "order of priority" fallback does not cover a rate limit. All three trivy-action invocations across `_build-image.yml` and `check-updates.yml` now put `ghcr.io/aquasecurity/trivy-db:2` first: the merge job has already run `docker/login-action` against ghcr.io and Trivy's OCI client reads those credentials from the docker config, so that pull is authenticated instead of sharing one anonymous per-IP allowance with every other runner on the fleet. `mirror.gcr.io` stays as the second entry. The DB is still fetched once per merge job (~20 per chain) because only the Trivy *binary* is cached, not the database - caching the DB across jobs is the next improvement, not done here
Expand Down
23 changes: 23 additions & 0 deletions docs/guides/symlink-releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,29 @@ ln -sfn /app/shared/storage "$RELEASE/storage"
ln -sfn /app/shared/.env "$RELEASE/.env"
```

## Shared storage and permissions

The entrypoint resolves `storage/` with `realpath` before it touches anything,
so the scaffold, the ownership fix and the mode fix all act on the real shared
directory rather than on the symlink. That matters because GNU `chown -R` and
`chmod -R` disagree about a symlink named on the command line: `chown -R`
defaults to `-P` and does not traverse it, while `chmod -R` follows it. Before
this was resolved, a freshly scaffolded `storage/app/public` stayed `root:root`
and the app could not write it, while every file in shared storage - sessions,
logs, caches - was rewritten to mode 775 on every container start.

Two consequences worth knowing:

- **Ownership is fixed once, not on every start.** If the top of `storage/` is
already owned by the app user, the recursive walk is skipped entirely. On a
large shared storage that is the difference between `O(files)` work on each
restart and none, which also keeps startup probes happy.
- **Directories get 775, files get 664.** A blanket `chmod -R 775` marked every
log and session file executable; they are no longer.

If you pre-create `shared/storage` yourself, chown it to the app user
(`PUID`/`PGID`, default uid 33) and the container will leave it alone.

## Container configuration

```yaml
Expand Down
69 changes: 59 additions & 10 deletions php-fpm-nginx/common/docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -755,11 +755,28 @@ generate_ssl_config() {
ssl_verify_client ${MTLS_VERIFY_CLIENT};
ssl_verify_depth ${MTLS_VERIFY_DEPTH};"

# nginx moved HTTP/2 from a `listen` parameter to its own directive in
# 1.25.1, and emitting the new form on an older build aborts startup
# outright: `[emerg] unknown directive "http2"`, so SSL_MODE=on never
# booted on bookworm at all (#55). bookworm ships 1.22.1. Pick the form
# from the running nginx rather than hardcoding either, so the trixie move
# (#52, nginx 1.26+, where the listen parameter is deprecated) needs no
# second edit here.
local ssl_listen_http2="" ssl_http2_directive="" nginx_ver
nginx_ver=$(nginx -v 2>&1 | sed -n 's|.*nginx/\([0-9][0-9.]*\).*|\1|p')
if [ -n "$nginx_ver" ] && \
[ "$(printf '%s\n' "1.25.1" "$nginx_ver" | sort -V | head -1)" = "1.25.1" ]; then
ssl_http2_directive="http2 on;"
else
ssl_listen_http2="http2"
fi
log_info "HTTPS enabled (nginx ${nginx_ver:-unknown}, HTTP/2 via ${ssl_http2_directive:-listen parameter})"

cat >> /etc/nginx/conf.d/default.conf <<EOF

server {
listen ${NGINX_HTTPS_PORT:-443} ssl ${NGINX_LISTEN_EXTRA};
http2 on;
listen ${NGINX_HTTPS_PORT:-443} ssl ${ssl_listen_http2} ${NGINX_LISTEN_EXTRA};
${ssl_http2_directive}
server_name _;
root ${NGINX_WEBROOT:-/var/www/html/public};
index ${NGINX_INDEX:-index.php index.html};
Expand Down Expand Up @@ -959,22 +976,54 @@ preflight_checks() {
}
fi

# On a release layout, storage is usually a SYMLINK to shared storage
# (releases/<id>/storage -> ../../shared/storage), and chown and chmod
# disagree about a symlink named on the command line: GNU `chown -R`
# defaults to -P and will NOT traverse it, while `chmod -R` follows it.
# Measured in bookworm, not assumed (#54):
#
# chown -R www-data release/storage -> app/public still root:root
# chmod -R 775 release/storage -> an existing 644 file became 775
#
# So the chown silently did nothing while the chmod rewrote every file
# in the SHARED tree - sessions, logs, caches - on every single start.
# Resolve the path once up front and let scaffold, chown and chmod all
# act on the same real directory.
local storage_dir bootstrap_dir
storage_dir=$(realpath "$workdir/storage" 2>/dev/null || printf '%s' "$workdir/storage")
bootstrap_dir=$(realpath "$workdir/bootstrap/cache" 2>/dev/null || printf '%s' "$workdir/bootstrap/cache")

# Scaffold the Laravel storage tree first: a freshly-provisioned (empty)
# volume mounted over storage/ otherwise lacks framework/{cache,sessions,
# views} and logs/, and the app fatals ("directory does not exist").
for d in framework/cache framework/sessions framework/views app/public logs; do
mkdir -p "$workdir/storage/$d" 2>/dev/null || true
mkdir -p "$storage_dir/$d" 2>/dev/null || true
done
mkdir -p "$workdir/bootstrap/cache" 2>/dev/null || true
mkdir -p "$bootstrap_dir" 2>/dev/null || true

# Auto-fix permissions if running as root (skip in rootless mode)
if [ "$(id -u)" = "0" ] && ! is_rootless; then
log_info "Auto-fixing Laravel directory permissions..."
for dir in storage bootstrap/cache; do
[ -d "$workdir/$dir" ] && {
chown -R www-data:www-data "$workdir/$dir" 2>/dev/null || true
chmod -R 775 "$workdir/$dir" 2>/dev/null || true
}
local laravel_user="${APP_USER:-www-data}"
local laravel_group="${APP_GROUP:-$laravel_user}"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The group defaults to the user name here, while setup_user_permissions defaults APP_GROUP to www-data. A missing group makes the whole chown fail silently.

Suggested change
local laravel_group="${APP_GROUP:-$laravel_user}"
local laravel_group="${APP_GROUP:-www-data}"

local laravel_uid
laravel_uid=$(id -u "$laravel_user" 2>/dev/null || echo "")
local dir
for dir in "$storage_dir" "$bootstrap_dir"; do
[ -d "$dir" ] || continue
# Same shortcut as setup_user_permissions: a tree already owned
# by the app user needs no recursive walk, and on a large shared
# storage that walk is O(files) on every start.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ownership skip runs after the scaffold has already created directories as root, so a skipped tree keeps those new directories root-owned. This reintroduces #⁠54…

After each scaffold mkdir, chown the created directory (non-recursively) to $laravel_user:$laravel_group, or create it as the app user, whether or not the recursive walk is skipped. Add an e2e case with shared/storage pre-owned by uid 33.

if [ -n "$laravel_uid" ] && [ "$(stat -c %u "$dir" 2>/dev/null)" = "$laravel_uid" ]; then
log_info "Ownership of $dir already correct - skipping recursive chown/chmod"
continue
fi
log_info "Auto-fixing Laravel directory permissions on $dir"
chown -R "$laravel_user:$laravel_group" "$dir" 2>/dev/null || true
# Directories need the execute bit; files do not. A blanket
# `chmod -R 775` marked every log, session and cache file
# executable, which is both wrong and noisy in audits.
find "$dir" -type d -exec chmod 775 {} + 2>/dev/null || true
find "$dir" -type f -exec chmod 664 {} + 2>/dev/null || true
done
fi

Expand Down
Loading
Loading