Make WordPress Core

Changeset 36451 for branches/4.0


Ignore:
Timestamp:
02/02/2016 04:59:23 PM (10 years ago)
Author:
ocean90
Message:

Better validation of the URL used in HTTP redirects.

Merges [36444] to the 4.0 branch.

Location:
branches/4.0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.0/src/wp-includes/pluggable.php

    r30413 r36451  
    12561256    $test = ( $cut = strpos($location, '?') ) ? substr( $location, 0, $cut ) : $location;
    12571257
    1258     $lp  = parse_url($test);
     1258    // @-operator is used to prevent possible warnings in PHP < 5.3.3.
     1259    $lp = @parse_url($test);
    12591260
    12601261    // Give up if malformed URL
     
    12661267        return $default;
    12671268
    1268     // Reject if scheme is set but host is not. This catches urls like https:host.com for which parse_url does not set the host field.
    1269     if ( isset($lp['scheme'])  && !isset($lp['host']) )
     1269    // Reject if certain components are set but host is not. This catches urls like https:host.com for which parse_url does not set the host field.
     1270    if ( ! isset( $lp['host'] ) && ( isset( $lp['scheme'] ) || isset( $lp['user'] ) || isset( $lp['pass'] ) || isset( $lp['port'] ) ) ) {
    12701271        return $default;
     1272    }
     1273
     1274    // Reject malformed components parse_url() can return on odd inputs.
     1275    foreach ( array( 'user', 'pass', 'host' ) as $component ) {
     1276        if ( isset( $lp[ $component ] ) && strpbrk( $lp[ $component ], ':/?#@' ) ) {
     1277            return $default;
     1278        }
     1279    }
    12711280
    12721281    $wpp = parse_url(home_url());
  • branches/4.0/tests/phpunit/tests/formatting/redirect.php

    r28939 r36451  
    44 * @group pluggable
    55 * @group formatting
     6 * @group redirect
    67 */
    78class Tests_Formatting_Redirect extends WP_UnitTestCase {
     9    function setUp() {
     10        add_filter( 'home_url', array( $this, 'home_url' ) );
     11    }
     12
     13    function tearDown() {
     14        remove_filter( 'home_url', array( $this, 'home_url' ) );
     15    }
     16
     17    function home_url() {
     18        return 'http://example.com/';
     19    }
     20
    821    function test_wp_sanitize_redirect() {
    922        $this->assertEquals('http://example.com/watchthelinefeedgo', wp_sanitize_redirect('http://example.com/watchthelinefeed%0Ago'));
     
    1629        $this->assertEquals('http://example.com/watchthecarriagereturngo', wp_sanitize_redirect('http://example.com/watchthecarriagereturn%0%0DDgo'));
    1730    }
     31
     32    /**
     33     * @dataProvider valid_url_provider
     34     */
     35    function test_wp_validate_redirect_valid_url( $url, $expected ) {
     36        $this->assertEquals( $expected, wp_validate_redirect( $url ) );
     37    }
     38
     39    /**
     40     * @dataProvider invalid_url_provider
     41     */
     42    function test_wp_validate_redirect_invalid_url( $url ) {
     43        $this->assertEquals( false, wp_validate_redirect( $url, false ) );
     44    }
     45
     46    function valid_url_provider() {
     47        return array(
     48            array( 'http://example.com', 'http://example.com' ),
     49            array( 'http://example.com/', 'http://example.com/' ),
     50            array( 'https://example.com/', 'https://example.com/' ),
     51            array( '//example.com', 'http://example.com' ),
     52            array( '//example.com/', 'http://example.com/' ),
     53            array( 'http://example.com/?foo=http://example.com/', 'http://example.com/?foo=http://example.com/' ),
     54            array( 'http://user@example.com/', 'http://user@example.com/' ),
     55            array( 'http://user:@example.com/', 'http://user:@example.com/' ),
     56            array( 'http://user:pass@example.com/', 'http://user:pass@example.com/' ),
     57        );
     58    }
     59
     60    function invalid_url_provider() {
     61        return array(
     62            // parse_url() fails
     63            array( '' ),
     64            array( 'http://:' ),
     65
     66            // non-safelisted domain
     67            array( 'http://non-safelisted.example/' ),
     68
     69            // unsupported schemes
     70            array( 'data:text/plain;charset=utf-8,Hello%20World!' ),
     71            array( 'file:///etc/passwd' ),
     72            array( 'ftp://example.com/' ),
     73
     74            // malformed input
     75            array( 'http:example.com' ),
     76            array( 'http:80' ),
     77            array( 'http://example.com:1234:5678/' ),
     78            array( 'http://user:pa:ss@example.com/' ),
     79
     80            array( 'http://user@@example.com' ),
     81            array( 'http://user@:example.com' ),
     82            array( 'http://user?@example.com' ),
     83            array( 'http://user@?example.com' ),
     84            array( 'http://user#@example.com' ),
     85            array( 'http://user@#example.com' ),
     86
     87            array( 'http://user@@example.com/' ),
     88            array( 'http://user@:example.com/' ),
     89            array( 'http://user?@example.com/' ),
     90            array( 'http://user@?example.com/' ),
     91            array( 'http://user#@example.com/' ),
     92            array( 'http://user@#example.com/' ),
     93
     94            array( 'http://user:pass@@example.com' ),
     95            array( 'http://user:pass@:example.com' ),
     96            array( 'http://user:pass?@example.com' ),
     97            array( 'http://user:pass@?example.com' ),
     98            array( 'http://user:pass#@example.com' ),
     99            array( 'http://user:pass@#example.com' ),
     100
     101            array( 'http://user:pass@@example.com/' ),
     102            array( 'http://user:pass@:example.com/' ),
     103            array( 'http://user:pass?@example.com/' ),
     104            array( 'http://user:pass@?example.com/' ),
     105            array( 'http://user:pass#@example.com/' ),
     106            array( 'http://user:pass@#example.com/' ),
     107
     108            array( 'http://user.pass@@example.com' ),
     109            array( 'http://user.pass@:example.com' ),
     110            array( 'http://user.pass?@example.com' ),
     111            array( 'http://user.pass@?example.com' ),
     112            array( 'http://user.pass#@example.com' ),
     113            array( 'http://user.pass@#example.com' ),
     114
     115            array( 'http://user.pass@@example.com/' ),
     116            array( 'http://user.pass@:example.com/' ),
     117            array( 'http://user.pass?@example.com/' ),
     118            array( 'http://user.pass@?example.com/' ),
     119            array( 'http://user.pass#@example.com/' ),
     120            array( 'http://user.pass@#example.com/' ),
     121        );
     122    }
    18123}
Note: See TracChangeset for help on using the changeset viewer.