agents: block all stdlib modules in agent-config code-refs (denylist bypass via cProfile.run/timeit) - #6596
Open
asroyxCySec wants to merge 1 commit into
Conversation
The agent-config denylist (`_BLOCKED_MODULES`) only compared the top-level
module name of a code-ref against a hand-maintained list of dangerous stdlib
modules. That list is inherently incomplete: equivalent code-execution gadgets
slip through. For example `profile` is blocked (its `profile.run("<code>")`
runs arbitrary code) but its C sibling `cProfile` is not, and
`cProfile.run("<code>")` is the identical gadget. `timeit.timeit`, `pydoc`,
`logging.config.fileConfig`, `bdb`, `trace` and `venv` are similarly reachable.
Agent-config tool/callback/model/schema references point to user-defined or ADK
packages, never to the standard library, so reject any stdlib top-level module
via `sys.stdlib_module_names` (Python 3.10+, matching requires-python). This
closes the bypass class instead of chasing individual gadgets. `_BLOCKED_MODULES`
is kept for documentation/defense-in-depth; legitimate references (user
packages, `google.adk.*`) are unaffected.
Adds regression tests for the previously-bypassing gadgets and a test that
user/ADK references still validate.
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.
Problem
config_agent_utils._validate_module_reference()protects YAML agent configs against arbitrary code execution by blocking a hand-maintained denylist of dangerous stdlib modules (_BLOCKED_MODULES) referenced intool/callback/model/schemacode-refs. The check only compares the top-level module name against the denylist.A top-level-name denylist is inherently incomplete: equivalent code-execution gadgets that are not in the list slip through. The clearest example is already visible in the current list —
profileis blocked (itsprofile.run("<code>")executes arbitrary code), but its C siblingcProfileis not, andcProfile.run("<code>")is the identical gadget. Other reachable, non-listed gadgets includetimeit.timeit("<code>"),pydoc,logging.config.fileConfig,bdb,trace, andvenv.Example bypass (before this change):
Fix
Agent-config code-refs point to user-defined or ADK packages, never to the Python standard library. So instead of chasing individual gadgets, reject any standard-library top-level module via
sys.stdlib_module_names(Python 3.10+, which matches the project'srequires-python). The existing_BLOCKED_MODULESset is kept as documentation / belt-and-suspenders. Legitimate references (user packages,google.adk.*) are unaffected because they are not part ofsys.stdlib_module_names.Testing
test_stdlib_gadget_modules_are_rejectedcovers the previously-bypassing gadgets (cProfile.run,timeit.timeit,pydoc,logging.config.fileConfig,bdb,trace,venv).test_non_stdlib_references_are_not_blockedconfirms user/ADK references still validate.os,subprocess, network modules, denylist-disable) continue to pass._validate_module_referencedirectly: all listed gadgets + the existing denylist entries are rejected, whilemy_company_pkg.my_toolandgoogle.adk.tools.google_searchare allowed.