From 20569217d9cd0a5723bf88f7ff7f2698e05e034d Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Wed, 19 Mar 2025 17:39:45 +0100 Subject: [PATCH] index: fail if user sets mirrors:isPrimary wrong Really, this is not meant to be set by the user in the config. But if they add something harmless that'll be ignored anyway, it seems that throwing an error is too much. So only throw the error if it is set wrongly. --- fdroidserver/index.py | 11 +++++++++++ tests/test_index.py | 13 +++++++++++++ 2 files changed, 24 insertions(+) diff --git a/fdroidserver/index.py b/fdroidserver/index.py index 0beba4ef..873f401e 100644 --- a/fdroidserver/index.py +++ b/fdroidserver/index.py @@ -1516,6 +1516,7 @@ def add_mirrors_to_repodict(repo_section, repodict): repodict['mirrors'] = [] canonical_url = repodict['address'] found_primary = False + errors = 0 for mirror in mirrors: if canonical_url == mirror['url']: found_primary = True @@ -1524,9 +1525,19 @@ def add_mirrors_to_repodict(repo_section, repodict): for k in sorted(mirror.keys()): sortedmirror[k] = mirror[k] repodict['mirrors'].insert(0, sortedmirror) + elif mirror.get('isPrimary'): + errors += 1 + logging.error( + _('Mirror config for {url} contains "isPrimary" key!').format( + url=mirror['url'] + ) + ) else: repodict['mirrors'].append(mirror) + if errors: + raise FDroidException(_('"isPrimary" key should not be added to mirrors!')) + if repodict['mirrors'] and not found_primary: repodict['mirrors'].insert(0, {'isPrimary': True, 'url': repodict['address']}) diff --git a/tests/test_index.py b/tests/test_index.py index 059386fb..3e078eef 100755 --- a/tests/test_index.py +++ b/tests/test_index.py @@ -814,6 +814,19 @@ class IndexTest(unittest.TestCase): with self.assertRaises(fdroidserver.exception.FDroidException): index.add_mirrors_to_repodict('repo', repodict) + def test_erroneous_isPrimary_in_mirrors_config(self): + """There can be only one primary mirror aka canonical URL""" + common.config = { + 'repo_url': 'http://one/fdroid', + 'mirrors': [ + {'url': 'http://one/fdroid', 'countryCode': 'SA'}, + {'url': 'http://two/fdroid', 'isPrimary': True}, + ], + } + repodict = {'address': common.config['repo_url']} + with self.assertRaises(fdroidserver.exception.FDroidException): + index.add_mirrors_to_repodict('repo', repodict) + class AltstoreIndexTest(unittest.TestCase): def test_make_altstore(self):