diff --git a/docs/changelog.rst b/docs/changelog.rst index b48e608c1..faa3fb33a 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -16,6 +16,8 @@ Released: under development for debugging purposes. (`#917 `_) +* Bugfix: Check filter strings for correctness. + (`#964 `_) * Bugfix: Replace hardcoded `index` with config value `root_doc`. (`#877 `_) * Bugfix: Fix unbounded memory usage in pickle environment. diff --git a/sphinx_needs/filter_common.py b/sphinx_needs/filter_common.py index 250bc40aa..a9c6df633 100644 --- a/sphinx_needs/filter_common.py +++ b/sphinx_needs/filter_common.py @@ -306,9 +306,14 @@ def filter_single_need( # Set filter_context as globals and not only locals in eval()! # Otherwise, the vars not be accessed in list comprehensions. if filter_compiled: - result = bool(eval(filter_compiled, filter_context)) + result = eval(filter_compiled, filter_context) else: - result = bool(eval(filter_string, filter_context)) + result = eval(filter_string, filter_context) + if isinstance(result, str): + # Raises NeedsInvalidFilter if the result is a string type + raise NeedsInvalidFilter( + f"Error when evaluating filter: expected output to have True/False but got a string <{result}>" + ) except Exception as e: raise NeedsInvalidFilter(f"Filter {filter_string} not valid. Error: {e}.") - return result + return bool(result)