{"record":{"id":"d76b6ec2aa268170","repo":"xtekky/gpt4free","slug":"name-is-not-available-in-the-restricted-os-shi","errorCode":null,"errorMessage":"'{name}' is not available in the restricted os shim.","messagePattern":"'(.+?)' is not available in the restricted os shim\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"g4f/mcp/pa_provider.py","lineNumber":460,"sourceCode":"    )\n\n    def _restricted_import(name, globals=None, locals=None, fromlist=(), level=0):\n        if level > 0:\n            raise ImportError(\n                \"Relative imports are not allowed inside a .pa.py sandbox.\"\n            )\n        base = name.split(\".\")[0]\n        # Return the restricted os shim instead of the real os module.\n        if base == \"os\":\n            _os_shim = _make_restricted_os()\n            if name == \"os\":\n                return _os_shim\n            # Handle \"os.submodule\" — try to resolve from the shim\n            obj = _os_shim\n            for part in name.split(\".\")[1:]:\n                obj = getattr(obj, part, None)\n                if obj is None:\n                    raise ImportError(\n                        f\"'{name}' is not available in the restricted os shim.\"\n                    )\n            return obj\n        if base not in allowed:\n            # Before rejecting, check if it's a workspace module (sibling .py file).\n            workspace = get_workspace_dir()\n            ws_module = _load_workspace_module(\n                base, workspace, globals, fromlist, level\n            )\n            if ws_module is not None:\n                # Handle submodule imports (e.g. \"pkg.sub\")\n                if name != base:\n                    # Try to resolve the full dotted path from the loaded module\n                    obj = ws_module\n                    for part in name.split(\".\")[1:]:\n                        obj = getattr(obj, part, None)\n                        if obj is None:\n                            raise ImportError(","sourceCodeStart":442,"sourceCodeEnd":478,"githubUrl":"https://github.com/xtekky/gpt4free/blob/973504e1770928ed5fb82f43da528f441ad9ddc3/g4f/mcp/pa_provider.py#L442-L478","documentation":"Raised inside the g4f MCP '.pa.py' sandbox when code does 'import os.<submodule>' (e.g. 'os.path') and the submodule attribute does not exist on the restricted os shim. The shim (_make_restricted_os in g4f/mcp/pa_provider.py:384) only exposes urandom, name, sep, linesep, altsep and pathsep, so almost every dotted os import fails. This is an intentional security restriction: filesystem, process and environment operations are unavailable in user-provided PA provider files.","triggerScenarios":"Executing a .pa.py file whose top-level code contains 'import os.path', 'import os.environ' or any 'import os.X' where X is not one of the six shim attributes. Only 'import os' followed by use of os.urandom/os.name/os.sep/os.linesep/os.altsep/os.pathsep succeeds.","commonSituations":"A provider script copied from a normal Python project that uses os.path.join or os.environ; porting example code that reads environment variables for API keys; any sandbox script that tries to detect the platform via dotted import instead of attribute access.","solutions":["Use plain 'import os' and only the attributes urandom, name, sep, linesep, altsep, pathsep","Replace os.path.join with pathlib.Path (pathlib is in SAFE_MODULES)","Replace os.environ lookups with values passed as provider parameters","If you maintain the server and truly need more of os, extend _SAFE_OS_ATTRS in _make_restricted_os — but each addition widens the sandbox attack surface"],"exampleFix":"// before\nimport os.path\np = os.path.join(base, \"file.json\")\n\n// after\nfrom pathlib import Path\np = Path(base) / \"file.json\"","handlingStrategy":"validation","validationCode":"# before writing the .pa.py, check the imports you plan to use\nALLOWED_OS_ATTRS = {\"urandom\", \"name\", \"sep\", \"linesep\", \"altsep\", \"pathsep\"}\nassert all(a in ALLOWED_OS_ATTRS for a in os_attrs_used), \"use plain 'import os' only\"","typeGuard":null,"tryCatchPattern":"try:\n    import os.subthing  # will raise ImportError in sandbox\nexcept ImportError as e:\n    if \"restricted os shim\" in str(e):\n        # rewrite the import: plain 'import os' or pathlib\n        ...","preventionTips":["Never use dotted os imports in .pa.py files; access os attributes directly after 'import os'","Prefer pathlib (allowed) over os.path for path manipulation in sandbox code","Lint sandbox scripts for 'import os.' before loading them with load_pa_provider"],"tags":["sandbox","import","security","os-module","pa-provider"],"backgroundTag":null,"analyzedSha":"973504e1770928ed5fb82f43da528f441ad9ddc3","analyzedAt":"2026-08-14T23:45:32.408Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}