Repository navigation
build(php): install extensions with PIE and fail the build on missing modules #367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
4b4df9b
e9eb20e
3c0f9c9
825d97d
ddc1124
052c3d2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,14 +37,37 @@ RUN curl -fsSL https://packages.sury.org/php/README.txt | bash && \ | |
| apt-get update && \ | ||
| apt-get install -y libc-client-dev | ||
|
|
||
| RUN pecl install imagick; \ | ||
| pecl install memcached; \ | ||
| pecl install redis; \ | ||
| pecl install apcu; \ | ||
| # https://github.com/php/pecl-encryption-mcrypt/issues/7#issuecomment-1050994596 | ||
| pecl install -n mcrypt; \ | ||
| pecl install gmagick-2.0.6RC1; \ | ||
| pecl install timezonedb; \ | ||
| # Extensions are built with PIE from Packagist/GitHub (not pecl.php.net); docker-php-ext-enable below enables them. | ||
| # CI passes its GITHUB_TOKEN as the optional `github_token` BuildKit secret for PIE's GitHub downloads; it never lands in a layer. | ||
| RUN --mount=type=secret,id=github_token \ | ||
| set -eu; \ | ||
| if [ -s /run/secrets/github_token ]; then \ | ||
| COMPOSER_AUTH="{\"github-oauth\":{\"github.com\":\"$(cat /run/secrets/github_token)\"}}"; \ | ||
| export COMPOSER_AUTH; \ | ||
| fi; \ | ||
| set -x; \ | ||
| curl -fsSL -o /usr/local/bin/pie https://github.com/php/pie/releases/download/1.5.2/pie.phar; \ | ||
| echo "fe70c59738cf56ec00a818cf9cb9593da171aac86f5f1f859995b8839dfd2cc7 /usr/local/bin/pie" | sha256sum -c -; \ | ||
| chmod +x /usr/local/bin/pie; \ | ||
| # Pin PIE's and Composer's dirs so the provenance scan and cleanup below always match. | ||
| export PIE_WORKING_DIRECTORY=/root/.pie COMPOSER_HOME=/root/.composer; \ | ||
| pie install --no-interaction --skip-enable-extension imagick/imagick:3.8.1; \ | ||
| pie install --no-interaction --skip-enable-extension php-memcached/php-memcached:3.4.0; \ | ||
| pie install --no-interaction --skip-enable-extension phpredis/phpredis:6.3.0; \ | ||
| pie install --no-interaction --skip-enable-extension apcu/apcu:5.1.28; \ | ||
| pie install --no-interaction --skip-enable-extension pecl/mcrypt:1.0.9; \ | ||
| # timezonedb stays unpinned: it only exists to ship newer tz data than PHP bundles, so it takes the latest release | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Document when a rebuild refreshes TimezoneDB. If a new TimezoneDB release appears after a local build, BuildKit can reuse this entire
|
||
| pie install --no-interaction --skip-enable-extension pecl/timezonedb; \ | ||
| # Record the exact commits PIE installed in the build log | ||
| php -r 'foreach (glob("/root/.pie/*/pie.lock") as $f) foreach (json_decode(file_get_contents($f), true)["packages"] as $p) echo "PIE installed ", $p["name"], " ", $p["version"], " ", $p["dist"]["reference"] ?? $p["source"]["reference"] ?? "?", "\n";'; \ | ||
| rm -rf /usr/local/bin/pie /root/.pie /root/.composer; \ | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| # gmagick isn't on Packagist: build the source of its pecl 2.0.6RC1 release | ||
| mkdir /tmp/gmagick; \ | ||
| curl -fsSL -o /tmp/gmagick.tar.gz https://github.com/vitoc/gmagick/archive/55423eec7715e89ad63334b35debf582b9566756.tar.gz; \ | ||
| echo "01fd8f0b0c7e92cd73b9165fa3ea4b247beb35bad8f6dda85d65e960607e97b5 /tmp/gmagick.tar.gz" | sha256sum -c -; \ | ||
| tar -xzf /tmp/gmagick.tar.gz -C /tmp/gmagick --strip-components=1; \ | ||
| ( cd /tmp/gmagick && phpize && ./configure && make -j"$(nproc)" && make install ); \ | ||
| rm -rf /tmp/gmagick /tmp/gmagick.tar.gz; \ | ||
| docker-php-ext-configure gd --with-freetype --with-jpeg --with-webp; \ | ||
| docker-php-ext-configure zip; \ | ||
| docker-php-ext-install gd; \ | ||
|
|
@@ -87,9 +110,8 @@ RUN curl -sSL "https://getcomposer.org/installer" | php \ | |
| && mv composer.phar /usr/local/bin/composer | ||
|
|
||
| # Install wp-cli | ||
| RUN curl -O "https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar" \ | ||
| && chmod +x wp-cli.phar \ | ||
| && mv wp-cli.phar /usr/local/bin/wp | ||
| RUN curl -fsSLo /usr/local/bin/wp "https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar" \ | ||
| && chmod +x /usr/local/bin/wp | ||
|
|
||
| # Setup a config file | ||
| RUN mkdir -p /etc/wp-cli | ||
|
|
@@ -126,6 +148,25 @@ COPY docker-entrypoint.sh /usr/local/bin/ | |
| COPY newrelic.ini /usr/local/etc/php/conf.d/newrelic.ini | ||
| COPY newrelic.ini /data/newrelic.ini | ||
|
|
||
| # Fail the build on a PHP startup warning or error, a broken php-fpm config, wp or composer, or a missing module. | ||
| # The list is the previous image's full `php -m`, so it also catches a module dropped from PHP itself. | ||
| RUN set -eu; \ | ||
| out="$(php -v 2>&1 && php -m 2>&1)"; \ | ||
| if printf '%s\n' "$out" | grep -qiE 'warning|error|notice|unable to'; then \ | ||
| echo "Build check failed: PHP printed these startup warnings or errors:" >&2; \ | ||
| printf '%s\n' "$out" | grep -iE 'warning|error|notice|unable to' >&2; \ | ||
| exit 1; \ | ||
| fi; \ | ||
| php-fpm -t; \ | ||
| wp --allow-root --version; \ | ||
| COMPOSER_ALLOW_SUPERUSER=1 composer --version; \ | ||
| modules="$(php -m | tr '[:upper:] ' '[:lower:]_')"; \ | ||
| for m in apcu bcmath calendar core ctype curl date dom exif fileinfo filter ftp gd gmagick gmp hash iconv imagick imap intl json libxml mbstring mcrypt memcached mysqli mysqlnd newrelic openssl pcntl pcre pdo pdo_mysql pdo_sqlite phar posix readline redis reflection session shmop simplexml soap sockets sodium spl sqlite3 standard sysvsem sysvshm timezonedb tokenizer xml xmlreader xmlwriter zend_opcache zip zlib; do \ | ||
| printf '%s\n' "$modules" | grep -qx "$m" || { echo "Build check failed: PHP module $m is missing" >&2; exit 1; }; \ | ||
| done; \ | ||
| # The checks above leave root-owned logs that www-data couldn't write at runtime; don't ship them | ||
| rm -f /var/log/php/access.log /var/log/php/error.log /var/log/newrelic/php_agent.log | ||
|
|
||
| WORKDIR /var/www/htdocs | ||
| RUN usermod -s /bin/bash www-data | ||
| USER www-data | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sensitive Data Exposure
Reachability: External
Exploitability: Difficult
CWE: CWE-522 — Insufficiently Protected Credentials
View Security blast radius
The CI token reaches package build code during the PHP extension build.
Every PHP matrix build receives the
github_tokensecret, including pull-request builds. Line 46 exportsCOMPOSER_AUTHwith the token value. Thepie installcommands at lines 54-60 run in the sameRUNstep and inherit that environment. PIE builds each downloaded extension, including the TimezoneDB source resolved at line 60. Build code from a compromised extension source can therefore read the token. A secret mount limits access to the file only. It does not stop commands in the mounted step from reading the value.Use anonymous downloads for PIE. If authenticated downloads are required, run them in a separate step that uses a download-only credential and does not run package build code. Also check the effective
GITHUB_TOKENpermissions for these jobs. Write access to repository contents makes repository modification possible in that case.Source: Learnings