Skip to content

Commit

Permalink
Limit query length (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
ah-net committed Feb 21, 2024
1 parent f1c9865 commit 83799ea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Block/Catalog/Product/ProductList/Toolbar/Plugin.php
Expand Up @@ -79,6 +79,10 @@ public function aroundGetAvailableOrders(Toolbar $subject, Closure $proceed)
return $proceed();
}

if (!$this->context->getResponse()) {
return $proceed();
}

/** @var SortFieldType[] $sortFields */
$sortFields = $this->context->getResponse()->getProperties()->getSortFields();

Expand Down
24 changes: 22 additions & 2 deletions Model/Catalog/Layer/Url/Strategy/QueryParameterStrategy.php
Expand Up @@ -22,6 +22,7 @@
use Magento\Framework\App\Request\Http as MagentoHttpRequest;
use Magento\Framework\Stdlib\CookieManagerInterface;
use Tweakwise\Magento2Tweakwise\Model\Catalog\Layer\Url;
use Magento\Search\Helper\Data;

class QueryParameterStrategy implements UrlInterface, FilterApplierInterface, CategoryUrlInterface
{
Expand Down Expand Up @@ -89,6 +90,11 @@ class QueryParameterStrategy implements UrlInterface, FilterApplierInterface, Ca
*/
protected $layerUrl;

/**
* @var Data
*/
private Data $searchConfig;

/**
* Magento constructor.
*
Expand All @@ -102,13 +108,15 @@ public function __construct(
StrategyHelper $strategyHelper,
CookieManagerInterface $cookieManager,
TweakwiseConfig $config,
Url $layerUrl
Url $layerUrl,
Data $searchConfig
) {
$this->url = $url;
$this->strategyHelper = $strategyHelper;
$this->cookieManager = $cookieManager;
$this->tweakwiseConfig = $config;
$this->layerUrl = $layerUrl;
$this->searchConfig = $searchConfig;
}

/**
Expand Down Expand Up @@ -508,7 +516,19 @@ protected function getLimit(MagentoHttpRequest $request)
*/
protected function getSearch(MagentoHttpRequest $request)
{
return $request->getQuery(self::PARAM_SEARCH);
$searchLength = 100;
$search = $request->getQuery(self::PARAM_SEARCH);
$maxQueryLength = $this->searchConfig->getMaxQueryLength();

if ($maxQueryLength) {
if ($maxQueryLength < $searchLength) {
$searchLength = $maxQueryLength;
}
}

$search = mb_substr((string) $search, 0, $searchLength);

return $search;
}

/**
Expand Down

0 comments on commit 83799ea

Please sign in to comment.