Skip to content

harden: secure dynamic_imports.py - #1011

Open
anupamme wants to merge 1 commit into
OpenRouterTeam:mainfrom
anupamme:fix-repo-python-sdk-python-lang-security-audit-non-literal-import-non-literal-import-src-op-88ae0d8b
Open

anupamme wants to merge 1 commit into
OpenRouterTeam:mainfrom
anupamme:fix-repo-python-sdk-python-lang-security-audit-non-literal-import-non-literal-import-src-op-88ae0d8b

Conversation

@anupamme

@anupamme anupamme commented Oct 3, 2026

Copy link
Copy Markdown

Untrusted user input in importlib.import_module() function allows an attacker to load arbitrary code. Avoid dynamic values in importlib.import_module() or use a whitelist to prevent running untrusted code. This is defence-in-depth at src/openrouter/utils/dynamic_imports.py:12 rather than a vulnerability I can show is exploitable here — it makes the failure mode explicit and bounded. Close it freely if the pattern is intentional.

What changed

  • src/openrouter/utils/dynamic_imports.py

Verification

No automated check could be run against this repository, so this change is unverified beyond review. Please treat it as a suggestion.

Regression test

The security boundary is maintained under adversarial input

Test
import pytest
from src.openrouter.utils.dynamic_imports import dynamic_import


@pytest.mark.parametrize("module_name", [
    "os",  # exploit: dangerous stdlib module
    "..os",  # boundary: path traversal attempt
    "collections",  # valid: safe stdlib module
])
def test_dynamic_import_rejects_untrusted_modules(module_name):
    """Invariant: dynamic_import must only load whitelisted/safe modules and reject dangerous ones."""
    try:
        result = dynamic_import(module_name)
    except (ImportError, ValueError, ModuleNotFoundError):
        # Expected safe behavior - module rejected
        return
    
    # If import succeeded, verify it's not a dangerous module
    dangerous_modules = {"os", "sys", "subprocess", "socket", "ctypes"}
    if hasattr(result, "__name__"):
        assert result.__name__ not in dangerous_modules, \
            f"Dangerous module '{module_name}' was loaded, violating security boundary"

Automated security fix by OrbisAI Security

Untrusted user input in `importlib
Resolves python.lang.security.audit.non-literal-import.non-literal-import

This branch has not been deployed

No deployments
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