It's not just strings with whitespace but string results in general. Ensure that strings are appropriately quoted, if not quoted already.

pull/3553/head
derekpierre 2024-09-13 12:41:43 -04:00
parent 2ed9f9708e
commit a07350aa30
No known key found for this signature in database
3 changed files with 34 additions and 13 deletions

View File

@ -295,17 +295,7 @@ class ReturnValueTest:
f"Index '{index}' not found in returned data."
)
# string results that have whitespace but are not already quoted will
# cause a problem for subsequent literal_eval
if isinstance(processed_data, str):
# string that contains whitespace, check if already quoted; if not, quote it
if " " in processed_data and not (
(processed_data.startswith("'") and processed_data.endswith("'"))
or (processed_data.startswith('"') and processed_data.endswith('"'))
):
# quote the string
processed_data = f"'{processed_data}'"
elif isinstance(processed_data, (list, tuple)):
if isinstance(processed_data, (list, tuple)):
# convert any bytes in list to hex (include nested lists/tuples); no additional indexing
processed_data = [
self._process_data(data=item, index=None) for item in processed_data

View File

@ -148,6 +148,20 @@ class JsonApiCondition(AccessControlCondition):
return result
@staticmethod
def _process_result_for_eval(result: Any):
# strings that are not already quoted will cause a problem for literal_eval
if isinstance(result, str):
# check if already quoted; if not, quote it
if not (
(result.startswith("'") and result.endswith("'"))
or (result.startswith('"') and result.endswith('"'))
):
# quote the string
result = f"'{result}'"
return result
def verify(self, **context) -> Tuple[bool, Any]:
"""
Verifies the offchain condition is met by performing a read operation on the endpoint
@ -161,5 +175,7 @@ class JsonApiCondition(AccessControlCondition):
resolved_return_value_test = self.return_value_test.with_resolved_context(
**context
)
eval_result = resolved_return_value_test.eval(result) # test
result_for_eval = self._process_result_for_eval(result)
eval_result = resolved_return_value_test.eval(result_for_eval) # test
return eval_result, result

View File

@ -123,7 +123,22 @@ def test_json_api_condition_verify(mocker):
assert value == 1
def test_json_api_condition_verify_string(mocker):
def test_json_api_condition_verify_string_without_whitespace(mocker):
mock_response = mocker.Mock(status_code=200)
mock_response.json.return_value = {"store": {"book": [{"title": "Title"}]}}
mocker.patch("requests.get", return_value=mock_response)
condition = JsonApiCondition(
endpoint="https://api.example.com/data",
query="$.store.book[0].title",
return_value_test=ReturnValueTest("==", "'Title'"),
)
result, value = condition.verify()
assert result is True
assert value == "Title"
def test_json_api_condition_verify_string_with_whitespace(mocker):
mock_response = mocker.Mock(status_code=200)
mock_response.json.return_value = {"store": {"book": [{"title": "Test Title"}]}}
mocker.patch("requests.get", return_value=mock_response)