From 8e6b4346dc505ad4a279bb98c75ce7f2d5cf3a1d Mon Sep 17 00:00:00 2001 From: sebres Date: Fri, 18 Aug 2017 13:30:50 +0200 Subject: [PATCH] avoid using "ANSI_X3.4-1968" as preferred encoding, if missing environment variables 'LANGUAGE', 'LC_ALL', 'LC_CTYPE', and 'LANG' (especially critical if default value `encoding = auto` configured). As PoC and coverage (this case fails without this "fix"): $ env -i PATH="$PATH" bin/fail2ban-testcases --fast --no-network testAddBanInvalidEncoded --- fail2ban/helpers.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/fail2ban/helpers.py b/fail2ban/helpers.py index 556ca173..7eaa59f8 100644 --- a/fail2ban/helpers.py +++ b/fail2ban/helpers.py @@ -34,6 +34,10 @@ from .server.mytime import MyTime PREFER_ENC = locale.getpreferredencoding() +# correct prefered encoding if lang not set in environment: +if PREFER_ENC.startswith('ANSI_'): # pragma: no cover + if all((os.getenv(v) in (None, "") for v in ('LANGUAGE', 'LC_ALL', 'LC_CTYPE', 'LANG'))): + PREFER_ENC = 'UTF-8'; def formatExceptionInfo():