|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Magento\Framework\App\Response { |
| 4 | + if (!class_exists(HttpFactory::class, false)) { |
| 5 | + class HttpFactory |
| 6 | + { |
| 7 | + public function create() |
| 8 | + { |
| 9 | + } |
| 10 | + } |
| 11 | + } |
| 12 | +} |
| 13 | + |
| 14 | +namespace Magento\Framework\View\Element { |
| 15 | + if (!class_exists(TemplateFactory::class, false)) { |
| 16 | + class TemplateFactory |
| 17 | + { |
| 18 | + public function create() |
| 19 | + { |
| 20 | + } |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +namespace Sansec\Shield\Test\Plugin { |
| 26 | + |
| 27 | + use Magento\Framework\App\FrontControllerInterface; |
| 28 | + use Magento\Framework\App\Response\HttpFactory as HttpResponseFactory; |
| 29 | + use Magento\Framework\View\Element\TemplateFactory; |
| 30 | + use PHPUnit\Framework\MockObject\Rule\InvocationOrder; |
| 31 | + use PHPUnit\Framework\TestCase; |
| 32 | + use Sansec\Shield\Model\Config; |
| 33 | + use Sansec\Shield\Model\IP; |
| 34 | + use Sansec\Shield\Model\Report; |
| 35 | + use Sansec\Shield\Model\Waf; |
| 36 | + use Sansec\Shield\Plugin\Shield; |
| 37 | + use Sansec\Shield\Test\RequestStub; |
| 38 | + |
| 39 | + class ShieldTest extends TestCase |
| 40 | + { |
| 41 | + /** @var array */ |
| 42 | + private $serverBackup; |
| 43 | + |
| 44 | + protected function setUp(): void |
| 45 | + { |
| 46 | + $this->serverBackup = $_SERVER; |
| 47 | + } |
| 48 | + |
| 49 | + protected function tearDown(): void |
| 50 | + { |
| 51 | + $_SERVER = $this->serverBackup; |
| 52 | + } |
| 53 | + |
| 54 | + private function buildPlugin(array $whitelistedIps, InvocationOrder $expectedWafCalls): Shield |
| 55 | + { |
| 56 | + $config = $this->createMock(Config::class); |
| 57 | + $config->method('isEnabled')->willReturn(true); |
| 58 | + $config->method('getWhitelistedIps')->willReturn($whitelistedIps); |
| 59 | + |
| 60 | + $waf = $this->createMock(Waf::class); |
| 61 | + $waf->expects($expectedWafCalls)->method('matchRequest')->willReturn([]); |
| 62 | + |
| 63 | + return new Shield( |
| 64 | + $config, |
| 65 | + $waf, |
| 66 | + $this->createMock(Report::class), |
| 67 | + new IP(), |
| 68 | + $this->createMock(HttpResponseFactory::class), |
| 69 | + $this->createMock(TemplateFactory::class) |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + private function dispatch(Shield $plugin): bool |
| 74 | + { |
| 75 | + $proceedCalled = false; |
| 76 | + $plugin->aroundDispatch( |
| 77 | + $this->createMock(FrontControllerInterface::class), |
| 78 | + function () use (&$proceedCalled) { |
| 79 | + $proceedCalled = true; |
| 80 | + }, |
| 81 | + new RequestStub() |
| 82 | + ); |
| 83 | + return $proceedCalled; |
| 84 | + } |
| 85 | + |
| 86 | + public function testRemoteAddrInWhitelistBypassesWaf() |
| 87 | + { |
| 88 | + $_SERVER['REMOTE_ADDR'] = '203.0.113.42'; |
| 89 | + $plugin = $this->buildPlugin(['203.0.113.42'], $this->never()); |
| 90 | + $this->assertTrue($this->dispatch($plugin)); |
| 91 | + } |
| 92 | + |
| 93 | + public function testForwardedHeaderInWhitelistDoesNotBypassWaf() |
| 94 | + { |
| 95 | + $_SERVER['REMOTE_ADDR'] = '198.51.100.1'; |
| 96 | + $_SERVER['HTTP_X_FORWARDED_FOR'] = '203.0.113.42'; |
| 97 | + $_SERVER['HTTP_CF_CONNECTING_IP'] = '203.0.113.42'; |
| 98 | + $plugin = $this->buildPlugin(['203.0.113.42'], $this->once()); |
| 99 | + $this->dispatch($plugin); |
| 100 | + } |
| 101 | + |
| 102 | + public function testEmptyWhitelistDoesNotBypassWaf() |
| 103 | + { |
| 104 | + $_SERVER['REMOTE_ADDR'] = '203.0.113.42'; |
| 105 | + $plugin = $this->buildPlugin([], $this->once()); |
| 106 | + $this->dispatch($plugin); |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments