8000 [DI] allow "." and "-" in env processor lines by nicolas-grekas · Pull Request #35029 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -1360,7 +1360,7 @@ private function registerSecretsConfiguration(array $config, ContainerBuilder $c
}

if ($config['decryption_env_var']) {
if (!preg_match('/^(?:\w*+:)*+\w++$/', $config['decryption_env_var'])) {
if (!preg_match('/^(?:[-.\w]*+:)*+\w++$/', $config['decryption_env_var'])) {
throw new InvalidArgumentException(sprintf('Invalid value "%s" set as "decryption_env_var": only "word" characters are allowed.', $config['decryption_env_var']));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public function get(string $name)
return $placeholder; // return first result
}
}
if (!preg_match('/^(?:\w*+:)*+\w++$/', $env)) {
if (!preg_match('/^(?:[-.\w]*+:)*+\w++$/', $env)) {
throw new InvalidArgumentException(sprintf('Invalid %s name: only "word" characters are allowed.', $name));
}
if ($this->has($name) && null !== ($defaultValue = parent::get($name)) && !\is_string($defaultValue)) {
throw new RuntimeException(sprintf('The default value of an env() parameter must be a string or null, but "%s" given to "%s".', \gettype($defaultValue), $name));
}

$uniqueName = md5($name.'_'.self::$counter++);
$placeholder = sprintf('%s_%s_%s', $this->getEnvPlaceholderUniquePrefix(), str_replace(':', '_', $env), $uniqueName);
$placeholder = sprintf('%s_%s_%s', $this->getEnvPlaceholderUniquePrefix(), strtr($env, ':-.', '___'), $uniqueName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replacing the 3 chars with the same replacement will be an issue: we can have multiple env lines producing the same placeholder, which will go wrong when reverting them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an md5 on the line before that will prevent any collisions from happening.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah indeed. I missed that.

$this->envPlaceholders[$env][$placeholder] = $placeholder;

return $placeholder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,11 @@ public function testDefaultToNullAllowed()
$bag->resolve();
$this->assertNotNull($bag->get('env(default::BAR)'));
}

public function testExtraCharsInProcessor()
{
$bag = new EnvPlaceholderParameterBag();
$bag->resolve();
$this->assertStringMatchesFormat('env_%s_key_a_b_c_FOO_%s', $bag->get('env(key:a.b-c:FOO)'));
}
}
0