Skip to content

Commit 6b3357d

Browse files
fix: enhance wc request validation
1 parent ee671ed commit 6b3357d

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

classes/Visualizer/Source/Json.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,16 +507,36 @@ public function refresh( $series ) {
507507
* @return bool True if it's a WooCommerce request, false otherwise.
508508
*/
509509
private function is_woocommerce_request( $url ) {
510-
// Check if the URL contains WooCommerce API patterns.
510+
if ( empty( $url ) || ! is_string( $url ) ) {
511+
return false;
512+
}
513+
514+
$parsed_url = function_exists( 'wp_parse_url' ) ? wp_parse_url( $url ) : parse_url( $url );
515+
if ( empty( $parsed_url ) || empty( $parsed_url['host'] ) || empty( $parsed_url['path'] ) ) {
516+
return false;
517+
}
518+
519+
$site_url = function_exists( 'home_url' ) ? home_url() : ( function_exists( 'site_url' ) ? site_url() : '' );
520+
$site_parts = $site_url ? ( function_exists( 'wp_parse_url' ) ? wp_parse_url( $site_url ) : parse_url( $site_url ) ) : array();
521+
if ( empty( $site_parts['host'] ) ) {
522+
return false;
523+
}
524+
525+
$target_host = strtolower( $parsed_url['host'] );
526+
$site_host = strtolower( $site_parts['host'] );
527+
if ( $target_host !== $site_host ) {
528+
return false;
529+
}
530+
531+
$path = '/' . ltrim( $parsed_url['path'], '/' );
511532
$wc_patterns = array(
512533
'/wp-json/wc/',
534+
'/wp-json/wc-analytics/',
513535
'/wc-analytics/',
514-
'/wc/v',
515-
'/reports/',
516536
);
517537

518538
foreach ( $wc_patterns as $pattern ) {
519-
if ( strpos( $url, $pattern ) !== false ) {
539+
if ( strpos( $path, $pattern ) !== false ) {
520540
return true;
521541
}
522542
}

0 commit comments

Comments
 (0)