<!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>[39219] trunk: REST API: Improve validation for usernames and passwords.</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 { 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/39219">39219</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/39219","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>rmccue</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-11-14 07:12:31 +0000 (Mon, 14 Nov 2016)</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'>REST API: Improve validation for usernames and passwords.

Also improves the slashing of user data in the REST API to avoid data loss.

Props jnylen0.
Fixes <a href="https://core.trac.wordpress.org/ticket/38739">#38739</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesrestapiendpointsclasswprestuserscontrollerphp">trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php</a></li>
<li><a href="#trunktestsphpunitincludesfunctionsphp">trunk/tests/phpunit/includes/functions.php</a></li>
<li><a href="#trunktestsphpunittestsrestapirestuserscontrollerphp">trunk/tests/phpunit/tests/rest-api/rest-users-controller.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesrestapiendpointsclasswprestuserscontrollerphp"></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/rest-api/endpoints/class-wp-rest-users-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php       2016-11-14 04:32:47 UTC (rev 39218)
+++ trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php 2016-11-14 07:12:31 UTC (rev 39219)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -417,7 +417,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $ret = wpmu_validate_user_signup( $user->user_login, $user->user_email );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( is_wp_error( $ret['errors'] ) && ! empty( $ret['errors']->errors ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                return $ret['errors'];
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $error = new WP_Error( 'rest_invalid_param', __( 'Invalid user parameter(s).' ), array( 'status' => 400 ) );
+                               foreach ( $ret['errors']->errors as $code => $messages ) {
+                                       foreach ( $messages as $message ) {
+                                               $error->add( $code, $message );
+                                       }
+                                       if ( $error_data = $error->get_error_data( $code ) ) {
+                                               $error->add_data( $error_data, $code );
+                                       }
+                               }
+                               return $error;
</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 class="lines" style="display: block; padding: 0 10px; color: #888">@@ -429,7 +438,7 @@
</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">                        $user->ID = $user_id;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $user_id  = wp_update_user( $user );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $user_id  = wp_update_user( wp_slash( (array) $user ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( is_wp_error( $user_id ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                return $user_id;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -437,7 +446,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        add_user_to_blog( get_site()->id, $user_id, '' );
</span><span class="cx" style="display: block; padding: 0 10px">                } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $user_id = wp_insert_user( $user );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $user_id = wp_insert_user( wp_slash( (array) $user ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( is_wp_error( $user_id ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                return $user_id;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -552,7 +561,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Ensure we're operating on the same user we already checked.
</span><span class="cx" style="display: block; padding: 0 10px">                $user->ID = $id;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $user_id = wp_update_user( $user );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $user_id = wp_update_user( wp_slash( (array) $user ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( is_wp_error( $user_id ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        return $user_id;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -997,6 +1006,61 @@
</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><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Check a username for the REST API.
+        *
+        * Performs a couple of checks like edit_user() in wp-admin/includes/user.php.
+        *
+        * @since 4.7.0
+        *
+        * @param  mixed            $value   The username submitted in the request.
+        * @param  WP_REST_Request  $request Full details about the request.
+        * @param  string           $param   The parameter name.
+        * @return WP_Error|string The sanitized username, if valid, otherwise an error.
+        */
+       public function check_username( $value, $request, $param ) {
+               $username = (string) rest_sanitize_value_from_schema( $value, $request, $param );
+
+               if ( ! validate_username( $username ) ) {
+                       return new WP_Error( 'rest_user_invalid_username', __( 'Username contains invalid characters.' ), array( 'status' => 400 ) );
+               }
+
+               /** This filter is documented in wp-includes/user.php */
+               $illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
+
+               if ( in_array( strtolower( $username ), array_map( 'strtolower', $illegal_logins ) ) ) {
+                       return new WP_Error( 'rest_user_invalid_username', __( 'Sorry, that username is not allowed.' ), array( 'status' => 400 ) );
+               }
+
+               return $username;
+       }
+
+       /**
+        * Check a user password for the REST API.
+        *
+        * Performs a couple of checks like edit_user() in wp-admin/includes/user.php.
+        *
+        * @since 4.7.0
+        *
+        * @param  mixed            $value   The password submitted in the request.
+        * @param  WP_REST_Request  $request Full details about the request.
+        * @param  string           $param   The parameter name.
+        * @return WP_Error|string The sanitized password, if valid, otherwise an error.
+        */
+       public function check_user_password( $value, $request, $param ) {
+               $password = (string) rest_sanitize_value_from_schema( $value, $request, $param );
+
+               if ( empty( $password ) ) {
+                       return new WP_Error( 'rest_user_invalid_password', __( 'Passwords cannot be empty.' ), array( 'status' => 400 ) );
+               }
+
+               if ( false !== strpos( $password, "\\" ) ) {
+                       return new WP_Error( 'rest_user_invalid_password', __( 'Passwords cannot contain the "\\" character.' ), array( 'status' => 400 ) );
+               }
+
+               return $password;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Retrieves the user's schema, conforming to JSON Schema.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 4.7.0
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1022,7 +1086,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        'context'     => array( 'edit' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'required'    => true,
</span><span class="cx" style="display: block; padding: 0 10px">                                        'arg_options' => array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                'sanitize_callback' => 'sanitize_user',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                         'sanitize_callback' => array( $this, 'check_username' ),
</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">                                'name'        => array(
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1066,9 +1130,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        'description' => __( 'Description of the resource.' ),
</span><span class="cx" style="display: block; padding: 0 10px">                                        'type'        => 'string',
</span><span class="cx" style="display: block; padding: 0 10px">                                        'context'     => array( 'embed', 'view', 'edit' ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        'arg_options' => array(
-                                               'sanitize_callback' => 'wp_filter_post_kses',
-                                       ),
</del><span class="cx" style="display: block; padding: 0 10px">                                 ),
</span><span class="cx" style="display: block; padding: 0 10px">                                'link'        => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'description' => __( 'Author URL to the resource.' ),
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1119,6 +1180,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        'type'        => 'string',
</span><span class="cx" style="display: block; padding: 0 10px">                                        'context'     => array(), // Password is never displayed.
</span><span class="cx" style="display: block; padding: 0 10px">                                        'required'    => true,
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                        'arg_options' => array(
+                                               'sanitize_callback' => array( $this, 'check_user_password' ),
+                                       ),
</ins><span class="cx" style="display: block; padding: 0 10px">                                 ),
</span><span class="cx" style="display: block; padding: 0 10px">                                'capabilities'    => array(
</span><span class="cx" style="display: block; padding: 0 10px">                                        'description' => __( 'All capabilities assigned to the resource.' ),
</span></span></pre></div>
<a id="trunktestsphpunitincludesfunctionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/includes/functions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/functions.php        2016-11-14 04:32:47 UTC (rev 39218)
+++ trunk/tests/phpunit/includes/functions.php  2016-11-14 07:12:31 UTC (rev 39219)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -161,3 +161,24 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        return $uploads;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+// Skip `setcookie` calls in auth_cookie functions due to warning:
+// Cannot modify header information - headers already sent by ...
+
+function wp_set_auth_cookie( $user_id, $remember = false, $secure = '', $token = '' ) {
+       $auth_cookie = null;
+       $expire = null;
+       $expiration = null;
+       $user_id = null;
+       $scheme = null;
+       /** This action is documented in wp-inclues/pluggable.php */
+       do_action( 'set_auth_cookie', $auth_cookie, $expire, $expiration, $user_id, $scheme );
+       $logged_in_cookie = null;
+       /** This action is documented in wp-inclues/pluggable.php */
+       do_action( 'set_logged_in_cookie', $logged_in_cookie, $expire, $expiration, $user_id, 'logged_in' );
+}
+
+function wp_clear_auth_cookie() {
+       /** This action is documented in wp-inclues/pluggable.php */
+       do_action( 'clear_auth_cookie' );
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsrestapirestuserscontrollerphp"></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/rest-api/rest-users-controller.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/rest-api/rest-users-controller.php      2016-11-14 04:32:47 UTC (rev 39218)
+++ trunk/tests/phpunit/tests/rest-api/rest-users-controller.php        2016-11-14 07:12:31 UTC (rev 39219)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -10,11 +10,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @group restapi
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        protected static $superadmin;
</ins><span class="cx" style="display: block; padding: 0 10px">         protected static $user;
</span><span class="cx" style="display: block; padding: 0 10px">        protected static $editor;
</span><span class="cx" style="display: block; padding: 0 10px">        protected static $site;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public static function wpSetUpBeforeClass( $factory ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                self::$superadmin = $factory->user->create( array(
+                       'role'       => 'administrator',
+                       'user_login' => 'superadmin',
+               ) );
</ins><span class="cx" style="display: block; padding: 0 10px">                 self::$user = $factory->user->create( array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'role' => 'administrator',
</span><span class="cx" style="display: block; padding: 0 10px">                ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -25,6 +30,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( is_multisite() ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        self::$site = $factory->blog->create( array( 'domain' => 'rest.wordpress.org', 'path' => '/' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        update_site_option( 'site_admins', array( 'superadmin' ) );
</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 class="lines" style="display: block; padding: 0 10px; color: #888">@@ -175,8 +181,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
</span><span class="cx" style="display: block; padding: 0 10px">                $response = $this->server->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px">                $headers = $response->get_headers();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertEquals( 50, $headers['X-WP-Total'] );
-               $this->assertEquals( 5, $headers['X-WP-TotalPages'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertEquals( 51, $headers['X-WP-Total'] );
+               $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $next_link = add_query_arg( array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'page'    => 2,
</span><span class="cx" style="display: block; padding: 0 10px">                        ), rest_url( 'wp/v2/users' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -190,7 +196,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $request->set_param( 'page', 3 );
</span><span class="cx" style="display: block; padding: 0 10px">                $response = $this->server->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px">                $headers = $response->get_headers();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertEquals( 51, $headers['X-WP-Total'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertEquals( 52, $headers['X-WP-Total'] );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
</span><span class="cx" style="display: block; padding: 0 10px">                $prev_link = add_query_arg( array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'page'    => 2,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -205,7 +211,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $request->set_param( 'page', 6 );
</span><span class="cx" style="display: block; padding: 0 10px">                $response = $this->server->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px">                $headers = $response->get_headers();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertEquals( 51, $headers['X-WP-Total'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertEquals( 52, $headers['X-WP-Total'] );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
</span><span class="cx" style="display: block; padding: 0 10px">                $prev_link = add_query_arg( array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'page'    => 5,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -217,7 +223,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $request->set_param( 'page', 8 );
</span><span class="cx" style="display: block; padding: 0 10px">                $response = $this->server->dispatch( $request );
</span><span class="cx" style="display: block; padding: 0 10px">                $headers = $response->get_headers();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertEquals( 51, $headers['X-WP-Total'] );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertEquals( 52, $headers['X-WP-Total'] );
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertEquals( 6, $headers['X-WP-TotalPages'] );
</span><span class="cx" style="display: block; padding: 0 10px">                $prev_link = add_query_arg( array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'page'    => 6,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -393,7 +399,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $request = new WP_REST_Request( 'GET', '/wp/v2/users' );
</span><span class="cx" style="display: block; padding: 0 10px">                $request->set_param( 'offset', 1 );
</span><span class="cx" style="display: block; padding: 0 10px">                $response = $this->server->dispatch( $request );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertCount( 3, $response->get_data() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertCount( 4, $response->get_data() );
</ins><span class="cx" style="display: block; padding: 0 10px">                 // 'offset' works with 'per_page'
</span><span class="cx" style="display: block; padding: 0 10px">                $request->set_param( 'per_page', 2 );
</span><span class="cx" style="display: block; padding: 0 10px">                $response = $this->server->dispatch( $request );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -715,6 +721,88 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->check_add_edit_user_response( $response );
</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">+        public function test_create_item_invalid_username() {
+               $this->allow_user_to_manage_multisite();
+               wp_set_current_user( self::$user );
+
+               $params = array(
+                       'username'    => '¯\_(ツ)_/¯',
+                       'password'    => 'testpassword',
+                       'email'       => 'test@example.com',
+                       'name'        => 'Test User',
+                       'nickname'    => 'testuser',
+                       'slug'        => 'test-user',
+                       'roles'       => array( 'editor' ),
+                       'description' => 'New API User',
+                       'url'         => 'http://example.com',
+               );
+
+               // Username rules are different (more strict) for multisite; see `wpmu_validate_user_signup`
+               if ( is_multisite() ) {
+                       $params['username'] = 'no-dashes-allowed';
+               }
+
+               $request = new WP_REST_Request( 'POST', '/wp/v2/users' );
+               $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+               $request->set_body_params( $params );
+
+               $response = $this->server->dispatch( $request );
+               $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+
+               $data = $response->get_data();
+               if ( is_multisite() ) {
+                       $this->assertInternalType( 'array', $data['additional_errors'] );
+                       $this->assertCount( 1, $data['additional_errors'] );
+                       $error = $data['additional_errors'][0];
+                       $this->assertEquals( 'user_name', $error['code'] );
+                       $this->assertEquals( 'Usernames can only contain lowercase letters (a-z) and numbers.', $error['message'] );
+               } else {
+                       $this->assertInternalType( 'array', $data['data']['params'] );
+                       $errors = $data['data']['params'];
+                       $this->assertInternalType( 'string', $errors['username'] );
+                       $this->assertEquals( 'Username contains invalid characters.', $errors['username'] );
+               }
+       }
+
+       function get_illegal_user_logins() {
+               return array( 'nope' );
+       }
+
+       public function test_create_item_illegal_username() {
+               $this->allow_user_to_manage_multisite();
+               wp_set_current_user( self::$user );
+
+               add_filter( 'illegal_user_logins', array( $this, 'get_illegal_user_logins' ) );
+
+               $params = array(
+                       'username'    => 'nope',
+                       'password'    => 'testpassword',
+                       'email'       => 'test@example.com',
+                       'name'        => 'Test User',
+                       'nickname'    => 'testuser',
+                       'slug'        => 'test-user',
+                       'roles'       => array( 'editor' ),
+                       'description' => 'New API User',
+                       'url'         => 'http://example.com',
+               );
+
+               $request = new WP_REST_Request( 'POST', '/wp/v2/users' );
+               $request->add_header( 'content-type', 'application/x-www-form-urlencoded' );
+               $request->set_body_params( $params );
+
+               $response = $this->server->dispatch( $request );
+
+               remove_filter( 'illegal_user_logins', array( $this, 'get_illegal_user_logins' ) );
+
+               $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+
+               $data = $response->get_data();
+               $this->assertInternalType( 'array', $data['data']['params'] );
+               $errors = $data['data']['params'];
+               $this->assertInternalType( 'string', $errors['username'] );
+               $this->assertEquals( 'Sorry, that username is not allowed.', $errors['username'] );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function test_create_new_network_user_on_site_does_not_add_user_to_sub_site() {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( ! is_multisite() ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->markTestSkipped( 'Test requires multisite.' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -810,7 +898,20 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                wpmu_delete_user( $user_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertErrorResponse( 'user_name', $switched_response );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertErrorResponse( 'rest_invalid_param', $switched_response, 400 );
+               $data = $switched_response->get_data();
+               $this->assertInternalType( 'array', $data['additional_errors'] );
+               $this->assertCount( 2, $data['additional_errors'] );
+               $errors = $data['additional_errors'];
+               foreach ( $errors as $error ) {
+                       // Check the code matches one we know.
+                       $this->assertContains( $error['code'], array( 'user_name', 'user_email' ) );
+                       if ( 'user_name' === $error['code'] ) {
+                               $this->assertEquals( 'Sorry, that username already exists!', $error['message'] );
+                       } else {
+                               $this->assertEquals( 'Sorry, that email address is already used!', $error['message'] );
+                       }
+               }
</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">        public function test_update_existing_network_user_on_sub_site_adds_user_to_site() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1305,6 +1406,213 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertErrorResponse( 'rest_user_invalid_id', $response, 404 );
</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">+        public function test_update_item_invalid_password() {
+               $this->allow_user_to_manage_multisite();
+               wp_set_current_user( self::$user );
+
+               $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', self::$editor ) );
+
+               $request->set_param( 'password', 'no\\backslashes\\allowed' );
+               $response = $this->server->dispatch( $request );
+               $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+
+               $request->set_param( 'password', '' );
+               $response = $this->server->dispatch( $request );
+               $this->assertErrorResponse( 'rest_invalid_param', $response, 400 );
+       }
+
+       public function verify_user_roundtrip( $input = array(), $expected_output = array() ) {
+               if ( isset( $input['id'] ) ) {
+                       // Existing user; don't try to create one
+                       $user_id = $input['id'];
+               } else {
+                       // Create a new user
+                       $request = new WP_REST_Request( 'POST', '/wp/v2/users' );
+                       foreach ( $input as $name => $value ) {
+                               $request->set_param( $name, $value );
+                       }
+                       $request->set_param( 'email', 'cbg@androidsdungeon.com' );
+                       $response = $this->server->dispatch( $request );
+                       $this->assertEquals( 201, $response->get_status() );
+                       $actual_output = $response->get_data();
+
+                       // Compare expected API output to actual API output
+                       $this->assertEquals( $expected_output['username']   , $actual_output['username'] );
+                       $this->assertEquals( $expected_output['name']       , $actual_output['name'] );
+                       $this->assertEquals( $expected_output['first_name'] , $actual_output['first_name'] );
+                       $this->assertEquals( $expected_output['last_name']  , $actual_output['last_name'] );
+                       $this->assertEquals( $expected_output['url']        , $actual_output['url'] );
+                       $this->assertEquals( $expected_output['description'], $actual_output['description'] );
+                       $this->assertEquals( $expected_output['nickname']   , $actual_output['nickname'] );
+
+                       // Compare expected API output to WP internal values
+                       $user = get_userdata( $actual_output['id'] );
+                       $this->assertEquals( $expected_output['username']   , $user->user_login );
+                       $this->assertEquals( $expected_output['name']       , $user->display_name );
+                       $this->assertEquals( $expected_output['first_name'] , $user->first_name );
+                       $this->assertEquals( $expected_output['last_name']  , $user->last_name );
+                       $this->assertEquals( $expected_output['url']        , $user->user_url );
+                       $this->assertEquals( $expected_output['description'], $user->description );
+                       $this->assertEquals( $expected_output['nickname']   , $user->nickname );
+                       $this->assertTrue( wp_check_password( addslashes( $expected_output['password'] ), $user->user_pass ) );
+
+                       $user_id = $actual_output['id'];
+               }
+
+               // Update the user
+               $request = new WP_REST_Request( 'PUT', sprintf( '/wp/v2/users/%d', $user_id ) );
+               foreach ( $input as $name => $value ) {
+                       if ( 'username' !== $name ) {
+                               $request->set_param( $name, $value );
+                       }
+               }
+               $response = $this->server->dispatch( $request );
+               $this->assertEquals( 200, $response->get_status() );
+               $actual_output = $response->get_data();
+
+               // Compare expected API output to actual API output
+               if ( isset( $expected_output['username'] ) ) {
+                       $this->assertEquals( $expected_output['username'], $actual_output['username'] );
+               }
+               $this->assertEquals( $expected_output['name']       , $actual_output['name'] );
+               $this->assertEquals( $expected_output['first_name'] , $actual_output['first_name'] );
+               $this->assertEquals( $expected_output['last_name']  , $actual_output['last_name'] );
+               $this->assertEquals( $expected_output['url']        , $actual_output['url'] );
+               $this->assertEquals( $expected_output['description'], $actual_output['description'] );
+               $this->assertEquals( $expected_output['nickname']   , $actual_output['nickname'] );
+
+               // Compare expected API output to WP internal values
+               $user = get_userdata( $actual_output['id'] );
+               if ( isset( $expected_output['username'] ) ) {
+                       $this->assertEquals( $expected_output['username'], $user->user_login );
+               }
+               $this->assertEquals( $expected_output['name']       , $user->display_name );
+               $this->assertEquals( $expected_output['first_name'] , $user->first_name );
+               $this->assertEquals( $expected_output['last_name']  , $user->last_name );
+               $this->assertEquals( $expected_output['url']        , $user->user_url );
+               $this->assertEquals( $expected_output['description'], $user->description );
+               $this->assertEquals( $expected_output['nickname']   , $user->nickname );
+               $this->assertTrue( wp_check_password( addslashes( $expected_output['password'] ), $user->user_pass ) );
+       }
+
+       public function test_user_roundtrip_as_editor() {
+               wp_set_current_user( self::$editor );
+               $this->assertEquals( ! is_multisite(), current_user_can( 'unfiltered_html' ) );
+               $this->verify_user_roundtrip( array(
+                       'id'          => self::$editor,
+                       'name'        => '\o/ ¯\_(ツ)_/¯',
+                       'first_name'  => '\o/ ¯\_(ツ)_/¯',
+                       'last_name'   => '\o/ ¯\_(ツ)_/¯',
+                       'url'         => '\o/ ¯\_(ツ)_/¯',
+                       'description' => '\o/ ¯\_(ツ)_/¯',
+                       'nickname'    => '\o/ ¯\_(ツ)_/¯',
+                       'password'    => 'o/ ¯_(ツ)_/¯ \'"',
+               ), array(
+                       'name'        => '\o/ ¯\_(ツ)_/¯',
+                       'first_name'  => '\o/ ¯\_(ツ)_/¯',
+                       'last_name'   => '\o/ ¯\_(ツ)_/¯',
+                       'url'         => 'http://o/%20¯_(ツ)_/¯',
+                       'description' => '\o/ ¯\_(ツ)_/¯',
+                       'nickname'    => '\o/ ¯\_(ツ)_/¯',
+                       'password'    => 'o/ ¯_(ツ)_/¯ \'"',
+               ) );
+       }
+
+       public function test_user_roundtrip_as_editor_html() {
+               wp_set_current_user( self::$editor );
+               if ( is_multisite() ) {
+                       $this->assertFalse( current_user_can( 'unfiltered_html' ) );
+                       $this->verify_user_roundtrip( array(
+                               'id'          => self::$editor,
+                               'name'        => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                               'first_name'  => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                               'last_name'   => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                               'url'         => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                               'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                               'nickname'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                               'password'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                       ), array(
+                               'name'        => 'div strong',
+                               'first_name'  => 'div strong',
+                               'last_name'   => 'div strong',
+                               'url'         => 'http://divdiv/div%20strongstrong/strong%20scriptoh%20noes/script',
+                               'description' => 'div <strong>strong</strong> oh noes',
+                               'nickname'    => 'div strong',
+                               'password'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                       ) );
+               } else {
+                       $this->assertTrue( current_user_can( 'unfiltered_html' ) );
+                       $this->verify_user_roundtrip( array(
+                               'id'          => self::$editor,
+                               'name'        => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                               'first_name'  => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                               'last_name'   => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                               'url'         => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                               'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                               'nickname'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                               'password'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                       ), array(
+                               'name'        => 'div strong',
+                               'first_name'  => 'div strong',
+                               'last_name'   => 'div strong',
+                               'url'         => 'http://divdiv/div%20strongstrong/strong%20scriptoh%20noes/script',
+                               'description' => 'div <strong>strong</strong> oh noes',
+                               'nickname'    => 'div strong',
+                               'password'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                       ) );
+               }
+       }
+
+       public function test_user_roundtrip_as_superadmin() {
+               wp_set_current_user( self::$superadmin );
+               $this->assertTrue( current_user_can( 'unfiltered_html' ) );
+               $valid_username = is_multisite() ? 'noinvalidcharshere' : 'no-invalid-chars-here';
+               $this->verify_user_roundtrip( array(
+                       'username'    => $valid_username,
+                       'name'        => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
+                       'first_name'  => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
+                       'last_name'   => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
+                       'url'         => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
+                       'description' => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
+                       'nickname'    => '\\\&\\\ &amp; &invalid; < &lt; &amp;lt;',
+                       'password'    => '& &amp; &invalid; < &lt; &amp;lt;',
+               ), array(
+                       'username'    => $valid_username,
+                       'name'        => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
+                       'first_name'  => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
+                       'last_name'   => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
+                       'url'         => 'http://&amp;%20&amp;%20&amp;invalid;%20%20&lt;%20&amp;lt;',
+                       'description' => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
+                       'nickname'    => '\\\&amp;\\\ &amp; &amp;invalid; &lt; &lt; &amp;lt;',
+                       'password'    => '& &amp; &invalid; < &lt; &amp;lt;',
+               ) );
+       }
+
+       public function test_user_roundtrip_as_superadmin_html() {
+               wp_set_current_user( self::$superadmin );
+               $this->assertTrue( current_user_can( 'unfiltered_html' ) );
+               $valid_username = is_multisite() ? 'noinvalidcharshere' : 'no-invalid-chars-here';
+               $this->verify_user_roundtrip( array(
+                       'username'    => $valid_username,
+                       'name'        => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                       'first_name'  => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                       'last_name'   => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                       'url'         => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                       'description' => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                       'nickname'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+                       'password'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+               ), array(
+                       'username'    => $valid_username,
+                       'name'        => 'div strong',
+                       'first_name'  => 'div strong',
+                       'last_name'   => 'div strong',
+                       'url'         => 'http://divdiv/div%20strongstrong/strong%20scriptoh%20noes/script',
+                       'description' => 'div <strong>strong</strong> oh noes',
+                       'nickname'    => 'div strong',
+                       'password'    => '<div>div</div> <strong>strong</strong> <script>oh noes</script>',
+               ) );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function test_delete_item() {
</span><span class="cx" style="display: block; padding: 0 10px">                $user_id = $this->factory->user->create( array( 'display_name' => 'Deleted User' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre>
</div>
</div>

</body>
</html>