Skip to content

fix(rocky9): prevent /health.php from clearing PHP-FPM auto_prepend_file - #787

Merged
rondinelisaad merged 2 commits into
scieloorg:codex/php8-rocky9-migration-pushablefrom
lgmore:fix/php-fpm-auto-prepend-poisoning
Nov 23, 2026
Merged

rondinelisaad merged 2 commits into
scieloorg:codex/php8-rocky9-migration-pushablefrom
lgmore:fix/php-fpm-auto-prepend-poisoning

Conversation

@lgmore

@lgmore lgmore commented Nov 19, 2026

Copy link
Copy Markdown

Summary

Fixes a PHP-FPM worker state poisoning bug observed while validating infrascielo/metodologia:v7.14-qa on SciELO Paraguay staging (PHP 8.5.12):

  • Root cause: docker/rocky9/httpd-scielo.conf sets:
    <Location "/health.php">
        SetEnv PHP_ADMIN_VALUE "auto_prepend_file="
    </Location>
    Because Apache routes PHP requests to persistent PHP-FPM workers over proxy:unix:/run/php-fpm/www.sock|fcgi://localhost/, PHP_ADMIN_VALUE overwrites auto_prepend_file for the lifetime of that PHP-FPM worker process across subsequent requests (pm.max_requests = 250). After Docker's periodic HEALTHCHECK hits /health.php a few times (pm.max_children = 8), all PHP-FPM workers stop loading htdocs/compat.php, causing endpoints that rely on compat.php (sendMail.php, articleRequestGraphicPage.php, and legacy compat.php polyfills) to fail with HTTP 500 (Call to undefined function scielo_utf8_encode()).

Changes

  1. docker/rocky9/compat-prepend.php + docker/rocky9/php-scielo.ini + docker/rocky9/Dockerfile:
    Install /opt/scielo-health/compat-prepend.php (copied and php -l checked during image build) and set auto_prepend_file = /opt/scielo-health/compat-prepend.php. It loads /var/www/html/htdocs/compat.php only when it exists and SCRIPT_FILENAME !== '/opt/scielo-health/health.php', preserving /health.php independence for older mounted trees without mutating worker INI state.
  2. docker/rocky9/httpd-scielo.conf:
    Remove <Location "/health.php"> SetEnv PHP_ADMIN_VALUE "auto_prepend_file=" </Location>.
  3. htdocs/applications/scielo-org/users/functions.php:
    Add a defensive SCIELO_COMPAT_LOADED require_once for htdocs/compat.php (matching the existing pattern in htdocs/oai/scielo-oai.php and htdocs/classMySQLDatabase.php) for standalone/non-container deployments.
  4. tests/security_regression.php:
    Add regression checks verifying httpd-scielo.conf does not set auto_prepend_file= and php-scielo.ini references /opt/scielo-health/compat-prepend.php.

Verification

  • php -l /opt/scielo-health/compat-prepend.php -> No syntax errors detected
  • /opt/httpd/bin/httpd -d /etc/httpd -f conf/httpd.conf -t -> Syntax OK
  • php /var/www/html/tests/security_regression.php inside v7.14-qa staging container -> Security regression tests passed
  • Restarted staging container, executed /health.php 12 times (HTTP 200), then verified sendMail.php and articleRequestGraphicPage.php across 8 consecutive requests (HTTP 200 on all 8/8 vs HTTP 500 before the fix).

@rondinelisaad

Copy link
Copy Markdown
Member

Faz sentido e corrige o problema real. Aprovação com uma ressalva de testes.

A causa descrita é consistente com um bug ainda aberto do PHP-FPM: valores recebidos por PHP_ADMIN_VALUE podem permanecer no worker e afetar requisições posteriores. Isso está documentado no php-src #24689; o uso de PHP_ADMIN_VALUE via FastCGI também consta na documentação oficial do PHP-FPM.
Reproduzi de forma determinística com PHP 8.5.13 e somente um worker:
Antes de /health.php:
pid=10
compat=loaded
auto_prepend_file=/var/www/html/htdocs/compat.php

Depois de /health.php, mesmo PID:
pid=10
compat=missing
auto_prepend_file=

Com a implementação do PR:
Antes e depois de /health.php, mesmo PID:
pid=10
compat=loaded
auto_prepend_file=/opt/scielo-health/compat-prepend.php

Portanto:

  • Remover o SetEnv PHP_ADMIN_VALUE é correto.
  • O wrapper com caminhos fixos não introduz entrada controlada pelo usuário.
  • A exclusão de /opt/scielo-health/health.php funcionou.
  • A carga defensiva em users/functions.php é redundante no container, mas segura e útil para execuções fora desse fluxo.
  • Os testes existentes, lint PHP e regressão passaram.
  • Nenhum segredo foi encontrado — confirmado localmente e pelo GitGuardian.
    Ressalva
    Os testes adicionados verificam somente strings nos arquivos. Eles não exercitam o comportamento do wrapper nem a sequência “requisição normal → healthcheck → requisição normal”.

Recomendo acrescentar um teste comportamental que confirme:

  1. compat.php é carregado para uma página normal.
  2. Não é carregado para o SCRIPT_FILENAME do healthcheck.
  3. Continua carregado depois de múltiplas consultas ao healthcheck.
    Isso é uma deficiência de cobertura, não uma falha na solução atual.
    Checks do GitHub
    O pr-checks está vermelho por conflito na configuração do CodeQL: o repositório está usando simultaneamente default setup e configuração avançada. Não é causado pelas mudanças do PR, mas o check precisa ser corrigido ou dispensado antes do merge. Ver execução.

Resumo de segurança: 0 críticas, 0 altas, 0 médias, 1 baixa — cobertura de regressão insuficiente.

@rondinelisaad
rondinelisaad merged commit 6f676d5 into scieloorg:codex/php8-rocky9-migration-pushable Nov 23, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants