XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

Enhance security in pa_provider by implementing import restrictions for sensitive submodules; remove unused AuthManager import in base_provider

889e0977
hlohaus <hlohaus@users.noreply.github.com>
提交于

代码差异

2 个文件 +46 -5
Modified g4f/mcp/pa_provider.py +46 -0
@@ -204,6 +204,32 @@ def _make_restricted_import(allowed: FrozenSet[str]):
204 204 """Return a ``__import__`` replacement that only allows *allowed* modules."""
205 205 original = _builtins.__import__
206 206
207 # Sensitive g4f submodules that must never be accessible inside a sandbox,
208 # regardless of the top-level module being in *allowed*.
209 _BLOCKED_SUBMODULES: FrozenSet[str] = frozenset({
210 # API-key / credential management
211 "g4f.tools.auth",
212 "g4f.tools.run_tools",
213 # App config (holds g4f_api_key, disable_custom_api_key, …)
214 "g4f.config",
215 # Cookie / session storage
216 "g4f.cookies",
217 # Internals that expose auth helpers transitively
218 "g4f.providers.retry_provider",
219 "g4f.providers.config_provider",
220 # Block the entire Provider package; only specific safe submodules are
221 # explicitly permitted via _ALLOWED_G4F_SUBPATHS below.
222 "g4f.Provider",
223 })
224
225 # Explicit allowlist for g4f sub-paths that would otherwise be blocked.
226 # Checked *before* the blocklist so these entries take priority.
227 _ALLOWED_G4F_SUBPATHS: FrozenSet[str] = frozenset({
228 "g4f.Provider.helper",
229 "g4f.Provider.base_provider",
230 "g4f.typing",
231 })
232
207 233 def _restricted_import(name, globals=None, locals=None, fromlist=(), level=0):
208 234 if level > 0:
209 235 raise ImportError(
@@ -215,6 +241,26 @@ def _make_restricted_import(allowed: FrozenSet[str]):
215 241 f"Import of '{name}' is not allowed in safe execution mode.\n"
216 242 f"Allowed top-level modules: {', '.join(sorted(allowed))}"
217 243 )
244 # Explicit allowlist takes priority over the blocklist below.
245 # This permits e.g. "g4f.Provider.helper" even though "g4f.Provider"
246 # is blocked.
247 for allowed_sub in _ALLOWED_G4F_SUBPATHS:
248 if name == allowed_sub or name.startswith(allowed_sub + "."):
249 return original(name, globals, locals, fromlist, level)
250 # Block sensitive g4f submodules even though g4f itself is allowed.
251 if name in _BLOCKED_SUBMODULES:
252 raise ImportError(
253 f"Import of '{name}' is not allowed inside a .pa.py sandbox "
254 f"for security reasons."
255 )
256 # Also block when a blocked submodule is the parent of a deeper import
257 # (e.g. "g4f.tools.auth.something", "g4f.Provider.OpenAI").
258 for blocked in _BLOCKED_SUBMODULES:
259 if name.startswith(blocked + "."):
260 raise ImportError(
261 f"Import of '{name}' is not allowed inside a .pa.py sandbox "
262 f"for security reasons."
263 )
218 264 return original(name, globals, locals, fromlist, level)
219 265
220 266 return _restricted_import
Modified g4f/providers/base_provider.py +0 -5
@@ -23,7 +23,6 @@ from .helper import concat_chunks
23 23 from ..cookies import get_cookies_dir
24 24 from ..requests import raise_for_status
25 25 from ..errors import ModelNotFoundError, ResponseError, MissingAuthError, NoValidHarFileError, PaymentRequiredError, CloudflareError
26 from ..tools.auth import AuthManager
27 26 from .. import debug
28 27
29 28 SAFE_PARAMETERS = [
@@ -408,10 +407,6 @@ class ProviderModelMixin:
408 407 if not cls.models_loaded and hasattr(cls, "get_cache_file"):
409 408 if cls.get_cache_file().exists():
410 409 cls.live += 1
411 elif not api_key:
412 api_key = AuthManager.load_api_key(cls)
413 if api_key:
414 cls.live += 1
415 410 cls.models_loaded = True
416 411 return cls.models
417 412