{"record":{"id":"26aa84ea749ab7c2","repo":"xtekky/gpt4free","slug":"import-of-name-is-not-allowed-inside-a-pa-py","errorCode":null,"errorMessage":"Import of '{name}' is not allowed inside a .pa.py sandbox for security reasons.","messagePattern":"Import of '(.+?)' is not allowed inside a \\.pa\\.py sandbox for security reasons\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"g4f/mcp/pa_provider.py","lineNumber":495,"sourceCode":"                        if obj is None:\n                            raise ImportError(\n                                f\"Cannot find submodule '{name}' in workspace module '{base}'.\"\n                            )\n                    return obj\n                return ws_module\n            raise ImportError(\n                f\"Import of '{name}' is not allowed in safe execution mode.\\n\"\n                f\"Allowed top-level modules: {', '.join(sorted(allowed))}\"\n            )\n        # Explicit allowlist takes priority over the blocklist below.\n        # This permits e.g. \"g4f.Provider.helper\" even though \"g4f.Provider\"\n        # is blocked.\n        for allowed_sub in _ALLOWED_G4F_SUBPATHS:\n            if name == allowed_sub or name.startswith(allowed_sub + \".\"):\n                return original(name, globals, locals, fromlist, level)\n        # Block sensitive g4f submodules even though g4f itself is allowed.\n        if name in _BLOCKED_SUBMODULES:\n            raise ImportError(\n                f\"Import of '{name}' is not allowed inside a .pa.py sandbox \"\n                f\"for security reasons.\"\n            )\n        # Also block when a blocked submodule is the parent of a deeper import\n        # (e.g. \"g4f.tools.auth.something\", \"g4f.Provider.OpenAI\").\n        for blocked in _BLOCKED_SUBMODULES:\n            if name.startswith(blocked + \".\"):\n                raise ImportError(\n                    f\"Import of '{name}' is not allowed inside a .pa.py sandbox \"\n                    f\"for security reasons.\"\n                )\n        return original(name, globals, locals, fromlist, level)\n\n    return _restricted_import\n\n\ndef _make_safe_globals(\n    allowed: FrozenSet[str] = SAFE_MODULES,","sourceCodeStart":477,"sourceCodeEnd":513,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/mcp/pa_provider.py#L477-L513","documentation":"Raised when a .pa.py sandbox imports a g4f submodule listed in _BLOCKED_SUBMODULES — g4f.tools.auth, g4f.tools.run_tools, g4f.config, g4f.cookies, g4f.providers.retry_provider, g4f.providers.config_provider, g4f.Provider, and similar credential/config-bearing modules. Even though 'g4f' itself is allowed, these submodules expose API keys, cookie stores and auth helpers, so the exact name is rejected.","triggerScenarios":"'import g4f.config', 'import g4f.cookies', 'import g4f.tools.auth', 'import g4f.Provider' (exact match on a blocked name) inside sandboxed provider code. Note _ALLOWED_G4F_SUBPATHS is checked first, so explicitly permitted paths like g4f.Provider.helper still work.","commonSituations":"Trying to read the host's stored g4f API key or cookies from a custom provider; importing the Provider package to reuse another provider's payload builder; code written before the blocklist was introduced that worked on older g4f versions.","solutions":["Import only the explicitly allowed subpaths (e.g. g4f.Provider.helper) — check _ALLOWED_G4F_SUBPATHS in pa_provider.py","Pass needed credentials as explicit parameters to your provider instead of reading g4f.config/cookies","Copy the small helper logic you need into the .pa.py file itself or a sibling workspace module","Never attempt to reach auth/cookie tooling from sandboxed code — the block is deliberate"],"exampleFix":"# before\nimport g4f.Provider\n\n# after\nimport g4f.Provider.helper  # explicitly allowlisted subpath","handlingStrategy":"try-catch","validationCode":"from g4f.mcp.pa_provider import _ALLOWED_G4F_SUBPATHS if False else None\n# simpler: static check before load\nBLOCKED = {\"g4f.tools.auth\", \"g4f.tools.run_tools\", \"g4f.config\", \"g4f.cookies\",\n           \"g4f.providers.retry_provider\", \"g4f.providers.config_provider\", \"g4f.Provider\"}\ndef is_blocked(name: str) -> bool:\n    return name in BLOCKED or any(name.startswith(b + \".\") for b in BLOCKED)","typeGuard":null,"tryCatchPattern":"try:\n    import g4f.config\nexcept ImportError as e:\n    if \"sandbox\" in str(e):\n        # credentials must arrive as provider parameters instead\n        ...","preventionTips":["Never import credential/config/cookie modules from sandboxed code","Pass API keys explicitly as provider parameters","Pre-check imports against the blocked list before loading a .pa.py file"],"tags":["sandbox","security","import","credentials","g4f"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}