Skip to content

Commit 3b1bf73

Browse files
williamdesDeepSeek
andcommitted
stats: Fall back to all_time for a filter that is not a known one
The filter of the page has been read from the query without checking it against filterTimes since 6aa5001, and 5ba27e0 worked around the analysis on the same lines without fixing it: an unknown value raised an undefined index and still ended up in selected_filter and in the cache key, so every spelling of it created its own cache entry. An array (?filter[]=day) made the page fail with a 500. Read the filter in one place, accept only a string that is a key of filterTimes, and let all_time be the answer for everything else. Signed-off-by: William Desportes <williamdes@wdes.fr> Co-Authored-By: DeepSeek <noreply@deepseek.com>
1 parent 1146734 commit 3b1bf73

2 files changed

Lines changed: 70 additions & 13 deletions

File tree

‎src/Controller/StatsController.php‎

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
use Cake\Cache\Cache;
2323
use Cake\ORM\TableRegistry;
2424

25+
use function array_key_exists;
26+
use function is_string;
2527
use function json_decode;
2628
use function json_encode;
2729

@@ -51,10 +53,7 @@ public function stats(): void
5153
{
5254
$filter = $this->getTimeFilter();
5355
$relatedEntries = [];
54-
$filter_string = $this->request->getQuery('filter');
55-
if (! $filter_string) {
56-
$filter_string = 'all_time';
57-
}
56+
$filter_string = $this->getFilterName();
5857

5958
$entriesWithCount = [];
6059
//Cache::clear();
@@ -73,7 +72,12 @@ public function stats(): void
7372
$this->set('related_entries', $relatedEntries);
7473
$this->set('columns', $this->Incidents->summarizableFields);
7574
$this->set('filter_times', $this->Incidents->filterTimes);
76-
$this->set('selected_filter', $this->request->getQuery('filter'));
75+
// No filter at all keeps no option selected, any other value selects
76+
// the filter actually used (which is all_time for an unknown one).
77+
$this->set(
78+
'selected_filter',
79+
$this->request->getQuery('filter') === null ? null : $filter_string,
80+
);
7781

7882
$query = [
7983
'group' => 'grouped_by',
@@ -107,17 +111,31 @@ public function stats(): void
107111
$this->set('download_stats', json_decode($downloadStats, true));
108112
}
109113

110-
/** @return mixed I am not sure about the type */
111-
protected function getTimeFilter()
114+
/**
115+
* Returns the name of the selected time filter.
116+
*
117+
* Any value which is not one of the known filters falls back to the all
118+
* time filter, so that it can be used in a cache key.
119+
*
120+
* @return string the name of the filter
121+
*/
122+
protected function getFilterName(): string
112123
{
113-
if ($this->request->getQuery('filter')) {
114-
$filter = $this->Incidents->filterTimes[$this->request->getQuery('filter')];
124+
$filterName = $this->request->getQuery('filter');
125+
if (! is_string($filterName) || ! array_key_exists($filterName, $this->Incidents->filterTimes)) {
126+
return 'all_time';
115127
}
116128

117-
if (isset($filter)) {
118-
return $filter;
119-
}
129+
return $filterName;
130+
}
120131

121-
return $this->Incidents->filterTimes['all_time'];
132+
/**
133+
* Returns the selected time filter.
134+
*
135+
* @return array<mixed> the filter with its label, limit and group
136+
*/
137+
protected function getTimeFilter(): array
138+
{
139+
return $this->Incidents->filterTimes[$this->getFilterName()];
122140
}
123141
}

‎tests/TestCase/Controller/StatsControllerTest.php‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use function array_sum;
3333
use function count;
3434
use function json_encode;
35+
use function urlencode;
3536

3637
/**
3738
* App\Controller\StatsController Test Case
@@ -156,6 +157,44 @@ public function testStatsBuildsStatisticsFromScratch(): void
156157
$this->assertSame(5, array_sum(array_column($relatedEntries['php_version'], 'count')));
157158
}
158159

160+
/** @return array<string, array{0: string}> */
161+
public static function unknownFilterProvider(): array
162+
{
163+
return [
164+
'unknown name' => ['doesnotexist'],
165+
'wrong case' => ['DAY'],
166+
'trailing space' => ['all_time '],
167+
'numeric' => ['0'],
168+
'markup' => ['<script>alert(1)</script>'],
169+
];
170+
}
171+
172+
#[DataProvider('unknownFilterProvider')]
173+
public function testAnUnknownFilterFallsBackToAllTime(string $filter): void
174+
{
175+
$this->session(['Developer.id' => 1, 'read_only' => false]);
176+
177+
$this->get('/stats?filter=' . urlencode($filter));
178+
179+
$this->assertResponseCode(200);
180+
$this->assertSame('all_time', $this->viewVariable('selected_filter'));
181+
$this->assertNotFalse(Cache::read('browser_all_time'));
182+
$this->assertNotFalse(Cache::read('downloadStats_all_time'));
183+
$this->assertNull(Cache::read('browser_' . $filter));
184+
$this->assertNull(Cache::read('downloadStats_' . $filter));
185+
}
186+
187+
public function testAnArrayFilterFallsBackToAllTime(): void
188+
{
189+
$this->session(['Developer.id' => 1, 'read_only' => false]);
190+
191+
$this->get('/stats?filter%5B%5D=day');
192+
193+
$this->assertResponseCode(200);
194+
$this->assertSame('all_time', $this->viewVariable('selected_filter'));
195+
$this->assertNotFalse(Cache::read('browser_all_time'));
196+
}
197+
159198
public function testStatsUsesTheCachedStatistics(): void
160199
{
161200
$this->session(['Developer.id' => 1, 'read_only' => false]);

0 commit comments

Comments
 (0)