API: Use User::getCanonicalName() when validating user names
authorBrad Jorsch <bjorsch@wikimedia.org>
Fri, 18 May 2018 09:59:04 +0000 (11:59 +0200)
committerBrad Jorsch <bjorsch@wikimedia.org>
Fri, 18 May 2018 09:59:04 +0000 (11:59 +0200)
This is a bit more interoperable than trying to roll our own by using
Title::makeTitleSafe().

We still need to handle IP addresses separately, of course.

Bug: T194916
Change-Id: Ie3900d768cbe15aef079b97d91f7fd23dc7c3e26

includes/api/ApiBase.php
tests/phpunit/includes/api/ApiBaseTest.php

index 0802e16..c2483cb 100644 (file)
@@ -1691,32 +1691,30 @@ abstract class ApiBase extends ContextSource {
                        return $value;
                }
 
-               $titleObj = Title::makeTitleSafe( NS_USER, $value );
-
-               if ( $titleObj ) {
-                       $value = $titleObj->getText();
+               $name = User::getCanonicalName( $value, 'valid' );
+               if ( $name !== false ) {
+                       return $name;
                }
 
                if (
-                       !User::isValidUserName( $value ) &&
                        // We allow ranges as well, for blocks.
-                       !IP::isIPAddress( $value ) &&
+                       IP::isIPAddress( $value ) ||
                        // See comment for User::isIP.  We don't just call that function
                        // here because it also returns true for things like
                        // 300.300.300.300 that are neither valid usernames nor valid IP
                        // addresses.
-                       !preg_match(
+                       preg_match(
                                '/^' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.xxx$/',
                                $value
                        )
                ) {
-                       $this->dieWithError(
-                               [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $value ) ],
-                               "baduser_{$encParamName}"
-                       );
+                       return IP::sanitizeIP( $value );
                }
 
-               return $value;
+               $this->dieWithError(
+                       [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $value ) ],
+                       "baduser_{$encParamName}"
+               );
        }
 
        /**@}*/
index e7db68e..bb3de4d 100644 (file)
@@ -995,6 +995,12 @@ class ApiBaseTest extends ApiTestCase {
                                'Foo bar',
                                [],
                        ],
+                       'User prefixed with "User:"' => [
+                               'User:foo_bar',
+                               [ ApiBase::PARAM_TYPE => 'user' ],
+                               'Foo bar',
+                               [],
+                       ],
                        'Invalid username "|"' => [
                                '|',
                                [ ApiBase::PARAM_TYPE => 'user' ],