diff --git a/fdroidserver/common.py b/fdroidserver/common.py index 16a0f413..423acc49 100644 --- a/fdroidserver/common.py +++ b/fdroidserver/common.py @@ -336,6 +336,12 @@ def read_config(opts=None): reading it. config.py is deprecated and supported for backwards compatibility. + config.yml requires ASCII or UTF-8 encoding because this code does + not auto-detect the file's encoding. That is left up to the YAML + library. YAML allows ASCII, UTF-8, UTF-16, and UTF-32 encodings. + Since it is a good idea to manage config.yml (WITHOUT PASSWORDS!) + in git, it makes sense to use a globally standard encoding. + """ global config, options @@ -354,7 +360,7 @@ def read_config(opts=None): if os.path.exists(config_file): logging.debug(_("Reading '{config_file}'").format(config_file=config_file)) - with open(config_file) as fp: + with open(config_file, encoding='utf-8') as fp: config = yaml.safe_load(fp) elif os.path.exists(old_config_file): logging.warning(_("""{oldfile} is deprecated, use {newfile}""") diff --git a/tests/common.TestCase b/tests/common.TestCase index a9d215f4..ad099352 100755 --- a/tests/common.TestCase +++ b/tests/common.TestCase @@ -1687,6 +1687,34 @@ class CommonTest(unittest.TestCase): config = fdroidserver.common.read_config(fdroidserver.common.options) self.assertEqual('yml', config.get('apksigner')) + def test_with_config_yml_utf8(self): + """Make sure it is possible to use config.yml in UTF-8 encoding.""" + testdir = tempfile.mkdtemp( + prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir + ) + os.chdir(testdir) + teststr = '/πÇÇ现代通用字-български-عربي1/ö/yml' + with open('config.yml', 'w', encoding='utf-8') as fp: + fp.write('apksigner: ' + teststr) + self.assertTrue(os.path.exists('config.yml')) + self.assertFalse(os.path.exists('config.py')) + config = fdroidserver.common.read_config(fdroidserver.common.options) + self.assertEqual(teststr, config.get('apksigner')) + + def test_with_config_yml_utf8_as_ascii(self): + """Make sure it is possible to use config.yml Unicode encoded as ASCII.""" + testdir = tempfile.mkdtemp( + prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir + ) + os.chdir(testdir) + teststr = '/πÇÇ现代通用字-български-عربي1/ö/yml' + with open('config.yml', 'w') as fp: + yaml.dump({'apksigner': teststr}, fp) + self.assertTrue(os.path.exists('config.yml')) + self.assertFalse(os.path.exists('config.py')) + config = fdroidserver.common.read_config(fdroidserver.common.options) + self.assertEqual(teststr, config.get('apksigner')) + def test_with_config_yml_with_env_var(self): """Make sure it is possible to use config.yml alone.""" testdir = tempfile.mkdtemp(