Skip to content

Commit

Permalink
Insecure Request Upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Feb 10, 2020
1 parent 75296de commit a43b52a
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/HealthCheckCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public function match(Context $context): bool
$hasHeader = $hasHeader || stripos($r->userAgent(), 'elb-healthchecker') !== false;
return $hasHeader || stripos($r->path(), '_ah/health') !== false;
}

public static function i()
{
return new static();
}
}
27 changes: 27 additions & 0 deletions src/Routes/InsecureRequestUpgradeRoute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Packaged\Routing\Routes;

use Packaged\Context\Context;
use Packaged\Routing\FuncCondition;
use Packaged\Routing\Handler\FuncHandler;
use Packaged\Routing\Route;
use Symfony\Component\HttpFoundation\RedirectResponse;

class InsecureRequestUpgradeRoute extends Route
{
public function __construct()
{
$this->add(FuncCondition::i(function (Context $c) { return !$c->request()->isSecure(true); }));
}

public function getHandler()
{
return new FuncHandler(
function (Context $c) {
return RedirectResponse::create(
str_replace('http:', 'https:', $c->request()->getUri())
);
}
);
}
}
38 changes: 38 additions & 0 deletions tests/HealthCheckConditionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Packaged\Tests\Routing;

use Packaged\Context\Context;
use Packaged\Http\Request;
use Packaged\Routing\HealthCheckCondition;
use PHPUnit\Framework\TestCase;

class HealthCheckConditionTest extends TestCase
{

public function testMatch()
{
$this->assertFalse(HealthCheckCondition::i()->match(new Context(Request::create('http://www.test.com:8080/'))));
$this->assertTrue(
HealthCheckCondition::i()->match(new Context(Request::create('http://www.test.com:8080/_ah/health')))
);

$ctx = new Context(Request::create('http://www.test.com:8080/'));
$this->assertFalse(HealthCheckCondition::i()->match($ctx));

$ctx = new Context(
Request::create('http://www.test.com:8080/', 'GET', [], [], [], ['HTTP_USER_AGENT' => 'GoogleHC/1'])
);
$this->assertTrue(HealthCheckCondition::i()->match($ctx));

$ctx = new Context(
Request::create('http://www.test.com:8080/', 'GET', [], [], [], ['HTTP_USER_AGENT' => 'kube-probe'])
);
$this->assertTrue(HealthCheckCondition::i()->match($ctx));

$ctx = new Context(
Request::create('http://www.test.com:8080/', 'GET', [], [], [], ['HTTP_USER_AGENT' => 'elb-healthchecker'])
);
$this->assertTrue(HealthCheckCondition::i()->match($ctx));
}
}
30 changes: 30 additions & 0 deletions tests/Routes/InsecureRequestUpgradeRouteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Packaged\Tests\Routing\Routes;

use Packaged\Context\Context;
use Packaged\Http\Request;
use Packaged\Routing\Routes\InsecureRequestUpgradeRoute;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\RedirectResponse;

class InsecureRequestUpgradeRouteTest extends TestCase
{
public function testHttpUpgrade()
{
$ctx = new Context(Request::create('http://www.google.com/a/b/c/?d=e&f=g'));
$route = InsecureRequestUpgradeRoute::i();
$this->assertTrue($route->match($ctx));
/** @var RedirectResponse|null $resp */
$resp = $route->getHandler()->handle($ctx);
$this->assertInstanceOf(RedirectResponse::class, $resp);
$this->assertEquals('https://www.google.com/a/b/c/?d=e&f=g', $resp->getTargetUrl());
}

public function testHttpsIgnore()
{
$ctx = new Context(Request::create('https://www.google.com/'));
$route = InsecureRequestUpgradeRoute::i();
$this->assertFalse($route->match($ctx));
}
}

0 comments on commit a43b52a

Please sign in to comment.