From dbcc609971dd9b55f48b8008b553d79fd372ddde Mon Sep 17 00:00:00 2001 From: Graham Campbell Date: Wed, 6 Mar 2019 09:39:45 +0000 Subject: [PATCH] Add some extra type checks on names (#351) --- src/Environment/AbstractVariables.php | 2 +- src/Environment/DotenvVariables.php | 20 +++++++++++ src/Environment/VariablesInterface.php | 6 ++++ tests/Dotenv/EnvironmentVariablesTest.php | 41 +++++++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/Environment/AbstractVariables.php b/src/Environment/AbstractVariables.php index 73bb2e5f..b1d5de46 100644 --- a/src/Environment/AbstractVariables.php +++ b/src/Environment/AbstractVariables.php @@ -47,7 +47,7 @@ public function isImmutable() */ public function has($name) { - return $this->get($name) !== null; + return is_string($name) && $this->get($name) !== null; } /** diff --git a/src/Environment/DotenvVariables.php b/src/Environment/DotenvVariables.php index 5daadb45..f19d6cea 100644 --- a/src/Environment/DotenvVariables.php +++ b/src/Environment/DotenvVariables.php @@ -2,6 +2,8 @@ namespace Dotenv\Environment; +use InvalidArgumentException; + /** * The default implementation of the environment variables interface. */ @@ -35,10 +37,16 @@ public function __construct(array $adapters, $immutable) * * @param string $name * + * @throws \InvalidArgumentException + * * @return string|null */ public function get($name) { + if (!is_string($name)) { + throw new InvalidArgumentException('Expected name to be a string.'); + } + foreach ($this->adapters as $adapter) { $result = $adapter->get($name); if ($result->isDefined()) { @@ -53,10 +61,16 @@ public function get($name) * @param string $name * @param string|null $value * + * @throws \InvalidArgumentException + * * @return void */ public function set($name, $value = null) { + if (!is_string($name)) { + throw new InvalidArgumentException('Expected name to be a string.'); + } + // Don't overwrite existing environment variables if we're immutable // Ruby's dotenv does this with `ENV[key] ||= value`. if ($this->isImmutable() && $this->get($name) !== null) { @@ -73,10 +87,16 @@ public function set($name, $value = null) * * @param string $name * + * @throws \InvalidArgumentException + * * @return void */ public function clear($name) { + if (!is_string($name)) { + throw new InvalidArgumentException('Expected name to be a string.'); + } + // Don't clear anything if we're immutable. if ($this->isImmutable()) { return; diff --git a/src/Environment/VariablesInterface.php b/src/Environment/VariablesInterface.php index 668bb8bd..de2a71fb 100644 --- a/src/Environment/VariablesInterface.php +++ b/src/Environment/VariablesInterface.php @@ -30,6 +30,8 @@ public function has($name); * * @param string $name * + * @throws \InvalidArgumentException + * * @return string|null */ public function get($name); @@ -40,6 +42,8 @@ public function get($name); * @param string $name * @param string|null $value * + * @throws \InvalidArgumentException + * * @return void */ public function set($name, $value = null); @@ -49,6 +53,8 @@ public function set($name, $value = null); * * @param string $name * + * @throws \InvalidArgumentException + * * @return void */ public function clear($name); diff --git a/tests/Dotenv/EnvironmentVariablesTest.php b/tests/Dotenv/EnvironmentVariablesTest.php index fa145599..beba2cb4 100644 --- a/tests/Dotenv/EnvironmentVariablesTest.php +++ b/tests/Dotenv/EnvironmentVariablesTest.php @@ -25,6 +25,14 @@ public function testCheckingWhetherVariableExists() $this->assertFalse($envVars->has('NON_EXISTING_VARIABLE')); } + public function testCheckingHasWithBadType() + { + $envVars = $this->envFactory->create(); + + $this->assertFalse($envVars->has(123)); + $this->assertFalse($envVars->has(null)); + } + public function testGettingVariableByName() { $envVars = $this->envFactory->create(); @@ -32,6 +40,17 @@ public function testGettingVariableByName() $this->assertSame('bar', $envVars->get('FOO')); } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Expected name to be a string. + */ + public function testGettingBadVariable() + { + $envVars = $this->envFactory->create(); + + $envVars->get(null); + } + public function testSettingVariable() { $envVars = $this->envFactory->create(); @@ -43,6 +62,17 @@ public function testSettingVariable() $this->assertSame('new', $envVars->get('FOO')); } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Expected name to be a string. + */ + public function testSettingBadVariable() + { + $envVars = $this->envFactory->create(); + + $envVars->set(null, 'foo'); + } + public function testClearingVariable() { $envVars = $this->envFactory->create(); @@ -52,6 +82,17 @@ public function testClearingVariable() $this->assertFalse($envVars->has('FOO')); } + /** + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage Expected name to be a string. + */ + public function testClearingBadVariable() + { + $envVars = $this->envFactory->create(); + + $envVars->clear(null); + } + public function testCannotSetVariableOnImmutableInstance() { $envVars = $this->envFactory->createImmutable();