From 8d8923a0656826ded390607c890b57e657b72443 Mon Sep 17 00:00:00 2001 From: DZH166 <248446924+DZH166@users.noreply.github.com> Date: Wed, 4 Nov 2026 01:10:11 +0800 Subject: [PATCH] fix(agents): treat root-relative config_path as absolute on Windows os.path.isabs is platform-dependent: on Windows a root-relative path such as /etc/passwd has no drive and is not absolute, so the AgentRefConfig guard let it through to the traversal check, which reports a different error than the documented one (and an unhandled commonpath error across drives). Reject config_path rooted under either platform's spelling so the guard behaves the same everywhere. --- src/google/adk/agents/config_agent_utils.py | 9 ++++++++- tests/unittests/agents/test_agent_config.py | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/google/adk/agents/config_agent_utils.py b/src/google/adk/agents/config_agent_utils.py index fcba9107b0d..a2201a2ef1a 100644 --- a/src/google/adk/agents/config_agent_utils.py +++ b/src/google/adk/agents/config_agent_utils.py @@ -1112,7 +1112,14 @@ def resolve_agent_reference( The created agent instance. """ if ref_config.config_path: - if os.path.isabs(ref_config.config_path): + # `os.path.isabs` is platform-dependent: on Windows a root-relative path + # such as `/etc/passwd` has no drive, so it is not "absolute" and would + # slip past this guard into the traversal check below, which reports a + # different error (or, across drives, an unhandled commonpath error). A + # config_path rooted under either platform's spelling is absolute here. + if os.path.isabs( + ref_config.config_path + ) or ref_config.config_path.startswith(("/", "\\")): raise ValueError( "Absolute paths are not allowed in AgentRefConfig config_path:" f" {ref_config.config_path!r}" diff --git a/tests/unittests/agents/test_agent_config.py b/tests/unittests/agents/test_agent_config.py index fbf41508956..5a9dac1ffb7 100644 --- a/tests/unittests/agents/test_agent_config.py +++ b/tests/unittests/agents/test_agent_config.py @@ -490,6 +490,23 @@ def test_resolve_agent_reference_blocks_absolute_path(): ) +def test_resolve_agent_reference_blocks_root_relative_path(): + """A root-relative config_path counts as absolute on every platform. + + On Windows `os.path.isabs` is False for a path that has a root but no + drive, so both spellings are rejected explicitly. + """ + for config_path in ("/etc/passwd", "\\etc\\passwd"): + ref_config = AgentRefConfig(config_path=config_path) + with pytest.raises( + ValueError, + match="Absolute paths are not allowed in AgentRefConfig config_path", + ): + config_agent_utils.resolve_agent_reference( + ref_config, "/workspace/main.yaml" + ) + + def test_resolve_agent_reference_blocks_path_traversal(): """Verify resolve_agent_reference raises ValueError for path traversal.""" ref_config = AgentRefConfig(config_path="../outside.yaml")