diff --git a/core/imageboard/search.php b/core/imageboard/search.php index bc25cb50a..2ef969a91 100644 --- a/core/imageboard/search.php +++ b/core/imageboard/search.php @@ -128,7 +128,11 @@ private static function find_images_internal(int $start = 0, ?int $limit = null, if (Extension::is_enabled(SpeedHaxInfo::KEY) && $config->get_int(SpeedHaxConfig::BIG_SEARCH) > 0) { $anon_limit = $config->get_int(SpeedHaxConfig::BIG_SEARCH); - if (!$user->can(Permissions::BIG_SEARCH) and count($tags) > $anon_limit) { + $counted_tags = $tags; + // exclude tags which start with "id>", "id<", or "order:id_" + // because those are added internally for post/next and post/prev + $counted_tags = array_filter($counted_tags, fn ($tag) => !\Safe\preg_match("/^id[><]|^order:id_/", $tag)); + if (!$user->can(Permissions::BIG_SEARCH) and count($counted_tags) > $anon_limit) { throw new PermissionDenied("Anonymous users may only search for up to $anon_limit tags at a time"); } } diff --git a/ext/speed_hax/test.php b/ext/speed_hax/test.php new file mode 100644 index 000000000..c2af16852 --- /dev/null +++ b/ext/speed_hax/test.php @@ -0,0 +1,56 @@ +set_int(SpeedHaxConfig::BIG_SEARCH, 1); + + $this->log_in_as_user(); + $image_id_1 = $this->post_image("tests/pbx_screenshot.jpg", "asdf post1"); + $image_id_2 = $this->post_image("tests/favicon.png", "asdf post2"); + + // default user isn't limited + $this->assert_search_results(["asdf"], [$image_id_2, $image_id_1], "User can search for one tag"); + $this->assert_search_results(["asdf", "post1"], [$image_id_1], "User can search for two tags"); + } + + public function testAnonTagLimit(): void + { + global $config; + $config->set_int(SpeedHaxConfig::BIG_SEARCH, 1); + + $this->log_in_as_user(); + $image_id_1 = $this->post_image("tests/pbx_screenshot.jpg", "asdf post1"); + $image_id_2 = $this->post_image("tests/favicon.png", "asdf post2"); + $this->log_out(); + + // default anon is limited + $this->assert_search_results(["asdf"], [$image_id_2, $image_id_1], "Anon can search for one tag"); + $this->assertException(PermissionDenied::class, function () use ($image_id_1) { + $this->assert_search_results(["asdf", "post1"], [$image_id_1]); + }); + } + + public function testAnonPostNext(): void + { + global $config; + $config->set_int(SpeedHaxConfig::BIG_SEARCH, 1); + + $this->log_in_as_user(); + $image_id_1 = $this->post_image("tests/pbx_screenshot.jpg", "asdf post1"); + $image_id_2 = $this->post_image("tests/favicon.png", "asdf post2"); + $this->log_out(); + + // post/next and post/prev use additional tags internally, + // but those ones shouldn't count towards the limit + $page = $this->get_page("post/next/$image_id_2", ["search" => "asdf"]); + $this->assertEquals(PageMode::REDIRECT, $page->mode); + $this->assertEquals($page->redirect, make_link("post/view/$image_id_1?#search=asdf")); + } +} diff --git a/ext/view/main.php b/ext/view/main.php index 41942225c..b40727bf3 100644 --- a/ext/view/main.php +++ b/ext/view/main.php @@ -9,10 +9,6 @@ require_once "events/image_info_set_event.php"; require_once "events/image_admin_block_building_event.php"; -use function MicroHTML\TR; -use function MicroHTML\TH; -use function MicroHTML\TD; - class ViewPost extends Extension { /** @var ViewPostTheme */