Make WordPress Core

Changeset 39296


Ignore:
Timestamp:
11/18/2016 07:32:03 PM (8 years ago)
Author:
joehoyle
Message:

REST API: Change “ipv4” types to “ip” to support ipv6.

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.

Location:
trunk
Files:
4 edited

Legend:

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

    r39278 r39296  
    870870
    871871/**
    872  * Determines if a IPv4 address is valid.
    873  *
    874  * Does not handle IPv6 addresses.
     872 * Determines if an IP address is valid.
     873 *
     874 * Handles both IPv4 and IPv6 addresses.
    875875 *
    876876 * @since 4.7.0
    877877 *
    878  * @param  string $ipv4 IP 32-bit address.
    879  * @return string|false The valid IPv4 address, otherwise false.
    880  */
    881 function rest_is_ip_address( $ipv4 ) {
    882     $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]?)$/';
    883 
    884     if ( ! preg_match( $pattern, $ipv4 ) ) {
     878 * @param  string $ip IP address.
     879 * @return string|false The valid IP address, otherwise false.
     880 */
     881function rest_is_ip_address( $ip ) {
     882    $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]?)$/';
     883
     884    if ( ! preg_match( $ipv4_pattern, $ip ) && ! Requests_IPv6::check_ipv6( $ip ) ) {
    885885        return false;
    886886    }
    887887
    888     return $ipv4;
     888    return $ip;
    889889}
    890890
     
    10541054                }
    10551055                break;
    1056             case 'ipv4' :
     1056            case 'ip' :
    10571057                if ( ! rest_is_ip_address( $value ) ) {
    1058                     /* translators: %s: IP address */ 
     1058                    /* translators: %s: IP address */
    10591059                    return new WP_Error( 'rest_invalid_param', sprintf( __( '%s is not a valid IP address.' ), $value ) );
    10601060                }
     
    11571157                return esc_url_raw( $value );
    11581158
    1159             case 'ipv4' :
     1159            case 'ip' :
    11601160                return sanitize_text_field( $value );
    11611161        }
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r39295 r39296  
    11161116                    'description'  => __( 'IP address for the object author.' ),
    11171117                    'type'         => 'string',
    1118                     'format'       => 'ipv4',
     1118                    'format'       => 'ip',
    11191119                    'context'      => array( 'edit' ),
    11201120                    'default'      => '127.0.0.1',
  • trunk/tests/phpunit/tests/rest-api/rest-schema-sanitization.php

    r39222 r39296  
    6464        $this->assertEquals( 'a@b.c', rest_sanitize_value_from_schema( 'a@b.c', $schema ) );
    6565        $this->assertEquals( 'invalid', rest_sanitize_value_from_schema( 'invalid', $schema ) );
     66    }
     67
     68    public function test_format_ip() {
     69        $schema = array(
     70            'type'  => 'string',
     71            'format' => 'ip',
     72        );
     73
     74        $this->assertEquals( '127.0.0.1', rest_sanitize_value_from_schema( '127.0.0.1', $schema ) );
     75        $this->assertEquals( 'hello', rest_sanitize_value_from_schema( 'hello', $schema ) );
     76        $this->assertEquals( '2001:DB8:0:0:8:800:200C:417A', rest_sanitize_value_from_schema( '2001:DB8:0:0:8:800:200C:417A', $schema ) );
    6677    }
    6778
  • trunk/tests/phpunit/tests/rest-api/rest-schema-validation.php

    r39222 r39296  
    8484    }
    8585
    86     public function test_format_ipv4() {
     86    public function test_format_ip() {
    8787        $schema = array(
    8888            'type'  => 'string',
    89             'format' => 'ipv4',
     89            'format' => 'ip',
    9090        );
     91
     92        // IPv4.
    9193        $this->assertTrue( rest_validate_value_from_schema( '127.0.0.1', $schema ) );
    9294        $this->assertWPError( rest_validate_value_from_schema( '3333.3333.3333.3333', $schema ) );
    9395        $this->assertWPError( rest_validate_value_from_schema( '1', $schema ) );
     96
     97        // IPv6.
     98        $this->assertTrue( rest_validate_value_from_schema( '::1', $schema ) ); // Loopback, compressed, non-routable.
     99        $this->assertTrue( rest_validate_value_from_schema( '::', $schema ) ); // Unspecified, compressed, non-routable.
     100        $this->assertTrue( rest_validate_value_from_schema( '0:0:0:0:0:0:0:1', $schema ) ); // Loopback, full.
     101        $this->assertTrue( rest_validate_value_from_schema( '0:0:0:0:0:0:0:0', $schema ) ); // Unspecified, full.
     102        $this->assertTrue( rest_validate_value_from_schema( '2001:DB8:0:0:8:800:200C:417A', $schema ) ); // Unicast, full.
     103        $this->assertTrue( rest_validate_value_from_schema( 'FF01:0:0:0:0:0:0:101', $schema ) ); // Multicast, full.
     104        $this->assertTrue( rest_validate_value_from_schema( '2001:DB8::8:800:200C:417A', $schema ) ); // Unicast, compressed.
     105        $this->assertTrue( rest_validate_value_from_schema( 'FF01::101', $schema ) ); // Multicast, compressed.
     106        $this->assertTrue( rest_validate_value_from_schema( 'fe80::217:f2ff:fe07:ed62', $schema ) );
     107        $this->assertWPError( rest_validate_value_from_schema( '', $schema ) ); // Empty string.
     108        $this->assertWPError( rest_validate_value_from_schema( '2001:DB8:0:0:8:800:200C:417A:221', $schema ) ); // Unicast, full.
     109        $this->assertWPError( rest_validate_value_from_schema( 'FF01::101::2', $schema ) ); // Multicast, compressed.
    94110    }
    95111
Note: See TracChangeset for help on using the changeset viewer.