<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[52370] trunk: Formatting: Use `is_scalar()` in `sanitize_key()`.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/52370">52370</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/52370","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>hellofromTonya</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-12-14 14:59:33 +0000 (Tue, 14 Dec 2021)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Formatting: Use `is_scalar()` in `sanitize_key()`.
This is a follow-up to <a href="https://core.trac.wordpress.org/changeset/52292">[52292]</a> which introduced `is_string()` to check the given key is a string to be sanitized, else the key is set to an empty string.
`sanitize_key()` is clearly identified (in the documentation) to only work with ''string'' keys. However, it had a bug in it that allowed non-strings to pass through it:
* A non-scalar "key" would throw a PHP Warning (which was resolved in <a href="https://core.trac.wordpress.org/changeset/52292">[52292]</a>.
* A non-string scalar "key" was handled by the PHP native `strtolower()` which converted it into a string.
While `is_string()` is valid, non-string scalar types passed as the key to be sanitized were being set to an empty string. Given that `strtolower()` handles these without error or deprecation as of PHP 8.1, `is_scalar()` protects the website from issues while retaining the past behavior of converting integer keys (for example) into a string.
Changes include:
* Using `is_scalar()` instead of `is_string()`
* Refactor for readability and less code
* More tests
Please note, this does not change the behavior of the function, nor redefine it to now accept non-string scalars.
References:
* https://developer.wordpress.org/reference/functions/sanitize_key/
* https://www.php.net/manual/en/function.strtolower.php
Follow-up <a href="https://core.trac.wordpress.org/changeset/52292">[52292]</a>.
Props wppunk, hellofromTonya, costdev, jrf.
Fixes <a href="https://core.trac.wordpress.org/ticket/54160">#54160</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesformattingphp">trunk/src/wp-includes/formatting.php</a></li>
<li><a href="#trunktestsphpunittestsformattingsanitizeKeyphp">trunk/tests/phpunit/tests/formatting/sanitizeKey.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesformattingphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/formatting.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/formatting.php 2021-12-14 09:47:51 UTC (rev 52369)
+++ trunk/src/wp-includes/formatting.php 2021-12-14 14:59:33 UTC (rev 52370)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2132,30 +2132,26 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.0.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $key String key
- * @return string Sanitized key
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string $key String key.
+ * @return string Sanitized key.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function sanitize_key( $key ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $raw_key = $key;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $sanitized_key = '';
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( ! is_string( $key ) ) {
- $key = '';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( is_scalar( $key ) ) {
+ $sanitized_key = strtolower( $key );
+ $sanitized_key = preg_replace( '/[^a-z0-9_\-]/', '', $sanitized_key );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( '' !== $key ) {
- $key = strtolower( $key );
- $key = preg_replace( '/[^a-z0-9_\-]/', '', $key );
- }
-
</del><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters a sanitized key string.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.0.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param string $key Sanitized key.
- * @param string $raw_key The key prior to sanitization.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string $sanitized_key Sanitized key.
+ * @param string $key The key prior to sanitization.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- return apply_filters( 'sanitize_key', $key, $raw_key );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return apply_filters( 'sanitize_key', $sanitized_key, $key );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunktestsphpunittestsformattingsanitizeKeyphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/formatting/sanitizeKey.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/formatting/sanitizeKey.php 2021-12-14 09:47:51 UTC (rev 52369)
+++ trunk/tests/phpunit/tests/formatting/sanitizeKey.php 2021-12-14 14:59:33 UTC (rev 52370)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -7,16 +7,21 @@
</span><span class="cx" style="display: block; padding: 0 10px"> class Tests_Formatting_SanitizeKey extends WP_UnitTestCase {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @ticket 54160
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 54160
</ins><span class="cx" style="display: block; padding: 0 10px"> * @dataProvider data_sanitize_key
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param mixed $key The key to sanitize.
- * @param mixed $expected The expected value.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param string $key The key to sanitize.
+ * @param string $expected The expected value.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> public function test_sanitize_key( $key, $expected ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertSame( $expected, sanitize_key( $key ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Data provider.
+ *
+ * @return array
+ */
</ins><span class="cx" style="display: block; padding: 0 10px"> public function data_sanitize_key() {
</span><span class="cx" style="display: block; padding: 0 10px"> return array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'an empty string key' => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -23,22 +28,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'key' => '',
</span><span class="cx" style="display: block; padding: 0 10px"> 'expected' => '',
</span><span class="cx" style="display: block; padding: 0 10px"> ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- 'a null key' => array(
- 'key' => null,
- 'expected' => '',
- ),
- 'an int 0 key' => array(
- 'key' => 0,
- 'expected' => '',
- ),
- 'a true key' => array(
- 'key' => true,
- 'expected' => '',
- ),
- 'an array key' => array(
- 'key' => array( 'Howdy, admin!' ),
- 'expected' => '',
- ),
</del><span class="cx" style="display: block; padding: 0 10px"> 'a lowercase key with commas' => array(
</span><span class="cx" style="display: block; padding: 0 10px"> 'key' => 'howdy,admin',
</span><span class="cx" style="display: block; padding: 0 10px"> 'expected' => 'howdyadmin',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -71,20 +60,81 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @ticket 54160
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @ticket 54160
+ * @dataProvider data_sanitize_key_nonstring_scalar
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @covers WP_Hook::sanitize_key
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param mixed $key The key to sanitize.
+ * @param string $expected The expected value.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_filter_sanitize_key() {
- $key = 'Howdy, admin!';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_sanitize_key_nonstring_scalar( $key, $expected ) {
+ $this->assertSame( $expected, sanitize_key( $key ) );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Data provider.
+ *
+ * @return array
+ */
+ public function data_sanitize_key_nonstring_scalar() {
+ return array(
+ 'integer type' => array(
+ 'key' => 0,
+ 'expected' => '0',
+ ),
+ 'boolean true' => array(
+ 'key' => true,
+ 'expected' => '1',
+ ),
+ 'boolean false' => array(
+ 'key' => false,
+ 'expected' => '',
+ ),
+ 'float type' => array(
+ 'key' => 0.123,
+ 'expected' => '0123',
+ ),
+ );
+ }
+
+ /**
+ * @ticket 54160
+ * @dataProvider data_sanitize_key_with_non_scalars
+ *
+ * @param mixed $nonscalar_key A non-scalar data type given as a key.
+ */
+ public function test_sanitize_key_with_non_scalars( $nonscalar_key ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> add_filter(
</span><span class="cx" style="display: block; padding: 0 10px"> 'sanitize_key',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- static function( $key ) {
- return 'Howdy, admin!';
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ function ( $sanitized_key, $key ) use ( $nonscalar_key ) {
+ $this->assertEmpty( $sanitized_key, 'Empty string not passed as first filtered argument' );
+ $this->assertSame( $nonscalar_key, $key, 'Given unsanitized key not passed as second filtered argument' );
+ return $sanitized_key;
+ },
+ 10,
+ 2
</ins><span class="cx" style="display: block; padding: 0 10px"> );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertEmpty( sanitize_key( $nonscalar_key ), 'Non-scalar key did not return empty string' );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $this->assertSame( $key, sanitize_key( $key ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+ * Data provider.
+ *
+ * @return array
+ */
+ public function data_sanitize_key_with_non_scalars() {
+ return array(
+ 'array type' => array(
+ 'key' => array( 'key' ),
+ 'expected' => '',
+ ),
+ 'null' => array(
+ 'key' => null,
+ 'expected' => '',
+ ),
+ 'object' => array(
+ 'key' => new stdClass(),
+ 'expected' => '',
+ ),
+ );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>