Quellcode durchsuchen

use function with preg_match to check username

Clément vor 9 Jahren
Ursprung
Commit
4eeae5171b
6 geänderte Dateien mit 12 neuen und 15 gelöschten Zeilen
  1. 7 4
      app/Controllers/userController.php
  2. 1 2
      app/Models/Auth.php
  3. 1 3
      cli/_cli.php
  4. 1 2
      cli/create-user.php
  5. 1 2
      cli/delete-user.php
  6. 1 2
      cli/do-install.php

+ 7 - 4
app/Controllers/userController.php

@@ -34,6 +34,11 @@ class FreshRSS_user_Controller extends Minz_ActionController {
 		return $passwordHash == '' ? '' : $passwordHash;
 		return $passwordHash == '' ? '' : $passwordHash;
 	}
 	}
 
 
+	public static function checkUsername($username) {
+		$match = '/^[a-zA-Z_]{1,38}$/';
+		return preg_match($match, $username) === 1;
+	}
+
 	/**
 	/**
 	 * This action displays the user profile page.
 	 * This action displays the user profile page.
 	 */
 	 */
@@ -103,9 +108,8 @@ class FreshRSS_user_Controller extends Minz_ActionController {
 		if (!is_array($userConfig)) {
 		if (!is_array($userConfig)) {
 			$userConfig = array();
 			$userConfig = array();
 		}
 		}
-        $aValid = array('-', '_', '.');
 
 
-		$ok = ($new_user_name != '') && ctype_alnum(str_replace($aValid, '', $new_user_name));
+		$ok = self::checkUsername($new_user_name);
 
 
 		if ($ok) {
 		if ($ok) {
 			$languages = Minz_Translate::availableLanguages();
 			$languages = Minz_Translate::availableLanguages();
@@ -188,8 +192,7 @@ class FreshRSS_user_Controller extends Minz_ActionController {
 		$db = FreshRSS_Context::$system_conf->db;
 		$db = FreshRSS_Context::$system_conf->db;
 		require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
 		require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
 
 
-        $aValid = array('-', '_', '.');
-		$ok = ctype_alnum(str_replace($aValid, '', $username));
+		$ok = self::checkUsername($username);
 		if ($ok) {
 		if ($ok) {
 			$default_user = FreshRSS_Context::$system_conf->default_user;
 			$default_user = FreshRSS_Context::$system_conf->default_user;
 			$ok &= (strcasecmp($username, $default_user) !== 0);	//It is forbidden to delete the default user
 			$ok &= (strcasecmp($username, $default_user) !== 0);	//It is forbidden to delete the default user

+ 1 - 2
app/Models/Auth.php

@@ -182,8 +182,7 @@ class FreshRSS_Auth {
 
 
 class FreshRSS_FormAuth {
 class FreshRSS_FormAuth {
 	public static function checkCredentials($username, $hash, $nonce, $challenge) {
 	public static function checkCredentials($username, $hash, $nonce, $challenge) {
-		$aValid = array('-', '_', '.');
-		if (!ctype_alnum(str_replace($aValid, '', $username)) ||
+		if (!FreshRSS_user_Controller::checkUsername($username) ||
 				!ctype_graph($challenge) ||
 				!ctype_graph($challenge) ||
 				!ctype_alnum($nonce)) {
 				!ctype_alnum($nonce)) {
 			Minz_Log::debug('Invalid credential parameters:' .
 			Minz_Log::debug('Invalid credential parameters:' .

+ 1 - 3
cli/_cli.php

@@ -20,9 +20,7 @@ function fail($message) {
 }
 }
 
 
 function cliInitUser($username) {
 function cliInitUser($username) {
-    $aValid = array('-', '_', '.');
-    
-	if (!ctype_alnum(str_replace($aValid, '', $username))) {
+	if (!FreshRSS_user_Controller::checkUsername($username)) {
 		fail('FreshRSS error: invalid username: ' . $username . "\n");
 		fail('FreshRSS error: invalid username: ' . $username . "\n");
 	}
 	}
 
 

+ 1 - 2
cli/create-user.php

@@ -16,9 +16,8 @@ if (empty($options['user'])) {
 	fail('Usage: ' . basename(__FILE__) . " --user username ( --password 'password' --api-password 'api_password'" .
 	fail('Usage: ' . basename(__FILE__) . " --user username ( --password 'password' --api-password 'api_password'" .
 		" --language en --email user@example.net --token 'longRandomString --no-default-feeds' )");
 		" --language en --email user@example.net --token 'longRandomString --no-default-feeds' )");
 }
 }
-$aValid = array('-', '_', '.');
 $username = $options['user'];
 $username = $options['user'];
-if (!ctype_alnum(str_replace($aValid, '', $username))) {
+if (!FreshRSS_user_Controller::checkUsername($username)) {
 	fail('FreshRSS error: invalid username “' . $username . '”');
 	fail('FreshRSS error: invalid username “' . $username . '”');
 }
 }
 
 

+ 1 - 2
cli/delete-user.php

@@ -9,9 +9,8 @@ $options = getopt('', array(
 if (empty($options['user'])) {
 if (empty($options['user'])) {
 	fail('Usage: ' . basename(__FILE__) . " --user username");
 	fail('Usage: ' . basename(__FILE__) . " --user username");
 }
 }
-$aValid = array('-', '_', '.');
 $username = $options['user'];
 $username = $options['user'];
-if (!ctype_alnum(str_replace($aValid, '', $username))) {
+if (!FreshRSS_user_Controller::checkUsername($username)) {
 	fail('FreshRSS error: invalid username “' . $username . '”');
 	fail('FreshRSS error: invalid username “' . $username . '”');
 }
 }
 
 

+ 1 - 2
cli/do-install.php

@@ -47,8 +47,7 @@ if ($requirements['all'] !== 'ok') {
 	fail($message);
 	fail($message);
 }
 }
 
 
-$aValid = array('-', '_', '.');
-if (!ctype_alnum(str_replace($aValid, '', $options['default_user']))) {
+if (!FreshRSS_user_Controller::checkUsername($options['default_user'])) {
 	fail('FreshRSS invalid default username (must be ASCII alphanumeric): ' . $options['default_user']);
 	fail('FreshRSS invalid default username (must be ASCII alphanumeric): ' . $options['default_user']);
 }
 }