Make WordPress Core


Ignore:
Timestamp:
05/06/2015 07:06:02 PM (11 years ago)
Author:
mdawaffe
Message:

WPDB: When checking that a string can be sent to MySQL, we shouldn't use mb_convert_encoding(), as it behaves differently to MySQL's character encoding conversion.

Merge of [32364] to the 4.1 branch.

Props mdawaffe, pento, nbachiyski, jorbin, johnjamesjacoby, jeremyfelt.

See #32165.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/4.1/tests/phpunit/tests/db/charset.php

    r32262 r32387  
    77 *
    88 * @group wpdb
     9 * @group security-153
    910 */
    1011class Tests_DB_Charset extends WP_UnitTestCase {
     
    2930                                'charset'  => 'latin1',
    3031                                'value'    => "\xf0\x9f\x8e\xb7",
    31                                 'expected' => "\xf0\x9f\x8e\xb7"
     32                                'expected' => "\xf0\x9f\x8e\xb7",
     33                                'length'   => array( 'type' => 'char', 'length' => 100 ),
     34                        ),
     35                        'latin1_char_length' => array(
     36                                // latin1. latin1 never changes.
     37                                'charset'  => 'latin1',
     38                                'value'    => str_repeat( 'A', 11 ),
     39                                'expected' => str_repeat( 'A', 10 ),
     40                                'length'   => array( 'type' => 'char', 'length' => 10 ),
     41                        ),
     42                        'latin1_byte_length' => array(
     43                                // latin1. latin1 never changes.
     44                                'charset'  => 'latin1',
     45                                'value'    => str_repeat( 'A', 11 ),
     46                                'expected' => str_repeat( 'A', 10 ),
     47                                'length'   => array( 'type' => 'byte', 'length' => 10 ),
    3248                        ),
    3349                        'ascii' => array(
     
    3551                                'charset'  => 'ascii',
    3652                                'value'    => 'Hello World',
    37                                 'expected' => 'Hello World'
     53                                'expected' => 'Hello World',
     54                                'length'   => array( 'type' => 'char', 'length' => 100 ),
     55                        ),
     56                        'ascii_char_length' => array(
     57                                // ascii gets special treatment, make sure it's covered
     58                                'charset'  => 'ascii',
     59                                'value'    => str_repeat( 'A', 11 ),
     60                                'expected' => str_repeat( 'A', 10 ),
     61                                'length'   => array( 'type' => 'char', 'length' => 10 ),
     62                        ),
     63                        'ascii_byte_length' => array(
     64                                // ascii gets special treatment, make sure it's covered
     65                                'charset'  => 'ascii',
     66                                'value'    => str_repeat( 'A', 11 ),
     67                                'expected' => str_repeat( 'A', 10 ),
     68                                'length'   => array( 'type' => 'byte', 'length' => 10 ),
    3869                        ),
    3970                        'utf8' => array(
     
    4172                                'charset'  => 'utf8',
    4273                                'value'    => "H€llo\xf0\x9f\x98\x88World¢",
    43                                 'expected' => 'H€lloWorld¢'
     74                                'expected' => 'H€lloWorld¢',
     75                                'length'   => array( 'type' => 'char', 'length' => 100 ),
     76                        ),
     77                        'utf8_23char_length' => array(
     78                                // utf8 only allows <= 3-byte chars
     79                                'charset'  => 'utf8',
     80                                'value'    => str_repeat( "²3", 10 ),
     81                                'expected' => str_repeat( "²3", 5 ),
     82                                'length'   => array( 'type' => 'char', 'length' => 10 ),
     83                        ),
     84                        'utf8_23byte_length' => array(
     85                                // utf8 only allows <= 3-byte chars
     86                                'charset'  => 'utf8',
     87                                'value'    => str_repeat( "²3", 10 ),
     88                                'expected' => "²3²3",
     89                                'length'   => array( 'type' => 'byte', 'length' => 10 ),
     90                        ),
     91                        'utf8_3char_length' => array(
     92                                // utf8 only allows <= 3-byte chars
     93                                'charset'  => 'utf8',
     94                                'value'    => str_repeat( "3", 11 ),
     95                                'expected' => str_repeat( "3", 10 ),
     96                                'length'   => array( 'type' => 'char', 'length' => 10 ),
     97                        ),
     98                        'utf8_3byte_length' => array(
     99                                // utf8 only allows <= 3-byte chars
     100                                'charset'  => 'utf8',
     101                                'value'    => str_repeat( "3", 11 ),
     102                                'expected' => "333",
     103                                'length'   => array( 'type' => 'byte', 'length' => 10 ),
    44104                        ),
    45105                        'utf8mb3' => array(
     
    47107                                'charset'  => 'utf8mb3',
    48108                                'value'    => "H€llo\xf0\x9f\x98\x88World¢",
    49                                 'expected' => 'H€lloWorld¢'
     109                                'expected' => 'H€lloWorld¢',
     110                                'length'   => array( 'type' => 'char', 'length' => 100 ),
     111                        ),
     112                        'utf8mb3_23char_length' => array(
     113                                // utf8mb3 should behave the same an utf8
     114                                'charset'  => 'utf8mb3',
     115                                'value'    => str_repeat( "²3", 10 ),
     116                                'expected' => str_repeat( "²3", 5 ),
     117                                'length'   => array( 'type' => 'char', 'length' => 10 ),
     118                        ),
     119                        'utf8mb3_23byte_length' => array(
     120                                // utf8mb3 should behave the same an utf8
     121                                'charset'  => 'utf8mb3',
     122                                'value'    => str_repeat( "²3", 10 ),
     123                                'expected' => "²3²3",
     124                                'length'   => array( 'type' => 'byte', 'length' => 10 ),
     125                        ),
     126                        'utf8mb3_3char_length' => array(
     127                                // utf8mb3 should behave the same an utf8
     128                                'charset'  => 'utf8mb3',
     129                                'value'    => str_repeat( "3", 11 ),
     130                                'expected' => str_repeat( "3", 10 ),
     131                                'length'   => array( 'type' => 'char', 'length' => 10 ),
     132                        ),
     133                        'utf8mb3_3byte_length' => array(
     134                                // utf8mb3 should behave the same an utf8
     135                                'charset'  => 'utf8mb3',
     136                                'value'    => str_repeat( "3", 10 ),
     137                                'expected' => "333",
     138                                'length'   => array( 'type' => 'byte', 'length' => 10 ),
    50139                        ),
    51140                        'utf8mb4' => array(
     
    53142                                'charset'  => 'utf8mb4',
    54143                                'value'    => "H€llo\xf0\x9f\x98\x88World¢",
    55                                 'expected' => "H€llo\xf0\x9f\x98\x88World¢"
     144                                'expected' => "H€llo\xf0\x9f\x98\x88World¢",
     145                                'length'   => array( 'type' => 'char', 'length' => 100 ),
     146                        ),
     147                        'utf8mb4_234char_length' => array(
     148                                // utf8mb4 allows 4-byte characters, too
     149                                'charset'  => 'utf8mb4',
     150                                'value'    => str_repeat( "²3𝟜", 10 ),
     151                                'expected' => "²3𝟜²3𝟜²3𝟜²",
     152                                'length'   => array( 'type' => 'char', 'length' => 10 ),
     153                        ),
     154                        'utf8mb4_234byte_length' => array(
     155                                // utf8mb4 allows 4-byte characters, too
     156                                'charset'  => 'utf8mb4',
     157                                'value'    => str_repeat( "²3𝟜", 10 ),
     158                                'expected' => "²3𝟜",
     159                                'length'   => array( 'type' => 'byte', 'length' => 10 ),
     160                        ),
     161                        'utf8mb4_4char_length' => array(
     162                                // utf8mb4 allows 4-byte characters, too
     163                                'charset'  => 'utf8mb4',
     164                                'value'    => str_repeat( "𝟜", 11 ),
     165                                'expected' => str_repeat( "𝟜", 10 ),
     166                                'length'   => array( 'type' => 'char', 'length' => 10 ),
     167                        ),
     168                        'utf8mb4_4byte_length' => array(
     169                                // utf8mb4 allows 4-byte characters, too
     170                                'charset'  => 'utf8mb4',
     171                                'value'    => str_repeat( "𝟜", 10 ),
     172                                'expected' => "𝟜𝟜",
     173                                'length'   => array( 'type' => 'byte', 'length' => 10 ),
    56174                        ),
    57175                        'koi8r' => array(
     
    59177                                'value'    => "\xfdord\xf2ress",
    60178                                'expected' => "\xfdord\xf2ress",
     179                                'length'   => array( 'type' => 'char', 'length' => 100 ),
     180                        ),
     181                        'koi8r_char_length' => array(
     182                                'charset'  => 'koi8r',
     183                                'value'    => str_repeat( "\xfd\xf2", 10 ),
     184                                'expected' => str_repeat( "\xfd\xf2", 5 ),
     185                                'length'   => array( 'type' => 'char', 'length' => 10 ),
     186                        ),
     187                        'koi8r_byte_length' => array(
     188                                'charset'  => 'koi8r',
     189                                'value'    => str_repeat( "\xfd\xf2", 10 ),
     190                                'expected' => str_repeat( "\xfd\xf2", 5 ),
     191                                'length'   => array( 'type' => 'byte', 'length' => 10 ),
    61192                        ),
    62193                        'hebrew' => array(
     
    64195                                'value'    => "\xf9ord\xf7ress",
    65196                                'expected' => "\xf9ord\xf7ress",
     197                                'length'   => array( 'type' => 'char', 'length' => 100 ),
     198                        ),
     199                        'hebrew_char_length' => array(
     200                                'charset'  => 'hebrew',
     201                                'value'    => str_repeat( "\xf9\xf7", 10 ),
     202                                'expected' => str_repeat( "\xf9\xf7", 5 ),
     203                                'length'   => array( 'type' => 'char', 'length' => 10 ),
     204                        ),
     205                        'hebrew_byte_length' => array(
     206                                'charset'  => 'hebrew',
     207                                'value'    => str_repeat( "\xf9\xf7", 10 ),
     208                                'expected' => str_repeat( "\xf9\xf7", 5 ),
     209                                'length'   => array( 'type' => 'byte', 'length' => 10 ),
    66210                        ),
    67211                        'cp1251' => array(
     
    69213                                'value'    => "\xd8ord\xd0ress",
    70214                                'expected' => "\xd8ord\xd0ress",
     215                                'length'   => array( 'type' => 'char', 'length' => 100 ),
     216                        ),
     217                        'cp1251_char_length' => array(
     218                                'charset'  => 'cp1251',
     219                                'value'    => str_repeat( "\xd8\xd0", 10 ),
     220                                'expected' => str_repeat( "\xd8\xd0", 5 ),
     221                                'length'   => array( 'type' => 'char', 'length' => 10 ),
     222                        ),
     223                        'cp1251_byte_length' => array(
     224                                'charset'  => 'cp1251',
     225                                'value'    => str_repeat( "\xd8\xd0", 10 ),
     226                                'expected' => str_repeat( "\xd8\xd0", 5 ),
     227                                'length'   => array( 'type' => 'byte', 'length' => 10 ),
    71228                        ),
    72229                        'tis620' => array(
     
    74231                                'value'    => "\xccord\xe3ress",
    75232                                'expected' => "\xccord\xe3ress",
     233                                'length'   => array( 'type' => 'char', 'length' => 100 ),
     234                        ),
     235                        'tis620_char_length' => array(
     236                                'charset'  => 'tis620',
     237                                'value'    => str_repeat( "\xcc\xe3", 10 ),
     238                                'expected' => str_repeat( "\xcc\xe3", 5 ),
     239                                'length'   => array( 'type' => 'char', 'length' => 10 ),
     240                        ),
     241                        'tis620_byte_length' => array(
     242                                'charset'  => 'tis620',
     243                                'value'    => str_repeat( "\xcc\xe3", 10 ),
     244                                'expected' => str_repeat( "\xcc\xe3", 5 ),
     245                                'length'   => array( 'type' => 'byte', 'length' => 10 ),
    76246                        ),
    77247                        'false' => array(
     
    79249                                'charset'  => false,
    80250                                'value'    => 100,
    81                                 'expected' => 100
     251                                'expected' => 100,
     252                                'length'   => false,
    82253                        ),
    83254                );
     
    95266                                'charset'  => 'big5',
    96267                                'value'    => $big5,
    97                                 'expected' => $big5
     268                                'expected' => $big5,
     269                                'length'   => array( 'type' => 'char', 'length' => 100 ),
     270                        );
     271
     272                        $fields['big5_char_length'] = array(
     273                                'charset'  => 'big5',
     274                                'value'    => str_repeat( $big5, 10 ),
     275                                'expected' => str_repeat( $big5, 3 ) . 'a',
     276                                'length'   => array( 'type' => 'char', 'length' => 10 ),
     277                        );
     278
     279                        $fields['big5_byte_length'] = array(
     280                                'charset'  => 'big5',
     281                                'value'    => str_repeat( $big5, 10 ),
     282                                'expected' => str_repeat( $big5, 2 ) . 'a',
     283                                'length'   => array( 'type' => 'byte', 'length' => 10 ),
    98284                        );
    99285                }
     
    167353
    168354                $all_ascii_fields = array(
    169                         'post_content' => array( 'value' => 'foo foo foo!', 'format' => '%s', 'charset' => false ),
    170                         'post_excerpt' => array( 'value' => 'bar bar bar!', 'format' => '%s', 'charset' => false ),
     355                        'post_content' => array( 'value' => 'foo foo foo!', 'format' => '%s', 'charset' => $charset ),
     356                        'post_excerpt' => array( 'value' => 'bar bar bar!', 'format' => '%s', 'charset' => $charset ),
    171357                );
    172358
    173359                // This is the same data used in process_field_charsets_for_nonexistent_table()
    174360                $non_ascii_string_fields = array(
    175                         'post_content' => array( 'value' => '¡foo foo foo!', 'format' => '%s', 'charset' => $charset, 'ascii' => false ),
    176                         'post_excerpt' => array( 'value' => '¡bar bar bar!', 'format' => '%s', 'charset' => $charset, 'ascii' => false ),
     361                        'post_content' => array( 'value' => '¡foo foo foo!', 'format' => '%s', 'charset' => $charset ),
     362                        'post_excerpt' => array( 'value' => '¡bar bar bar!', 'format' => '%s', 'charset' => $charset ),
    177363                );
    178364
     
    541727                self::$_wpdb->query( $drop );
    542728        }
     729
     730        function test_strip_invalid_test_for_column_bails_if_ascii_input_too_long() {
     731                global $wpdb;
     732
     733                // TEXT column
     734                $stripped = $wpdb->strip_invalid_text_for_column( $wpdb->comments, 'comment_content', str_repeat( 'A', 65536 ) );
     735                $this->assertEquals( 65535, strlen( $stripped ) );
     736
     737                // VARCHAR column
     738                $stripped = $wpdb->strip_invalid_text_for_column( $wpdb->comments, 'comment_agent', str_repeat( 'A', 256 ) );
     739                $this->assertEquals( 255, strlen( $stripped ) );
     740        }
    543741}
Note: See TracChangeset for help on using the changeset viewer.