Skip to content

Commit d9ce166

Browse files
committed
bug symfony#52928 [Dotenv] Allow environment variables starting with an underscore (xabbuh)
This PR was merged into the 5.4 branch. Discussion ---------- [Dotenv] Allow environment variables starting with an underscore | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#52630 | License | MIT Commits ------- a34731e allow environment variables starting with an underscore
2 parents 6d288bf + a34731e commit d9ce166

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/Symfony/Component/Dotenv/Dotenv.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626
final class Dotenv
2727
{
28-
public const VARNAME_REGEX = '(?i:[A-Z][A-Z0-9_]*+)';
28+
public const VARNAME_REGEX = '(?i:_?[A-Z][A-Z0-9_]*+)';
2929
public const STATE_VARNAME = 0;
3030
public const STATE_VALUE = 1;
3131

@@ -351,8 +351,8 @@ private function lexValue(): string
351351
++$this->cursor;
352352
$value = str_replace(['\\"', '\r', '\n'], ['"', "\r", "\n"], $value);
353353
$resolvedValue = $value;
354-
$resolvedValue = $this->resolveVariables($resolvedValue, $loadedVars);
355354
$resolvedValue = $this->resolveCommands($resolvedValue, $loadedVars);
355+
$resolvedValue = $this->resolveVariables($resolvedValue, $loadedVars);
356356
$resolvedValue = str_replace('\\\\', '\\', $resolvedValue);
357357
$v .= $resolvedValue;
358358
} else {
@@ -374,8 +374,8 @@ private function lexValue(): string
374374
}
375375
$value = rtrim($value);
376376
$resolvedValue = $value;
377-
$resolvedValue = $this->resolveVariables($resolvedValue, $loadedVars);
378377
$resolvedValue = $this->resolveCommands($resolvedValue, $loadedVars);
378+
$resolvedValue = $this->resolveVariables($resolvedValue, $loadedVars);
379379
$resolvedValue = str_replace('\\\\', '\\', $resolvedValue);
380380

381381
if ($resolvedValue === $value && preg_match('/\s+/', $value)) {

src/Symfony/Component/Dotenv/Tests/DotenvTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public static function getEnvDataWithFormatErrors()
5353
["FOO=\nBAR=\${FOO:-\'a{a}a}", "Unsupported character \"'\" found in the default value of variable \"\$FOO\". in \".env\" at line 2.\n...\\nBAR=\${FOO:-\'a{a}a}...\n ^ line 2 offset 24"],
5454
["FOO=\nBAR=\${FOO:-a\$a}", "Unsupported character \"\$\" found in the default value of variable \"\$FOO\". in \".env\" at line 2.\n...FOO=\\nBAR=\${FOO:-a\$a}...\n ^ line 2 offset 20"],
5555
["FOO=\nBAR=\${FOO:-a\"a}", "Unclosed braces on variable expansion in \".env\" at line 2.\n...FOO=\\nBAR=\${FOO:-a\"a}...\n ^ line 2 offset 17"],
56+
['_=FOO', "Invalid character in variable name in \".env\" at line 1.\n..._=FOO...\n ^ line 1 offset 0"],
5657
];
5758

5859
if ('\\' !== \DIRECTORY_SEPARATOR) {
@@ -175,6 +176,10 @@ public static function getEnvData()
175176
["FOO=\nBAR=\${FOO:=TEST}", ['FOO' => 'TEST', 'BAR' => 'TEST']],
176177
["FOO=\nBAR=\$FOO:=TEST}", ['FOO' => 'TEST', 'BAR' => 'TEST}']],
177178
["FOO=foo\nFOOBAR=\${FOO}\${BAR}", ['FOO' => 'foo', 'FOOBAR' => 'foo']],
179+
180+
// underscores
181+
['_FOO=BAR', ['_FOO' => 'BAR']],
182+
['_FOO_BAR=FOOBAR', ['_FOO_BAR' => 'FOOBAR']],
178183
];
179184

180185
if ('\\' !== \DIRECTORY_SEPARATOR) {

0 commit comments

Comments
 (0)