Skip to content

Commit d5659ca

Browse files
committed
Allow IP whitelist to be an array
1 parent 96e26b0 commit d5659ca

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

config/stagefront.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@
7979

8080
/**
8181
* Allow access from whitelisted IP's.
82-
* Enter a string of comma separated IP's or null to allow all IP's.
83-
* For example: '1.2.3.4,1.2.3.4'
82+
* Enter an array or string of comma separated IP's or null to allow all IP's.
83+
* For example: ['1.2.3.4', '1.2.3.4'] or '1.2.3.4,1.2.3.4'
8484
* If empty, all IP's are allowed.
8585
*
8686
* Default: null

src/Shield.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ protected function currentUrlIsIgnored()
7777
protected function clientIpIsWhitelisted()
7878
{
7979
$clientIp = Request::ip();
80-
$whitelist = explode(',', $this->getIpWhitelist());
81-
$ips = array_map('trim', $whitelist);
80+
$ips = array_map('trim', $this->getIpWhitelist());
8281

8382
return in_array($clientIp, $ips);
8483
}
@@ -90,17 +89,23 @@ protected function clientIpIsWhitelisted()
9089
*/
9190
protected function hasIpWhitelist()
9291
{
93-
return ! empty(trim($this->getIpWhitelist()));
92+
return ! empty($this->getIpWhitelist());
9493
}
9594

9695
/**
9796
* Get the IP whitelist from the config file.
9897
*
99-
* @return string
98+
* @return array
10099
*/
101100
protected function getIpWhitelist()
102101
{
103-
return Config::get('stagefront.ip_whitelist', '');
102+
$whitelist = Config::get('stagefront.ip_whitelist', []);
103+
104+
if (is_array($whitelist)) {
105+
return $whitelist;
106+
}
107+
108+
return explode(',', $whitelist);
104109
}
105110

106111
/**

tests/StageFrontTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,26 @@ public function it_allows_access_to_whitelisted_ips_only()
270270
->assertOk();
271271
}
272272

273+
/** @test */
274+
public function you_can_add_a_whitelist_as_an_array()
275+
{
276+
$this->url = Config::get('stagefront.url');
277+
$this->registerRoute('/page', 'Some Page');
278+
279+
$this->enableStageFront();
280+
$this->setIntendedUrl('/page');
281+
282+
Config::set('stagefront.ip_whitelist', ['0.0.0.0', '1.1.1.1']);
283+
Config::set('stagefront.ip_whitelist_only', true);
284+
Config::set('stagefront.ip_whitelist_require_login', false);
285+
286+
$this->get('/page', ['REMOTE_ADDR' => '1.2.3.4'])
287+
->assertStatus(403);
288+
289+
$this->get('/page', ['REMOTE_ADDR' => '1.1.1.1'])
290+
->assertOk();
291+
}
292+
273293
/** @test */
274294
public function it_allows_access_to_whitelisted_ips_only_with_required_login()
275295
{

0 commit comments

Comments
 (0)