Issue Bug Closed

#91710Resolving env-Variable in Site Configuration ignores values that resolve to "false"

Philipp Seiler 06/25/20, 13:53 updated 07/09/24, 12:00

Description

  • Have an environment variable like MY_VARIABLE=0 or MY_VARIABLE=“0” or MY_VARIABLE=”“ or MY_VARIABLE=.
  • Add this variable to the site configuration YAML:
myVariable: '%env(MY_VARIABLE)'
  • Access this env-var in TypoScript via e.g.
myVariable = TEXT
myVariable.data = site:myVariable
  • When TYPO3 parses the SiteConfiguration, the YamlFileLoader will detect the \%env(…)%*-Syntax and replace the value accordingly with *getenv().
  • However, the method getValueFromEnv in \TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader will evaluate the env-value to boolean FALSE, leading to the original string being used as a fallback.
  • The resulting value in the SiteConfiguration will then still be:
myVariable: '%env(MY_VARIABLE)'
  • This is of course wrong. Sometimes variables specifically need to be “0” or even empty. The check for env-value should not skip false-like values, but simply check if the env-variable is set or not.

A quick fix would be to adjust the *getValueFromEnv*-method like this:

$matches = [];
preg_match_all('/%env\([\'"]?(\w+)[\'"]?\)%/', $value, $matches);
$envVars = array_combine($matches[0], $matches[1]);
foreach ($envVars as $substring => $envVarName) {
    $envVar = getenv($envVarName);
    $value = $envVar !== false ? str_replace($substring, $envVar, $value) : $value;
}
return $value;

Note the $envVar !== false ? …. Originally this was $envVar ? ….

Reviews 2