mirror of
https://github.com/searxng/searxng.git
synced 2026-05-30 06:34:11 +02:00
[mod] hardening of the Result.filter_urls() method (#6117)
Exceptions in the execution of the callback must be caught / ignored and logged
on the ERROR log.
To test, apply this patch to provoke a ValueError exception::
diff --git a/searx/data/tracker_patterns.py b/searx/data/tracker_patterns.py
index ed4415bce..695ed05d2 100644
--- a/searx/data/tracker_patterns.py
+++ b/searx/data/tracker_patterns.py
@@ -114,6 +114,7 @@ class TrackerPatternsDB:
Returns bool ``True`` to use URL unchanged (``False`` to ignore URL).
If URL should be modified, the returned string is the new URL to use.
"""
+ raise ValueError("test callback exceptions")
new_url = url
parsed_new_url = urlparse(url=new_url)
Start a `make run` instance and query for example `amazon` .. have a look at the
ERROR log:
ERROR searx.result_types: filter_urls (field 'url'): ignore ValueError('test callback exceptions') from callback searx/data/tracker_patterns.py:117
Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>
This commit is contained in:
@@ -19,6 +19,7 @@
|
|||||||
__all__ = ["Result"]
|
__all__ = ["Result"]
|
||||||
|
|
||||||
import typing as t
|
import typing as t
|
||||||
|
import types
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
@@ -29,7 +30,9 @@ from collections.abc import Callable
|
|||||||
|
|
||||||
import msgspec
|
import msgspec
|
||||||
|
|
||||||
from searx import logger as log
|
from searx import logger
|
||||||
|
|
||||||
|
log = logger.getChild("result_types")
|
||||||
|
|
||||||
WHITESPACE_REGEX = re.compile('( |\t|\n)+', re.M | re.U)
|
WHITESPACE_REGEX = re.compile('( |\t|\n)+', re.M | re.U)
|
||||||
UNSET = object()
|
UNSET = object()
|
||||||
@@ -125,8 +128,20 @@ def _filter_urls(
|
|||||||
if not url_src:
|
if not url_src:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
new_url = filter_func(result, field_name, url_src)
|
try:
|
||||||
# log.debug("filter_urls: filter_func(result, %s) '%s' -> '%s'", field_name, field_value, new_url)
|
new_url = filter_func(result, field_name, url_src)
|
||||||
|
except Exception as exc: # pylint: disable=broad-exception-caught
|
||||||
|
# pylint: disable=no-member
|
||||||
|
_tb: types.TracebackType = exc.__traceback__.tb_next.tb_next # type: ignore
|
||||||
|
log.error(
|
||||||
|
"filter_urls (field '%s'): ignore %s from callback %s:%s",
|
||||||
|
field_name,
|
||||||
|
repr(exc),
|
||||||
|
_tb.tb_frame.f_code.co_filename,
|
||||||
|
_tb.tb_lineno,
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
if isinstance(new_url, bool):
|
if isinstance(new_url, bool):
|
||||||
if new_url:
|
if new_url:
|
||||||
# log.debug("filter_urls: unchanged field %s URL %s", field_name, field_value)
|
# log.debug("filter_urls: unchanged field %s URL %s", field_name, field_value)
|
||||||
|
|||||||
Reference in New Issue
Block a user