From a3fc7a761e3990a56e58bfa7aa17cb28bab5d993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20P=C3=A9rez=20Arnaud?= Date: Tue, 24 Nov 2026 11:31:08 +0100 Subject: [PATCH 1/2] fix: Ignore non-standard ScopedUIInfo when reading mdui elements, close #287 --- src/pyff/builtins.py | 14 ++--- src/pyff/samlmd.py | 20 +++---- src/pyff/store.py | 14 ++--- .../metadata/test-scoped-display-name.xml | 60 +++++++++++++++++++ src/pyff/test/test_pipeline.py | 33 ++++++++++ 5 files changed, 117 insertions(+), 24 deletions(-) create mode 100644 src/pyff/test/data/metadata/test-scoped-display-name.xml diff --git a/src/pyff/builtins.py b/src/pyff/builtins.py index 5cb49de0..63e6efc4 100644 --- a/src/pyff/builtins.py +++ b/src/pyff/builtins.py @@ -912,14 +912,14 @@ def select(req: Plumbing.Request, *opts): def _strings(elt): lst = [] for attr in [ - f"{{{NS['mdui']}}}DisplayName", - f"{{{NS['md']}}}ServiceName", - f"{{{NS['md']}}}OrganizationDisplayName", - f"{{{NS['md']}}}OrganizationName", - f"{{{NS['mdui']}}}Keywords", - f"{{{NS['shibmd']}}}Scope", + f".//{{{NS['mdui']}}}UIInfo/{{{NS['mdui']}}}DisplayName", + f".//{{{NS['md']}}}ServiceName", + f".//{{{NS['md']}}}OrganizationDisplayName", + f".//{{{NS['md']}}}OrganizationName", + f".//{{{NS['mdui']}}}UIInfo/{{{NS['mdui']}}}Keywords", + f".//{{{NS['shibmd']}}}Scope", ]: - lst.extend([s.text for s in elt.iter(attr)]) + lst.extend([s.text for s in elt.iterfind(attr)]) lst.append(elt.get('entityID')) return [item for item in lst if item is not None] diff --git a/src/pyff/samlmd.py b/src/pyff/samlmd.py index acaea918..f5584ec2 100644 --- a/src/pyff/samlmd.py +++ b/src/pyff/samlmd.py @@ -765,12 +765,12 @@ def gen_icon(e): def entity_icon_url(e, langs=None): - for ico in filter_lang(e.iter("{{{}}}Logo".format(NS['mdui'])), langs=langs): + for ico in filter_lang(e.iterfind(".//{{{}}}UIInfo/{{{}}}Logo".format(NS['mdui'], NS['mdui'])), langs=langs): return dict(url=ico.text, width=ico.get('width'), height=ico.get('height')) def privacy_statement_url(entity, langs): - for url in filter_lang(entity.iter("{{{}}}PrivacyStatementURL".format(NS['mdui'])), langs=langs): + for url in filter_lang(entity.iterfind(".//{{{}}}UIInfo/{{{}}}PrivacyStatementURL".format(NS['mdui'], NS['mdui'])), langs=langs): return url.text @@ -798,12 +798,12 @@ def entity_extended_display_i18n(entity, default_lang=None): ) name_dict.update(lang_dict(entity.iter("{{{}}}ServiceName".format(NS['md'])), lambda e: e.text, default_lang=default_lang)) name_dict.update( - lang_dict(entity.iter("{{{}}}DisplayName".format(NS['mdui'])), lambda e: e.text, default_lang=default_lang) + lang_dict(entity.iterfind(".//{{{}}}UIInfo/{{{}}}DisplayName".format(NS['mdui'], NS['mdui'])), lambda e: e.text, default_lang=default_lang) ) desc_dict = lang_dict(entity.iter("{{{}}}OrganizationURL".format(NS['md'])), lambda e: e.text, default_lang=default_lang) desc_dict.update( - lang_dict(entity.iter("{{{}}}Description".format(NS['mdui'])), lambda e: e.text, default_lang=default_lang) + lang_dict(entity.iterfind(".//{{{}}}UIInfo/{{{}}}Description".format(NS['mdui'], NS['mdui'])), lambda e: e.text, default_lang=default_lang) ) return name_dict, desc_dict @@ -887,7 +887,7 @@ def entity_extended_display(entity, langs=None): display = serviceName.text break - for displayName in filter_lang(entity.iter("{{{}}}DisplayName".format(NS['mdui'])), langs=langs): + for displayName in filter_lang(entity.iterfind(".//{{{}}}UIInfo/{{{}}}DisplayName".format(NS['mdui'], NS['mdui'])), langs=langs): info = display display = displayName.text break @@ -896,7 +896,7 @@ def entity_extended_display(entity, langs=None): info = organizationUrl.text break - for description in filter_lang(entity.iter("{{{}}}Description".format(NS['mdui'])), langs=langs): + for description in filter_lang(entity.iterfind(".//{{{}}}UIInfo/{{{}}}Description".format(NS['mdui'], NS['mdui'])), langs=langs): info = description.text break @@ -912,7 +912,7 @@ def entity_display_name(entity: Element, langs=None) -> str: :param entity: An EntityDescriptor element :param langs: The list of languages to search in priority order """ - for displayName in filter_lang(entity.iter("{{{}}}DisplayName".format(NS['mdui'])), langs=langs): + for displayName in filter_lang(entity.iterfind(".//{{{}}}UIInfo/{{{}}}DisplayName".format(NS['mdui'], NS['mdui'])), langs=langs): return displayName.text.strip() for serviceName in filter_lang(entity.iter("{{{}}}ServiceName".format(NS['md'])), langs=langs): @@ -1008,7 +1008,7 @@ def discojson(e, sources=None, langs=None, fallback_to_favicon=False, icon_store icon_info['url'] = ico d['entity_icon_url'] = icon_info - keywords = filter_lang(e.iter("{{{}}}Keywords".format(NS['mdui'])), langs=langs) + keywords = filter_lang(e.iterfind(".//{{{}}}UIInfo/{{{}}}Keywords".format(NS['mdui'], NS['mdui'])), langs=langs) if keywords is not None: lst = [elt.text for elt in keywords] if len(lst) > 0: @@ -1270,7 +1270,7 @@ def entity_simple_info(e, langs=None): d['service_name'] = entity_service_name(e, langs) d['service_descr'] = entity_service_description(e, langs) d['entity_attributes'] = entity_attribute_dict(e) - keywords = filter_lang(e.iter("{{{}}}Keywords".format(NS['mdui'])), langs=langs) + keywords = filter_lang(e.iterfind(".//{{{}}}UIInfo/{{{}}}Keywords".format(NS['mdui'], NS['mdui'])), langs=langs) if keywords is not None: lst = [elt.text for elt in keywords] if len(lst) > 0: @@ -1280,7 +1280,7 @@ def entity_simple_info(e, langs=None): def entity_info(e, langs=None): d = entity_simple_summary(e) - keywords = filter_lang(e.iter("{{{}}}Keywords".format(NS['mdui'])), langs=langs) + keywords = filter_lang(e.iterfind(".//{{{}}}UIInfo/{{{}}}Keywords".format(NS['mdui'], NS['mdui'])), langs=langs) if keywords is not None: lst = [elt.text for elt in keywords] if len(lst) > 0: diff --git a/src/pyff/store.py b/src/pyff/store.py index f0e2c782..3a799edb 100644 --- a/src/pyff/store.py +++ b/src/pyff/store.py @@ -418,14 +418,14 @@ def search(self, query=None, path=None, entity_filter=None, related=None): def _strings(elt): lst = [] for attr in [ - '{{{}}}DisplayName'.format(NS['mdui']), - '{{{}}}ServiceName'.format(NS['md']), - '{{{}}}OrganizationDisplayName'.format(NS['md']), - '{{{}}}OrganizationName'.format(NS['md']), - '{{{}}}Keywords'.format(NS['mdui']), - '{{{}}}Scope'.format(NS['shibmd']), + './/{{{}}}UIInfo/{{{}}}DisplayName'.format(NS['mdui'], NS['mdui']), + './/{{{}}}ServiceName'.format(NS['md']), + './/{{{}}}OrganizationDisplayName'.format(NS['md']), + './/{{{}}}OrganizationName'.format(NS['md']), + './/{{{}}}UIInfo/{{{}}}Keywords'.format(NS['mdui'], NS['mdui']), + './/{{{}}}Scope'.format(NS['shibmd']), ]: - lst.extend([s.text for s in elt.iter(attr)]) + lst.extend([s.text for s in elt.iterfind(attr)]) lst.append(elt.get('entityID')) return [item for item in lst if item is not None] diff --git a/src/pyff/test/data/metadata/test-scoped-display-name.xml b/src/pyff/test/data/metadata/test-scoped-display-name.xml new file mode 100644 index 00000000..c3aa6a68 --- /dev/null +++ b/src/pyff/test/data/metadata/test-scoped-display-name.xml @@ -0,0 +1,60 @@ + + + + + example.com + 81098135.example.com + + Scoped Example Universitet + Scoped Example University + + + Example universitet + Example University + Identity Provider för Example universitet + Identity Provider for Example University + http://www.example.com/ + http://www.example.com/english/ + https://www.example.com/static/images/umu_logo.jpg + https://www.example.com/static/images/logo.jpg + https://www.example.com/static/images/logo_eng.jpg + exempel + example + + + example.com + example.net + 10.0.0.0/8 + + + + + urn:oasis:names:tc:SAML:2.0:nameid-format:transient + + + + ExempelU + ExampleU + Exempel Universitetet + The Example University + http://www.example.com + http://www.example.com/english + + + Example University + Example helpdesk + helpdesk@example.com + + + Example University + Example helpdesk + helpdesk@example.com + + + Example University + Servicedesk Example universitet + support@example.com + + diff --git a/src/pyff/test/test_pipeline.py b/src/pyff/test/test_pipeline.py index 7f50e087..3378a2f6 100644 --- a/src/pyff/test/test_pipeline.py +++ b/src/pyff/test/test_pipeline.py @@ -1622,3 +1622,36 @@ def test_discojson_sp_trustinfo_in_attr_broken_base64(self): pass finally: shutil.rmtree(tmpdir) + + def test_discojson_scoped_display_name(self): + with patch.multiple("sys", exit=self.sys_exit): + tmpdir = tempfile.mkdtemp() + os.rmdir(tmpdir) # lets make sure 'store' can recreate it + try: + self.exec_pipeline(""" +- load: + - file://%s/metadata/test-scoped-display-name.xml +- select +- discojson +- publish: + output: %s/disco.json + raw: true + update_store: false +""" % (self.datadir, tmpdir)) + fn = "%s/disco.json" % tmpdir + assert os.path.exists(fn) + with open(fn, 'r') as f: + idps_json = json.load(f) + + idp_json = idps_json[0] + assert idp_json['title'] == 'Example University' + assert idp_json['title_langs']['sv'] == 'Example universitet' + assert idp_json['title_langs']['en'] == 'Example University' + assert idp_json['descr'] == 'Identity Provider for Example University' + assert idp_json['descr_langs']['sv'] == 'Identity Provider för Example universitet' + assert idp_json['descr_langs']['en'] == 'Identity Provider for Example University' + assert idp_json['keywords'] == 'example' + except IOError: + pass + finally: + shutil.rmtree(tmpdir) From 04c9795b93d9429b0915a6490ceff76a931ea055 Mon Sep 17 00:00:00 2001 From: Johan Wassberg Date: Thu, 26 Nov 2026 10:45:25 +0100 Subject: [PATCH 2/2] Add tests for ScopedUIInfo in samlmd helpers and store search --- .../metadata/test-scoped-display-name.xml | 6 +++ src/pyff/test/test_repo.py | 54 +++++++++++++++++++ src/pyff/test/test_store.py | 30 +++++++++++ 3 files changed, 90 insertions(+) diff --git a/src/pyff/test/data/metadata/test-scoped-display-name.xml b/src/pyff/test/data/metadata/test-scoped-display-name.xml index c3aa6a68..1a816d39 100644 --- a/src/pyff/test/data/metadata/test-scoped-display-name.xml +++ b/src/pyff/test/data/metadata/test-scoped-display-name.xml @@ -9,6 +9,12 @@ Scoped Example Universitet Scoped Example University + Scoped beskrivning + Scoped description + scopednyckelord + scopedkeyword + https://www.example.com/static/images/scoped_logo.jpg + https://www.example.com/scoped-privacy Example universitet diff --git a/src/pyff/test/test_repo.py b/src/pyff/test/test_repo.py index a731d06e..ead702e9 100644 --- a/src/pyff/test/test_repo.py +++ b/src/pyff/test/test_repo.py @@ -9,11 +9,15 @@ entity_display_name, entity_domains, entity_extended_display, + entity_extended_display_i18n, entity_icon_url, + entity_info, + entity_simple_info, entity_simple_summary, is_idp, is_sp, metadata_expiration, + privacy_statement_url, set_entity_attributes, sha1_id, ) @@ -200,3 +204,53 @@ def test_non_metadata(self): assert False except MetadataException: pass + + +class TestScopedUIInfo(TestCase): + """ + OpenAthens metadata carries non-standard oaf:ScopedUIInfo elements, with mdui children, ahead of the + IdP's own mdui:UIInfo. Only the latter may be used to describe the entity. + """ + + def setUp(self): + datadir = resource_filename('metadata', 'test/data') + self.e = root(parse_xml(os.path.join(datadir, 'test-scoped-display-name.xml'))) + + def test_display_name(self): + assert entity_display_name(self.e) == 'Example University' + assert entity_display_name(self.e, langs=['sv']) == 'Example universitet' + + def test_extended_display(self): + name, descr = entity_extended_display(self.e) + assert name == 'Example University' + assert descr == 'Identity Provider for Example University' + + def test_extended_display_i18n(self): + names, descrs = entity_extended_display_i18n(self.e) + assert names == {'en': 'Example University', 'sv': 'Example universitet'} + assert descrs == { + 'en': 'Identity Provider for Example University', + 'sv': 'Identity Provider för Example universitet', + } + + def test_icon_url(self): + for langs in (None, ['en']): + icon = entity_icon_url(self.e, langs=langs) + assert icon is not None + assert 'scoped' not in icon['url'] + + def test_privacy_statement_url(self): + assert privacy_statement_url(self.e, ['en']) is None + + def test_simple_info(self): + info = entity_simple_info(self.e) + assert info['title'] == 'Example University' + assert info['descr'] == 'Identity Provider for Example University' + assert info['keywords'] == 'example' + + def test_info(self): + info = entity_info(self.e) + assert info['title'] == 'Example University' + assert info['descr'] == 'Identity Provider for Example University' + assert info['keywords'] == 'example' + assert info['privacy_statement_url'] is None diff --git a/src/pyff/test/test_store.py b/src/pyff/test/test_store.py index 4f725423..f043ddc4 100644 --- a/src/pyff/test/test_store.py +++ b/src/pyff/test/test_store.py @@ -149,6 +149,21 @@ def test_search_swamid(self): assert e[0] is not None assert e[0].get('entityID') is not None + def test_search_scoped_ui_info(self): + """Terms found only in non-standard oaf:ScopedUIInfo must not be searchable.""" + scoped = parse_xml(os.path.join(self.datadir, 'test-scoped-display-name.xml')) + entity_id = root(scoped).get('entityID') + store = RedisWhooshStore(directory=self.dir, clear=True, name="test", redis=fakeredis.FakeStrictRedis()) + store.update(scoped, etag='scoped', lazy=False) + # terms from the entity's own mdui:UIInfo + for q in ('example', 'university'): + e = list(store.search(q)) + assert len(e) == 1 + assert e[0].get('entityID') == entity_id + # terms that only occur in oaf:ScopedUIInfo + for q in ('scoped', 'scopednyckelord', 'scopedkeyword'): + assert list(store.search(q)) == [] + class TestMemoryStore(TestCase): def setUp(self): @@ -168,6 +183,21 @@ def test_create_store(self): assert str(store) assert not store.attributes() + def test_search_scoped_ui_info(self): + """Terms found only in non-standard oaf:ScopedUIInfo must not be searchable.""" + scoped = parse_xml(os.path.join(self.datadir, 'test-scoped-display-name.xml')) + entity_id = root(scoped).get('entityID') + store = MemoryStore() + store.update(scoped) + # terms from the entity's own mdui:UIInfo + for q in ('example university', 'universitet', 'exempel', 'example'): + e = list(store.search(q)) + assert len(e) == 1 + assert e[0].get('entityID') == entity_id + # terms that only occur in oaf:ScopedUIInfo + for q in ('scoped', 'scopednyckelord', 'scopedkeyword'): + assert list(store.search(q)) == [] + def test_parse_wayf(self): assert self.wayf_source is not None assert os.path.exists(self.wayf_source)