Description
Although REST API should be stateless for anonymous calls, PHP session is always created. This is caused by the fact that session_start() is called implicitly from '\Magento\Framework\Session\SessionManager' constructor.
There are 2 issues with this: Spammed PHP session which will never be used, if remote address validation is enabled for sessions, clients with dynamic IP address will get 302 redirect instead of REST API result, and this is undesirable.
Preconditions
- Magento 2.4-develop
Steps to reproduce
- Make a REST request, for example from Swagger UI, but it can be a request from any client. For example, call /V1/directory/countries
Expected result
- Received JSON response with countries;
- No PHP session is started;
- No PHPSESSID in the response cookies.
Actual result
- PHP session is started and is perhaps never used (because the request is anonymous)
Why?
The reason for this is that in di.xml \Magento\Authorization\Model\CompositeUserContext
is fed with userContexts
argument, and at least 2 of them will start PHP session: customerSessionUserContext
and adminSessionUserContext
.
How to fix
My PoC solution was to modify vendor/magento/module-customer/etc/webapi_rest/di.xml
and vendor/magento/module-user/etc/webapi_rest/di.xml
so that types for userContext
would be Proxies, and they would be created on-demand.
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Authorization">
<plugin name="customerAuthorization" type="Magento\Customer\Model\Plugin\CustomerAuthorization" />
</type>
<type name="Magento\Authorization\Model\CompositeUserContext">
<arguments>
<argument name="userContexts" xsi:type="array">
<item name="customerSessionUserContext" xsi:type="array">
<!-- *********************** LET IT BE A PROXY ************************** -->
<item name="type" xsi:type="object">Magento\Customer\Model\Authorization\CustomerSessionUserContext\Proxy</item>
<item name="sortOrder" xsi:type="string">20</item>
</item>
<item name="adminSessionUserContext" xsi:type="array">
<!-- *********************** LET IT BE A PROXY ************************** -->
<item name="type" xsi:type="object">Magento\User\Model\Authorization\AdminSessionUserContext\Proxy</item>
<item name="sortOrder" xsi:type="string">30</item>
</item>
</argument>
</arguments>
</type>
</config>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Authorization\Model\CompositeUserContext">
<arguments>
<argument name="userContexts" xsi:type="array">
<item name="adminSessionUserContext" xsi:type="array">
<!-- *********************** LET IT BE A PROXY ************************** -->
<item name="type" xsi:type="object">Magento\User\Model\Authorization\AdminSessionUserContext\Proxy</item>
<item name="sortOrder" xsi:type="string">30</item>
</item>
</argument>
</arguments>
</type>
</config>
I do not know which one of the changes fixed the issue, but I achieved the expected result.