From 420730ba1f25cf74ff940caa66864838a51e61d2 Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 6 Sep 2025 16:47:54 -0400 Subject: [PATCH] Replace `str_random` with `Str::random` (#1676) Co-authored-by: RMartinOscar <40749467+RMartinOscar@users.noreply.github.com> --- .../Admin/Resources/ApiKeyResource/Pages/CreateApiKey.php | 3 ++- .../Resources/DatabaseResource/Pages/ListDatabases.php | 3 ++- app/Helpers/Utilities.php | 3 ++- app/Services/Api/KeyCreationService.php | 3 ++- app/Services/Databases/DatabaseManagementService.php | 3 ++- app/Services/Users/ToggleTwoFactorService.php | 3 ++- app/Services/Users/UserCreationService.php | 2 +- .../2017_09_23_173628_RemoveDaemonSecretFromServersTable.php | 3 ++- ...2017_09_23_185022_RemoveDaemonSecretFromSubusersTable.php | 3 ++- ...0_02_202007_ChangeToABetterUniqueServiceConfiguration.php | 3 ++- .../2017_11_19_122708_MigratePubPrivFormatToSingleKey.php | 5 +++-- 11 files changed, 22 insertions(+), 12 deletions(-) diff --git a/app/Filament/Admin/Resources/ApiKeyResource/Pages/CreateApiKey.php b/app/Filament/Admin/Resources/ApiKeyResource/Pages/CreateApiKey.php index 351c577a2..a1411d081 100644 --- a/app/Filament/Admin/Resources/ApiKeyResource/Pages/CreateApiKey.php +++ b/app/Filament/Admin/Resources/ApiKeyResource/Pages/CreateApiKey.php @@ -10,6 +10,7 @@ use Filament\Actions\Action; use Filament\Actions\ActionGroup; use Filament\Resources\Pages\CreateRecord; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Str; class CreateApiKey extends CreateRecord { @@ -36,7 +37,7 @@ class CreateApiKey extends CreateRecord protected function handleRecordCreation(array $data): Model { $data['identifier'] = ApiKey::generateTokenIdentifier(ApiKey::TYPE_APPLICATION); - $data['token'] = str_random(ApiKey::KEY_LENGTH); + $data['token'] = Str::random(ApiKey::KEY_LENGTH); $data['user_id'] = auth()->user()->id; $data['key_type'] = ApiKey::TYPE_APPLICATION; diff --git a/app/Filament/Server/Resources/DatabaseResource/Pages/ListDatabases.php b/app/Filament/Server/Resources/DatabaseResource/Pages/ListDatabases.php index 5440a71fd..5e71635f6 100644 --- a/app/Filament/Server/Resources/DatabaseResource/Pages/ListDatabases.php +++ b/app/Filament/Server/Resources/DatabaseResource/Pages/ListDatabases.php @@ -17,6 +17,7 @@ use Filament\Forms\Components\Select; use Filament\Forms\Components\TextInput; use Filament\Resources\Pages\ListRecords; use Filament\Support\Enums\IconSize; +use Illuminate\Support\Str; class ListDatabases extends ListRecords { @@ -63,7 +64,7 @@ class ListDatabases extends ListRecords ]) ->action(function ($data, DatabaseManagementService $service) use ($server) { if (empty($data['database'])) { - $data['database'] = str_random(12); + $data['database'] = Str::random(12); } $data['database'] = 's'. $server->id . '_' . $data['database']; diff --git a/app/Helpers/Utilities.php b/app/Helpers/Utilities.php index c810788cc..b99c5e55a 100644 --- a/app/Helpers/Utilities.php +++ b/app/Helpers/Utilities.php @@ -4,6 +4,7 @@ namespace App\Helpers; use Carbon\Carbon; use Cron\CronExpression; +use Illuminate\Support\Str; use Illuminate\Support\ViewErrorBag; class Utilities @@ -14,7 +15,7 @@ class Utilities */ public static function randomStringWithSpecialCharacters(int $length = 16): string { - $string = str_random($length); + $string = Str::random($length); // Given a random string of characters, randomly loop through the characters and replace some // with special characters to avoid issues with MySQL password requirements on some servers. try { diff --git a/app/Services/Api/KeyCreationService.php b/app/Services/Api/KeyCreationService.php index 878d00f83..492661a03 100644 --- a/app/Services/Api/KeyCreationService.php +++ b/app/Services/Api/KeyCreationService.php @@ -3,6 +3,7 @@ namespace App\Services\Api; use App\Models\ApiKey; +use Illuminate\Support\Str; class KeyCreationService { @@ -33,7 +34,7 @@ class KeyCreationService $data = array_merge($data, [ 'key_type' => $this->keyType, 'identifier' => ApiKey::generateTokenIdentifier($this->keyType), - 'token' => str_random(ApiKey::KEY_LENGTH), + 'token' => Str::random(ApiKey::KEY_LENGTH), ]); if ($this->keyType !== ApiKey::TYPE_APPLICATION) { diff --git a/app/Services/Databases/DatabaseManagementService.php b/app/Services/Databases/DatabaseManagementService.php index b82c06934..8506b738c 100644 --- a/app/Services/Databases/DatabaseManagementService.php +++ b/app/Services/Databases/DatabaseManagementService.php @@ -10,6 +10,7 @@ use Illuminate\Database\ConnectionInterface; use App\Exceptions\Repository\DuplicateDatabaseNameException; use App\Exceptions\Service\Database\TooManyDatabasesException; use App\Exceptions\Service\Database\DatabaseClientFeatureNotEnabledException; +use Illuminate\Support\Str; class DatabaseManagementService { @@ -85,7 +86,7 @@ class DatabaseManagementService $data = array_merge($data, [ 'server_id' => $server->id, - 'username' => sprintf('u%d_%s', $server->id, str_random(10)), + 'username' => sprintf('u%d_%s', $server->id, Str::random(10)), 'password' => Utilities::randomStringWithSpecialCharacters(24), ]); diff --git a/app/Services/Users/ToggleTwoFactorService.php b/app/Services/Users/ToggleTwoFactorService.php index 434e5d3ef..26df0e7e8 100644 --- a/app/Services/Users/ToggleTwoFactorService.php +++ b/app/Services/Users/ToggleTwoFactorService.php @@ -3,6 +3,7 @@ namespace App\Services\Users; use App\Models\User; +use Illuminate\Support\Str; use PragmaRX\Google2FA\Google2FA; use Illuminate\Database\ConnectionInterface; use App\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid; @@ -48,7 +49,7 @@ class ToggleTwoFactorService if ((!$toggleState && !$user->use_totp) || $toggleState) { $user->recoveryTokens()->delete(); for ($i = 0; $i < 10; $i++) { - $token = str_random(10); + $token = Str::random(10); $user->recoveryTokens()->forceCreate([ 'token' => password_hash($token, PASSWORD_DEFAULT), ]); diff --git a/app/Services/Users/UserCreationService.php b/app/Services/Users/UserCreationService.php index 644dac7f5..eb4789b1f 100644 --- a/app/Services/Users/UserCreationService.php +++ b/app/Services/Users/UserCreationService.php @@ -37,7 +37,7 @@ class UserCreationService $this->connection->beginTransaction(); if (empty($data['password'])) { $generateResetToken = true; - $data['password'] = $this->hasher->make(str_random(30)); + $data['password'] = $this->hasher->make(Str::random(30)); } $isRootAdmin = array_key_exists('root_admin', $data) && $data['root_admin']; diff --git a/database/migrations/2017_09_23_173628_RemoveDaemonSecretFromServersTable.php b/database/migrations/2017_09_23_173628_RemoveDaemonSecretFromServersTable.php index a26c13c9d..db593a46c 100644 --- a/database/migrations/2017_09_23_173628_RemoveDaemonSecretFromServersTable.php +++ b/database/migrations/2017_09_23_173628_RemoveDaemonSecretFromServersTable.php @@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; use App\Contracts\Repository\DaemonKeyRepositoryInterface; +use Illuminate\Support\Str; return new class extends Migration { @@ -21,7 +22,7 @@ return new class extends Migration $inserts[] = [ 'user_id' => $server->owner_id, 'server_id' => $server->id, - 'secret' => DaemonKeyRepositoryInterface::INTERNAL_KEY_IDENTIFIER . str_random(40), + 'secret' => DaemonKeyRepositoryInterface::INTERNAL_KEY_IDENTIFIER . Str::random(40), 'expires_at' => Carbon::now()->addMinutes(config('panel.api.key_expire_time', 720))->toDateTimeString(), 'created_at' => Carbon::now()->toDateTimeString(), 'updated_at' => Carbon::now()->toDateTimeString(), diff --git a/database/migrations/2017_09_23_185022_RemoveDaemonSecretFromSubusersTable.php b/database/migrations/2017_09_23_185022_RemoveDaemonSecretFromSubusersTable.php index 6216dd5d8..979c31ee5 100644 --- a/database/migrations/2017_09_23_185022_RemoveDaemonSecretFromSubusersTable.php +++ b/database/migrations/2017_09_23_185022_RemoveDaemonSecretFromSubusersTable.php @@ -5,6 +5,7 @@ use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; use App\Contracts\Repository\DaemonKeyRepositoryInterface; +use Illuminate\Support\Str; return new class extends Migration { @@ -19,7 +20,7 @@ return new class extends Migration $inserts[] = [ 'user_id' => $subuser->user_id, 'server_id' => $subuser->server_id, - 'secret' => DaemonKeyRepositoryInterface::INTERNAL_KEY_IDENTIFIER . str_random(40), + 'secret' => DaemonKeyRepositoryInterface::INTERNAL_KEY_IDENTIFIER . Str::random(40), 'expires_at' => Carbon::now()->addMinutes(config('panel.api.key_expire_time', 720))->toDateTimeString(), 'created_at' => Carbon::now()->toDateTimeString(), 'updated_at' => Carbon::now()->toDateTimeString(), diff --git a/database/migrations/2017_10_02_202007_ChangeToABetterUniqueServiceConfiguration.php b/database/migrations/2017_10_02_202007_ChangeToABetterUniqueServiceConfiguration.php index 1a2497cd3..31d073a4c 100644 --- a/database/migrations/2017_10_02_202007_ChangeToABetterUniqueServiceConfiguration.php +++ b/database/migrations/2017_10_02_202007_ChangeToABetterUniqueServiceConfiguration.php @@ -1,5 +1,6 @@ select(['id', 'tag'])->get()->each(function ($option) { DB::table('service_options')->where('id', $option->id)->update([ - 'tag' => str_random(10), + 'tag' => Str::random(10), ]); }); }); diff --git a/database/migrations/2017_11_19_122708_MigratePubPrivFormatToSingleKey.php b/database/migrations/2017_11_19_122708_MigratePubPrivFormatToSingleKey.php index 82c25e65e..e5582ba54 100644 --- a/database/migrations/2017_11_19_122708_MigratePubPrivFormatToSingleKey.php +++ b/database/migrations/2017_11_19_122708_MigratePubPrivFormatToSingleKey.php @@ -6,6 +6,7 @@ use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; use Illuminate\Contracts\Encryption\DecryptException; +use Illuminate\Support\Str; return new class extends Migration { @@ -19,7 +20,7 @@ return new class extends Migration try { $decrypted = Crypt::decrypt($item->secret); } catch (DecryptException $exception) { - $decrypted = str_random(32); + $decrypted = Str::random(32); } finally { DB::table('api_keys')->where('id', $item->id)->update([ 'secret' => $decrypted, @@ -66,7 +67,7 @@ return new class extends Migration DB::transaction(function () { DB::table('api_keys')->get()->each(function ($item) { DB::table('api_keys')->where('id', $item->id)->update([ - 'public' => str_random(16), + 'public' => Str::random(16), 'secret' => Crypt::encrypt($item->secret), ]); });