From 5f3eb601df6073fc20bf977967b09328e3c8d5e5 Mon Sep 17 00:00:00 2001 From: Jochen Sprickerhof Date: Thu, 21 Apr 2022 09:54:30 +0200 Subject: [PATCH] Add check for repo/archive_url --- examples/config.yml | 2 ++ fdroidserver/common.py | 8 ++++++ tests/common.TestCase | 57 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+) diff --git a/examples/config.yml b/examples/config.yml index c66d157f..67e5d5b9 100644 --- a/examples/config.yml +++ b/examples/config.yml @@ -48,6 +48,8 @@ # The same policy is applied to the archive repo, if there is one. # repo_maxage: 0 +# Canonical URL of the repositoy, needs to end in /repo. Is is used to identity +# the repo in the client, as well. # repo_url: https://MyFirstFDroidRepo.org/fdroid/repo # repo_name: My First F-Droid Repo Demo # repo_description: >- diff --git a/fdroidserver/common.py b/fdroidserver/common.py index 61285c08..2658d4a4 100644 --- a/fdroidserver/common.py +++ b/fdroidserver/common.py @@ -433,6 +433,14 @@ def read_config(opts=None): limit = config['git_mirror_size_limit'] config['git_mirror_size_limit'] = parse_human_readable_size(limit) + if 'repo_url' in config: + if not config['repo_url'].endswith('/repo'): + raise FDroidException(_('repo_url needs to end with /repo')) + + if 'archive_url' in config: + if not config['archive_url'].endswith('/archive'): + raise FDroidException(_('archive_url needs to end with /archive')) + confignames_to_delete = set() for configname, dictvalue in config.items(): if configname == 'java_paths': diff --git a/tests/common.TestCase b/tests/common.TestCase index dcdaa20a..be2a04bd 100755 --- a/tests/common.TestCase +++ b/tests/common.TestCase @@ -1845,6 +1845,63 @@ class CommonTest(unittest.TestCase): config = fdroidserver.common.read_config(fdroidserver.common.options) self.assertEqual('yml', config.get('apksigner')) + def test_config_repo_url(self): + """repo_url ends in /repo, archive_url ends in /archive.""" + testdir = tempfile.mkdtemp( + prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir + ) + os.chdir(testdir) + with open('config.yml', 'w') as fp: + fp.write('repo_url: https://MyFirstFDroidRepo.org/fdroid/repo\n') + fp.write('archive_url: https://MyFirstFDroidRepo.org/fdroid/archive') + config = fdroidserver.common.read_config() + self.assertEqual('https://MyFirstFDroidRepo.org/fdroid/repo', config.get('repo_url')) + self.assertEqual('https://MyFirstFDroidRepo.org/fdroid/archive', config.get('archive_url')) + + def test_config_repo_url_extra_slash(self): + """repo_url ends in /repo, archive_url ends in /archive.""" + testdir = tempfile.mkdtemp( + prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir + ) + os.chdir(testdir) + with open('config.yml', 'w') as fp: + fp.write('repo_url: https://MyFirstFDroidRepo.org/fdroid/repo/') + with self.assertRaises(FDroidException): + fdroidserver.common.read_config() + + def test_config_repo_url_not_repo(self): + """repo_url ends in /repo, archive_url ends in /archive.""" + testdir = tempfile.mkdtemp( + prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir + ) + os.chdir(testdir) + with open('config.yml', 'w') as fp: + fp.write('repo_url: https://MyFirstFDroidRepo.org/fdroid/foo') + with self.assertRaises(FDroidException): + fdroidserver.common.read_config() + + def test_config_archive_url_extra_slash(self): + """repo_url ends in /repo, archive_url ends in /archive.""" + testdir = tempfile.mkdtemp( + prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir + ) + os.chdir(testdir) + with open('config.yml', 'w') as fp: + fp.write('archive_url: https://MyFirstFDroidRepo.org/fdroid/archive/') + with self.assertRaises(FDroidException): + fdroidserver.common.read_config() + + def test_config_archive_url_not_repo(self): + """repo_url ends in /repo, archive_url ends in /archive.""" + testdir = tempfile.mkdtemp( + prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir + ) + os.chdir(testdir) + with open('config.yml', 'w') as fp: + fp.write('archive_url: https://MyFirstFDroidRepo.org/fdroid/foo') + with self.assertRaises(FDroidException): + fdroidserver.common.read_config() + def test_write_to_config_yml(self): testdir = tempfile.mkdtemp( prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir