Skip to content

Reset Webapi\Request state between requests (#1 in frankenphp-base)#2

Open
acourtiol wants to merge 1 commit into
opengento:mainfrom
acourtiol:feat/webapi-request-reset
Open

Reset Webapi\Request state between requests (#1 in frankenphp-base)#2
acourtiol wants to merge 1 commit into
opengento:mainfrom
acourtiol:feat/webapi-request-reset

Conversation

@acourtiol
Copy link
Copy Markdown

Addresses the worker-mode state-leak vector in Magento\Framework\Webapi\Request — the most concrete "Laminas Http patch" candidate I identified while looking at opengento/magento2-frankenphp-base#1.

What's leaking and why

Magento\Framework\Webapi\Request extends Magento\Framework\HTTP\PhpEnvironment\Request (which extends Laminas\Http\PhpEnvironment\Request). The parent chain holds per-request state on protected fields: $module, $controller, $action, $pathInfo, $requestString, $params, $aliases, $dispatched, $forwarded, $baseUrl, $basePath, $requestUri, $method, $headers, $metadata, $content, and more.

Magento\Framework\App\Request\Http (the sibling class used for the /index.php path) implements ResetAfterRequestInterface and resets all of those fields between requests. But Webapi\Request doesn't implement the interface — and neither does the shared parent.

So in worker mode:

  • /index.php requests stay clean across the worker's lifetime.
  • /rest/V1/... and /soap/... requests on the same worker inherit state from previous Webapi requests on that worker.

This is historically the source of intermittent "wrong store", "wrong area", "auth context leaks" reports under long-running PHP runtimes (Swoole / RoadRunner / FrankenPHP worker).

The fix

App/Webapi/Request.php — subclass that implements ResetAfterRequestInterface with a _resetState() body mirroring Magento\Framework\App\Request\Http::_resetState(). Same parent, same per-request fields, same reset body applies cleanly.

Wired via etc/di.xml preference so all DI consumers of Magento\Framework\Webapi\Request get our subclass.

Compatibility

Magento\Framework\Webapi\Request is byte-identical between Magento 2.4.7 and 2.4.9-beta1 (Copyright header aside). The protected fields we touch are declared by the shared HTTP\PhpEnvironment\Request parent and are stable across the 2.4.x line.

Scope notes for review

This is the most concrete state-leak vector I identified. There are other Laminas-extending Magento classes that may deserve similar treatment:

  • Magento\Framework\HTTP\Client\* — outbound HTTP clients hold cookies / auth state across requests.
  • Any custom modules extending HTTP\PhpEnvironment\Request without implementing ResetAfterRequestInterface.

Those are out of scope for this PR; happy to expand on a follow-up.

Stacking

Independent of PR #1 (App\Media) — different files, no overlap.

…p-base#1)

Closes the worker-mode state-leak vector in `Magento\Framework\Webapi\Request`
(REST + SOAP request object).

What's leaking and why
----------------------
`Magento\Framework\Webapi\Request` extends `Magento\Framework\HTTP\PhpEnvironment\Request`
(which extends `Laminas\Http\PhpEnvironment\Request`). The parent chain holds
per-request state on protected fields: $module, $controller, $action,
$pathInfo, $requestString, $params, $aliases, $dispatched, $forwarded,
$baseUrl, $basePath, $requestUri, $method, $headers, $metadata, $content,
and more.

`Magento\Framework\App\Request\Http` (the SIBLING class used for the
/index.php path) implements ResetAfterRequestInterface and resets all of
those fields between requests. But `Webapi\Request` does NOT implement
the interface — neither does the shared parent. So in worker mode:

  - /index.php requests stay clean across the worker's lifetime.
  - /rest/V1/... and /soap/... requests on the same worker inherit
    state from previous Webapi requests on that worker.

This has historically been the source of intermittent "wrong store",
"wrong area", "auth context leaks" reports under Swoole / RoadRunner /
FrankenPHP worker setups.

The fix
-------
Add `Opengento\Application\Webapi\Request` — a subclass that implements
ResetAfterRequestInterface with a `_resetState()` body mirroring
`Magento\Framework\App\Request\Http::_resetState()`. Both classes have
the same parent and the same per-request fields, so the same reset body
applies cleanly.

Wired in via etc/di.xml preference so DI consumers of
`Magento\Framework\Webapi\Request` receive our subclass transparently.

Compatibility
-------------
`Magento\Framework\Webapi\Request` is byte-identical between Magento
2.4.7 and 2.4.9-beta1 (Copyright header aside). The protected fields
we touch are declared by the shared `HTTP\PhpEnvironment\Request` parent
and are stable across the 2.4.x line.

Scope notes for review
----------------------
This is the most concrete "Laminas Http patch" candidate I identified
for issue opengento/magento2-frankenphp-base#1 ("Add Laminas Http
Patch"). There are other Laminas-extending Magento classes that may
deserve similar treatment (`Magento\Framework\HTTP\Client\*` outbound
HTTP clients hold cookies/auth across requests in worker mode), but
those are out of scope for this PR — happy to expand on follow-up.
acourtiol added a commit to acourtiol/magento2-application that referenced this pull request May 11, 2026
Adds Opengento\Application\App\Media — the missing third worker-mode entry
point alongside App\Http (pub/index.php) and App\StaticResource
(pub/static.php). Closes the get.php gap that's currently behind two open
issues on magento2-frankenphp-base:

  - opengento#2 "Product Image Cache generation failure" (PLP/PDP image 404s)
  - #3 "Make all entry points benefit of shared bootstrap"

How it works
------------
App\Media implements AppInterface. Per request, it:
  1. Snapshots State::$_areaCode (via reflection — see "Why reflection")
  2. Clears it
  3. Constructs Magento\MediaStorage\App\Media via the worker's shared
     ObjectManager with request-derived (mediaDirectory, configCacheFile,
     isAllowed, relativeFileName) arguments — same contract stock
     pub/get.php uses
  4. Delegates to MagentoMedia::launch() (which sets area to GLOBAL
     internally, materializes the file, returns the FileResponse)
  5. In finally{}, restores the original area code so the next /index.php
     or /static.php request on the same worker sees the area BootstrapPool
     set up for it

The Magento\MediaStorage\App\Media constructor signature is byte-identical
between 2.4.8-p4 and 2.4.9-beta1 (verified by diffing both sources), so
this layer is dual-version compatible.

Why reflection
--------------
MagentoMedia::launch() calls State::setAreaCode(AREA_GLOBAL) unconditionally
on every invocation. In worker mode the State singleton persists across
requests and State::setAreaCode() throws "Area code is already set" on the
second call — State doesn't implement ResetAfterRequestInterface (and isn't
auto-tracked by AppObjectManager's Resetter since it's resolved via get(),
not create()), so the Resetter never clears it between requests.

State exposes no public reset API: setAreaCode throws, emulateAreaCode wraps
and restores, getAreaCode throws when unset. Reflection on the private
_areaCode field is the smallest contained workaround. The save+restore is
in a finally{} so a media-app failure can't leave the worker in a broken
area state for other endpoints.

If/when Magento adds ResetAfterRequestInterface to State (or exposes a
clear() method), the reflection here can be deleted.

composer.json
-------------
Also bumps require constraints to match the "PHP 8.4+, Magento 2.4.8+"
support matrix:

  - php: ^8.3 -> ^8.4
  - magento/framework: * -> ^103.0   (covers 2.4.8 + 2.4.9 dev)
  - magento/module-media-storage: * -> ^100.4
  - psr/log: * -> ^3.0
@acourtiol acourtiol force-pushed the feat/webapi-request-reset branch from 014ff83 to aef9323 Compare May 11, 2026 07:34
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