Changeset 53584
- Timestamp:
- 06/28/2022 11:25:21 AM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/wp-db.php
r53575 r53584 646 646 647 647 /** 648 * Backward scompatibility, where wpdb::prepare() has not quoted formatted/argnum placeholders.648 * Backward compatibility, where wpdb::prepare() has not quoted formatted/argnum placeholders. 649 649 * 650 650 * Historically this could be used for table/field names, or for some string formatting, e.g. 651 * $wpdb->prepare( 'WHERE `%1s` = "%1s something %1s" OR %1$s = "%-10s"', 'field_1', 'a', 'b', 'c' ); 651 * 652 * $wpdb->prepare( 'WHERE `%1s` = "%1s something %1s" OR %1$s = "%-10s"', 'field_1', 'a', 'b', 'c' ); 652 653 * 653 654 * But it's risky, e.g. forgetting to add quotes, resulting in SQL Injection vulnerabilities: 654 * $wpdb->prepare( 'WHERE (id = %1s) OR (id = %2$s)', $_GET['id'], $_GET['id'] ); // ?id=id 655 * 656 * $wpdb->prepare( 'WHERE (id = %1s) OR (id = %2$s)', $_GET['id'], $_GET['id'] ); // ?id=id 655 657 * 656 658 * This feature is preserved while plugin authors update their code to use safer approaches: 657 * $wpdb->prepare( 'WHERE %1s = %s', $_GET['key'], $_GET['value'] ); 658 * $wpdb->prepare( 'WHERE %i = %s', $_GET['key'], $_GET['value'] ); 659 * 660 * $wpdb->prepare( 'WHERE %1s = %s', $_GET['key'], $_GET['value'] ); 661 * $wpdb->prepare( 'WHERE %i = %s', $_GET['key'], $_GET['value'] ); 659 662 * 660 663 * While changing to false will be fine for queries not using formatted/argnum placeholders, 661 664 * any remaining cases are most likely going to result in SQL errors (good, in a way): 662 * $wpdb->prepare( 'WHERE %1s = "%-10s"', 'my_field', 'my_value' ); 665 * 666 * $wpdb->prepare( 'WHERE %1s = "%-10s"', 'my_field', 'my_value' ); 663 667 * true = WHERE my_field = "my_value " 664 668 * false = WHERE 'my_field' = "'my_value '" 669 * 665 670 * But there may be some queries that result in an SQL Injection vulnerability: 666 * $wpdb->prepare( 'WHERE id = %1s', $_GET['id'] ); // ?id=id 667 * So there may need to be a `_doing_it_wrong()` phase, after we know everyone can use Identifier 668 * placeholders (%i), but before this feature is disabled or removed. 671 * 672 * $wpdb->prepare( 'WHERE id = %1s', $_GET['id'] ); // ?id=id 673 * 674 * So there may need to be a `_doing_it_wrong()` phase, after we know everyone can use 675 * identifier placeholders (%i), but before this feature is disabled or removed. 669 676 * 670 677 * @since 6.1.0 … … 1377 1384 1378 1385 /** 1379 * Escapes an identifier for a MySQL database (e.g. table/field names).1386 * Escapes an identifier for a MySQL database, e.g. table/field names. 1380 1387 * 1381 1388 * @since 6.1.0 1382 1389 * 1383 1390 * @param string $identifier Identifier to escape. 1384 * @return string Escaped Identifier1391 * @return string Escaped identifier. 1385 1392 */ 1386 1393 public function escape_identifier( $identifier ) { … … 1389 1396 1390 1397 /** 1391 * Escapes an identifier value.1392 *1393 1398 * Escapes an identifier value without adding the surrounding quotes. 1394 1399 * 1395 * - Permitted characters in quoted identifiers include the full Unicode Basic Multilingual Plane (BMP), except U+00001396 * - To quote the identifier itself, then you need to double the character, e.g. `a``b`1397 * 1398 * @link https://dev.mysql.com/doc/refman/8.0/en/identifiers.html1400 * - Permitted characters in quoted identifiers include the full Unicode 1401 * Basic Multilingual Plane (BMP), except U+0000. 1402 * - To quote the identifier itself, you need to double the character, e.g. `a``b`. 1403 * 1399 1404 * @since 6.1.0 1400 1405 * @access private 1401 1406 * 1407 * @link https://dev.mysql.com/doc/refman/8.0/en/identifiers.html 1408 * 1402 1409 * @param string $identifier Identifier to escape. 1403 * @return string Escaped Identifier1410 * @return string Escaped identifier. 1404 1411 */ 1405 1412 private function _escape_identifier_value( $identifier ) { … … 1434 1441 * Examples: 1435 1442 * 1436 * $wpdb->prepare( "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d OR `other_field` LIKE %s", array( 'foo', 1337, '%bar' ) ); 1437 * $wpdb->prepare( "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 'foo' ); 1443 * $wpdb->prepare( 1444 * "SELECT * FROM `table` WHERE `column` = %s AND `field` = %d OR `other_field` LIKE %s", 1445 * array( 'foo', 1337, '%bar' ) 1446 * ); 1447 * 1448 * $wpdb->prepare( 1449 * "SELECT DATE_FORMAT(`field`, '%%c') FROM `table` WHERE `column` = %s", 1450 * 'foo' 1451 * ); 1438 1452 * 1439 1453 * @since 2.3.0 … … 1441 1455 * by updating the function signature. The second parameter was changed 1442 1456 * from `$args` to `...$args`. 1443 * @since 6.1.0 Added '%i' for Identifiers, e.g. table or field names.1444 * Check support via `wpdb::has_cap( 'identifier_placeholders' )` 1445 * This preserves compatibility with sprin f, as the C version uses %d and $i1446 * as a signed integer, whereas PHP only supports %d.1457 * @since 6.1.0 Added `%i` for identifiers, e.g. table or field names. 1458 * Check support via `wpdb::has_cap( 'identifier_placeholders' )`. 1459 * This preserves compatibility with sprintf(), as the C version uses 1460 * `%d` and `$i` as a signed integer, whereas PHP only supports `%d`. 1447 1461 * 1448 1462 * @link https://www.php.net/sprintf Description of syntax. … … 1479 1493 * Specify the formatting allowed in a placeholder. The following are allowed: 1480 1494 * 1481 * - Sign specifier . eg,$+d1482 * - Numbered placeholders . eg,%1$s1483 * - Padding specifier, including custom padding characters . eg,%05s, %'#5s1484 * - Alignment specifier . eg,%05-s1485 * - Precision specifier . eg,%.2f1495 * - Sign specifier, e.g. $+d 1496 * - Numbered placeholders, e.g. %1$s 1497 * - Padding specifier, including custom padding characters, e.g. %05s, %'#5s 1498 * - Alignment specifier, e.g. %05-s 1499 * - Precision specifier, e.g. %.2f 1486 1500 */ 1487 1501 $allowed_format = '(?:[1-9][0-9]*[$])?[-+0-9]*(?: |0|\'.)?[-+0-9]*(?:\.[0-9]+)?'; 1488 1502 1489 1503 /* 1490 * If a %s placeholder already has quotes around it, removing the existing quotes and re-inserting them1491 * ensures the quotes are consistent.1504 * If a %s placeholder already has quotes around it, removing the existing quotes 1505 * and re-inserting them ensures the quotes are consistent. 1492 1506 * 1493 * For backward compatibility, this is only applied to %s, and not to placeholders like %1$s, which are frequently1494 * used in the middle of longer strings, or as table name placeholders.1507 * For backward compatibility, this is only applied to %s, and not to placeholders like %1$s, 1508 * which are frequently used in the middle of longer strings, or as table name placeholders. 1495 1509 */ 1496 1510 $query = str_replace( "'%s'", '%s', $query ); // Strip any existing single quotes. 1497 1511 $query = str_replace( '"%s"', '%s', $query ); // Strip any existing double quotes. 1498 1512 1499 $query = preg_replace( "/%(?:%|$|(?!($allowed_format)?[sdfFi]))/", '%%\\1', $query ); // Escape any unescaped percents (i.e. anything unrecognised). 1513 // Escape any unescaped percents (i.e. anything unrecognised). 1514 $query = preg_replace( "/%(?:%|$|(?!($allowed_format)?[sdfFi]))/", '%%\\1', $query ); 1500 1515 1501 1516 // Extract placeholders from the query. … … 1503 1518 1504 1519 $split_query_count = count( $split_query ); 1505 $placeholder_count = ( ( $split_query_count - 1 ) / 3 ); // Split always returns with 1 value before the first placeholder (even with $query = "%s"), then 3 additional values per placeholder. 1506 1507 // If args were passed as an array (as in vsprintf), move them up. 1520 /* 1521 * Split always returns with 1 value before the first placeholder (even with $query = "%s"), 1522 * then 3 additional values per placeholder. 1523 */ 1524 $placeholder_count = ( ( $split_query_count - 1 ) / 3 ); 1525 1526 // If args were passed as an array, as in vsprintf(), move them up. 1508 1527 $passed_as_array = ( isset( $args[0] ) && is_array( $args[0] ) && 1 === count( $args ) ); 1509 1528 if ( $passed_as_array ) { … … 1512 1531 1513 1532 $new_query = ''; 1514 $key = 2; // keys 0 and 1 in $split_query contain values before the first placeholder.1533 $key = 2; // Keys 0 and 1 in $split_query contain values before the first placeholder. 1515 1534 $arg_id = 0; 1516 1535 $arg_identifiers = array(); 1517 1536 $arg_strings = array(); 1537 1518 1538 while ( $key < $split_query_count ) { 1519 1539 $placeholder = $split_query[ $key ]; … … 1529 1549 if ( 'i' === $type ) { 1530 1550 $placeholder = '`%' . $format . 's`'; 1531 $argnum_pos = strpos( $format, '$' ); // Using a simple strpos() due to previous checking (e.g. $allowed_format). 1551 // Using a simple strpos() due to previous checking (e.g. $allowed_format). 1552 $argnum_pos = strpos( $format, '$' ); 1553 1532 1554 if ( false !== $argnum_pos ) { 1533 $arg_identifiers[] = ( intval( substr( $format, 0, $argnum_pos ) ) - 1 ); // sprintf argnum starts at 1, $arg_id from 0. 1555 // sprintf() argnum starts at 1, $arg_id from 0. 1556 $arg_identifiers[] = ( intval( substr( $format, 0, $argnum_pos ) ) - 1 ); 1534 1557 } else { 1535 1558 $arg_identifiers[] = $arg_id; 1536 1559 } 1537 } elseif ( 'd' !== $type && 'F' !== $type ) { // i.e. ('s' === $type), where 'd' and 'F' keeps $placeholder unchanged, and we ensure string escaping is used as a safe default (e.g. even if 'x'). 1560 } elseif ( 'd' !== $type && 'F' !== $type ) { 1561 /* 1562 * i.e. ( 's' === $type ), where 'd' and 'F' keeps $placeholder unchanged, 1563 * and we ensure string escaping is used as a safe default (e.g. even if 'x'). 1564 */ 1538 1565 $argnum_pos = strpos( $format, '$' ); 1566 1539 1567 if ( false !== $argnum_pos ) { 1540 1568 $arg_strings[] = ( intval( substr( $format, 0, $argnum_pos ) ) - 1 ); 1541 1569 } 1542 if ( true !== $this->allow_unsafe_unquoted_parameters || '' === $format ) { // Unquoted strings for backwards compatibility (dangerous). 1570 1571 // Unquoted strings for backward compatibility (dangerous). 1572 if ( true !== $this->allow_unsafe_unquoted_parameters || '' === $format ) { 1543 1573 $placeholder = "'%" . $format . "s'"; 1544 1574 } 1545 1575 } 1546 1576 1547 $new_query .= $split_query[ $key - 2 ] . $split_query[ $key - 1 ] . $placeholder; // Glue (-2), any leading characters (-1), then the new $placeholder. 1577 // Glue (-2), any leading characters (-1), then the new $placeholder. 1578 $new_query .= $split_query[ $key - 2 ] . $split_query[ $key - 1 ] . $placeholder; 1548 1579 1549 1580 $key += 3; 1550 1581 $arg_id++; 1551 1582 } 1552 $query = $new_query . $split_query[ $key - 2 ]; // Replace $query; and add remaining $query characters, or index 0 if there were no placeholders. 1583 1584 // Replace $query; and add remaining $query characters, or index 0 if there were no placeholders. 1585 $query = $new_query . $split_query[ $key - 2 ]; 1553 1586 1554 1587 $dual_use = array_intersect( $arg_identifiers, $arg_strings ); 1588 1555 1589 if ( count( $dual_use ) ) { 1556 1590 wp_load_translations_early(); … … 1572 1606 if ( $args_count !== $placeholder_count ) { 1573 1607 if ( 1 === $placeholder_count && $passed_as_array ) { 1574 // If the passed query only expected one argument, but the wrong number of arguments were sent as an array, bail. 1608 /* 1609 * If the passed query only expected one argument, 1610 * but the wrong number of arguments was sent as an array, bail. 1611 */ 1575 1612 wp_load_translations_early(); 1576 1613 _doing_it_wrong( … … 1583 1620 } else { 1584 1621 /* 1585 * If we don't have the right number of placeholders, but they were passed as individual arguments, 1622 * If we don't have the right number of placeholders, 1623 * but they were passed as individual arguments, 1586 1624 * or we were expecting multiple arguments in an array, throw a warning. 1587 1625 */ … … 1604 1642 if ( $args_count < $placeholder_count ) { 1605 1643 $max_numbered_placeholder = 0; 1644 1606 1645 for ( $i = 2, $l = $split_query_count; $i < $l; $i += 3 ) { 1607 $argnum = intval( substr( $split_query[ $i ], 1 ) ); // Assume a leading number is for a numbered placeholder, e.g. '%3$s'. 1646 // Assume a leading number is for a numbered placeholder, e.g. '%3$s'. 1647 $argnum = intval( substr( $split_query[ $i ], 1 ) ); 1648 1608 1649 if ( $max_numbered_placeholder < $argnum ) { 1609 1650 $max_numbered_placeholder = $argnum; 1610 1651 } 1611 1652 } 1653 1612 1654 if ( ! $max_numbered_placeholder || $args_count < $max_numbered_placeholder ) { 1613 1655 return ''; … … 1636 1678 '4.8.2' 1637 1679 ); 1638 $value = ''; // Preserving old behaviour, where values are escaped as strings. 1639 } 1680 1681 // Preserving old behavior, where values are escaped as strings. 1682 $value = ''; 1683 } 1684 1640 1685 $args_escaped[] = $this->_real_escape( $value ); 1641 1686 } … … 3873 3918 3874 3919 /** 3875 * Determine DB or WPDB support fora particular feature.3876 * 3877 * Capability sniffs for the database server and current version of wpdb.3878 * 3879 * Database sniffs testbased on the version of MySQL the site is using.3920 * Determines whether the database or WPDB supports a particular feature. 3921 * 3922 * Capability sniffs for the database server and current version of WPDB. 3923 * 3924 * Database sniffs are based on the version of MySQL the site is using. 3880 3925 * 3881 3926 * WPDB sniffs are added as new features are introduced to allow theme and plugin … … 3927 3972 case 'utf8mb4_520': // @since 4.6.0 3928 3973 return version_compare( $version, '5.6', '>=' ); 3929 case 'identifier_placeholders': // @since 6.1.0, wpdb::prepare() supports identifiers via '%i' - e.g. table/field names. 3974 case 'identifier_placeholders': // @since 6.1.0 3975 /* 3976 * As of WordPress 6.1, wpdb::prepare() supports identifiers via '%i', 3977 * e.g. table/field names. 3978 */ 3930 3979 return true; 3931 3980 }
Note: See TracChangeset
for help on using the changeset viewer.