Skip to content

Commit 7417448

Browse files
Reject env files with missmatched quotes
1 parent 692830c commit 7417448

File tree

7 files changed

+54
-3
lines changed

7 files changed

+54
-3
lines changed

src/Parser.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static function parseValue($value)
5555
*/
5656
public static function parseQuotedValue($value)
5757
{
58-
$data = array_reduce(str_split($value), function ($data, $char) use ($value) {
58+
$result = array_reduce(str_split($value), function ($data, $char) use ($value) {
5959
switch ($data[1]) {
6060
case Parser::INITIAL_STATE:
6161
if ($char === '"' || $char === '\'') {
@@ -94,7 +94,13 @@ public static function parseQuotedValue($value)
9494
}
9595
}, array('', Parser::INITIAL_STATE));
9696

97-
return trim($data[0]);
97+
if ($result[1] === Parser::QUOTED_STATE || $result[1] === Parser::ESCAPE_STATE) {
98+
throw new InvalidFileException(
99+
'Dotenv values starting with a quote must finish with a closing quote.'
100+
);
101+
}
102+
103+
return trim($result[0]);
98104
}
99105

100106
/**

tests/Dotenv/DotenvTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,47 @@ public function testLargeDotenvLoadsEnvironmentVars()
8787
*/
8888
public function testSpacedValuesWithoutQuotesThrowsException()
8989
{
90-
$dotenv = new Dotenv(dirname(__DIR__).'/fixtures/env-wrong', 'spaced-wrong.env');
90+
$dotenv = new Dotenv(dirname(__DIR__).'/fixtures/env', 'spaced-wrong.env');
91+
$dotenv->load();
92+
}
93+
94+
/**
95+
* @expectedException \Dotenv\Exception\InvalidFileException
96+
* @expectedExceptionMessage Dotenv values starting with a quote must finish with a closing quote.
97+
*/
98+
public function testMissingClosingSingleQuoteThrowsException()
99+
{
100+
$dotenv = new Dotenv(dirname(__DIR__).'/fixtures/env', 'squote-wrong.env');
101+
$dotenv->load();
102+
}
103+
104+
/**
105+
* @expectedException \Dotenv\Exception\InvalidFileException
106+
* @expectedExceptionMessage Dotenv values starting with a quote must finish with a closing quote.
107+
*/
108+
public function testMissingClosingDoubleQuoteThrowsException()
109+
{
110+
$dotenv = new Dotenv(dirname(__DIR__).'/fixtures/env', 'dquote-wrong.env');
111+
$dotenv->load();
112+
}
113+
114+
/**
115+
* @expectedException \Dotenv\Exception\InvalidFileException
116+
* @expectedExceptionMessage Dotenv values starting with a quote must finish with a closing quote.
117+
*/
118+
public function testMissingClosingQuotesThrowsException()
119+
{
120+
$dotenv = new Dotenv(dirname(__DIR__).'/fixtures/env', 'quotes-wrong.env');
121+
$dotenv->load();
122+
}
123+
124+
/**
125+
* @expectedException \Dotenv\Exception\InvalidFileException
126+
* @expectedExceptionMessage Dotenv values starting with a quote must finish with a closing quote.
127+
*/
128+
public function testMissingClosingQuoteWithEscapeThrowsException()
129+
{
130+
$dotenv = new Dotenv(dirname(__DIR__).'/fixtures/env', 'escape-wrong.env');
91131
$dotenv->load();
92132
}
93133

tests/fixtures/env/dquote-wrong.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TEST="erert

tests/fixtures/env/escape-wrong.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TEST="\

tests/fixtures/env/quotes-wrong.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
TEST="erert
2+
TEST='erert

tests/fixtures/env/squote-wrong.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
TEST='erert

0 commit comments

Comments
 (0)