Просмотр исходного кода

Merge pull request #1423 from plopoyop/feature/username-in-cli

[WIP] Feature/username in cli
Alexandre Alapetite 9 лет назад
Родитель
Сommit
fb6bb8e826

+ 2 - 0
CHANGELOG.md

@@ -15,6 +15,8 @@
 	* Allow empty strings in CLI do-install [#1435](https://github.com/FreshRSS/FreshRSS/pull/1435)
 	* Allow empty strings in CLI do-install [#1435](https://github.com/FreshRSS/FreshRSS/pull/1435)
 * Security
 * Security
 	* No version number for anonymous users [#1404](https://github.com/FreshRSS/FreshRSS/issues/1404)
 	* No version number for anonymous users [#1404](https://github.com/FreshRSS/FreshRSS/issues/1404)
+* Misc.
+	* Relaxed requirements for username to `/^[0-9a-zA-Z]|[0-9a-zA-Z_]{2,38}/$` [#1423](https://github.com/FreshRSS/FreshRSS/pull/1423)
 
 
 
 
 ## 2016-12-26 FreshRSS 1.6.2
 ## 2016-12-26 FreshRSS 1.6.2

+ 1 - 1
app/Controllers/javascriptController.php

@@ -26,7 +26,7 @@ class FreshRSS_javascript_Controller extends Minz_ActionController {
 		header('Pragma: no-cache');
 		header('Pragma: no-cache');
 
 
 		$user = isset($_GET['user']) ? $_GET['user'] : '';
 		$user = isset($_GET['user']) ? $_GET['user'] : '';
-		if (ctype_alnum($user)) {
+		if (FreshRSS_user_Controller::checkUsername($user)) {
 			try {
 			try {
 				$salt = FreshRSS_Context::$system_conf->salt;
 				$salt = FreshRSS_Context::$system_conf->salt;
 				$conf = get_user_configuration($user);
 				$conf = get_user_configuration($user);

+ 12 - 2
app/Controllers/userController.php

@@ -34,6 +34,16 @@ class FreshRSS_user_Controller extends Minz_ActionController {
 		return $passwordHash == '' ? '' : $passwordHash;
 		return $passwordHash == '' ? '' : $passwordHash;
 	}
 	}
 
 
+	/**
+	 * The username is also used as folder name, file name, and part of SQL table name.
+	 * '_' is a reserved internal username.
+	 */
+	const USERNAME_PATTERN = '[0-9a-zA-Z]|[0-9a-zA-Z_]{2,38}';
+
+	public static function checkUsername($username) {
+		return preg_match('/^' . self::USERNAME_PATTERN . '$/', $username) === 1;
+	}
+
 	/**
 	/**
 	 * This action displays the user profile page.
 	 * This action displays the user profile page.
 	 */
 	 */
@@ -104,7 +114,7 @@ class FreshRSS_user_Controller extends Minz_ActionController {
 			$userConfig = array();
 			$userConfig = array();
 		}
 		}
 
 
-		$ok = ($new_user_name != '') && ctype_alnum($new_user_name);
+		$ok = self::checkUsername($new_user_name);
 
 
 		if ($ok) {
 		if ($ok) {
 			$languages = Minz_Translate::availableLanguages();
 			$languages = Minz_Translate::availableLanguages();
@@ -187,7 +197,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');
 
 
-		$ok = ctype_alnum($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

+ 2 - 2
app/Models/Auth.php

@@ -182,7 +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) {
-		if (!ctype_alnum($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:' .
@@ -211,7 +211,7 @@ class FreshRSS_FormAuth {
 			// Token has expired (> 1 month) or does not exist.
 			// Token has expired (> 1 month) or does not exist.
 			// TODO: 1 month -> use a configuration instead
 			// TODO: 1 month -> use a configuration instead
 			@unlink($token_file);
 			@unlink($token_file);
-			return array(); 	
+			return array();
 		}
 		}
 
 
 		$credentials = @file_get_contents($token_file);
 		$credentials = @file_get_contents($token_file);

+ 1 - 1
app/Models/Feed.php

@@ -442,7 +442,7 @@ class FreshRSS_Feed extends Minz_Model {
 				file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
 				file_put_contents(USERS_PATH . '/_/log_pshb.txt', date('c') . "\t" . $text . "\n", FILE_APPEND);
 			}
 			}
 			$currentUser = Minz_Session::param('currentUser');
 			$currentUser = Minz_Session::param('currentUser');
-			if (ctype_alnum($currentUser) && !file_exists($path . '/' . $currentUser . '.txt')) {
+			if (FreshRSS_user_Controller::checkUsername($currentUser) && !file_exists($path . '/' . $currentUser . '.txt')) {
 				touch($path . '/' . $currentUser . '.txt');
 				touch($path . '/' . $currentUser . '.txt');
 			}
 			}
 		}
 		}

+ 1 - 1
app/Models/UserDAO.php

@@ -85,7 +85,7 @@ class FreshRSS_UserDAO extends Minz_ModelPdo {
 	}
 	}
 
 
 	public static function touch($username = '') {
 	public static function touch($username = '') {
-		if (($username == '') || (!ctype_alnum($username))) {
+		if (!FreshRSS_user_Controller::checkUsername($username)) {
 			$username = Minz_Session::param('currentUser', '_');
 			$username = Minz_Session::param('currentUser', '_');
 		}
 		}
 		return touch(join_path(DATA_PATH , 'users', $username, 'config.php'));
 		return touch(join_path(DATA_PATH , 'users', $username, 'config.php'));

+ 1 - 1
app/install.php

@@ -553,7 +553,7 @@ function printStep2() {
 		<div class="form-group">
 		<div class="form-group">
 			<label class="group-name" for="default_user"><?php echo _t('install.default_user'); ?></label>
 			<label class="group-name" for="default_user"><?php echo _t('install.default_user'); ?></label>
 			<div class="group-controls">
 			<div class="group-controls">
-				<input type="text" id="default_user" name="default_user" required="required" size="16" maxlength="16" pattern="[0-9a-zA-Z]{1,16}" value="<?php echo isset($_SESSION['default_user']) ? $_SESSION['default_user'] : ''; ?>" placeholder="<?php echo httpAuthUser() == '' ? 'alice' : httpAuthUser(); ?>" tabindex="3" />
+				<input type="text" id="default_user" name="default_user" required="required" size="16" pattern="<?php echo FreshRSS_user_Controller::USERNAME_PATTERN; ?>" value="<?php echo isset($_SESSION['default_user']) ? $_SESSION['default_user'] : ''; ?>" placeholder="<?php echo httpAuthUser() == '' ? 'alice' : httpAuthUser(); ?>" tabindex="3" />
 			</div>
 			</div>
 		</div>
 		</div>
 
 

+ 1 - 1
app/views/auth/formLogin.phtml

@@ -9,7 +9,7 @@
 		<input type="hidden" name="_csrf" value="<?php echo FreshRSS_Auth::csrfToken(); ?>" />
 		<input type="hidden" name="_csrf" value="<?php echo FreshRSS_Auth::csrfToken(); ?>" />
 		<div>
 		<div>
 			<label for="username"><?php echo _t('gen.auth.username'); ?></label>
 			<label for="username"><?php echo _t('gen.auth.username'); ?></label>
-			<input type="text" id="username" name="username" size="16" required="required" maxlength="16" pattern="[0-9a-zA-Z]{1,16}" autofocus="autofocus" />
+			<input type="text" id="username" name="username" size="16" required="required" pattern="<?php echo FreshRSS_user_Controller::USERNAME_PATTERN; ?>" autofocus="autofocus" />
 		</div>
 		</div>
 		<div>
 		<div>
 			<label for="passwordPlain"><?php echo _t('gen.auth.password'); ?></label>
 			<label for="passwordPlain"><?php echo _t('gen.auth.password'); ?></label>

+ 1 - 1
app/views/auth/register.phtml

@@ -5,7 +5,7 @@
 		<input type="hidden" name="_csrf" value="<?php echo FreshRSS_Auth::csrfToken(); ?>" />
 		<input type="hidden" name="_csrf" value="<?php echo FreshRSS_Auth::csrfToken(); ?>" />
 		<div>
 		<div>
 			<label class="group-name" for="new_user_name"><?php echo _t('gen.auth.username'), '<br />', _i('help'), ' ', _t('gen.auth.username.format'); ?></label>
 			<label class="group-name" for="new_user_name"><?php echo _t('gen.auth.username'), '<br />', _i('help'), ' ', _t('gen.auth.username.format'); ?></label>
-			<input id="new_user_name" name="new_user_name" type="text" size="16" required="required" maxlength="16" autocomplete="off" pattern="[0-9a-zA-Z]{1,16}" />
+			<input id="new_user_name" name="new_user_name" type="text" size="16" required="required" autocomplete="off" pattern="<?php echo FreshRSS_user_Controller::USERNAME_PATTERN; ?>" />
 		</div>
 		</div>
 
 
 		<div>
 		<div>

+ 1 - 1
app/views/user/manage.phtml

@@ -22,7 +22,7 @@
 		<div class="form-group">
 		<div class="form-group">
 			<label class="group-name" for="new_user_name"><?php echo _t('admin.user.username'); ?></label>
 			<label class="group-name" for="new_user_name"><?php echo _t('admin.user.username'); ?></label>
 			<div class="group-controls">
 			<div class="group-controls">
-				<input id="new_user_name" name="new_user_name" type="text" size="16" required="required" maxlength="16" autocomplete="off" pattern="[0-9a-zA-Z]{1,16}" placeholder="demo" />
+				<input id="new_user_name" name="new_user_name" type="text" size="16" required="required" autocomplete="off" pattern="<?php echo FreshRSS_user_Controller::USERNAME_PATTERN; ?>" placeholder="demo" />
 			</div>
 			</div>
 		</div>
 		</div>
 
 

+ 1 - 1
cli/_cli.php

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

+ 1 - 1
cli/create-user.php

@@ -17,7 +17,7 @@ if (empty($options['user'])) {
 		" --language en --email user@example.net --token 'longRandomString --no-default-feeds' )");
 		" --language en --email user@example.net --token 'longRandomString --no-default-feeds' )");
 }
 }
 $username = $options['user'];
 $username = $options['user'];
-if (!ctype_alnum($username)) {
+if (!FreshRSS_user_Controller::checkUsername($username)) {
 	fail('FreshRSS error: invalid username “' . $username . '”');
 	fail('FreshRSS error: invalid username “' . $username . '”');
 }
 }
 
 

+ 1 - 1
cli/delete-user.php

@@ -10,7 +10,7 @@ if (empty($options['user'])) {
 	fail('Usage: ' . basename(__FILE__) . " --user username");
 	fail('Usage: ' . basename(__FILE__) . " --user username");
 }
 }
 $username = $options['user'];
 $username = $options['user'];
-if (!ctype_alnum($username)) {
+if (!FreshRSS_user_Controller::checkUsername($username)) {
 	fail('FreshRSS error: invalid username “' . $username . '”');
 	fail('FreshRSS error: invalid username “' . $username . '”');
 }
 }
 
 

+ 1 - 1
cli/do-install.php

@@ -48,7 +48,7 @@ if ($requirements['all'] !== 'ok') {
 	fail($message);
 	fail($message);
 }
 }
 
 
-if (!ctype_alnum($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']);
 }
 }
 
 

+ 1 - 1
cli/reconfigure.php

@@ -45,7 +45,7 @@ foreach ($dBparams as $dBparam) {
 }
 }
 $config->db = $db;
 $config->db = $db;
 
 
-if (!ctype_alnum($config->default_user)) {
+if (!FreshRSS_user_Controller::checkUsername($config->default_user)) {
 	fail('FreshRSS invalid default username (must be ASCII alphanumeric): ' . $config->default_user);
 	fail('FreshRSS invalid default username (must be ASCII alphanumeric): ' . $config->default_user);
 }
 }
 
 

+ 1 - 1
lib/lib_rss.php

@@ -285,7 +285,7 @@ function uSecString() {
 }
 }
 
 
 function invalidateHttpCache($username = '') {
 function invalidateHttpCache($username = '') {
-	if (($username == '') || (!ctype_alnum($username))) {
+	if (!FreshRSS_user_Controller::checkUsername($username)) {
 		Minz_Session::_param('touch', uTimeString());
 		Minz_Session::_param('touch', uTimeString());
 		$username = Minz_Session::param('currentUser', '_');
 		$username = Minz_Session::param('currentUser', '_');
 	}
 	}

+ 1 - 1
p/api/greader.php

@@ -152,7 +152,7 @@ function authorizationToUser() {
 		$headerAuthX = explode('/', $headerAuth, 2);
 		$headerAuthX = explode('/', $headerAuth, 2);
 		if (count($headerAuthX) === 2) {
 		if (count($headerAuthX) === 2) {
 			$user = $headerAuthX[0];
 			$user = $headerAuthX[0];
-			if (ctype_alnum($user)) {
+			if (FreshRSS_user_Controller::checkUsername($user)) {
 				FreshRSS_Context::$user_conf = get_user_configuration($user);
 				FreshRSS_Context::$user_conf = get_user_configuration($user);
 				if (FreshRSS_Context::$user_conf == null) {
 				if (FreshRSS_Context::$user_conf == null) {
 					Minz_Log::warning('Invalid API user ' . $user . ': configuration cannot be found.');
 					Minz_Log::warning('Invalid API user ' . $user . ': configuration cannot be found.');