diff --git a/.env.example b/.env.example index 38134e54..97db46b5 100644 --- a/.env.example +++ b/.env.example @@ -37,14 +37,16 @@ PWD_METHOD = SALTED2MD5 # Salt # Change it to any random string to secure your passwords & tokens. # -SALT = change-it+to*what)you^like +# You can run [php artisan salt:random] to generate a new salt. +# +SALT = 2c5ca184f017a9a1ffbd198ef69b0c0e # App Key should be setted to any random, **32 character** string, # otherwise all the encrypted strings will not be safe. # # You can run [php artisan key:generate] to generate a new key. # -APP_KEY=NkccevHHNRoRBTdGZ4osmKnwdebrjCYw +APP_KEY = base64:gkb/zouNF6UOSfnr/o+izVMS57WQS3+62YqZBuDyBhU= # Mail Configurations # Leave MAIL_HOST empty to disable password resetting diff --git a/app/Console/Commands/KeyRandomCommand.php b/app/Console/Commands/KeyRandomCommand.php new file mode 100644 index 00000000..c88449e3 --- /dev/null +++ b/app/Console/Commands/KeyRandomCommand.php @@ -0,0 +1,74 @@ +generateRandomKey(); + + if ($this->option('show')) { + return $this->line(''.$key.''); + } + + // Next, we will replace the application key in the environment file so it is + // automatically setup for this developer. This key gets generated using a + // secure random byte generator and is later base64 encoded for storage. + $this->setKeyInEnvironmentFile($key); + + $this->laravel['config']['app.key'] = $key; + + $this->info("Application key [$key] set successfully."); + } + + /** + * Set the application key in the environment file. + * + * @param string $key + * @return void + */ + protected function setKeyInEnvironmentFile($key) + { + // Unlike Illuminate\Foundation\Console\KeyGenerateCommand, + // I add soame spaces to the replace pattern. + file_put_contents($this->laravel->environmentFilePath(), str_replace( + 'APP_KEY = '.$this->laravel['config']['app.key'], + 'APP_KEY = '.$key, + file_get_contents($this->laravel->environmentFilePath()) + )); + } + + /** + * Generate a random key for the application. + * + * @return string + */ + protected function generateRandomKey() + { + return 'base64:'.base64_encode(random_bytes( + $this->laravel['config']['app.cipher'] == 'AES-128-CBC' ? 16 : 32 + )); + } +} diff --git a/app/Console/Commands/SaltRandomCommand.php b/app/Console/Commands/SaltRandomCommand.php new file mode 100644 index 00000000..9e4cfd93 --- /dev/null +++ b/app/Console/Commands/SaltRandomCommand.php @@ -0,0 +1,70 @@ +generateRandomSalt(); + + if ($this->option('show')) { + return $this->line(''.$salt.''); + } + + // Next, we will replace the application salt in the environment file so it is + // automatically setup for this developer. This salt gets generated using a + // secure random byte generator and is later base64 encoded for storage. + $this->setKeyInEnvironmentFile($salt); + + $this->laravel['config']['secure.salt'] = $salt; + + $this->info("Application salt [$salt] set successfully."); + } + + /** + * Set the application salt in the environment file. + * + * @param string $salt + * @return void + */ + protected function setKeyInEnvironmentFile($salt) + { + file_put_contents($this->laravel->environmentFilePath(), str_replace( + 'SALT = '.$this->laravel['config']['secure.salt'], + 'SALT = '.$salt, + file_get_contents($this->laravel->environmentFilePath()) + )); + } + + /** + * Generate a random salt for the application. + * + * @return string + */ + protected function generateRandomSalt() + { + return bin2hex(random_bytes(16)); + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 5e4a31b2..96922bbc 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -14,6 +14,8 @@ class Kernel extends ConsoleKernel */ protected $commands = [ Commands\Inspire::class, + Commands\KeyRandomCommand::class, + Commands\SaltRandomCommand::class, ]; /** diff --git a/app/Http/Controllers/SetupController.php b/app/Http/Controllers/SetupController.php index 053e1c89..542dfbf7 100644 --- a/app/Http/Controllers/SetupController.php +++ b/app/Http/Controllers/SetupController.php @@ -33,14 +33,6 @@ class SetupController extends Controller } else { $config = config('database.connections.mysql'); - // generate new APP_KEY - if (is_writable(app()->environmentFile())) { - Artisan::call('key:generate'); - Log::info("[SetupWizard] Application key set successfully.", ['key' => config('app.key')]); - } else { - Log::warning("[SetupWizard] Failed to set application key. No write permission."); - } - return view('setup.wizard.welcome')->with('server', "{$config['username']}@{$config['host']}"); } } @@ -66,6 +58,21 @@ class SetupController extends Controller 'site_name' => 'required' ]); + if (isset($_POST['generate_random'])) { + // generate new APP_KEY & SALT randomly + if (is_writable(app()->environmentFile())) { + Artisan::call('key:random'); + Artisan::call('salt:random'); + + Log::info("[SetupWizard] Random application key & salt set successfully.", [ + 'key' => config('app.key'), + 'salt' => config('secure.salt') + ]); + } else { + Log::warning("[SetupWizard] Failed to set application key. No write permission."); + } + } + // create tables Artisan::call('migrate', ['--force' => true]); Log::info("[SetupWizard] Tables migrated."); diff --git a/resources/lang/en/setup.yml b/resources/lang/en/setup.yml index 17c20602..13318f0f 100644 --- a/resources/lang/en/setup.yml +++ b/resources/lang/en/setup.yml @@ -51,6 +51,8 @@ wizard: confirm-pwd: Confirm password site-name: Site name site-name-notice: This will be shown at title bar and homepage. + secure: Security + secure-notice: Generate random APP_KEY and SALT to make your site secured. finish: title: Installation complete diff --git a/resources/lang/zh_CN/setup.yml b/resources/lang/zh_CN/setup.yml index 24b90515..c1d2d5fa 100644 --- a/resources/lang/zh_CN/setup.yml +++ b/resources/lang/zh_CN/setup.yml @@ -51,6 +51,8 @@ wizard: confirm-pwd: 重复密码 site-name: 站点名称 site-name-notice: 将会显示在首页以及标题栏 + secure: 站点安全 + secure-notice: 生成随机 APP_KEY 和 SALT 以保证安全 finish: title: 安装成功! diff --git a/resources/views/setup/wizard/info.tpl b/resources/views/setup/wizard/info.tpl index 087ae040..373a1a37 100644 --- a/resources/views/setup/wizard/info.tpl +++ b/resources/views/setup/wizard/info.tpl @@ -36,6 +36,15 @@

{{ trans('setup.wizard.info.site-name-notice') }}

+ + + + + + @if (count($errors) > 0)