|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\Session\Handlers\Database; |
| 15 | + |
| 16 | +use CodeIgniter\Test\CIUnitTestCase; |
| 17 | +use CodeIgniter\Test\Mock\MockConnection; |
| 18 | +use CodeIgniter\Test\Mock\MockResult; |
| 19 | +use Config\Session as SessionConfig; |
| 20 | +use PHPUnit\Framework\Attributes\Group; |
| 21 | + |
| 22 | +/** |
| 23 | + * @internal |
| 24 | + */ |
| 25 | +#[Group('Others')] |
| 26 | +final class PostgreHandlerMockTest extends CIUnitTestCase |
| 27 | +{ |
| 28 | + private function createHandler(bool $matchIP, object $mockDb): PostgreHandler |
| 29 | + { |
| 30 | + $config = new SessionConfig(); |
| 31 | + $config->driver = PostgreHandler::class; |
| 32 | + $config->savePath = 'ci_sessions'; |
| 33 | + $config->matchIP = $matchIP; |
| 34 | + |
| 35 | + $handler = new PostgreHandler($config, "192.168.1.1' OR '1'='1"); |
| 36 | + $this->setPrivateProperty($handler, 'db', $mockDb); |
| 37 | + |
| 38 | + return $handler; |
| 39 | + } |
| 40 | + |
| 41 | + private function createMockDb(): object |
| 42 | + { |
| 43 | + return new class (['DBDriver' => 'Postgre']) extends MockConnection { |
| 44 | + /** |
| 45 | + * @var list<array{sql: string, binds: array<array-key, mixed>|null}> |
| 46 | + */ |
| 47 | + public array $executed = []; |
| 48 | + |
| 49 | + public function query(string $sql, $binds = null, bool $setEscapeFlags = true, string $queryClass = ''): MockResult |
| 50 | + { |
| 51 | + $this->executed[] = [ |
| 52 | + 'sql' => $sql, |
| 53 | + 'binds' => is_array($binds) ? $binds : null, |
| 54 | + ]; |
| 55 | + |
| 56 | + return new MockResult($this->connID, $this->resultID); |
| 57 | + } |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + public function testLockAndReleaseSessionWithoutMatchIP(): void |
| 62 | + { |
| 63 | + $db = $this->createMockDb(); |
| 64 | + $handler = $this->createHandler(false, $db); |
| 65 | + |
| 66 | + $lockSession = self::getPrivateMethodInvoker($handler, 'lockSession'); |
| 67 | + $releaseLock = self::getPrivateMethodInvoker($handler, 'releaseLock'); |
| 68 | + |
| 69 | + $sessionID = "sess'id; DROP TABLE users; --"; |
| 70 | + $this->assertTrue($lockSession($sessionID)); |
| 71 | + $this->assertTrue($releaseLock()); |
| 72 | + |
| 73 | + $this->assertCount(2, $db->executed); |
| 74 | + |
| 75 | + $this->assertSame('SELECT pg_advisory_lock(hashtext(?))', $db->executed[0]['sql']); |
| 76 | + $this->assertSame([$sessionID], $db->executed[0]['binds']); |
| 77 | + |
| 78 | + $this->assertSame('SELECT pg_advisory_unlock(hashtext(?))', $db->executed[1]['sql']); |
| 79 | + $this->assertSame([$sessionID], $db->executed[1]['binds']); |
| 80 | + } |
| 81 | + |
| 82 | + public function testLockAndReleaseSessionWithMatchIP(): void |
| 83 | + { |
| 84 | + $db = $this->createMockDb(); |
| 85 | + $handler = $this->createHandler(true, $db); |
| 86 | + |
| 87 | + $lockSession = self::getPrivateMethodInvoker($handler, 'lockSession'); |
| 88 | + $releaseLock = self::getPrivateMethodInvoker($handler, 'releaseLock'); |
| 89 | + |
| 90 | + $sessionID = "sess'id; DROP TABLE users; --"; |
| 91 | + $this->assertTrue($lockSession($sessionID)); |
| 92 | + $this->assertTrue($releaseLock()); |
| 93 | + |
| 94 | + $this->assertCount(2, $db->executed); |
| 95 | + |
| 96 | + $this->assertSame('SELECT pg_advisory_lock(hashtext(?), hashtext(?))', $db->executed[0]['sql']); |
| 97 | + $this->assertSame([$sessionID, "192.168.1.1' OR '1'='1"], $db->executed[0]['binds']); |
| 98 | + |
| 99 | + $this->assertSame('SELECT pg_advisory_unlock(hashtext(?), hashtext(?))', $db->executed[1]['sql']); |
| 100 | + $this->assertSame([$sessionID, "192.168.1.1' OR '1'='1"], $db->executed[1]['binds']); |
| 101 | + } |
| 102 | +} |
0 commit comments