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:
parent
e278f94273
commit
0757dc87a1
@ -117,7 +117,6 @@ high protein milk
|
|||||||
|
|
||||||
# JOI
|
# JOI
|
||||||
joi
|
joi
|
||||||
join
|
|
||||||
countdown
|
countdown
|
||||||
|
|
||||||
# ASMR
|
# ASMR
|
||||||
|
|||||||
@ -59,20 +59,27 @@ def _is_banned(query: str) -> bool:
|
|||||||
_load_banned_terms()
|
_load_banned_terms()
|
||||||
query_lower = query.lower()
|
query_lower = query.lower()
|
||||||
|
|
||||||
# Exact match check
|
# Exact match check (word-boundary aware to avoid "hero" matching "ero", etc.)
|
||||||
for term in _banned_terms:
|
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
|
return True
|
||||||
|
|
||||||
# Fuzzy match check for words in the query
|
# Fuzzy match check for words in the query
|
||||||
query_words = query_lower.split()
|
query_words = query_lower.split()
|
||||||
for word in query_words:
|
for word in query_words:
|
||||||
# Skip short words (4 chars or less) to avoid false positives (e.g. "lofi" matching "loli")
|
# Skip short words (5 chars or less) to avoid false positives (e.g. "hero" matching "ero")
|
||||||
if len(word) <= 4:
|
if len(word) <= 5:
|
||||||
continue
|
continue
|
||||||
for term in _banned_terms:
|
for term in _banned_terms:
|
||||||
# Skip fuzzy matching for short banned terms (too many false positives)
|
# 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
|
continue
|
||||||
# Only fuzzy match for terms with similar length
|
# Only fuzzy match for terms with similar length
|
||||||
if abs(len(word) - len(term)) > 2:
|
if abs(len(word) - len(term)) > 2:
|
||||||
@ -122,7 +129,7 @@ def search():
|
|||||||
return make_error_response("Page must be greater than 0", 400)
|
return make_error_response("Page must be greater than 0", 400)
|
||||||
|
|
||||||
if _is_banned(query):
|
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)
|
sanitized_query = re.sub(r'[^\w\s\-\'"\.]+', "", query)
|
||||||
search_query = f"ytsearch{limit * page}:{sanitized_query}"
|
search_query = f"ytsearch{limit * page}:{sanitized_query}"
|
||||||
|
|||||||
@ -79,7 +79,7 @@ export default function SearchPage() {
|
|||||||
setHasMore(response.hasMore);
|
setHasMore(response.hasMore);
|
||||||
return response;
|
return response;
|
||||||
} catch (err: any) {
|
} 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);
|
console.error("Search error:", err);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user