From 4c73120757af7a095f828423611a311bf40171ae Mon Sep 17 00:00:00 2001 From: Johan Wassberg Date: Tue, 17 Nov 2026 14:48:34 +0100 Subject: [PATCH] Handle load of files with URL special characters Closes #291. --- src/pyff/resource.py | 3 ++- src/pyff/test/test_pipeline.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/pyff/resource.py b/src/pyff/resource.py index 4781f3fe..5d620779 100644 --- a/src/pyff/resource.py +++ b/src/pyff/resource.py @@ -300,7 +300,8 @@ def _setup(self): if os.path.isdir(pth): self.url = f"dir://{pth}" elif os.path.isfile(pth) or os.path.isabs(self.url): - self.url = f"file://{pth}" + # Escape the path, as for files found in a directory: '#', '?' and '%' are URL syntax. + self.url = f"file://{urlescape(pth)}" if self.url.startswith('file://') or self.url.startswith('dir://'): self.never_expires = True diff --git a/src/pyff/test/test_pipeline.py b/src/pyff/test/test_pipeline.py index e6dfda73..25f1b4bc 100644 --- a/src/pyff/test/test_pipeline.py +++ b/src/pyff/test/test_pipeline.py @@ -280,6 +280,39 @@ def test_no_fail_on_error_invalid_dir(self): assert md.lookup('https://idp.example.com/saml2/idp/metadata.php') +class LoadFileNameTest(PipeLineTest): + """ + A metadata file loads whatever characters its name contains, also when it is listed directly rather than found + in a directory: cf. issue #291. + """ + + NAMES = [ + 'plain.xml', + 'a#b.xml', + 'a?b.xml', + 'a%20b.xml', + 'https%3A%2F%2Fidp.example.com%2Fidp.xml', + 'åäö.xml', + 'Räksmörgås#1.xml', + ] + + def setUp(self): + super().setUp() + self.tmpdir = tempfile.mkdtemp() + + def tearDown(self): + shutil.rmtree(self.tmpdir) + + def test_file_name_with_url_special_characters(self): + for name in self.NAMES: + with self.subTest(name=name): + path = os.path.join(self.tmpdir, name) + shutil.copy(os.path.join(self.datadir, 'metadata', 'test01.xml'), path) + _res, md = self.exec_pipeline(f"- load:\n - {path}\n") + assert md.lookup('https://idp.example.com/saml2/idp/metadata.php'), f"{name} was not loaded" + os.unlink(path) + + class SortTest(PipeLineTest): EID1 = "https://idp.aco.net/idp/shibboleth" EID2 = "https://idp.example.com/saml2/idp/metadata.php"