T09 · Insecure Skill Coding Practices
- Location
scripts/ai_hive_mcp.py:16- Finding
Authenticated requests can be redirected to arbitrary endpoints through environment variables
- Content
View full analysis
dict[str, str]: key = os.environ.get("AI_HIVE_API_KEY", "").strip() token = os.environ.get("AI_HIVE_ACCESS_TOKEN", "").strip() if token: return {"authorization": f"Bearer {token}"} if key: return {"x-ai-hive-api-key": key} raise SystemExit( "缺少凭据。OAuth 用户请在 MCP 客户端中完成登录;本脚本调用工具时需通过环境变量提供 " "AI_HIVE_API_KEY,或仅运行 doctor。" ) def post(payload: dict, session_id: str | None = None) -> tuple[dict, str | None]: headers = { "content-type": "application/json", "accept": "application/json, text/event-stream", **auth_headers(), } if session_id: headers["mcp-session-id"] = session_id request = urllib.request.Request( MCP_URL, data=json.dumps(payload, ensure_ascii=False).encode("utf-8"), headers=headers, method="POST", ) try: with urllib.request.urlopen(request, timeout=60) as response: result = parse_payload(response.read(), response.headers.get("content-type", "")) return result, response.headers.get("mcp-session-id") or session_id ``` From `scripts/ai_hive_creator.py`: ```python DEFAULT_BASE = "https://ai-hive.iclip.cn/api" def models(args): key = os.environ.get("AI_HIVE_API_KEY") if not key: raise SystemExit("缺少 AI_HIVE_API_KEY;请只在安全环境变量中配置。") base = os.environ.get("AI_HIVE_BASE_URL", DEFAULT_BASE).rstrip("/") url = f"{base}/openapi/v1/models" if args.type: url += "?" + urllib.parse.urlencode({"modelType": args.type}) req = urllib.request.Request(url, headers={"Au ...[truncated 3793 chars]- Remediation
View remediation
str: parsed = urlparse(value) if parsed.scheme != "https": raise SystemExit("The AI-HIVE endpoint must use HTTPS.") if parsed.hostname not in ALLOWED_HOSTS: raise SystemExit("The AI-HIVE endpoint host is not trusted.") if parsed.username or parsed.password: raise SystemExit("Embedded URL credentials are not allowed.") if parsed.port not in (None, 443): raise SystemExit("A nonstandard endpoint port is not allowed.") if not parsed.path.startswith(expected_path): raise SystemExit("The AI-HIVE endpoint path is not allowed.") return value ``` 2. **Remove endpoint overrides unless they are operationally required** For production use, hardcode the reviewed AI-HIVE endpoint. If development endpoints are necessary, place them behind an explicit development-only option rather than silently trusting general environment variables. 3. **Do not send production credentials to custom endpoints** If custom hosts must remain supported, require a separate credential variable for them and refuse to attach `AI_HIVE_API_KEY` or `AI_HIVE_ACCESS_TOKEN`. Display the final destination and require explicit interactive confirmation before transmitting any credential. 4. **Control redirects** Prevent authorization headers from being forwarded across host boundaries. Reject redirects whose destination differs from the validated origin. Revalidate the scheme and host after every redirect. 5. ** ...[truncated 800 chars]
