Files
Geoff TaylorandGitHub 3ca56162bb fix: prevent WooCommerce session creation on CORS preflight OPTIONS requests (#992)
Move session initialization from graphql_process_http_request hook to
init_graphql_request hook with an is_graphql_http_request() guard.

graphql_process_http_request fires before WPGraphQL's OPTIONS check in
Router::process_http_request(), causing wc_load_cart() to create a new
session for every preflight request. init_graphql_request fires inside
Request constructor which is only instantiated after the OPTIONS exit.

The is_graphql_http_request() guard ensures initialize_session_and_cart()
only runs for actual HTTP requests, not programmatic graphql() calls in
tests or internal usage.
2026-03-24 03:06:02 -04:00

27 lines
799 B
PHP

<?php
/**
* Tests that CORS preflight OPTIONS requests to the GraphQL endpoint
* do not create WooCommerce sessions.
*
* @see https://github.com/wp-graphql/wp-graphql-woocommerce/issues/908
*/
class OptionsRequestCest {
public function _before( FunctionalTester $I ) {
if ( ! defined( 'GRAPHQL_WOOCOMMERCE_SECRET_KEY' ) ) {
define( 'GRAPHQL_WOOCOMMERCE_SECRET_KEY', 'testestestestestestestestestest!!' );
}
}
/**
* Test that an OPTIONS request to /graphql does not return a woocommerce-session header.
*/
public function testOptionsRequestDoesNotCreateSession( FunctionalTester $I ) {
$I->sendOptions( '/graphql' );
$I->seeResponseCodeIs( 200 );
// OPTIONS response should NOT contain a woocommerce-session header.
$I->dontSeeHttpHeader( 'woocommerce-session' );
}
}