Make WordPress Core


Ignore:
Timestamp:
11/04/2025 06:29:36 PM (6 months ago)
Author:
johnjamesjacoby
Message:

Canonical: prevent "Undefined array key" PHP warnings when host is not set.

This change is necessary to prevent scanning tools from polluting debug/error logs of some hosting configurations with PHP warnings simply by omitting the Host header from their requests.

This commit makes sure that all of the required host, path, query, and scheme array keys inside of the redirect_canonical() function are always set after various operations have been performed on them.

It also includes 1 new test case and 2 additional tests, to verify the problem and its fix are working as intended, as well as a small modification to the get_canonical() phpunit helper specifically to account for HTTP_HOST maybe not being set.

Props artz91, johnjamesjacoby, mindctrl, sirlouen.

Fixes #63316.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/canonical.php

    r60496 r61136  
    7878    }
    7979
     80    // Notice fixing.
     81    $original += array(
     82        'host'   => '',
     83        'path'   => '',
     84        'query'  => '',
     85        'scheme' => '',
     86    );
     87
    8088    $redirect     = $original;
    8189    $redirect_url = false;
    8290    $redirect_obj = false;
    83 
    84     // Notice fixing.
    85     if ( ! isset( $redirect['path'] ) ) {
    86         $redirect['path'] = '';
    87     }
    88     if ( ! isset( $redirect['query'] ) ) {
    89         $redirect['query'] = '';
    90     }
    9191
    9292    /*
     
    617617
    618618    // Notice prevention after new parse_url( $redirect_url ) calls
    619     if ( ! isset( $redirect['path'] ) ) {
    620         $redirect['path'] = '';
    621     }
    622     if ( ! isset( $redirect['query'] ) ) {
    623         $redirect['query'] = '';
    624     }
     619    $redirect += array(
     620        'host'   => '',
     621        'path'   => '',
     622        'query'  => '',
     623        'scheme' => '',
     624    );
    625625
    626626    // Trailing /index.php.
Note: See TracChangeset for help on using the changeset viewer.