|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Patchstack\Processor; |
| 7 | +use Patchstack\Extensions\Test\Extension; |
| 8 | + |
| 9 | +final class FirewallDatasetTest extends TestCase |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @var Processor |
| 13 | + */ |
| 14 | + protected $processor; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var array |
| 18 | + */ |
| 19 | + protected $rules; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var array |
| 23 | + */ |
| 24 | + protected $datasets; |
| 25 | + |
| 26 | + /** |
| 27 | + * Setup the test for testing the header location redirect. |
| 28 | + * |
| 29 | + * @return void |
| 30 | + */ |
| 31 | + protected function setUp(): void |
| 32 | + { |
| 33 | + $this->datasets = json_decode(file_get_contents(dirname(__FILE__) . '/data/Datasets.json'), true); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Setup the firewall processor. |
| 38 | + * |
| 39 | + * @param array $rules |
| 40 | + * @return void |
| 41 | + */ |
| 42 | + private function setUpFirewallProcessor(array $rules) |
| 43 | + { |
| 44 | + $this->processor = new Processor( |
| 45 | + new Extension(), |
| 46 | + $rules, |
| 47 | + [], |
| 48 | + [], |
| 49 | + [], |
| 50 | + $this->datasets |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Alters the payload between tests. |
| 56 | + * For most firewall rules there's no difference if testing against GET or POST. |
| 57 | + * Therefore, both can be used for testing payloads. |
| 58 | + * |
| 59 | + * @return void |
| 60 | + */ |
| 61 | + private function alterPayload(array $payload = []) |
| 62 | + { |
| 63 | + $_POST = []; |
| 64 | + $_GET = []; |
| 65 | + |
| 66 | + $_POST = isset($payload['POST']) ? $payload['POST'] : []; |
| 67 | + $_GET = isset($payload['GET']) ? $payload['GET'] : []; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Test specific firewall rules. |
| 72 | + * |
| 73 | + * @return void |
| 74 | + */ |
| 75 | + public function testRules() |
| 76 | + { |
| 77 | + // Since the Opis/Closure package does not support PHP 8.1+, we have to use Laravel's ported version for 8.1+. |
| 78 | + require dirname(__FILE__) . '/../vendor/autoload.php'; |
| 79 | + if (PHP_VERSION_ID < 80100) { |
| 80 | + \Opis\Closure\SerializableClosure::setSecretKey('secret'); |
| 81 | + class_alias('\Opis\Closure\SerializableClosure', 'SerializeClosure'); |
| 82 | + } else { |
| 83 | + \Laravel\SerializableClosure\SerializableClosure::setSecretKey('secret'); |
| 84 | + class_alias('\Laravel\SerializableClosure\SerializableClosure', 'SerializeClosure'); |
| 85 | + } |
| 86 | + |
| 87 | + // For easier access we store the datasets inside of $datasets as it will function |
| 88 | + // like this in the firewall rule processor as well. |
| 89 | + $datasets = $this->datasets; |
| 90 | + |
| 91 | + // Create the firewall rule. |
| 92 | + $function = function () use ($datasets) { |
| 93 | + return in_array($_SERVER['REMOTE_ADDR'], $datasets['ps_ips']); |
| 94 | + }; |
| 95 | + $wrapper = new SerializeClosure($function); |
| 96 | + $rule = (object) [ |
| 97 | + 'id' => 1, |
| 98 | + 'title' => 'Determine if IP is in blacklist', |
| 99 | + 'rule' => base64_encode(serialize($wrapper)), |
| 100 | + 'cat' => 'TEST', |
| 101 | + 'type' => 'BLOCK' |
| 102 | + ]; |
| 103 | + |
| 104 | + // Test the rule. |
| 105 | + $_SERVER['REMOTE_ADDR'] = '1.1.1.1'; |
| 106 | + $this->setUpFirewallProcessor([$rule]); |
| 107 | + $this->assertFalse($this->processor->launch(false)); |
| 108 | + $_SERVER['REMOTE_ADDR'] = ''; |
| 109 | + |
| 110 | + // Create the firewall rule. |
| 111 | + $function = function () use ($datasets) { |
| 112 | + return preg_match($datasets['ps_sqli'], $_GET['id']) === 1; |
| 113 | + }; |
| 114 | + $wrapper = new SerializeClosure($function); |
| 115 | + $rule = (object) [ |
| 116 | + 'id' => 1, |
| 117 | + 'title' => 'Determine if union all select regex is a hit in the id GET parameter', |
| 118 | + 'rule' => base64_encode(serialize($wrapper)), |
| 119 | + 'cat' => 'TEST', |
| 120 | + 'type' => 'BLOCK' |
| 121 | + ]; |
| 122 | + |
| 123 | + // Test the rule. |
| 124 | + $this->setUpFirewallProcessor([$rule]); |
| 125 | + $this->alterPayload( |
| 126 | + ['GET' => [ |
| 127 | + 'id' => '1 UNION ALL SELECT 1,2,3,4,5,@@version-- ' |
| 128 | + ]] |
| 129 | + ); |
| 130 | + $this->assertFalse($this->processor->launch(false)); |
| 131 | + $this->alterPayload(); |
| 132 | + } |
| 133 | +} |
0 commit comments