|
7 | 7 | namespace test\unit\Ingenerator\PHPUtils\Session;
|
8 | 8 |
|
9 | 9 |
|
| 10 | +use Closure; |
10 | 11 | use Ingenerator\PHPUtils\Session\MysqlSession;
|
| 12 | +use PDO; |
| 13 | +use PDOStatement; |
| 14 | +use PHPUnit\Framework\Attributes\DataProvider; |
11 | 15 | use PHPUnit\Framework\TestCase;
|
| 16 | +use UnexpectedValueException; |
12 | 17 |
|
13 | 18 | class MysqlSessionTest extends TestCase
|
14 | 19 | {
|
| 20 | + private array $old_ini_vars = []; |
15 | 21 |
|
16 | 22 | public function test_it_is_initialisable()
|
17 | 23 | {
|
18 | 24 | $this->assertInstanceOf(MysqlSession::class, $this->newSubject());
|
19 | 25 | }
|
20 | 26 |
|
21 |
| - protected function newSubject() |
| 27 | + public static function provider_validate_invalid_sid(): array |
22 | 28 | {
|
23 |
| - return new MysqlSession(new PDOMock, 'insecure-salt'); |
| 29 | + return [ |
| 30 | + 'with path injection attempt' => [ |
| 31 | + [], |
| 32 | + '../../../../../../../../../../../../../../windows/win.ini', |
| 33 | + ], |
| 34 | + 'with url injection attempt' => [ |
| 35 | + [], |
| 36 | + 'http://some-inexistent-website.acu/some_inexistent_file_with_long_name?.jpg', |
| 37 | + ], |
| 38 | + 'with SQL injection attempt' => [ |
| 39 | + [], |
| 40 | + "'; TRUNCATE sessions;", |
| 41 | + ], |
| 42 | + 'with invalid chars (in default bits per character)' => [ |
| 43 | + [], |
| 44 | + str_pad('v', 32, 'a'), |
| 45 | + ], |
| 46 | + 'with SID too short (with default length configuration)' => [ |
| 47 | + [], |
| 48 | + str_repeat('a', 31), |
| 49 | + ], |
| 50 | + 'with SID too long (with default length configuration)' => [ |
| 51 | + [], |
| 52 | + str_repeat('a', 33), |
| 53 | + ], |
| 54 | + 'with invalid characters (using custom bits)' => [ |
| 55 | + ['session.sid_bits_per_character' => '5'], |
| 56 | + str_pad(',', 32, 'a'), |
| 57 | + ], |
| 58 | + 'too long (with custom length)' => [ |
| 59 | + ['session.sid_length' => '44'], |
| 60 | + str_repeat('a', 45), |
| 61 | + ], |
| 62 | + 'too short (with custom length)' => [ |
| 63 | + ['session.sid_length' => '44'], |
| 64 | + str_repeat('a', 43), |
| 65 | + ], |
| 66 | + ]; |
24 | 67 | }
|
25 | 68 |
|
26 |
| -} |
| 69 | + #[DataProvider('provider_validate_invalid_sid')] |
| 70 | + public function test_it_rejects_invalid_session_ids_without_checking_database(array $config, string $sid): void |
| 71 | + { |
| 72 | + $this->configurePhpIniVars($config); |
| 73 | + $subject = $this->newSubject(pdo: $this->mockPDOExpectingNoCalls()); |
| 74 | + $this->assertFalse($subject->validateId($sid)); |
| 75 | + } |
| 76 | + |
| 77 | + public static function provider_validate_own_sid(): array |
| 78 | + { |
| 79 | + return [ |
| 80 | + 'with default config' => [ |
| 81 | + [], |
| 82 | + '/^[0-9a-f]{32}$/', |
| 83 | + ], |
| 84 | + 'with 5 bits per char' => [ |
| 85 | + ['session.sid_bits_per_character' => '5'], |
| 86 | + '/^[0-9a-v]{32}$/', |
| 87 | + ], |
| 88 | + 'with 6 bits per char' => [ |
| 89 | + ['session.sid_bits_per_character' => '6'], |
| 90 | + '/^[0-9a-zA-Z,-]{32}$/', |
| 91 | + ], |
| 92 | + 'with custom length' => [ |
| 93 | + ['session.sid_length' => '22'], |
| 94 | + '/^[0-9a-f]{22}$/', |
| 95 | + ], |
| 96 | + 'with custom length and chars' => [ |
| 97 | + ['session.sid_length' => '22', 'session.sid_bits_per_character' => '5'], |
| 98 | + '/^[0-9a-v,-]{22}$/', |
| 99 | + ], |
| 100 | + ]; |
| 101 | + } |
| 102 | + |
| 103 | + #[DataProvider('provider_validate_own_sid')] |
| 104 | + public function test_sid_that_it_creates_is_valid(array $config, string $expected_pattern): void |
| 105 | + { |
| 106 | + $this->configurePhpIniVars($config); |
| 107 | + |
| 108 | + // This mocking isn't very nice - it is coupled to the implementation details of the SQL queries and the |
| 109 | + // results the class expects (and how it fetches them internally). I've tried to limit that as much as possible, |
| 110 | + // it would obviously be better if this ran as an integration test against an actual mysql instance - however |
| 111 | + // really here I only want to test that the validation is done against the database e.g. not short-circuited by |
| 112 | + // the pattern matching. |
| 113 | + $pdo = $this->mockPDOToPrepareQueries(function ($query) { |
| 114 | + $result = $this->createMock(PDOStatement::class); |
| 115 | + |
| 116 | + if (str_starts_with($query, 'INSERT INTO `sessions`')) { |
| 117 | + // Result of the query is never read |
| 118 | + return $result; |
| 119 | + } |
| 120 | + |
| 121 | + if (str_starts_with($query, 'SELECT GET_LOCK')) { |
| 122 | + // Just needs to return 1 to show that the lock is acquired. Note that validateSid does not release the |
| 123 | + // lock if it successfully loads a session (it'll be released on session_write_close or end of request) |
| 124 | + $result->expects($this->once())->method('fetchColumn')->willReturn('1'); |
| 125 | + return $result; |
| 126 | + } |
| 127 | + |
| 128 | + if (str_starts_with($query, 'SELECT `session_data`')) { |
| 129 | + // Just needs to return some data, even empty |
| 130 | + $result->expects($this->once())->method('fetchAll')->willReturn([['session_data' => serialize([])]]); |
| 131 | + return $result; |
| 132 | + } |
| 133 | + |
| 134 | + throw new UnexpectedValueException('Un-mocked query '.$query); |
| 135 | + }); |
| 136 | + |
| 137 | + $subject = $this->newSubject(pdo: $pdo); |
| 138 | + |
| 139 | + $sid = $subject->create_sid(); |
| 140 | + // Sanity check that the settings applied as expected |
| 141 | + $this->assertMatchesRegularExpression($expected_pattern, $sid, 'Should generate SID in expected format'); |
| 142 | + |
| 143 | + $this->assertTrue($subject->validateId($sid), 'Should validate SID "'.$sid.'"'); |
| 144 | + } |
| 145 | + |
| 146 | + protected function setUp(): void |
| 147 | + { |
| 148 | + parent::setUp(); |
| 149 | + // Force to the normal default configs |
| 150 | + $this->configurePhpIniVars([ |
| 151 | + 'session.sid_bits_per_character' => '4', |
| 152 | + 'session.sid_length' => '32', |
| 153 | + ]); |
| 154 | + } |
| 155 | + |
| 156 | + protected function tearDown(): void |
| 157 | + { |
| 158 | + parent::tearDown(); |
| 159 | + foreach ($this->old_ini_vars as $key => $value) { |
| 160 | + @ini_set($key, $value); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + protected function newSubject( |
| 165 | + ?PDO $pdo = null |
| 166 | + ) |
| 167 | + { |
| 168 | + return new MysqlSession( |
| 169 | + $pdo ?? $this->mockPDOExpectingNoCalls(), |
| 170 | + 'insecure-salt', |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + private function mockPDOExpectingNoCalls(): PDO |
| 175 | + { |
| 176 | + $pdo = $this->createMock(PDO::class); |
| 177 | + $pdo->expects($this->never())->method($this->anything()); |
| 178 | + return $pdo; |
| 179 | + } |
| 180 | + |
| 181 | + private function configurePhpIniVars(array $config): void |
| 182 | + { |
| 183 | + foreach ($config as $setting => $value) { |
| 184 | + $this->old_ini_vars[$setting] = @ini_set($setting, $value); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + private function mockPDOToPrepareQueries(Closure $callback): PDO |
| 189 | + { |
| 190 | + $pdo = $this->createMock(PDO::class); |
| 191 | + $pdo->expects($this->any())->method('prepare')->willReturnCallback($callback); |
| 192 | + return $pdo; |
| 193 | + } |
27 | 194 |
|
28 |
| -class PDOMock extends \PDO { |
29 |
| - public function __construct() {} |
30 | 195 | }
|
0 commit comments