use utf-8 codec if ensure_ascii is False to avoid UnicodeError#5
use utf-8 codec if ensure_ascii is False to avoid UnicodeError#5neuralhax wants to merge 2 commits intoTheHive-Project:masterfrom
Conversation
|
I confirm the bug, I had the same problem and now it's fixed |
Well, I have been missing something indeed. The original patch fixed problem with MaxMind analyzer, but introduced problems with other analyzers, e.g. AbuseIPDB, where I started to observe following error: |
|
@xlaruen you are right. I was able to reproduce the abuseip part and I've applied your commit and it works, for now |
When using latest Cortex version (currently 3.0.0-RC3) which is using cortexutils 2.0.0, I noticed that MaxMind_GeoIP_3_0 analyzer always fails with "Invalid IP address" error, e.g.:
The root cause of that error is following exception:
Latest Cortex 2.x version using cortexutils 1.3.0 doesn't have such problem. Difference is that in cortexutils 2.0.0, the report is written into the file instead of standard output. Standard output is set to use utf-8 encoding in
__set_encoding()function, but the same is not done when writing into the file. Python 2 is using ascii codec by default and whenjson.dump()function is used withensure_ascii=Falseargument (as in this case) and data contains non-ASCII characters as well, it will lead to UnicodeError. Such behaviour is also described in Pythonjson.dump()documentation:Unless I'm missing something, I believe this can be fixed by using utf-8 encoding for the output file in the same way as is done in
__set_encoding()function forsys.stdoutandsys.stderr. With this patch, I no longer observe the reported error.