8000 properly parse backslashes in unquoted env vars · symfony/symfony@6cee739 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6cee739

Browse files
committed
properly parse backslashes in unquoted env vars
1 parent b6f9f8d commit 6cee739

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/Symfony/Component/Dotenv/Dotenv.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,10 +224,11 @@ private function lexValue()
224224
throw $this->createFormatException('Missing quote to end the value');
225225
}
226226
++$this->cursor;
227-
$value = str_replace(array('\\\\', '\\"', '\r', '\n'), array('\\', '"', "\r", "\n"), $value);
227+
$value = str_replace(array('\\"', '\r', '\n'), array('"', "\r", "\n"), $value);
228228
$resolvedValue = $value;
229229
$resolvedValue = $this->resolveVariables($resolvedValue);
230230
$resolvedValue = $this->resolveCommands($resolvedValue);
231+
$resolvedValue = str_replace('\\\\', '\\', $resolvedValue);
231232
$v .= $resolvedValue;
232233
} else {
233234
$value = '';
@@ -250,6 +251,7 @@ private function lexValue()
250251
$resolvedValue = $value;
251252
$resolvedValue = $this->resolveVariables($resolvedValue);
252253
$resolvedValue = $this->resolveCommands($resolvedValue);
254+
$resolvedValue = str_replace('\\\\', '\\', $resolvedValue);
253255

254256
if ($resolvedValue === $value && preg_match('/\s+/', $value)) {
255257
throw $this->createFormatException('A value containing spaces must be surrounded by quotes');
@@ -350,7 +352,7 @@ private function resolveVariables($value)
350352
}
351353

352354
$regex = '/
353-
(\\\\)? # escaped with a backslash?
355+
((?:\\\\)?\\\\)? # escaped with a backslash?
354356
\$
355357
(?!\() # no opening parenthesis
356358
(\{)? # optional brace
@@ -382,7 +384,7 @@ private function resolveVariables($value)
382384
$value .= '}';
383385
}
384386

385-
return $value;
387+
return $matches[1].$value;
386388
}, $value);
387389

388390
// unescape $

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ public function getEnvData()
6666
$_ENV['REMOTE'] = 'remote';
6767

6868
$tests = array(
69+
// backslashes
70+
array('FOO=foo\\\\bar', array('FOO' => 'foo\\bar')),
71+
array("FOO='foo\\\\bar'", array('FOO' => 'foo\\\\bar')),
72+
array('FOO="foo\\\\bar"', array('FOO' => 'foo\\bar')),
73+
74+
// escape backslash in front of variable
75+
array("BAR=bar\nFOO=foo\\\\\$BAR", array('BAR' => 'bar', 'FOO' => 'foo\\bar')),
76+
array("BAR=bar\nFOO='foo\\\\\$BAR'", array('BAR' => 'bar', 'FOO' => 'foo\\\\$BAR')),
77+
array("BAR=bar\nFOO=\"foo\\\\\$BAR\"", array('BAR' => 'bar', 'FOO' => 'foo\\bar')),
78+
6979
// spaces
7080
array('FOO=bar', array('FOO' => 'bar')),
7181
array(' FOO=bar ', array('FOO' => 'bar')),

0 commit comments

Comments
 (0)
0