Skip to content

Commit

Permalink
Merge pull request #27 from zf-fr/add-docker-ip
Browse files Browse the repository at this point in the history
Take into account Docker localhost
  • Loading branch information
bakura10 committed Jun 27, 2016
2 parents 7654bde + 94cfa45 commit cfe745d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
# 5.1.1

* Adds support for detecting localhost requests coming from Docker environment

# 5.1.0

* ZfrEbWorker adds a new security improvement by restricting the internal worker to localhost only.
Expand Down
4 changes: 2 additions & 2 deletions src/Middleware/LocalhostCheckerMiddleware.php
Expand Up @@ -31,8 +31,8 @@ public function __invoke(
$serverParams = $request->getServerParams();
$remoteAddr = $serverParams['REMOTE_ADDR'] ?? '';

// If request is not originating from localhost, we simply return 200
if (!in_array($remoteAddr, $this->localhost)) {
// If request is not originating from localhost or from Docker local IP, we simply return 200
if (!in_array($remoteAddr, $this->localhost) && !fnmatch('172.17.*', $remoteAddr)) {
return $response->withStatus(403);
}

Expand Down
24 changes: 24 additions & 0 deletions test/Middleware/LocalhostCheckerMiddlewareTest.php
Expand Up @@ -24,6 +24,30 @@ public function testReturns403IfNotFromLocalhost()
$this->assertEquals(403, $returnedResponse->getStatusCode());
}

public function dockerIpAddresses()
{
return [['172.17.42.1'], ['172.17.0.1']];
}

/**
* @dataProvider dockerIpAddresses
*/
public function testDelegatesIfFromDockerLocal(string $ipAddress)
{
$request = $this->prophesize(ServerRequestInterface::class);
$response = new Response();

$request->getServerParams()->shouldBeCalled()->willReturn(['REMOTE_ADDR' => $ipAddress]);

$middleware = new LocalhostCheckerMiddleware();

$returnedResponse = $middleware->__invoke($request->reveal(), $response, function($request, $response, $out) {
return $response;
});

$this->assertEquals(200, $returnedResponse->getStatusCode());
}

public function testDelegateIfFromIPv4Localhost()
{
$request = $this->prophesize(ServerRequestInterface::class);
Expand Down

0 comments on commit cfe745d

Please sign in to comment.