Explorar el Código

A few misc. security enhancements (#9591)

Alexandre Alapetite hace 12 horas
padre
commit
289a00be00

+ 1 - 1
app/Controllers/extensionController.php

@@ -361,7 +361,7 @@ class FreshRSS_extension_Controller extends FreshRSS_ActionController {
 		header("Content-Type: {$content_type}");
 		header("Content-Disposition: inline; filename='{$filename}'");
 		header('Referrer-Policy: same-origin');
-		if (file_exists(DATA_PATH . '/no-cache.txt') || !httpConditional($mtime, cacheSeconds: 604800, cachePrivacy: 2)) {
+		if (file_exists(DATA_PATH . '/no-cache.txt') || !httpConditional($mtime, cacheSeconds: 604800, cachePrivacy: 0)) {
 			$contents = $extension->getFile($filename);
 			$this->view->content = is_string($contents) ? $contents : '';
 		}

+ 1 - 1
app/Controllers/indexController.php

@@ -112,7 +112,7 @@ class FreshRSS_index_Controller extends FreshRSS_ActionController {
 		}
 
 		$id = Minz_Request::paramInt('id');
-		if ($id !== 0) {
+		if ($id !== 0 && FreshRSS_Auth::hasAccess()) {
 			if (Minz_Request::paramString('type') === 'tag') {
 				$tagDAO = FreshRSS_Factory::createTagDao();
 				$tag = $tagDAO->searchById($id);

+ 1 - 1
app/Models/BooleanSearch.php

@@ -67,7 +67,7 @@ class FreshRSS_BooleanSearch implements \Stringable {
 		if (preg_match_all('/\bsearch:(?P<delim>[\'"])(?P<search>.*)(?P=delim)/U', $input, $matchesFound)) {
 			$all_matches[] = $matchesFound;
 		}
-		if (preg_match_all('/\bsearch:(?P<search>[^\s"\']*)/', $input, $matchesFound)) {
+		if (preg_match_all('/\bsearch:(?P<search>(?:[^\s"\'()]|(?<=\\\\)[()])*)/', $input, $matchesFound)) {
 			$all_matches[] = $matchesFound;
 		}
 

+ 4 - 1
app/Models/Context.php

@@ -100,6 +100,9 @@ final class FreshRSS_Context {
 	 */
 	public static function initUser(string $username = '', bool $userMustExist = true): void {
 		FreshRSS_Context::$user_conf = null;
+		// Per-user caches must not survive a change of user:
+		self::$categories = [];
+		self::$tags = [];
 		if (!isset($_SESSION)) {
 			Minz_Session::init('FreshRSS');
 		}
@@ -303,7 +306,7 @@ final class FreshRSS_Context {
 			}
 		}
 
-		self::$number = Minz_Request::paramInt('nb') ?: FreshRSS_Context::userConf()->posts_per_page;
+		self::$number = max(0, Minz_Request::paramInt('nb')) ?: FreshRSS_Context::userConf()->posts_per_page;
 		if (self::$number > FreshRSS_Context::userConf()->max_posts_per_rss) {
 			self::$number = max(
 				FreshRSS_Context::userConf()->max_posts_per_rss,

+ 1 - 1
app/Utils/httpUtil.php

@@ -140,7 +140,7 @@ final class FreshRSS_http_Util {
 			if (is_string($idn) && $idn != '') {
 				$puny = idn_to_ascii($idn);
 				$pos = strpos($url, $idn);
-				if ($puny != false && $pos !== false) {
+				if ($pos !== false && is_string($puny) && preg_match('/^[a-z0-9._-]+$/iD', $puny) === 1) {
 					$url = substr_replace($url, $puny, $pos, strlen($idn));
 				}
 			}

+ 35 - 0
tests/app/Models/SearchTest.php

@@ -361,6 +361,29 @@ final class SearchTest extends \PHPUnit\Framework\TestCase {
 				],
 				'(search:"A or B") OR (search:"(C OR D)")',
 			],
+			'expanded name inside parentheses' => [
+				[
+					['search' => 'author:Alice', 'name' => 'First'],
+					['search' => 'intitle:World', 'name' => 'Second'],
+				],
+				'(inurl:a OR search:First) (-search:Second)',
+				[
+					'(((e.link LIKE ?)) OR ((e.author LIKE ?))) AND (NOT ((e.title LIKE ?)))',
+					['%a%', '%Alice%', '%World%'],
+				],
+				'(inurl:a OR search:First) (-search:Second)',
+			],
+			'expanded negated name as saved by a user query' => [
+				[
+					['search' => 'author:Alice', 'name' => 'First'],
+				],
+				'(-search:First) (inurl:a OR inurl:b)',
+				[
+					'(NOT ((e.author LIKE ?))) AND ((e.link LIKE ?) OR (e.link LIKE ?))',
+					['%Alice%', '%a%', '%b%'],
+				],
+				'(-search:First) (inurl:a OR inurl:b)',
+			],
 			'separate groups with AND' => [
 				[
 					['search' => 'author:Alice'],
@@ -1401,6 +1424,18 @@ final class SearchTest extends \PHPUnit\Framework\TestCase {
 				'-search:News -search:Newsletter',
 				'-author:Alice -author:Bob',
 			],
+			'Names inside parentheses' => [
+				$overlappingQueries,
+				'(-search:News) (a OR search:Newsletter)',
+				'(-search:News) (a OR search:Newsletter)',
+				'(-author:Alice) (a OR author:Bob)',
+			],
+			'Negated name before parentheses is saved inside parentheses' => [
+				$overlappingQueries,
+				'-search:News (a OR b)',
+				'(-search:News) (a OR b)',
+				'-author:Alice (a OR b)',
+			],
 		];
 	}
 

+ 20 - 0
tests/app/Utils/httpUtilTest.php

@@ -45,6 +45,26 @@ final class httpUtilTest extends \PHPUnit\Framework\TestCase {
 		];
 	}
 
+	#[DataProvider('provideUrlsForCheckUrl')]
+	public function test_checkUrl(string $url, string|false $expected): void {
+		self::assertSame($expected, FreshRSS_http_Util::checkUrl($url));
+	}
+
+	/** @return Traversable<string,array{string,string|false}> */
+	public static function provideUrlsForCheckUrl(): Traversable {
+		yield 'plain' => ['https://example.net/feed?a=1&amp;b=2', 'https://example.net/feed?a=1&amp;b=2'];
+		yield 'underscore' => ['https://ex_ample.net/', 'https://ex_ample.net/'];
+		yield 'IPv6' => ['https://[2001:db8::1]/feed', 'https://[2001:db8::1]/feed'];
+		if (function_exists('idn_to_ascii')) {
+			yield 'IDN' => ['https://bücher.example/feed', 'https://xn--bcher-kva.example/feed'];
+			yield 'IDN with ideographic full stop' => ['https://日本語。example/', 'https://xn--wgv71a119e.example/'];
+		}
+		// IDNA (UTS #46) maps fullwidth punctuation to ASCII: must not produce characters absent from the (possibly HTML-encoded) input
+		yield 'fullwidth markup in host' => ['https://example.net/"><b>', false];
+		yield 'fullwidth path and query in host' => ['https://example.net/path?a=1&b=2', false];
+		yield 'fullwidth userinfo in host' => ['https://example.net@example.org/', false];
+	}
+
 	#[\Override]
 	protected function tearDown(): void {
 		FreshRSS_Context::initSystem(reload: true);	// Restore the default internal host allowlist