From 4ec877d299bcdef1da021dc8012d928b5929611f Mon Sep 17 00:00:00 2001 From: Mehmet <101359750+KoseceMehmet@users.noreply.github.com> Date: Sun, 8 Nov 2026 17:47:15 +0300 Subject: [PATCH] Search: quote parentheses and OR when serialising terms (#9541) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `FreshRSS_Search::__toString()` did not quote values containing parentheses or the `OR` keyword, so serialising a parsed search and parsing it again could change its structure (e.g. `"a(b"` was serialised as `a(b`). This makes the serialisation lossless for such values, and adds tests, including a round-trip test (parse → toString → parse). --- app/Models/Search.php | 2 +- tests/app/Models/SearchTest.php | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/app/Models/Search.php b/app/Models/Search.php index 596f7217c..befdd5596 100644 --- a/app/Models/Search.php +++ b/app/Models/Search.php @@ -173,7 +173,7 @@ class FreshRSS_Search implements \Stringable { // Discard user queries return $s; } - if (strpbrk($s, ' "\'\\/:') !== false || $s === '') { + if (strpbrk($s, ' "\'\\/:()') !== false || $s === '' || preg_match('/\bOR\b/i', $s) === 1) { return '"' . addcslashes($s, '"') . '"'; } return $s; diff --git a/tests/app/Models/SearchTest.php b/tests/app/Models/SearchTest.php index 82e93161d..e3f06d4a2 100644 --- a/tests/app/Models/SearchTest.php +++ b/tests/app/Models/SearchTest.php @@ -1234,9 +1234,45 @@ final class SearchTest extends \PHPUnit\Framework\TestCase { 'intitle:/^A B OR C D/i', 'intitle:/^A B OR C D/i', ], + [ + '"a(b"', + '"a(b"', + ], + [ + 'intitle:"(test)"', + 'intitle:"(test)"', + ], + [ + '"a)OR(b"', + '"a)OR(b"', + ], + [ + '"x.OR.y"', + '"x.OR.y"', + ], ]; } + #[DataProvider('provideBooleanSearchToStringRoundTrip')] + public static function testBooleanSearchToStringRoundTrip(string $input): void { + $search = new FreshRSS_BooleanSearch($input); + $reparsed = new FreshRSS_BooleanSearch($search->toString()); + self::assertSame($search->toString(), $reparsed->toString()); + } + + /** @return list */ + public static function provideBooleanSearchToStringRoundTrip(): array { + return [ + ['"a(b"'], + ['"a)OR(b"'], + ['"a b" ("cd)OR(ef")'], + ['intitle:"(x)" author:"a OR b"'], + ['"x.OR.y" OR z'], + ['intitle:/^(ab|cd)/ "e)f"'], + ]; + } + + /** * @param list $queries */