fix: resolve banned term false positives and show server error in frontend

- Exact match now uses word boundaries (\b) instead of substring to prevent
  'ero' matching 'hero', 'joi' matching 'join'
- Fuzzy match threshold raised from <=4 to <=5 chars to skip short words
- Added 'gooning' to fuzzy skip list ('cooling' matched it at distance 2)
- Removed 'join' from banned_terms.txt (legitimate word)
- Frontend now reads err.response.data.error to display server messages
- Error message for blocked searches: 'Unable to query — banned search term detected.'
This commit is contained in:
Jarian Cottingham 2026-07-04 14:09:43 +00:00
parent e278f94273
commit 0757dc87a1
3 changed files with 14 additions and 8 deletions

View File

@ -117,7 +117,6 @@ high protein milk
# JOI
joi
join
countdown
# ASMR

View File

@ -59,20 +59,27 @@ def _is_banned(query: str) -> bool:
_load_banned_terms()
query_lower = query.lower()
# Exact match check
# Exact match check (word-boundary aware to avoid "hero" matching "ero", etc.)
for term in _banned_terms:
if term in query_lower:
if re.search(r'\b' + re.escape(term) + r'\b', query_lower):
return True
# Multi-word exact match (e.g. "no nut november") — substring OK for phrases
for term in _banned_terms:
if ' ' in term and term in query_lower:
return True
# Fuzzy match check for words in the query
query_words = query_lower.split()
for word in query_words:
# Skip short words (4 chars or less) to avoid false positives (e.g. "lofi" matching "loli")
if len(word) <= 4:
# Skip short words (5 chars or less) to avoid false positives (e.g. "hero" matching "ero")
if len(word) <= 5:
continue
for term in _banned_terms:
# Skip fuzzy matching for short banned terms (too many false positives)
if len(term) <= 4:
if len(term) <= 5:
continue
# Skip fuzzy matching for terms that cause false positives
if term in ("strip", "gooning"):
continue
# Only fuzzy match for terms with similar length
if abs(len(word) - len(term)) > 2:
@ -122,7 +129,7 @@ def search():
return make_error_response("Page must be greater than 0", 400)
if _is_banned(query):
return make_error_response("This search is not allowed. Please choose different keywords.", 400)
return make_error_response("Unable to query — banned search term detected.", 400)
sanitized_query = re.sub(r'[^\w\s\-\'"\.]+', "", query)
search_query = f"ytsearch{limit * page}:{sanitized_query}"

View File

@ -79,7 +79,7 @@ export default function SearchPage() {
setHasMore(response.hasMore);
return response;
} catch (err: any) {
setError(err.message || "Failed to search videos. Please try again.");
setError(err.response?.data?.error || err.message || "Failed to search videos. Please try again.");
console.error("Search error:", err);
return null;
}