Make WordPress Core


Ignore:
Timestamp:
11/09/2021 10:37:19 PM (5 years ago)
Author:
hellofromTonya
Message:

HTTP API: Introduce 'http_allowed_safe_ports' filter in wp_http_validate_url().

Adds a new filter 'http_allowed_safe_ports' to control which ports are allowed for remote requests. By default, ports 80, 443, and 8080 are allowed for safe remote requests.

Adds tests.

Follow-up to [24480].

Props xknown, johnbillion, jorbin, costdev, dd32.
Fixes #54331.

File:
1 edited

Legend:

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

    r49108 r52084  
    515515 */
    516516function wp_http_validate_url( $url ) {
     517        if ( ! is_string( $url ) || '' === $url || is_numeric( $url ) ) {
     518                return false;
     519        }
     520
    517521        $original_url = $url;
    518522        $url          = wp_kses_bad_protocol( $url, array( 'http', 'https' ) );
    … …  
    535539
    536540        $parsed_home = parse_url( get_option( 'home' ) );
    537 
    538         if ( isset( $parsed_home['host'] ) ) {
    539                 $same_host = strtolower( $parsed_home['host'] ) === strtolower( $parsed_url['host'] );
    540         } else {
    541                 $same_host = false;
    542         }
     541        $same_host   = isset( $parsed_home['host'] ) && strtolower( $parsed_home['host'] ) === strtolower( $parsed_url['host'] );
     542        $host        = trim( $parsed_url['host'], '.' );
    543543
    544544        if ( ! $same_host ) {
    545                 $host = trim( $parsed_url['host'], '.' );
    546545                if ( preg_match( '#^(([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)\.){3}([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)$#', $host ) ) {
    547546                        $ip = $host;
    … …  
    582581
    583582        $port = $parsed_url['port'];
    584         if ( 80 === $port || 443 === $port || 8080 === $port ) {
     583
     584        /**
     585         * Controls the list of ports considered safe in HTTP API.
     586         *
     587         * Allows to change and allow external requests for the HTTP request.
     588         *
     589         * @since 5.9.0
     590         *
     591         * @param array  $allowed_ports Array of integers for valid ports.
     592         * @param string $host          Host name of the requested URL.
     593         * @param string $url           Requested URL.
     594         */
     595        $allowed_ports = apply_filters( 'http_allowed_safe_ports', array( 80, 443, 8080 ), $host, $url );
     596        if ( in_array( $port, $allowed_ports, true ) ) {
    585597                return $url;
    586598        }
Note: See TracChangeset for help on using the changeset viewer.