-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI] Validate env vars in config #23888
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
Conversation
I suggest wait for #23901 before continuing on this topic - that will provide a much better base to fix this issue. |
@nicolas-grekas could you briefly describe the path you have in mind for this, and how it leverages From a technical pov this approach was the only thing i could come up with, regarding decoupled state between di and config. I like how it works out-of-the-box for everyone :) My idea here was to generate a better default value using |
The env placeholders contain the prefixes - so we can extract that, and use |
@nicolas-grekas first round, wdyt? |
*/ | ||
public static function setPlaceholder($placeholder, $type) | ||
{ | ||
switch ($type) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO. can be done at the calling side..
@@ -92,7 +91,7 @@ public function __construct() | |||
public function getPasses() | |||
{ | |||
return array_merge( | |||
array($this->mergePass), | |||
array(new RegisterEnvVarProcessorsPass(), $this->mergePass), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now runs even earlier.. anything to do? Patched PhpDumper test for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, I just realized that this may just make it impossible to register a new env processor in practice, isn't it?
continue; | ||
} | ||
foreach ($placeholders as $placeholder) { | ||
BaseNode::setPlaceholder($placeholder, reset($envTypes[$prefix])); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about getParam("env($env)")
as default value instead, if available :) That justifies computing default values on the calling side.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's do. We also need to validate the type of the default value before calling setPlaceholder (its type needs to be in $envTypes).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BUT, calling "reset" here prevents taking advantage of the "multiple types provided" info. So maybe pass an array instead, $type=>$value?
@@ -43,14 +44,23 @@ public function process(ContainerBuilder $container) | |||
foreach ($class::getProvidedTypes() as $prefix => $type) { | |||
$processors[$prefix] = new ServiceClosureArgument(new Reference($id)); | |||
$types[$prefix] = self::validateProvidedTypes($type, $class); | |||
|
|||
if (!$types[$prefix]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this happen at all? explode always creates an array with at least one element, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this happen at all?
nope :)
@nicolas-grekas pushed latest changes real quick. Think this is heading towards what you have in mind; see BaseNode. |
foreach (self::$placeholders[$value] as $placeholder) { | ||
try { | ||
$this->normalize($placeholder); | ||
break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be return $value;
and the current return below should be throw $e;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now with test :)
Tests looking good. Tried to keep the big code blob at a minimum, but tweaks are welcome :) Status: needs review deps=high failure expected. |
foreach (self::$placeholders[$leftSide] as $placeholder) { | ||
try { | ||
$this->validateType($placeholder); | ||
break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return + throw here also (and below)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or at least, the throw part is missing if return is not for here
@nicolas-grekas now with test for validating on merge :) see simplified BaseNode::resolvePlaceholderValues() + merge(). |
@@ -79,6 +79,13 @@ public function mergeEnvPlaceholders(self $bag) | |||
$this->envPlaceholders[$env] += $placeholders; | |||
} | |||
} | |||
foreach ($bag->getProvidedTypes() as $prefix => $types) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this file doesn't need to be modified: merging happens after setProvidedTypes is called, and should not happen anymore after
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They get lost in MergeExtensionConfigurationParameterBag::__construct
as we overwrite $resolvingBag
.
But agree; this method is called mergeEnvPlaceholders
.. so doesnt imply it merges types. Now fixed.
@nicolas-grekas what about this;
But then with env placeholders where I think current behavior is OK as envs are normalized values (nothing happens with them after compile, i.e. no config normalization, except processing). Thus you'd have WDYT? edit: hm that only applies if a type conversion happens, and we lose the placeholder. For string-to-string we could in fact return the processed value if it still contains the original placeholder (and perhaps if not also). Not sure who's exactly responsible here :) |
$default = null; | ||
if ($hasEnv = (false === $i && $defaultEnvBag->has("env($env)"))) { | ||
switch ($type = gettype($default = $defaultEnvBag->get("env($env)"))) { | ||
case 'boolean': $type = 'bool'; break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we rename our allowedTypes to match those of gettype and remove that switch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Think so 👍 preferring FQ over short names.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While at it should we infer boolean|integer|float|string
from scalar
while resolving? Nice to have perhaps.
case 'float': $values[$type] = 0.0; break; | ||
case 'int': $values[$type] = 0; break; | ||
case 'string': $values[$type] = ''; break; | ||
default: $values[$type] = null; break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this case possible? if not, default should be string, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only if we forget to update it after adding types. Should be never, just a safety net.
default: throw
then? In case of non-fatal behavior id say null
.. why assume string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no default case and let php crash? fine fore me :)
Does the milestone change mean it won't make it into the 3.4 branch? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(with minor comments)
return self::$placeholders[$value]; | ||
} | ||
|
||
if (0 === strpos($value, self::$placeholderUniquePrefix, 0)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3rd arg not needed
|
||
private static function getType($value): string | ||
{ | ||
switch ($type = gettype($value)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
\gettype
@@ -15,6 +15,8 @@ | |||
* This class is the entry point for config normalization/merging/finalization. | |||
* | |||
* @author Johannes M. Schmitt <[email protected]> | |||
* | |||
* @final since version 4.1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unrelated to this PR? If yes, should be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure :) https://github.com/symfony/symfony/pull/23888/files#diff-e321d9e71b12d99326a74385f020e2eeR66
see also #23888 (comment)
i think it's reasonable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then: missing changelog/upgrade entries
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changelog entry added. Suggestion for an upgrade note?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes!
in *4.1: The ... class has been made final
in *5.0: The ... class has been made final
:)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :)
*/ | ||
public function getEnvPlaceholderUniquePrefix(): string | ||
{ | ||
return $this->envPlaceholderUniquePrefix ?? $this->envPlaceholderUniquePrefix = 'env_'.bin2hex(random_bytes(16)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
8 instead of 16 is enough (we use 7 bytes in ContainerBuilder::hash()
)
@@ -50,6 +56,39 @@ public function __construct(?string $name, NodeInterface $parent = null, string | |||
$this->pathSeparator = $pathSeparator; | |||
} | |||
|
|||
/** | |||
* Register possible (dummy) values for a dynamic placeholder value. Matching configuration values will be processed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would move all sentences but the first one as a phpdoc description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mean split into short & long description right? Did that for now.
} | ||
|
||
/** | ||
* Set a common prefix for dynamic placeholder values. Matching configuration values will be skipped from being |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sets + same comment as above about separating the first sentence from the other ones.
} | ||
|
||
/** | ||
* Reset all current placeholders available. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resets
public static function resetPlaceholders(): void | ||
{ | ||
self::$placeholderUniquePrefix = null; | ||
self::$placeholders = array(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having static properties like this is very dangerous as we need to make sure that they are reset properly. I can see the finally
calls here and there to reset this, but is there is any other way? Avoiding static properties would be great.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not that dangerous: placeholders are unique strings so there is not really any way to mess up with them, even if someone forgets to reset them.
About removing the static state, that's really hard...
BUT, @ro0NL we should make these methods @internal
for sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can settle indeed, i dont see a elegant workaround to avoid static currently, that is without overhauling the config component itself :)
Now marked internal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also we deal with it properly where needed; it's not scattered all over the environment.
@@ -363,4 +459,85 @@ public function getParent() | |||
* @return mixed The finalized value | |||
*/ | |||
abstract protected function finalizeValue($value); | |||
|
|||
/** | |||
* Test if placeholder values are allowed for this node. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests
} | ||
|
||
/** | ||
* Get allowed dynamic types for this node. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gets
/** | ||
* Resets all current placeholders available. | ||
*/ | ||
public static function resetPlaceholders(): void |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this one also needs to be internal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops :) updated.
Thank you @ro0NL. |
$container = new ContainerBuilder(); | ||
$container->registerExtension(new EnvExtension()); | ||
$container->prependExtensionConfig('env_extension', array( | ||
'simple_array_node' => '%env(json:FOO)%', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
json:
can return a simple_array, e.g. in the case the env is something like [1, 2]
- of course json
doesnt guarrentee that its like this, it could also be {"a": 1, "b": 2}
, so its a bit of a question, is it valid or not - I think its more useful if it passes validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah i think we need to reassure / revise integration a bit. Or put different im curious if the change to json prefix affects the possibilities here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the json change only added the possibility to accept null
in addition to array
so shouldn't change anything here - this was on purpose so that its still possible to do some validation based on it always being an array.
I wonder if it would make sense to add a simple_array:
decoder to further help with validation
*/ | ||
protected function allowPlaceholders(): bool | ||
{ | ||
return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why have you made it false by default for ArrayNode
? It seems this will limit how much of a config you can change in other peoples bundles, i.e. you can only put env(json:FOO)
if the owner things of you in advance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comments on closed PRs are likely to be lost, please consider opening a new issue/PR if you think something is buggy or can be improved. Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case i asked for it on another closed PR :P But yeah.. not sure if there's any real issue now (hence i asked :D)
@mcfedr see symfony/symfony-docs#8382 (comment) , tried to explain the general proces there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, makes sense, so for simple_array nodes it will accept env(json:FOO)
but not when it should validate the structure.
FYI: Just got hit by this BC break when upgrading to 4.1.0:
for configuration with env defaults:
Issue & failing test coming soon. |
This PR registers the env placeholders in
Config\BaseNode
with its default value or an empty string. It doesnt request real env vars during compilation,What it does is if a config value exactly matches a env placeholder, we validate/normalize the default value/empty string but we keep returning the env placeholder as usual. If a placeholder occurs in the middle of a string it also proceeds as usual.
The latter to me is OK as you need to expect any string value during runtime anyway, including the empty string.