Skip to content

fix: set the superglobals like PHP so $_ENV and $_REQUEST reach the script - #17

Merged
nlemoine merged 3 commits into
mainfrom
fix/env-superglobal
Nov 26, 2026
Merged

nlemoine merged 3 commits into
mainfrom
fix/env-superglobal

Conversation

@nlemoine

@nlemoine nlemoine commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

$_ENV and $_REQUEST did not reach the target script. A GET to ?a=1 gave $_GET = ['a' => '1'] but $_REQUEST = [], and a value a GlobalsHandler put in $_ENV was lost.

The bootstrap set the superglobals with $GLOBALS[$name] = .... With auto_globals_jit=1 (the default), PHP builds $_ENV, $_SERVER and $_REQUEST when it compiles a file that names them, or when filter_input() needs them, and that replaced the values. $_SERVER survived only because the CLI SAPI builds it at startup. What the script saw depended on the ini: under variables_order=GPCS (the value of the php.ini files PHP ships) $_ENV and $_REQUEST were empty, under EGPCS (PHP's built-in value when no php.ini is loaded, like the official Docker images) $_ENV was the process environment instead of what the client sent, and $_REQUEST was still empty.

Two commits.

The first one assigns every superglobal by name, like Symfony's Request::overrideGlobals(), so they are built while the bootstrap compiles. $_ENV is now exactly what the client sends, whatever the ini. That is what the README ("a clean set of superglobals built from the HTTP request only") and SECURITY.md already describe, and it is coherent with $_SERVER, which never had the environment without InheritEnvGlobalsHandler. This is a behavior change for EGPCS setups: without a handler, the script's $_ENV no longer holds the process environment. getenv() still works, and InheritEnvGlobalsHandler brings the variables back. Also, extra keys a handler adds to the array no longer become global variables in the script, that came from the $GLOBALS loop.

The second one builds $_REQUEST in the script's PHP process, from the $_GET, $_POST and $_COOKIE it receives and with its own request_order, like PHP and overrideGlobals() do. Before, it was built in the calling process with its ini, and with array_merge(), which loses nested keys and renumbers numeric ones. Handlers still see a _REQUEST, changes they make to _GET/_POST/_COOKIE now reach it, and a _REQUEST they change themselves is sent as is.

I asked for a second opinion on $_ENV before choosing. Merging the client values into what PHP built would have avoided the EGPCS change, but it makes $_ENV depend on the ini and lets the parent's $_ENV reach the script without a handler (Symfony Process exports it as real environment variables), which the isolation test is meant to prevent.

Tests run the script under GPCS, EGPCS and EGPCS without JIT through a small PHP wrapper passed as phpExecutable, and under several request_order values. The $_REQUEST assertions commented out in ClientTest ("needs further investigation") pass again. The README now also says that filter_input() can't see the emulated superglobals.

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.65%. Comparing base (35a99a8) to head (45889df).

Additional details and impacted files
@@             Coverage Diff              @@
##               main      #17      +/-   ##
============================================
+ Coverage     93.61%   93.65%   +0.03%     
- Complexity      578      581       +3     
============================================
  Files            16       17       +1     
  Lines          1347     1355       +8     
============================================
+ Hits           1261     1269       +8     
  Misses           86       86              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nlemoine
nlemoine force-pushed the fix/env-superglobal branch from cfb1ba4 to 7bc0c63 Compare November 26, 2026 12:18
@nlemoine nlemoine changed the title fix: pass $_ENV and $_REQUEST to the script fix: set the superglobals like PHP so $_ENV and $_REQUEST reach the script Nov 26, 2026
The bootstrap set the superglobals through $GLOBALS. With
auto_globals_jit, on by default, PHP builds $_ENV, $_SERVER and
$_REQUEST when it compiles a file that names them, or when
filter_input() needs them, and replaced the values set that way. The
script got an empty $_ENV and $_REQUEST under the variables_order of
the php.ini files PHP ships (GPCS), and the process environment instead
of the client's values when no php.ini is loaded (EGPCS). $_SERVER only
survived because the CLI SAPI builds it at startup.

The bootstrap now assigns every superglobal by name, like Symfony's
Request::overrideGlobals(), so they are built while it compiles. $_ENV
is now exactly what the client sends, whatever the ini, as the README
and SECURITY.md already describe.

Behaviour change: with variables_order=EGPCS and no globals handler,
the script's $_ENV no longer holds the process environment. Use
InheritEnvGlobalsHandler, or getenv(). Extra keys a globals handler
adds to the array no longer become global variables in the script.
$_REQUEST was computed in the calling process, with its request_order,
and with array_merge(), which drops nested keys and renumbers numeric
ones. The script's PHP process now builds it from the $_GET, $_POST and
$_COOKIE it receives, with its own request_order (or variables_order),
each letter once and a recursive merge, like PHP and Symfony's
Request::overrideGlobals().

Globals handlers still see a _REQUEST built the same way. Changes they
make to _GET, _POST or _COOKIE now reach $_REQUEST, and a _REQUEST they
change themselves is sent as is.

The $_REQUEST assertions that were commented out in ClientTest pass
again.
PHP falls back to variables_order for $_REQUEST only when
request_order is not set. Set but empty, it builds an empty $_REQUEST.
ini_get() returns '' in both cases, so the order now comes from
ini_get_all(), which gives null for a directive that is not set.

Review follow-ups in the same area:
- A handler that removes _REQUEST no longer triggers an undefined key
  warning in the calling process.
- The bootstrap no longer fails with a TypeError when a handler sets
  _GET, _POST or _COOKIE to something other than an array.
- The README and the GlobalsHandler docblock say that keys outside the
  documented shape are ignored, how a changed _REQUEST is handled, and
  what filter_input() really returns.
- Tests pin request_order through a PHP wrapper instead of depending on
  the php.ini of the machine, and check literal values.
@nlemoine
nlemoine force-pushed the fix/env-superglobal branch from 9c25358 to 45889df Compare November 26, 2026 12:55
@nlemoine
nlemoine merged commit e5982db into main Nov 26, 2026
14 checks passed
@nlemoine
nlemoine deleted the fix/env-superglobal branch November 26, 2026 13:07
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.

1 participant