Make WordPress Core

Changeset 36444


Ignore:
Timestamp:
02/02/2016 03:10:09 PM (11 years ago)
Author:
ocean90
Message:

Better validation of the URL used in HTTP redirects.

Location:
trunk
Files:
2 edited

Legend:

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

    r36314 r36444  
    13371337                return $default;
    13381338
    1339         // 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.
    1340         if ( isset($lp['scheme'])  && !isset($lp['host']) )
     1339        // 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.
     1340        if ( ! isset( $lp['host'] ) && ( isset( $lp['scheme'] ) || isset( $lp['user'] ) || isset( $lp['pass'] ) || isset( $lp['port'] ) ) ) {
    13411341                return $default;
     1342        }
     1343
     1344        // Reject malformed components parse_url() can return on odd inputs.
     1345        foreach ( array( 'user', 'pass', 'host' ) as $component ) {
     1346                if ( isset( $lp[ $component ] ) && strpbrk( $lp[ $component ], ':/?#@' ) ) {
     1347                        return $default;
     1348                }
     1349        }
    13421350
    13431351        $wpp = parse_url(home_url());
  • trunk/tests/phpunit/tests/formatting/redirect.php

    r33707 r36444  
    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'));
     
    2134                $this->assertEquals('http://example.com/@username', wp_sanitize_redirect('http://example.com/@username'));
    2235        }
     36
     37        /**
     38         * @dataProvider valid_url_provider
     39         */
     40        function test_wp_validate_redirect_valid_url( $url, $expected ) {
     41                $this->assertEquals( $expected, wp_validate_redirect( $url ) );
     42        }
     43
     44        /**
     45         * @dataProvider invalid_url_provider
     46         */
     47        function test_wp_validate_redirect_invalid_url( $url ) {
     48                $this->assertEquals( false, wp_validate_redirect( $url, false ) );
     49        }
     50
     51        function valid_url_provider() {
     52                return array(
     53                        array( 'http://example.com', 'http://example.com' ),
     54                        array( 'http://example.com/', 'http://example.com/' ),
     55                        array( 'https://example.com/', 'https://example.com/' ),
     56                        array( '//example.com', 'http://example.com' ),
     57                        array( '//example.com/', 'http://example.com/' ),
     58                        array( 'http://example.com/?foo=http://example.com/', 'http://example.com/?foo=http://example.com/' ),
     59                        array( 'http://user@example.com/', 'http://user@example.com/' ),
     60                        array( 'http://user:@example.com/', 'http://user:@example.com/' ),
     61                        array( 'http://user:pass@example.com/', 'http://user:pass@example.com/' ),
     62                );
     63        }
     64
     65        function invalid_url_provider() {
     66                return array(
     67                        // parse_url() fails
     68                        array( '' ),
     69                        array( 'http://:' ),
     70
     71                        // non-safelisted domain
     72                        array( 'http://non-safelisted.example/' ),
     73
     74                        // unsupported schemes
     75                        array( 'data:text/plain;charset=utf-8,Hello%20World!' ),
     76                        array( 'file:///etc/passwd' ),
     77                        array( 'ftp://example.com/' ),
     78
     79                        // malformed input
     80                        array( 'http:example.com' ),
     81                        array( 'http:80' ),
     82                        array( 'http://example.com:1234:5678/' ),
     83                        array( 'http://user:pa:ss@example.com/' ),
     84
     85                        array( 'http://user@@example.com' ),
     86                        array( 'http://user@:example.com' ),
     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
     92                        array( 'http://user@@example.com/' ),
     93                        array( 'http://user@:example.com/' ),
     94                        array( 'http://user?@example.com/' ),
     95                        array( 'http://user@?example.com/' ),
     96                        array( 'http://user#@example.com/' ),
     97                        array( 'http://user@#example.com/' ),
     98
     99                        array( 'http://user:pass@@example.com' ),
     100                        array( 'http://user:pass@:example.com' ),
     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
     106                        array( 'http://user:pass@@example.com/' ),
     107                        array( 'http://user:pass@:example.com/' ),
     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
     113                        array( 'http://user.pass@@example.com' ),
     114                        array( 'http://user.pass@:example.com' ),
     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
     120                        array( 'http://user.pass@@example.com/' ),
     121                        array( 'http://user.pass@:example.com/' ),
     122                        array( 'http://user.pass?@example.com/' ),
     123                        array( 'http://user.pass@?example.com/' ),
     124                        array( 'http://user.pass#@example.com/' ),
     125                        array( 'http://user.pass@#example.com/' ),
     126                );
     127        }
    23128}
Note: See TracChangeset for help on using the changeset viewer.