From 1af70bfa3ca85f7af4bdebaeeca3c218ff4520b2 Mon Sep 17 00:00:00 2001 From: Remy Noel Date: Fri, 12 May 2023 18:17:48 +0200 Subject: [PATCH 1/2] fix: add test exposing issue with subpaths Any base path matched by a subpath with a different root will also return the nodes of the subpath (which is obviously erroneous) --- tests/service/test_standard_interfaces.py | 34 +++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/service/test_standard_interfaces.py b/tests/service/test_standard_interfaces.py index dc8213ca..bc137fcb 100644 --- a/tests/service/test_standard_interfaces.py +++ b/tests/service/test_standard_interfaces.py @@ -83,6 +83,40 @@ async def test_introspectable_interface(): bus2.disconnect() +@pytest.mark.asyncio +async def test_introspect_matching_sub_paths(): + bus1 = await MessageBus().connect() + bus2 = await MessageBus().connect() + + interface = ExampleInterface("test.interface1") + + bus1.export("/a/test/path1", interface) + bus1.export("/a/subpath/a/test/path2", interface) + + async def introspect_subpath(path, expected_subnodes): + reply = await bus2.call( + Message( + destination=bus1.unique_name, + path=path, + interface="org.freedesktop.DBus.Introspectable", + member="Introspect", + ) + ) + assert reply.signature == "s" + node = intr.Node.parse(reply.body[0]) + assert {n.name for n in node.nodes} == expected_subnodes + + await introspect_subpath("/", {"a"}) + await introspect_subpath("/a", {"test", "subpath"}) + await introspect_subpath("/a/test", {"path1"}) + await introspect_subpath("/a/test/path1", set()) + await introspect_subpath("/a/subpath/a/test", {"path2"}) + await introspect_subpath("/a/subpath/a/test/path2", set()) + + bus1.disconnect() + bus2.disconnect() + + @pytest.mark.asyncio async def test_peer_interface(): bus1 = await MessageBus().connect() From 9107ff1e4ea889d7074a3e06e799c3ab8fa9d430 Mon Sep 17 00:00:00 2001 From: Remy Noel Date: Fri, 12 May 2023 18:51:47 +0200 Subject: [PATCH 2/2] fix: prevent matching subpaths upon introspections --- src/dbus_fast/message_bus.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/dbus_fast/message_bus.py b/src/dbus_fast/message_bus.py index 73a9d9a9..648a812f 100644 --- a/src/dbus_fast/message_bus.py +++ b/src/dbus_fast/message_bus.py @@ -673,11 +673,10 @@ def _introspect_export_path(self, path: str) -> intr.Node: children = set() for export_path in self._path_exports: - try: - child_path = export_path.split(path, maxsplit=1)[1] - except IndexError: + if not export_path.startswith(path): continue + child_path = export_path.split(path, maxsplit=1)[1] child_path = child_path.lstrip("/") child_name = child_path.split("/", maxsplit=1)[0]