MISP / PyMISP

Python library using the MISP Rest API
Other
445 stars 280 forks source link

UnboundLocalError when invoking `values_in_warninglist([])` #1053

Closed 0xThiebaut closed 1 year ago

0xThiebaut commented 1 year ago

Invoking misp.values_in_warninglist([]) (e.g. when the values are dynamically generated) throws an UnboundLocalError as the following condition does not define d when data is considered Falsy. Ideally, values_in_warninglist should return without API call when provided a Falsy input.

https://github.com/MISP/PyMISP/blob/69e660ef03108cc16a52b170e7ab4440bd202520/pymisp/api.py#L3712-L3722

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
Cell In[9], line 16
     14 for object in event.Object:
     15     iocs.extend([attribute.value for attribute in object.Attribute if attribute.to_ids])
---> 16 false_positives = misp.values_in_warninglist(iocs)
     18 for attribute in event.Attribute:
     19     if attribute.to_ids and attribute.value in false_positives:

File REDACTED\site-packages\pymisp\api.py:1316, in PyMISP.values_in_warninglist(self, value)
   1311 def values_in_warninglist(self, value: Iterable) -> Dict:
   1312     """Check if IOC values are in warninglist
   1313 
   1314     :param value: iterator with values to check
   1315     """
-> 1316     response = self._prepare_request('POST', 'warninglists/checkValue', data=value)
   1317     return self._check_json_response(response)

File REDACTED\site-packages\pymisp\api.py:3720, in PyMISP._prepare_request(self, request_type, url, data, params, kw_params, output_type, content_type)
   3717     d = json.dumps(data, default=pymisp_json_default)
   3719 logger.debug(f'{request_type} - {url}')
-> 3720 if d is not None:
   3721     logger.debug(d)
   3723 if kw_params:
   3724     # CakePHP params in URL

UnboundLocalError: cannot access local variable 'd' where it is not associated with a value
Rafiot commented 1 year ago

Right, the logic in that method was pretty bad. I fixed that. Data being an empty Iterator isn't great, but it is possibly an expected request, so I'd rather not just drop the request all together.

Thank you for the report!