Improved code to generate SFTP usernames

Fixes edge case where specific server names could cause daemon errors
due to an invalid SFTP username being created by the panel.
This commit is contained in:
Dane Everitt 2017-01-13 22:22:25 -05:00
parent 31864de3f4
commit b71604566e
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
2 changed files with 17 additions and 4 deletions

View file

@ -11,6 +11,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
### Fixed ### Fixed
* Bug causing error logs to be spammed if someone timed out on an ajax based page. * Bug causing error logs to be spammed if someone timed out on an ajax based page.
* Fixes edge case where specific server names could cause daemon errors due to an invalid SFTP username being created by the panel.
### Changed ### Changed
* Admin API and base routes for user management now define the fields that should be passed to repositories rather than passing all fields. * Admin API and base routes for user management now define the fields that should be passed to repositories rather than passing all fields.

View file

@ -52,14 +52,26 @@ class ServerRepository
* format: mumble_67c7a4b0. * format: mumble_67c7a4b0.
* *
* @param string $name * @param string $name
* @param string $uuid * @param string $identifier
* @return string * @return string
*/ */
protected function generateSFTPUsername($name, $uuid = null) protected function generateSFTPUsername($name, $identifier = null)
{ {
$uuid = is_null($uuid) ? str_random(8) : $uuid; if (is_null($identifier) || ! ctype_alnum($identifier)) {
$unique = str_random(8);
} else {
if (strlen($identifier) < 8) {
$unique = $identifier . str_random((8 - strlen($identifier)));
} else {
$unique = substr($identifier, 0, 8);
}
}
return strtolower(substr(preg_replace('/\s+/', '', $name), 0, 6) . '_' . $uuid); // Filter the Server Name
$name = trim(preg_replace('/[^\w]+/', '', $name), '_');
$name = (strlen($name) < 1) ? str_random(6) : $name;
return strtolower(substr($name, 0, 6) . '_' . $unique);
} }
/** /**