Skip to content

[FrameworkBundle] Derivate kernel.secret from the decryption secret when its env var is not defined #56985

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

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add support for setting `headers` with `Symfony\Bundle\FrameworkBundle\Controller\TemplateController`
* Derivate `kernel.secret` from the decryption secret when its env var is not defined

7.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ public function load(array $configs, ContainerBuilder $container): void
$this->registerDebugConfiguration($config['php_errors'], $container, $loader);
$this->registerRouterConfiguration($config['router'], $container, $loader, $config['enabled_locales']);
$this->registerPropertyAccessConfiguration($config['property_access'], $container, $loader);
$this->registerSecretsConfiguration($config['secrets'], $container, $loader);
$this->registerSecretsConfiguration($config['secrets'], $container, $loader, $config['secret'] ?? null);

$container->getDefinition('exception_listener')->replaceArgument(3, $config['exceptions']);

Expand Down Expand Up @@ -1755,7 +1755,7 @@ private function registerPropertyAccessConfiguration(array $config, ContainerBui
;
}

private function registerSecretsConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader): void
private function registerSecretsConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader, ?string $secret): void
{
if (!$this->readConfigEnabled('secrets', $container, $config)) {
$container->removeDefinition('console.command.secrets_set');
Expand All @@ -1771,6 +1771,9 @@ private function registerSecretsConfiguration(array $config, ContainerBuilder $c

$loader->load('secrets.php');

$container->resolveEnvPlaceholders($secret, null, $usedEnvs);
$secretEnvVar = 1 === \count($usedEnvs ?? []) ? substr(key($usedEnvs), 1 + (strrpos(key($usedEnvs), ':') ?: -1)) : null;
$container->getDefinition('secrets.vault')->replaceArgument(2, $secretEnvVar);
$container->getDefinition('secrets.vault')->replaceArgument(0, $config['vault_directory']);

if ($config['local_dotenv_file']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
->args([
abstract_arg('Secret dir, set in FrameworkExtension'),
service('secrets.decryption_key')->ignoreOnInvalid(),
abstract_arg('Secret env var, set in FrameworkExtension'),
])

->set('secrets.env_var_loader', StaticEnvVarLoader::class)
Expand Down
9 changes: 8 additions & 1 deletion src/Symfony/Bundle/FrameworkBundle/Secrets/SodiumVault.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@ class SodiumVault extends AbstractVault implements EnvVarLoaderInterface
private string|\Stringable|null $decryptionKey = null;
private string $pathPrefix;
private ?string $secretsDir;
private ?string $derivedSecretEnvVar;

/**
* @param $decryptionKey A string or a stringable object that defines the private key to use to decrypt the vault
* or null to store generated keys in the provided $secretsDir
*/
public function __construct(string $secretsDir, #[\SensitiveParameter] string|\Stringable|null $decryptionKey = null)
public function __construct(string $secretsDir, #[\SensitiveParameter] string|\Stringable|null $decryptionKey = null, ?string $derivedSecretEnvVar = null)
{
$this->pathPrefix = rtrim(strtr($secretsDir, '/', \DIRECTORY_SEPARATOR), \DIRECTORY_SEPARATOR).\DIRECTORY_SEPARATOR.basename($secretsDir).'.';
$this->decryptionKey = $decryptionKey;
$this->secretsDir = $secretsDir;
$this->derivedSecretEnvVar = $derivedSecretEnvVar;
}

public function generateKeys(bool $override = false): bool
Expand Down Expand Up @@ -177,6 +179,11 @@ public function loadEnvVars(): array
$envs[$name] = LazyString::fromCallable($reveal, $name);
}

if ($this->derivedSecretEnvVar && !\array_key_exists($this->derivedSecretEnvVar, $envs)) {
$decryptionKey = $this->decryptionKey;
$envs[$this->derivedSecretEnvVar] = LazyString::fromCallable(static fn () => base64_encode(hash('sha256', $decryptionKey, true)));
}

return $envs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,13 @@ public function testEncryptAndDecrypt()

$this->assertSame([], $vault->list());
}

public function testDerivedSecretEnvVar()
{
$vault = new SodiumVault($this->secretsDir, null, 'MY_SECRET');
$vault->generateKeys();
$vault->seal('FOO', 'bar');

$this->assertSame(['FOO', 'MY_SECRET'], array_keys($vault->loadEnvVars()));
}
}
Loading