From 6ea4ffd3a711539196ea53137812f17e3c5a91ce Mon Sep 17 00:00:00 2001 From: Dennis Smink Date: Mon, 6 Jul 2020 16:37:15 +0200 Subject: [PATCH 1/2] Values with spaces break application Setting app name to: My Company Name will break the .env file. https://cln.sh/BNoLe8sCHBTu7vuzrouz+ ``` The environment file is invalid! Failed to parse dotenv file due to unexpected whitespace. Failed at [My Company Name]. ``` E.g. Ploi Is Awesome Company vs "Ploi Is Awesome Company" --- src/EnvironmentSetCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EnvironmentSetCommand.php b/src/EnvironmentSetCommand.php index d9dbdaa..e266d19 100644 --- a/src/EnvironmentSetCommand.php +++ b/src/EnvironmentSetCommand.php @@ -78,7 +78,7 @@ public function handle(): void public function setEnvVariable(string $envFileContent, string $key, string $value): array { $oldPair = $this->readKeyValuePair($envFileContent, $key); - $newPair = $key . '=' . $value; + $newPair = $key . '="' . $value . '"'; // For existed key. if ($oldPair !== null) { From 5d6820a72ac61a6a702e0e48f6a7461ac7003a42 Mon Sep 17 00:00:00 2001 From: Liam Hammett Date: Sat, 18 Jul 2020 23:08:38 +0100 Subject: [PATCH 2/2] Only wrap necessary values in quotes, and add tests --- src/EnvironmentSetCommand.php | 8 +++++++- tests/Unit/EnvironmentSetCommandTest.php | 12 ++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/EnvironmentSetCommand.php b/src/EnvironmentSetCommand.php index e266d19..4a04fca 100644 --- a/src/EnvironmentSetCommand.php +++ b/src/EnvironmentSetCommand.php @@ -78,7 +78,13 @@ public function handle(): void public function setEnvVariable(string $envFileContent, string $key, string $value): array { $oldPair = $this->readKeyValuePair($envFileContent, $key); - $newPair = $key . '="' . $value . '"'; + + // Wrap values that have a space or equals in quotes to escape them + if (preg_match('/\s/',$value) || strpos($value, '=') !== false) { + $value = '"' . $value . '"'; + } + + $newPair = $key . '=' . $value; // For existed key. if ($oldPair !== null) { diff --git a/tests/Unit/EnvironmentSetCommandTest.php b/tests/Unit/EnvironmentSetCommandTest.php index 7c2c14b..864d5ef 100644 --- a/tests/Unit/EnvironmentSetCommandTest.php +++ b/tests/Unit/EnvironmentSetCommandTest.php @@ -181,7 +181,14 @@ public function setEnvVariableDataProvider(): array 'some_key', '=========', str_replace('some_key=some_value', - 'some_key==========', $envFileContent), + 'some_key="========="', $envFileContent), + ], + [ + &$envFileContent, + 'value_with_spaces', + 'this is a value', + str_replace('value_with_spaces=', + 'value_with_spaces="this is a value"', $envFileContent), ], ]; } @@ -353,6 +360,7 @@ protected function getTestEnvFile(): string . ' spaces_in_the_quotes = " " ' . "\n" . 'a_lot_of_equals_signs_one=======' . "\n" . 'a_lot_of_equals_signs_two = ====== ' . "\n" - . 'a_lot_of_equals_signs_three = "======" ' . "\n"; + . 'a_lot_of_equals_signs_three = "======" ' . "\n" + . 'value_with_spaces=' . "\n"; } }