5353 * @package OC\User
5454 */
5555class Manager extends PublicEmitter implements IUserManager {
56+ /** @see \OC\Config\UserConfig::USER_MAX_LENGTH */
57+ public const MAX_USERID_LENGTH = 64 ;
58+
5659 /**
5760 * @var \OCP\UserInterface[] $backends
5861 */
@@ -131,6 +134,10 @@ public function get($uid) {
131134 return $ this ->cachedUsers [$ uid ];
132135 }
133136
137+ if (strlen ($ uid ) > self ::MAX_USERID_LENGTH ) {
138+ return null ;
139+ }
140+
134141 $ cachedBackend = $ this ->cache ->get (sha1 ($ uid ));
135142 if ($ cachedBackend !== null && isset ($ this ->backends [$ cachedBackend ])) {
136143 // Cache has the info of the user backend already, so ask that one directly
@@ -190,6 +197,10 @@ public function getUserObject($uid, $backend, $cacheUser = true) {
190197 * @return bool
191198 */
192199 public function userExists ($ uid ) {
200+ if (strlen ($ uid ) > self ::MAX_USERID_LENGTH ) {
201+ return false ;
202+ }
203+
193204 $ user = $ this ->get ($ uid );
194205 return ($ user !== null );
195206 }
@@ -705,14 +716,14 @@ public function getByEmail($email) {
705716 public function validateUserId (string $ uid , bool $ checkDataDirectory = false ): void {
706717 $ l = Server::get (IFactory::class)->get ('lib ' );
707718
708- // Check the name for bad characters
719+ // Check the ID for bad characters
709720 // Allowed are: "a-z", "A-Z", "0-9", spaces and "_.@-'"
710721 if (preg_match ('/[^a-zA-Z0-9 _.@\- \']/ ' , $ uid )) {
711722 throw new \InvalidArgumentException ($ l ->t ('Only the following characters are allowed in an Login: '
712723 . ' "a-z", "A-Z", "0-9", spaces and "_.@- \'" ' ));
713724 }
714725
715- // No empty username
726+ // No empty user ID
716727 if (trim ($ uid ) === '' ) {
717728 throw new \InvalidArgumentException ($ l ->t ('A valid Login must be provided ' ));
718729 }
@@ -722,11 +733,16 @@ public function validateUserId(string $uid, bool $checkDataDirectory = false): v
722733 throw new \InvalidArgumentException ($ l ->t ('Login contains whitespace at the beginning or at the end ' ));
723734 }
724735
725- // Username only consists of 1 or 2 dots (directory traversal)
736+ // User ID only consists of 1 or 2 dots (directory traversal)
726737 if ($ uid === '. ' || $ uid === '.. ' ) {
727738 throw new \InvalidArgumentException ($ l ->t ('Login must not consist of dots only ' ));
728739 }
729740
741+ // User ID is too long
742+ if (strlen ($ uid ) > self ::MAX_USERID_LENGTH ) {
743+ throw new \InvalidArgumentException ($ l ->t ('Login is too long ' ));
744+ }
745+
730746 if (!$ this ->verifyUid ($ uid , $ checkDataDirectory )) {
731747 throw new \InvalidArgumentException ($ l ->t ('Login is invalid because files already exist for this user ' ));
732748 }
0 commit comments