Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Prevents booting providers when running env:decrypt #44654

Merged
merged 14 commits into from
Oct 20, 2022
36 changes: 24 additions & 12 deletions src/Illuminate/Foundation/Console/EnvironmentDecryptCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class EnvironmentDecryptCommand extends Command
{--cipher= : The encryption cipher}
{--env= : The environment to be decrypted}
{--force : Overwrite the existing environment file}
{--filename= : Where to write the decrypted file contents}';
{--path= : Path to write the decrypted file}
{--filename= : Filename of the decrypted file}';

/**
* The name of the console command.
Expand Down Expand Up @@ -82,17 +83,13 @@ public function handle()

$key = $this->parseKey($key);

$environmentFile = $this->option('env')
$encryptedFile = ($this->option('env')
? base_path('.env').'.'.$this->option('env')
: $this->laravel->environmentFilePath();
: $this->laravel->environmentFilePath()).'.encrypted';

$encryptedFile = $environmentFile.'.encrypted';
$outputFile = $this->outputFilePath();

$filename = $this->option('filename')
? base_path($this->option('filename'))
: $environmentFile;

if (Str::endsWith($filename, '.encrypted')) {
if (Str::endsWith($outputFile, '.encrypted')) {
$this->components->error('Invalid filename.');

return Command::FAILURE;
Expand All @@ -104,7 +101,7 @@ public function handle()
return Command::FAILURE;
}

if ($this->files->exists($environmentFile) && ! $this->option('force')) {
if ($this->files->exists($outputFile) && ! $this->option('force')) {
$this->components->error('Environment file already exists.');

return Command::FAILURE;
Expand All @@ -114,7 +111,7 @@ public function handle()
$encrypter = new Encrypter($key, $cipher);

$this->files->put(
$filename,
$outputFile,
$encrypter->decrypt($this->files->get($encryptedFile))
);
} catch (Exception $e) {
Expand All @@ -125,7 +122,7 @@ public function handle()

$this->components->info('Environment successfully decrypted.');

$this->components->twoColumnDetail('Decrypted file', $filename);
$this->components->twoColumnDetail('Decrypted file', $outputFile);

$this->newLine();
}
Expand All @@ -144,4 +141,19 @@ protected function parseKey(string $key)

return $key;
}

/**
* Get the output file path that should be used for the command.
*
* @return string
*/
protected function outputFilePath()
{
$path = Str::finish($this->option('path') ?: base_path(), DIRECTORY_SEPARATOR);

$outputFile = $this->option('filename') ?: ('.env'.($this->option('env') ? '.'.$this->option('env') : ''));
$outputFile = ltrim($outputFile, DIRECTORY_SEPARATOR);

return $path.$outputFile;
}
}
22 changes: 22 additions & 0 deletions src/Illuminate/Foundation/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ public function handle($input, $output = null)
$this->commandStartedAt = Carbon::now();

try {
if ($input->getFirstArgument() === 'env:decrypt') {
$this->bootstrapWithoutBootingProviders();
}

$this->bootstrap();

return $this->getArtisan()->run($input, $output);
Expand Down Expand Up @@ -323,6 +327,10 @@ public function registerCommand($command)
*/
public function call($command, array $parameters = [], $outputBuffer = null)
{
if ($command === 'env:decrypt') {
$this->bootstrapWithoutBootingProviders();
}

$this->bootstrap();

return $this->getArtisan()->call($command, $parameters, $outputBuffer);
Expand Down Expand Up @@ -384,6 +392,20 @@ public function bootstrap()
}
}

/**
* Bootstrap the application without booting service providers.
*
* @return void
*/
public function bootstrapWithoutBootingProviders()
{
$this->app->bootstrapWith(
collect($this->bootstrappers())->reject(function ($bootstrapper) {
return $bootstrapper === \Illuminate\Foundation\Bootstrap\BootProviders::class;
})->all()
);
}

/**
* Get the Artisan application instance.
*
Expand Down
46 changes: 46 additions & 0 deletions tests/Integration/Console/EnvironmentDecryptCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,52 @@ public function testItWritesTheEnvironmentFileCustomFilename()
->with(base_path('.env'), 'APP_NAME="Laravel Two"');
}

public function testItWritesTheEnvironmentFileCustomPath()
{
$this->filesystem->shouldReceive('exists')
->once()
->andReturn(true)
->shouldReceive('exists')
->once()
->andReturn(false)
->shouldReceive('get')
->once()
->andReturn(
(new Encrypter('abcdefghijklmnopabcdefghijklmnop', 'AES-256-CBC'))
->encrypt('APP_NAME="Laravel Two"')
);

$this->artisan('env:decrypt', ['--env' => 'production', '--key' => 'abcdefghijklmnopabcdefghijklmnop', '--path' => '/tmp'])
->expectsOutputToContain('Environment successfully decrypted.')
->assertExitCode(0);

$this->filesystem->shouldHaveReceived('put')
->with('/tmp'.DIRECTORY_SEPARATOR.'.env.production', 'APP_NAME="Laravel Two"');
}

public function testItWritesTheEnvironmentFileCustomPathAndFilename()
{
$this->filesystem->shouldReceive('exists')
->once()
->andReturn(true)
->shouldReceive('exists')
->once()
->andReturn(false)
->shouldReceive('get')
->once()
->andReturn(
(new Encrypter('abcdefghijklmnopabcdefghijklmnop', 'AES-256-CBC'))
->encrypt('APP_NAME="Laravel Two"')
);

$this->artisan('env:decrypt', ['--env' => 'production', '--key' => 'abcdefghijklmnopabcdefghijklmnop', '--filename' => '.env', '--path' => '/tmp'])
->expectsOutputToContain('Environment successfully decrypted.')
->assertExitCode(0);

$this->filesystem->shouldHaveReceived('put')
->with('/tmp'.DIRECTORY_SEPARATOR.'.env', 'APP_NAME="Laravel Two"');
}

public function testItCannotOverwriteEncryptedFiles()
{
$this->artisan('env:decrypt', ['--env' => 'production', '--key' => 'abcdefghijklmnop', '--filename' => '.env.production.encrypted'])
Expand Down