Skip to content

fix: validate module names before dynamic import#875

Open
RinZ27 wants to merge 4 commits intomodelscope:mainfrom
RinZ27:fix-module-injection
Open

fix: validate module names before dynamic import#875
RinZ27 wants to merge 4 commits intomodelscope:mainfrom
RinZ27:fix-module-injection

Conversation

@RinZ27
Copy link

@RinZ27 RinZ27 commented Mar 4, 2026

Validation for module names in LLMAgent and loader ensures that only alphanumeric characters and underscores are processed before being passed to import_module. While the trust_remote_code flag provides a foundational layer of security, adding strict character-level checks prevents potential injection of unintended modules during the initialization of external handlers and callbacks. During my analysis of the core loading logic, I identified several paths where module identifiers were accepted from configuration files without sufficient sanitization.

Existing functionality for trusted remote code remains intact, as the new validation logic only rejects malformed or malicious strings. These changes were verified locally by ensuring that standard plugin loading works as expected while strings containing special characters or path sequences trigger a clear ValueError. Adhering to the project's security posture, this hardening measure provides a more resilient implementation for handling dynamic extensions within the framework.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the security posture of the agent framework by introducing robust validation for module names during dynamic import operations. By strictly enforcing character-level checks, it mitigates risks associated with loading external handlers, callbacks, and code from untrusted sources, thereby preventing potential injection vulnerabilities without impacting existing trusted remote code functionality.

Highlights

  • Enhanced Security for Dynamic Imports: Implemented strict validation for module names used in dynamic imports within LLMAgent and loader components. This ensures that only module names consisting of alphanumeric characters, hyphens, and underscores are processed, preventing potential injection of unintended or malicious modules.
  • Refactored Dependencies: The threading module import was removed from ms_agent/agent/llm_agent.py, indicating a potential simplification or change in concurrency management.
  • Documentation Update: The 'Agent Skills' section was removed from the README.md file, streamlining the documentation.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • README.md
    • Removed the detailed 'Agent Skills' section and its sub-sections.
  • ms_agent/agent/llm_agent.py
    • Imported the re module for regular expression operations.
    • Removed the threading module import.
    • Implemented strict validation for handler_file names, ensuring they contain only alphanumeric characters, hyphens, or underscores before dynamic import.
    • Applied similar strict validation for _callback module names to prevent malicious injection.
  • ms_agent/agent/loader.py
    • Imported the re module for regular expression operations.
    • Introduced validation for code_file names, restricting them to alphanumeric characters, hyphens, or underscores before dynamic import.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@RinZ27 RinZ27 force-pushed the fix-module-injection branch from 5d559ac to 771134e Compare March 4, 2026 06:58
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces security hardening by validating module names before they are dynamically imported in LLMAgent and AgentLoader. This is a valuable measure to prevent potential module injection attacks. The changes correctly use os.path.basename to prevent directory traversal and sanitize module names. However, the regular expression used for validation is overly permissive and inconsistent with the pull request description. My review includes suggestions to tighten this regex for more robust validation that aligns with Python's identifier conventions.

@RinZ27 RinZ27 force-pushed the fix-module-injection branch from 771134e to 2c82792 Compare March 4, 2026 07:07
RinZ27 and others added 3 commits March 4, 2026 14:08
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant