From a38b863ae4ead265ad7a479d1d46313dadb70e60 Mon Sep 17 00:00:00 2001 From: Joe Hoyle Date: Fri, 18 Nov 2016 19:33:31 +0000 Subject: [PATCH] =?UTF-8?q?REST=20API:=20Change=20=E2=80=9Cipv4=E2=80=9D?= =?UTF-8?q?=20types=20to=20=E2=80=9Cip=E2=80=9D=20to=20support=20ipv6.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Stop presuming IP address are IPv4, instead make the type “ip” to be agnostic of IP version. This fixes requests with ipv6 addresses for comments in core. Props dd32, schlessera, danielbachhuber. Fixes #38818. Built from https://develop.svn.wordpress.org/trunk@39296 git-svn-id: http://core.svn.wordpress.org/trunk@39236 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/rest-api.php | 22 +++++++++---------- .../class-wp-rest-comments-controller.php | 2 +- wp-includes/version.php | 2 +- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/wp-includes/rest-api.php b/wp-includes/rest-api.php index 741e76837f..ffc691916f 100644 --- a/wp-includes/rest-api.php +++ b/wp-includes/rest-api.php @@ -869,23 +869,23 @@ function rest_parse_request_arg( $value, $request, $param ) { } /** - * Determines if a IPv4 address is valid. + * Determines if an IP address is valid. * - * Does not handle IPv6 addresses. + * Handles both IPv4 and IPv6 addresses. * * @since 4.7.0 * - * @param string $ipv4 IP 32-bit address. - * @return string|false The valid IPv4 address, otherwise false. + * @param string $ip IP address. + * @return string|false The valid IP address, otherwise false. */ -function rest_is_ip_address( $ipv4 ) { - $pattern = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/'; +function rest_is_ip_address( $ip ) { + $ipv4_pattern = '/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/'; - if ( ! preg_match( $pattern, $ipv4 ) ) { + if ( ! preg_match( $ipv4_pattern, $ip ) && ! Requests_IPv6::check_ipv6( $ip ) ) { return false; } - return $ipv4; + return $ip; } /** @@ -1053,9 +1053,9 @@ function rest_validate_value_from_schema( $value, $args, $param = '' ) { return new WP_Error( 'rest_invalid_email', __( 'Invalid email address.' ) ); } break; - case 'ipv4' : + case 'ip' : if ( ! rest_is_ip_address( $value ) ) { - /* translators: %s: IP address */ + /* translators: %s: IP address */ return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not a valid IP address.' ), $value ) ); } break; @@ -1156,7 +1156,7 @@ function rest_sanitize_value_from_schema( $value, $args ) { case 'uri' : return esc_url_raw( $value ); - case 'ipv4' : + case 'ip' : return sanitize_text_field( $value ); } } diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php index bfa3159bf1..09223c06b9 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php @@ -1115,7 +1115,7 @@ class WP_REST_Comments_Controller extends WP_REST_Controller { 'author_ip' => array( 'description' => __( 'IP address for the object author.' ), 'type' => 'string', - 'format' => 'ipv4', + 'format' => 'ip', 'context' => array( 'edit' ), 'default' => '127.0.0.1', ), diff --git a/wp-includes/version.php b/wp-includes/version.php index ec63ca0885..ea82272c88 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.7-beta4-39295'; +$wp_version = '4.7-beta4-39296'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.