From fcc09ab7c948b6f8f8b26e8c5fac4b0355d27ae4 Mon Sep 17 00:00:00 2001 From: Betafer Date: Wed, 24 Jun 2026 13:29:56 +0200 Subject: [PATCH] Harden range filter labels against malformed values --- src/Product/SearchProvider.php | 31 +++++++++++++++++-- .../Product/SearchProviderTest.php | 10 ++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/Product/SearchProvider.php b/src/Product/SearchProvider.php index 6ad576f2e..9a28f269c 100644 --- a/src/Product/SearchProvider.php +++ b/src/Product/SearchProvider.php @@ -442,8 +442,14 @@ private function labelRangeFilters(array $facets) foreach ($facet->getFilters() as $filter) { $filterValue = $filter->getValue(); - $min = empty($filterValue[0]) ? $facet->getProperty('min') : $filterValue[0]; - $max = empty($filterValue[1]) ? $facet->getProperty('max') : $filterValue[1]; + $min = $this->getNumericRangeBoundary( + isset($filterValue[0]) ? $filterValue[0] : null, + $facet->getProperty('min') + ); + $max = $this->getNumericRangeBoundary( + isset($filterValue[1]) ? $filterValue[1] : null, + $facet->getProperty('max') + ); if ($facet->getType() === 'weight') { $unit = Configuration::get('PS_WEIGHT_UNIT'); $filter->setLabel( @@ -468,6 +474,27 @@ private function labelRangeFilters(array $facets) } } + /** + * Keep range labels resilient to malformed facet values coming from the URL. + * + * @param mixed $value + * @param mixed $fallback + * + * @return int|float|string + */ + private function getNumericRangeBoundary($value, $fallback) + { + if (is_scalar($value) && is_numeric($value)) { + return $value; + } + + if (is_scalar($fallback) && is_numeric($fallback)) { + return $fallback; + } + + return 0; + } + /** * This method generates a URL stub for each filter inside the given facets * and assigns this stub to the filters. diff --git a/tests/php/FacetedSearch/Product/SearchProviderTest.php b/tests/php/FacetedSearch/Product/SearchProviderTest.php index a9adba7c0..ecd09f233 100644 --- a/tests/php/FacetedSearch/Product/SearchProviderTest.php +++ b/tests/php/FacetedSearch/Product/SearchProviderTest.php @@ -411,4 +411,14 @@ public function testRenderFacetsWithFacetsCollectionAndFilters() ) ); } + + public function testNumericRangeBoundaryFallsBackWhenValueIsMalformed() + { + $method = new \ReflectionMethod(SearchProvider::class, 'getNumericRangeBoundary'); + $method->setAccessible(true); + + $this->assertSame('12.50', $method->invoke($this->provider, '12.50', '1')); + $this->assertSame('1', $method->invoke($this->provider, '', '1')); + $this->assertSame(0, $method->invoke($this->provider, '', '')); + } }