|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yousha\PhpSecurityLinter\Tests\Unit\Rules; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Yousha\PhpSecurityLinter\Rules\OwaspRules; |
| 9 | + |
| 10 | +final class OwaspRulesTest extends TestCase |
| 11 | +{ |
| 12 | + private array $rules; |
| 13 | + |
| 14 | + protected function setUp(): void |
| 15 | + { |
| 16 | + parent::setUp(); |
| 17 | + $this->rules = OwaspRules::getRules(); |
| 18 | + } |
| 19 | + |
| 20 | + public function ruleDataProvider(): array |
| 21 | + { |
| 22 | + $rules = OwaspRules::getRules(); |
| 23 | + $data = []; |
| 24 | + foreach ($rules as $rule) { |
| 25 | + // Extract the ID from the message |
| 26 | + $idMatch = []; |
| 27 | + preg_match('/^(OWASP-[A-Z0-9-]+):/', (string) $rule['message'], $idMatch); |
| 28 | + $id = $idMatch[1] ?? 'unknown'; |
| 29 | + $data[] = [$rule, $id]; |
| 30 | + } |
| 31 | + |
| 32 | + return $data; |
| 33 | + } |
| 34 | + |
| 35 | + public function testOwaspA07WeakPasswordHashPattern(): void |
| 36 | + { |
| 37 | + $pattern = null; |
| 38 | + foreach ($this->rules as $rule) { |
| 39 | + if (str_starts_with((string) $rule['message'], 'OWASP-A07:')) { |
| 40 | + $pattern = $rule['pattern']; |
| 41 | + break; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + $this->assertNotNull($pattern, 'OWASP-A07 (Weak Hash) rule not found'); |
| 46 | + |
| 47 | + $vulnerableCode1 = '<?php $hash = hash("md5", $password); ?>'; |
| 48 | + $vulnerableCode2 = '<?php $hash = hash("sha1", $data); ?>'; |
| 49 | + $vulnerableCode3 = '<?php $hash = hash("md2", $input); ?>'; |
| 50 | + $vulnerableCode4 = '<?php $hash = hash("md4", $input); ?>'; |
| 51 | + $vulnerableCode5 = '<?php $hash = hash("sha224", $input); ?>'; |
| 52 | + |
| 53 | + $this->assertMatchesRegularExpression($pattern, $vulnerableCode1); |
| 54 | + $this->assertMatchesRegularExpression($pattern, $vulnerableCode2); |
| 55 | + $this->assertMatchesRegularExpression($pattern, $vulnerableCode3); |
| 56 | + $this->assertMatchesRegularExpression($pattern, $vulnerableCode4); |
| 57 | + $this->assertMatchesRegularExpression($pattern, $vulnerableCode5); |
| 58 | + |
| 59 | + $safeCode1 = '<?php $hash = hash("sha256", $password); ?>'; |
| 60 | + $safeCode2 = '<?php $hash = hash("sha512", $data); ?>'; |
| 61 | + $safeCode3 = '<?php $hash = password_hash($password, PASSWORD_DEFAULT); ?>'; |
| 62 | + |
| 63 | + $this->assertDoesNotMatchRegularExpression($pattern, $safeCode1); |
| 64 | + $this->assertDoesNotMatchRegularExpression($pattern, $safeCode2); |
| 65 | + $this->assertDoesNotMatchRegularExpression($pattern, $safeCode3); |
| 66 | + } |
| 67 | + |
| 68 | + public function testOwaspA08InsecureDeserializationUnserializePattern(): void |
| 69 | + { |
| 70 | + $pattern = null; |
| 71 | + foreach ($this->rules as $rule) { |
| 72 | + if (str_starts_with((string) $rule['message'], 'OWASP-A08:')) { |
| 73 | + if (str_contains((string) $rule['message'], 'unserialize')) { |
| 74 | + $pattern = $rule['pattern']; |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + $this->assertNotNull($pattern, 'OWASP-A08 (unserialize) rule not found'); |
| 81 | + |
| 82 | + $vulnerableCode1 = '<?php $data = unserialize($_GET["obj"]); ?>'; |
| 83 | + $vulnerableCode2 = '<?php $data = unserialize($_POST["data"]); ?>'; |
| 84 | + $vulnerableCode3 = '<?php $data = unserialize($_REQUEST["input"]); ?>'; |
| 85 | + $vulnerableCode4 = '<?php $data = unserialize($_COOKIE["stored"]); ?>'; |
| 86 | + |
| 87 | + $this->assertMatchesRegularExpression($pattern, $vulnerableCode1); |
| 88 | + $this->assertMatchesRegularExpression($pattern, $vulnerableCode2); |
| 89 | + $this->assertMatchesRegularExpression($pattern, $vulnerableCode3); |
| 90 | + $this->assertMatchesRegularExpression($pattern, $vulnerableCode4); |
| 91 | + |
| 92 | + // Safe code should not match |
| 93 | + $safeCode1 = '<?php $data = unserialize($trustedData); ?>'; // Variable, not $_GET/POST |
| 94 | + $safeCode2 = '<?php $data = unserialize("a:1:{s:5:\"hello\";s:5:\"world\";}"); ?>'; // Hardcoded string |
| 95 | + |
| 96 | + $this->assertDoesNotMatchRegularExpression($pattern, $safeCode1); |
| 97 | + $this->assertDoesNotMatchRegularExpression($pattern, $safeCode2); |
| 98 | + } |
| 99 | + |
| 100 | + public function testOwaspA10SsrFPattern(): void |
| 101 | + { |
| 102 | + $pattern = null; |
| 103 | + foreach ($this->rules as $rule) { |
| 104 | + if (str_starts_with((string) $rule['message'], 'OWASP-A10:')) { |
| 105 | + $pattern = $rule['pattern']; |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + $this->assertNotNull($pattern, 'OWASP-A10 rule not found'); |
| 111 | + |
| 112 | + $vulnerableCode1 = '<?php $content = file_get_contents($_GET["url"]); ?>'; |
| 113 | + $vulnerableCode2 = '<?php $fp = fopen($_POST["resource"], "r"); ?>'; |
| 114 | + $vulnerableCode3 = '<?php $ch = curl_init($_REQUEST["endpoint"]); ?>'; |
| 115 | + $vulnerableCode4 = '<?php $fp = fsockopen($_GET["host"], 80); ?>'; |
| 116 | + $vulnerableCode5 = '<?php $fp = stream_socket_client("tcp://" . $_POST["address"] . ":80"); ?>'; |
| 117 | + |
| 118 | + $this->assertMatchesRegularExpression($pattern, $vulnerableCode1); |
| 119 | + $this->assertMatchesRegularExpression($pattern, $vulnerableCode2); |
| 120 | + $this->assertMatchesRegularExpression($pattern, $vulnerableCode3); |
| 121 | + $this->assertMatchesRegularExpression($pattern, $vulnerableCode4); |
| 122 | + $this->assertMatchesRegularExpression($pattern, $vulnerableCode5); |
| 123 | + |
| 124 | + // Safe code should not match |
| 125 | + $safeCode1 = '<?php $content = file_get_contents("https://api.example.com/data"); ?>'; // Hardcoded URL |
| 126 | + $safeCode2 = '<?php $fp = fopen($validatedUrl, "r"); ?>'; // Variable, not $_GET/POST |
| 127 | + |
| 128 | + $this->assertDoesNotMatchRegularExpression($pattern, $safeCode1); |
| 129 | + $this->assertDoesNotMatchRegularExpression($pattern, $safeCode2); |
| 130 | + } |
| 131 | + |
| 132 | + public function testOwaspA07ReflectedXssPattern(): void |
| 133 | + { |
| 134 | + $pattern = null; |
| 135 | + foreach ($this->rules as $rule) { |
| 136 | + if (str_starts_with((string) $rule['message'], 'OWASP-A07:')) { |
| 137 | + if (str_contains((string) $rule['message'], 'Reflected XSS')) { |
| 138 | + $pattern = $rule['pattern']; |
| 139 | + break; |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + $this->assertNotNull($pattern, 'OWASP-A07 (Reflected XSS) rule not found'); |
| 145 | + |
| 146 | + $vulnerableCode1 = '<?php echo $_GET["name"]; ?>'; |
| 147 | + $vulnerableCode2 = '<?php echo $_POST["comment"]; ?>'; |
| 148 | + $vulnerableCode3 = '<?php echo $_REQUEST["input"]; ?>'; |
| 149 | + $vulnerableCode4 = '<?php echo $_COOKIE["username"]; ?>'; |
| 150 | + |
| 151 | + $this->assertMatchesRegularExpression($pattern, $vulnerableCode1); |
| 152 | + $this->assertMatchesRegularExpression($pattern, $vulnerableCode2); |
| 153 | + $this->assertMatchesRegularExpression($pattern, $vulnerableCode3); |
| 154 | + $this->assertMatchesRegularExpression($pattern, $vulnerableCode4); |
| 155 | + |
| 156 | + // Safe code should not match (or might need more specific pattern) |
| 157 | + $safeCode1 = '<?php echo htmlspecialchars($_GET["name"]); ?>'; // Output is sanitized |
| 158 | + |
| 159 | + $safeCode2 = '<?php echo $safeVariable; ?>'; // Variable, not $_GET/POST |
| 160 | + $this->assertDoesNotMatchRegularExpression($pattern, $safeCode2); |
| 161 | + } |
| 162 | +} |
0 commit comments