From 151b24ea07acda8f02af8ada7dec9612fd81329d Mon Sep 17 00:00:00 2001 From: Geoff Taylor Date: Wed, 2 Oct 2019 11:05:43 -0400 Subject: [PATCH] escape hatch finalized --- includes/class-woocommerce-filters.php | 31 +++++++++++++++++++++----- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/includes/class-woocommerce-filters.php b/includes/class-woocommerce-filters.php index 0ba17365..f9e95c42 100644 --- a/includes/class-woocommerce-filters.php +++ b/includes/class-woocommerce-filters.php @@ -26,14 +26,13 @@ class WooCommerce_Filters { */ public static function add_filters() { // Setup QL session handler. - $graphql_endpoint = '/' . apply_filters( 'graphql_endpoint', 'graphql' ); - if ( isset( $_SERVER['REQUEST_URI'] ) && $graphql_endpoint !== $_SERVER['REQUEST_URI'] ) { - return; - } if ( ! defined( 'NO_QL_SESSION_HANDLER' ) ) { + // Check if request is a GraphQL POST request. self::$session_header = apply_filters( 'woocommerce_graphql_session_header_name', 'woocommerce-session' ); - add_filter( 'woocommerce_cookie', array( __CLASS__, 'woocommerce_cookie' ) ); - add_filter( 'woocommerce_session_handler', array( __CLASS__, 'init_ql_session_handler' ) ); + if ( self::is_graphql_post_request() ) { + add_filter( 'woocommerce_cookie', array( __CLASS__, 'woocommerce_cookie' ) ); + add_filter( 'woocommerce_session_handler', array( __CLASS__, 'init_ql_session_handler' ) ); + } add_filter( 'graphql_response_headers_to_send', array( __CLASS__, 'add_session_header_to_expose_headers' ) ); add_filter( 'graphql_access_control_allow_headers', array( __CLASS__, 'add_session_header_to_allow_headers' ) ); } @@ -89,4 +88,24 @@ class WooCommerce_Filters { $allowed_headers[] = self::$session_header; return $allowed_headers; } + + /** + * Confirm that the current uri is the GraphQL endpoint. + * + * @return bool + */ + private static function is_graphql_post_request() { + if ( isset( $_SERVER['REQUEST_URI'] ) ) { + $haystack = wp_unslash( $_SERVER['REQUEST_URI'] ); + $needle = apply_filters( 'graphql_endpoint', 'graphql' ); + $length = strlen( $needle ); + if ( 0 === $length ) { + return true; + } + + return ( substr( $haystack, -$length ) === $needle ); + } + + return false; + } }