From 9676c8e17a8392ff2e86705b6f748ef839fae759 Mon Sep 17 00:00:00 2001 From: William Allen Date: Mon, 16 Nov 2026 10:53:41 -0500 Subject: [PATCH] Reject incomplete filters Before, if a request gave only some of a filter's `field`, `compare` and `value` parameters, the filter was silently dropped. When it was the only filter, the default match-all filter took its place. On queryTests, which has no row limit by default, a URL like `?filtercount=1&value1=foo` returned every test for the day and could run out of memory on large projects. An incomplete filter inside a block returned a 500 instead. The shared filter parser now returns a 400 that names the missing parameters. A filter with none of its parameters is still ignored, because the filter panels count empty blocks without sending them. New tests in `QueryTestsPageFiltersTest` cover both cases. --- app/cdash/include/filterdataFunctions.php | 13 ++++++- .../Pages/QueryTestsPageFiltersTest.php | 38 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/app/cdash/include/filterdataFunctions.php b/app/cdash/include/filterdataFunctions.php index 52e55a3d3c..5ea64960bc 100644 --- a/app/cdash/include/filterdataFunctions.php +++ b/app/cdash/include/filterdataFunctions.php @@ -617,17 +617,28 @@ function get_sql_compare_and_value($compare, $value): array // Parse a filter's field, compare, and value from the request and return an // associative array representation of this filter. +// Returns an empty array if none of them were given, and aborts with a 400 if +// only some of them were. // This function also sets the 'hasdateclause' field in $filterdata // if necessary. function parse_filter_from_request($field_var, $compare_var, $value_var, &$filterdata): array { $required_params = [$field_var, $compare_var, $value_var]; + $missing_params = []; foreach ($required_params as $param) { if (!array_key_exists($param, $_REQUEST)) { - return []; + $missing_params[] = $param; } } + if ($missing_params === $required_params) { + // The filter UIs count empty blocks of filters in filtercount, but send none of their parameters. + return []; + } + if ($missing_params !== []) { + // Ignoring an incomplete filter would widen the query instead, possibly to every test of the day. + abort(400, 'Incomplete filter: missing ' . implode(', ', $missing_params)); + } $fieldinfo = htmlspecialchars(pdo_real_escape_string($_REQUEST[$field_var])); $fieldinfo = explode('/', $fieldinfo, 2); diff --git a/tests/Browser/Pages/QueryTestsPageFiltersTest.php b/tests/Browser/Pages/QueryTestsPageFiltersTest.php index 3ac53f2dbf..bbb3aab03f 100644 --- a/tests/Browser/Pages/QueryTestsPageFiltersTest.php +++ b/tests/Browser/Pages/QueryTestsPageFiltersTest.php @@ -195,6 +195,44 @@ public function testFiltersTests(string $field, string $compare, string $value, }); } + public function testIgnoresFiltersWithoutParameters(): void + { + $build = $this->createBuild(); + $build->tests()->create(['testname' => 'kept', 'status' => Test::PASSED]); + $build->tests()->create(['testname' => 'removed', 'status' => Test::PASSED]); + + $this->browse(function (Browser $browser): void { + // The filters panel counts an empty block of filters, but doesn't send its parameters. + $browser->visit("/queryTests.php?project={$this->project->name}&date=2025-01-15" + . '&filtercount=2&showfilters=1&field1=testname&compare1=61&value1=kept'); + $this->assertTestNames($browser, ['kept']); + }); + } + + /** + * @return array + */ + public static function incompleteFilters(): array + { + return [ + 'value only' => ['&value1=kept', 'field1, compare1'], + 'no compare' => ['&field1=testname&value1=kept', 'compare1'], + 'no value' => ['&field1=testname&compare1=61', 'value1'], + 'in a block' => ['&field1=block&field1count=1&field1field1=testname&field1value1=kept', 'field1compare1'], + ]; + } + + #[DataProvider('incompleteFilters')] + public function testRejectsIncompleteFilters(string $filter, string $missing): void + { + $this->browse(function (Browser $browser) use ($filter, $missing): void { + // Ignoring the incomplete filter would list every test of the day instead. + $browser->visit("/queryTests.php?project={$this->project->name}&date=2025-01-15&filtercount=1&showfilters=1{$filter}") + ->waitForTextIn('@error-message', "Incomplete filter: missing {$missing}") + ->assertNotPresent('@tests-table'); + }); + } + public function testFilteringByBuildStartTimeIncludesOtherDays(): void { foreach (['2025-01-13', '2025-01-15', '2025-01-17'] as $date) {