fix: cap search keywords and feed queries to prevent CWE-407 amplification

- shop.py: strip empty tokens, cap keywords to 10 before passing to model
- product.py: add .limit(200) per keyword query — was unbounded .all()
- feeds.py: add .limit(1000) on product/content feed queries — was unbounded
This commit is contained in:
russell@unturf.com 2026-03-28 15:12:41 -04:00
parent eec598604b
commit f9cbebb6b5
3 changed files with 6 additions and 2 deletions

View file

@ -682,7 +682,8 @@ def get_products_by_keywords(dbsession, keywords, shop=None):
if not shop: if not shop:
from .shop import Shop from .shop import Shop
query = query.join(Shop, Product.shop_id == Shop.id).filter(Shop.environment == 0) query = query.join(Shop, Product.shop_id == Shop.id).filter(Shop.environment == 0)
products = query.all() # Limit per-keyword results to prevent memory exhaustion (CWE-407).
products = query.limit(200).all()
for product in products: for product in products:
if product.id not in scores: if product.id not in scores:

View file

@ -46,12 +46,14 @@ def get_public_items(shop):
products = ( products = (
shop.products.filter(Product.visibility == 1, Product.is_sellable == True) shop.products.filter(Product.visibility == 1, Product.is_sellable == True)
.order_by(Product.updated_timestamp.desc()) .order_by(Product.updated_timestamp.desc())
.limit(1000)
.all() .all()
) )
content = ( content = (
shop.products.filter(Product.visibility == 1, Product.is_sellable == False) shop.products.filter(Product.visibility == 1, Product.is_sellable == False)
.order_by(Product.updated_timestamp.desc()) .order_by(Product.updated_timestamp.desc())
.limit(1000)
.all() .all()
) )

View file

@ -133,8 +133,9 @@ def search(request):
if keywords is None: if keywords is None:
return HTTPFound(get_referer_or_home(request)) return HTTPFound(get_referer_or_home(request))
keyword_list = [k for k in keywords.split(" ") if k][:10]
products = get_products_by_keywords( products = get_products_by_keywords(
request.dbsession, keywords.split(" "), request.shop request.dbsession, keyword_list, request.shop
) )
hit_count = len(products) hit_count = len(products)