fix: block standard library imports in YAML agent config resolution#5275
Open
KevinZhao wants to merge 1 commit intogoogle:mainfrom
Open
fix: block standard library imports in YAML agent config resolution#5275KevinZhao wants to merge 1 commit intogoogle:mainfrom
KevinZhao wants to merge 1 commit intogoogle:mainfrom
Conversation
Add validation to reject standard library module imports when resolving code references from YAML agent configurations. This prevents arbitrary code execution (e.g. `os.system`, `subprocess.call`) when configs are loaded from untrusted sources such as the `/builder/save` endpoint. Uses `sys.stdlib_module_names` (Python 3.10+) to automatically cover all 303 stdlib modules without maintaining a manual denylist. Project-level and third-party module imports remain unaffected. The validation is applied to all three `importlib.import_module()` call sites in `config_agent_utils.py`: - `resolve_fully_qualified_name()` (agent_class, model_code, schemas) - `_resolve_agent_code_reference()` (sub_agents[].code) - `resolve_code_reference()` (callbacks, model_code, schemas)
Collaborator
|
Response from ADK Triaging Agent Hello @KevinZhao, thank you for creating this PR! To help reviewers to review your PR more efficiently, could you please associate the github issue with this PR? If there is no existing issue, could you please create one? In addition, could you please provide a testing plan and the test results in the PR description? Thanks! |
Author
|
Hi @adk-bot, thanks for the review! Both items have been addressed:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5278
Summary
YAML agent configurations can reference arbitrary Python modules via
importlib.import_module()in code fields such asmodel_code,agent_class,tools, callbacks, andsub_agents. When configs originate from untrusted sources (e.g. the/builder/saveendpoint), this allows importing dangerous standard library modules likeos,subprocess, orpickle— potentially leading to arbitrary code execution.Fix
Add a shared
_validate_module_path()function that rejects imports from Python standard library modules beforeimportlib.import_module()is called. The validation usessys.stdlib_module_names(Python 3.10+, which aligns with the project'srequires-python >= 3.10) to automatically cover all 303 stdlib modules without a hand-maintained denylist.Applied to all three
importlib.import_module()call sites inconfig_agent_utils.py:resolve_fully_qualified_name()— used foragent_class,model_code,input_schema,output_schema_resolve_agent_code_reference()— used forsub_agents[].coderesolve_code_reference()— used for all four callback typesWhat is NOT affected
google.adk.*) — not in stdlibpydantic,langchain,openai, etc.) — not in stdlibmy_project.my_agent) — not in stdlibExample
Testing Plan
Blocked paths (should raise
ValueError):os.system→ blocked (osis insys.stdlib_module_names)subprocess.call→ blocked (subprocessis in stdlib)pickle.loads→ blocked (pickleis in stdlib)socket.socket→ blocked (socketis in stdlib)Allowed paths (should work as before):
google.adk.agents.LlmAgent→ allowed (not in stdlib)google.adk.tools.google_search→ allowed (not in stdlib)my_project.my_module.my_callback→ allowed (not in stdlib)langchain.tools.MyTool→ allowed (not in stdlib)Test results:
Validation function correctly discriminates between stdlib and non-stdlib modules across all 303 stdlib module names.