10000 [Dotenv] parse concatenated variable values by xabbuh · Pull Request #23351 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Dotenv] parse concatenated variable values #23351

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

Merged
merged 1 commit into from
Jul 3, 2017
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
[Dotenv] parse concatenated variable values
  • Loading branch information
xabbuh committed Jul 3, 2017
commit aaaf64ddde2aa22cb90a1c60fd902865589f5b18
133 changes: 85 additions & 48 deletions src/Symfony/Component/Dotenv/Dotenv.php
10000
Original file line number Diff line number Diff line change
Expand Up @@ -168,73 +168,110 @@ private function lexValue()
throw $this->createFormatException('Whitespace are not supported before the value');
}

$value = '';
$singleQuoted = false;
$notQuoted = false;
if ("'" === $this->data[$this->cursor]) {
$singleQuoted = true;
++$this->cursor;
while ("\n" !== $this->data[$this->cursor]) {
if ("'" === $this->data[$this->cursor]) {
if ($this->cursor + 1 === $this->end) {
break;
}
if ("'" !== $this->data[$this->cursor + 1]) {
$v = '';

do {
if ("'" === $this->data[$this->cursor]) {
$value = '';
++$this->cursor;

while ("\n" !== $this->data[$this->cursor]) {
if ("'" === $this->data[$this->cursor]) {
break;
}

$value .= $this->data[$this->cursor];
++$this->cursor;
}
$value .= $this->data[$this->cursor];
++$this->cursor;

if ($this->cursor === $this->end) {
if ($this->cursor === $this->end) {
throw $this->createFormatException('Missing quote to end the value');
}
}
if ("\n" === $this->data[$this->cursor]) {
throw $this->createFormatException('Missing quote to end the value');
}
}
if ("\n" === $this->data[$this->cursor]) {
throw $this->createFormatException('Missing quote to end the value');
}
++$this->cursor;
} elseif ('"' === $this->data[$this->cursor]) {
++$this->cursor;
while ('"' !== $this->data[$this->cursor] || ('\\' === $this->data[$this->cursor - 1] && '\\' !== $this->data[$this->cursor - 2])) {
$value .= $this->data[$this->cursor];
++$this->cursor;
$v .= $value;
} elseif ('"' === $this->data[$this->cursor]) {
$value = '';
++$this->cursor;

while ('"' !== $this->data[$this->cursor] || ('\\' === $this->data[$this->cursor - 1] && '\\' !== $this->data[$this->cursor - 2])) {
$value .= $this->data[$this->cursor];
++$this->cursor;

if ($this->cursor === $this->end) {
if ($this->cursor === $this->end) {
throw $this->createFormatException('Missing quote to end the value');
}
}
if ("\n" === $this->data[$this->cursor]) {
throw $this->createFormatException('Missing quote to end the value');
}
}
if ("\n" === $this->data[$this->cursor]) {
throw $this->createFormatException('Missing quote to end the value');
}
++$this->cursor;
$value = str_replace(array('\\\\', '\\"', '\r', '\n'), array('\\', '"', "\r", "\n"), $value);
} else {
$notQuoted = true;
$prevChr = $this->data[$this->cursor - 1];
while ($this->cursor < $this->end && "\n" !== $this->data[$this->cursor] && !((' ' === $prevChr || "\t" === $prevChr) && '#' === $this->data[$this->cursor])) {
if ('\\' === $this->data[$this->cursor] && isset($this->data[$this->cursor + 1]) && ('"' === $this->data[$this->cursor + 1] || "'" === $this->data[$this->cursor + 1])) {
++$this->cursor;
$value = str_replace(array('\\\\', '\\"', '\r', '\n'), array('\\', '"', "\r", "\n"), $value);
$resolvedValue = $value;
$resolvedValue = $this->resolveVariables($resolvedValue);
$resolvedValue = $this->resolveCommands($resolvedValue);
$v .= $resolvedValue;
} else {
$value = '';
$prevChr = $this->data[$this->cursor - 1];
while ($this->cursor < $this->end && !in_array($this->data[$this->cursor], array("\n", '"', "'"), true) && !((' ' === $prevChr || "\t" === $prevChr) && '#' === $this->data[$this->cursor])) {
if ('\\' === $this->data[$this->cursor] && isset($this->data[$this->cursor + 1]) && ('"' === $this->data[$this->cursor + 1] || "'" === $this->data[$this->cursor + 1])) {
++$this->cursor;
}

$value .= $prevChr = $this->data[$this->cursor];

if ('$' === $this->data[$this->cursor] && isset($this->data[$this->cursor + 1]) && '(' === $this->data[$this->cursor + 1]) {
++$this->cursor;
$value .= '('.$this->lexNestedExpression().')';
}

++$this->cursor;
}
$value = rtrim($value);
$resolvedValue = $value;
$resolvedValue = $this->resolveVariables($resolvedValue);
$resolvedValue = $this->resolveCommands($resolvedValue);

$value .= $prevChr = $this->data[$this->cursor];
++$this->cursor;
if ($resolvedValue === $value && preg_match('/\s+/', $value)) {
throw $this->createFormatException('A value containing spaces must be surrounded by quotes');
}

$v .= $resolvedValue;

if ($this->cursor < $this->end && '#' === $this->data[$this->cursor]) {
break;
}
}
$value = rtrim($value);
}
} while ($this->cursor < $this->end && "\n" !== $this->data[$this->cursor]);

$this->skipEmptyLines();

$currentValue = $value;
if (!$singleQuoted) {
$value = $this->resolveVariables($value);
$value = $this->resolveCommands($value);
return $v;
}

private function lexNestedExpression()
{
++$this->cursor;
$value = '';

while ("\n" !== $this->data[$this->cursor] && ')' !== $this->data[$this->cursor]) {
$value .= $this->data[$this->cursor];

if ('(' === $this->data[$this->cursor]) {
Copy link
Member

Choose a reason for hiding this comment

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

Looks like we don't have test for that, do we?

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

But we could indeed add a test with non-matching parentheses.

$value .= $this->lexNestedExpression().')';
}

++$this->cursor;

if ($this->cursor === $this->end) {
throw $this->createFormatException('Missing closing parenthesis.');
}
}

if ($notQuoted && $currentValue == $value && preg_match('/\s+/', $value)) {
throw $this->createFormatException('A value containing spaces must be surrounded by quotes');
if ("\n" === $this->data[$this->cursor]) {
throw $this->createFormatException('Missing closing parenthesis.');
}

return $value;
Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/Dotenv/Tests/DotenvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ public function getEnvData()
array("FOO='bar'\n", array('FOO' => 'bar')),
array("FOO='bar\"foo'\n", array('FOO' => 'bar"foo')),
array("FOO=\"bar\\\"foo\"\n", array('FOO' => 'bar"foo')),
array("FOO='bar''foo'\n", array('FOO' => 'bar\'foo')),
array('FOO="bar\nfoo"', array('FOO' => "bar\nfoo")),
array('FOO="bar\rfoo"', array('FOO' => "bar\rfoo")),
array('FOO=\'bar\nfoo\'', array('FOO' => 'bar\nfoo')),
Expand All @@ -99,6 +98,10 @@ public function getEnvData()
array('FOO=\\"BAR', array('FOO' => '"BAR')),

// concatenated values
array("FOO='bar''foo'\n", array('FOO' => 'barfoo')),
array("FOO='bar '' baz'", array('FOO' => 'bar baz')),
array("FOO=bar\nBAR='baz'\"\$FOO\"", array('FOO' => 'bar', 'BAR' => 'bazbar')),
array("FOO='bar '\\'' baz'", array('FOO' => "bar ' baz")),

// comments
array("#FOO=bar\nBAR=foo", array('BAR' => 'foo')),
Expand Down
0