|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Flarum. |
| 5 | + * |
| 6 | + * For detailed copyright and license information, please view the |
| 7 | + * LICENSE file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Flarum\Tests\integration\api\discussions; |
| 11 | + |
| 12 | +use Flarum\Discussion\Discussion; |
| 13 | +use Flarum\Post\Post; |
| 14 | +use Flarum\Testing\integration\RetrievesAuthorizedUsers; |
| 15 | +use Flarum\Testing\integration\TestCase; |
| 16 | +use PHPUnit\Framework\Attributes\Test; |
| 17 | + |
| 18 | +class PgsqlSearchConfigurationInjectionTest extends TestCase |
| 19 | +{ |
| 20 | + use RetrievesAuthorizedUsers; |
| 21 | + |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + parent::setUp(); |
| 25 | + |
| 26 | + if ($this->database()->getDriverName() !== 'pgsql') { |
| 27 | + $this->markTestSkipped('Postgres-specific filter path.'); |
| 28 | + } |
| 29 | + |
| 30 | + $this->database()->rollBack(); |
| 31 | + |
| 32 | + $this->database()->table('discussions')->insert($this->rowsThroughFactory(Discussion::class, [ |
| 33 | + ['id' => 1, 'title' => 'hello world', 'user_id' => 1], |
| 34 | + ])); |
| 35 | + $this->database()->table('posts')->insert($this->rowsThroughFactory(Post::class, [ |
| 36 | + ['id' => 1, 'discussion_id' => 1, 'user_id' => 1, 'content' => '<t><p>hello world body</p></t>'], |
| 37 | + ])); |
| 38 | + |
| 39 | + $this->database()->beginTransaction(); |
| 40 | + |
| 41 | + $this->populateDatabase(); |
| 42 | + } |
| 43 | + |
| 44 | + protected function tearDown(): void |
| 45 | + { |
| 46 | + parent::tearDown(); |
| 47 | + |
| 48 | + $this->database()->table('discussions')->delete(); |
| 49 | + $this->database()->table('posts')->delete(); |
| 50 | + |
| 51 | + // Reset the setting in case the post-condition assertion was hit before reset. |
| 52 | + $this->database()->table('settings')->where('key', 'pgsql_search_configuration')->update(['value' => 'english']); |
| 53 | + } |
| 54 | + |
| 55 | + #[Test] |
| 56 | + public function baseline_default_search_config_returns_200(): void |
| 57 | + { |
| 58 | + // Sanity check: with the default config, a search succeeds. |
| 59 | + $response = $this->send( |
| 60 | + $this->request('GET', '/api/discussions') |
| 61 | + ->withQueryParams(['filter' => ['q' => 'hello']]) |
| 62 | + ); |
| 63 | + |
| 64 | + $this->assertEquals(200, $response->getStatusCode(), (string) $response->getBody()); |
| 65 | + } |
| 66 | + |
| 67 | + #[Test] |
| 68 | + public function malicious_search_config_is_not_interpolated_into_sql(): void |
| 69 | + { |
| 70 | + // Admin (or anyone who can write to the settings table) attempts to inject |
| 71 | + // SQL via the pgsql_search_configuration setting. |
| 72 | + $payload = "english') || pg_sleep(0) || ('"; |
| 73 | + |
| 74 | + /** @var \Flarum\Settings\SettingsRepositoryInterface $settings */ |
| 75 | + $settings = $this->app()->getContainer()->make(\Flarum\Settings\SettingsRepositoryInterface::class); |
| 76 | + $settings->set('pgsql_search_configuration', $payload); |
| 77 | + |
| 78 | + $response = $this->send( |
| 79 | + $this->request('GET', '/api/discussions') |
| 80 | + ->withQueryParams(['filter' => ['q' => 'hello']]) |
| 81 | + ); |
| 82 | + |
| 83 | + $body = (string) $response->getBody(); |
| 84 | + |
| 85 | + // Under the original vulnerability, the payload was concatenated into |
| 86 | + // SQL as code: Postgres parsed `tsvector || pg_sleep(...)` and raised |
| 87 | + // SQLSTATE 42883 "operator does not exist: tsvector || void". |
| 88 | + // |
| 89 | + // Under the fix, the payload is bound as a parameter and cast to |
| 90 | + // `regconfig`. Postgres rejects it with SQLSTATE 42602 "invalid name |
| 91 | + // syntax" — the value reached the DB as data, not code. |
| 92 | + // |
| 93 | + // We assert on the SQLSTATE, which is unambiguous: 42883 ↔ injection |
| 94 | + // open, 42602 ↔ injection closed. |
| 95 | + $this->assertStringNotContainsString( |
| 96 | + '42883', |
| 97 | + $body, |
| 98 | + "Postgres tried to evaluate the payload as SQL (operator-does-not-exist error). Injection still possible.\nResponse body: $body" |
| 99 | + ); |
| 100 | + $this->assertStringContainsString( |
| 101 | + '42602', |
| 102 | + $body, |
| 103 | + "Expected Postgres to reject the payload at the regconfig cast (SQLSTATE 42602).\nResponse body: $body" |
| 104 | + ); |
| 105 | + } |
| 106 | +} |
0 commit comments