require config.yml use UTF-8 as encoding

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.
This commit is contained in:
Hans-Christoph Steiner 2021-06-14 16:00:45 +02:00
parent 48c4354629
commit 1f5534d060
No known key found for this signature in database
GPG Key ID: 3E177817BA1B9BFA
2 changed files with 35 additions and 1 deletions

View File

@ -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}""")

View File

@ -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(