From b71604566e254d8bf4aa4dcfa823ec087068b21a Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Fri, 13 Jan 2017 22:22:25 -0500 Subject: [PATCH] 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. --- CHANGELOG.md | 1 + app/Repositories/ServerRepository.php | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd10b6458..d28754c9d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ This project follows [Semantic Versioning](http://semver.org) guidelines. ### Fixed * 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 * Admin API and base routes for user management now define the fields that should be passed to repositories rather than passing all fields. diff --git a/app/Repositories/ServerRepository.php b/app/Repositories/ServerRepository.php index deb6a1b75..702ae15dc 100644 --- a/app/Repositories/ServerRepository.php +++ b/app/Repositories/ServerRepository.php @@ -52,14 +52,26 @@ class ServerRepository * format: mumble_67c7a4b0. * * @param string $name - * @param string $uuid + * @param string $identifier * @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); } /**