Make WordPress Core

Changeset 58022


Ignore:
Timestamp:
04/18/2024 03:15:50 AM (10 months ago)
Author:
peterwilsoncc
Message:

Upgrade/Install: Validate source & destination values in WP_Ugrader.

Adds a missing string and some additional validation of paths in the upgrader class.

Follow up to [56992].

Props costdev, jipmoors, karlijnbok, swissspidy, afragen, mukesh27.
Fixes #59712.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-upgrader.php

    r56550 r58022  
    197197        $this->strings['fs_no_folder'] = __( 'Unable to locate needed folder (%s).' );
    198198
     199        $this->strings['no_package']           = __( 'Package not available.' );
    199200        $this->strings['download_failed']      = __( 'Download failed.' );
    200201        $this->strings['installing_package']   = __( 'Installing the latest version…' );
     
    528529        }
    529530
    530         if ( empty( $source ) || empty( $destination ) ) {
     531        if (
     532            ( ! is_string( $source ) || '' === $source || trim( $source ) !== $source ) ||
     533            ( ! is_string( $destination ) || '' === $destination || trim( $destination ) !== $destination )
     534        ) {
    531535            return new WP_Error( 'bad_request', $this->strings['bad_request'] );
    532536        }
  • trunk/tests/phpunit/tests/admin/wpUpgrader.php

    r56992 r58022  
    138138                'fs_no_themes_dir',
    139139                'fs_no_folder',
     140                'no_package',
    140141                'download_failed',
    141142                'installing_package',
     
    778779    public function data_install_package_invalid_paths() {
    779780        return array(
    780             'empty string' => array( 'path' => '' ),
     781            'empty string'                   => array( 'path' => '' ),
    781782
    782783            // Type checks.
    783             'empty array'  => array( 'path' => array() ),
    784             '(int) 0'      => array( 'path' => 0 ),
    785             '(int) -0'     => array( 'path' => -0 ),
    786             '(float) 0.0'  => array( 'path' => 0.0 ),
    787             '(float) -0.0' => array( 'path' => -0.0 ),
    788             '(bool) false' => array( 'path' => false ),
    789             'null'         => array( 'path' => null ),
     784            'empty array'                    => array( 'path' => array() ),
     785            'populated array'                => array( 'path' => array( '/' ) ),
     786            '(int) 0'                        => array( 'path' => 0 ),
     787            '(int) -0'                       => array( 'path' => -0 ),
     788            '(int) -1'                       => array( 'path' => -1 ),
     789            '(int) 1'                        => array( 'path' => 1 ),
     790            '(float) 0.0'                    => array( 'path' => 0.0 ),
     791            '(float) -0.0'                   => array( 'path' => -0.0 ),
     792            '(float) 1.0'                    => array( 'path' => 1.0 ),
     793            '(float) -1.0'                   => array( 'path' => -1.0 ),
     794            '(bool) false'                   => array( 'path' => false ),
     795            '(bool) true'                    => array( 'path' => true ),
     796            'null'                           => array( 'path' => null ),
     797            'empty object'                   => array( 'path' => new stdClass() ),
     798            'populated object'               => array( 'path' => (object) array( '/' ) ),
     799
     800            // Ensures that `trim()` is run triggering an empty array.
     801            'a string with spaces'           => array( 'path' => '   ' ),
     802            'a string with tabs'             => array( 'path' => "\t\t" ),
     803            'a string with new lines'        => array( 'path' => "\n\n" ),
     804            'a string with carriage returns' => array( 'path' => "\r\r" ),
     805
     806            // Ensure that strings with leading/trailing whitespace are invalid.
     807            'a path with a leading space'    => array( 'path' => ' /path' ),
     808            'a path with a trailing space'   => array( 'path' => '/path ' ),
     809            'a path with a leading tab'      => array( 'path' => "\t/path" ),
     810            'a path with a trailing tab'     => array( 'path' => "/path\t" ),
    790811        );
    791812    }
     
    15581579
    15591580    /**
     1581     * Tests that `WP_Upgrader::download_package()` returns a WP_Error object
     1582     * for an empty package.
     1583     *
     1584     * @ticket 59712
     1585     *
     1586     * @covers WP_Upgrader::download_package
     1587     */
     1588    public function test_download_package_should_return_a_wp_error_object_for_an_empty_package() {
     1589        self::$instance->init();
     1590
     1591        $result = self::$instance->download_package( '' );
     1592
     1593        $this->assertWPError(
     1594            $result,
     1595            'WP_Upgrader::download_package() did not return a WP_Error object'
     1596        );
     1597
     1598        $this->assertSame(
     1599            'no_package',
     1600            $result->get_error_code(),
     1601            'Unexpected WP_Error code'
     1602        );
     1603    }
     1604
     1605    /**
    15601606     * Tests that `WP_Upgrader::download_package()` returns a file with the
    15611607     * package name in it.
Note: See TracChangeset for help on using the changeset viewer.