|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Tests for module path validation in YAML agent config resolution.""" |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from google.adk.agents.config_agent_utils import _validate_module_path |
| 20 | + |
| 21 | + |
| 22 | +class TestValidateModulePath: |
| 23 | + """Tests for _validate_module_path blocklist enforcement.""" |
| 24 | + |
| 25 | + def test_safe_module_passes(self): |
| 26 | + """User-defined modules should pass validation.""" |
| 27 | + _validate_module_path("my_app.agents.my_agent") |
| 28 | + _validate_module_path("google.adk.agents") |
| 29 | + _validate_module_path("my_package") |
| 30 | + |
| 31 | + def test_os_module_blocked(self): |
| 32 | + """os module should be blocked.""" |
| 33 | + with pytest.raises(ValueError, match="blocked module"): |
| 34 | + _validate_module_path("os") |
| 35 | + |
| 36 | + def test_os_path_blocked(self): |
| 37 | + """os.path should be blocked.""" |
| 38 | + with pytest.raises(ValueError, match="blocked module"): |
| 39 | + _validate_module_path("os.path") |
| 40 | + |
| 41 | + def test_subprocess_blocked(self): |
| 42 | + """subprocess module should be blocked.""" |
| 43 | + with pytest.raises(ValueError, match="blocked module"): |
| 44 | + _validate_module_path("subprocess") |
| 45 | + |
| 46 | + def test_sys_blocked(self): |
| 47 | + """sys module should be blocked.""" |
| 48 | + with pytest.raises(ValueError, match="blocked module"): |
| 49 | + _validate_module_path("sys") |
| 50 | + |
| 51 | + def test_shutil_blocked(self): |
| 52 | + """shutil module should be blocked.""" |
| 53 | + with pytest.raises(ValueError, match="blocked module"): |
| 54 | + _validate_module_path("shutil") |
| 55 | + |
| 56 | + def test_pickle_blocked(self): |
| 57 | + """pickle module should be blocked.""" |
| 58 | + with pytest.raises(ValueError, match="blocked module"): |
| 59 | + _validate_module_path("pickle") |
| 60 | + |
| 61 | + def test_importlib_blocked(self): |
| 62 | + """importlib module should be blocked.""" |
| 63 | + with pytest.raises(ValueError, match="blocked module"): |
| 64 | + _validate_module_path("importlib") |
| 65 | + |
| 66 | + def test_builtins_blocked(self): |
| 67 | + """builtins module should be blocked.""" |
| 68 | + with pytest.raises(ValueError, match="blocked module"): |
| 69 | + _validate_module_path("builtins") |
| 70 | + |
| 71 | + def test_socket_blocked(self): |
| 72 | + """socket module should be blocked.""" |
| 73 | + with pytest.raises(ValueError, match="blocked module"): |
| 74 | + _validate_module_path("socket") |
| 75 | + |
| 76 | + def test_empty_path_blocked(self): |
| 77 | + """Empty module path should be rejected.""" |
| 78 | + with pytest.raises(ValueError, match="must not be empty"): |
| 79 | + _validate_module_path("") |
| 80 | + |
| 81 | + def test_invalid_characters_blocked(self): |
| 82 | + """Module paths with special characters should be rejected.""" |
| 83 | + with pytest.raises(ValueError, match="invalid characters"): |
| 84 | + _validate_module_path("os;import sys") |
| 85 | + |
| 86 | + def test_dunder_segment_blocked(self): |
| 87 | + """Module paths with __dunder__ segments should be rejected.""" |
| 88 | + with pytest.raises(ValueError, match="dunder segment"): |
| 89 | + _validate_module_path("my_app.__builtins__.evil") |
| 90 | + |
| 91 | + def test_google_adk_passes(self): |
| 92 | + """google.adk modules should pass (not blocked).""" |
| 93 | + _validate_module_path("google.adk.tools.my_tool") |
| 94 | + _validate_module_path("google.adk.agents.llm_agent") |
| 95 | + |
| 96 | + def test_multiprocessing_blocked(self): |
| 97 | + """multiprocessing should be blocked.""" |
| 98 | + with pytest.raises(ValueError, match="blocked module"): |
| 99 | + _validate_module_path("multiprocessing.pool") |
0 commit comments