T09 · Insecure Skill Coding Practices
Warning
- Location
- scripts/media_gen_client.py:43
- Finding
- Bearer Token Exposure Through Unrestricted HTTP Redirects<![CDATA[ ## Vulnerability Details **File Location**: `scripts/media_gen_client.py:43-62` **Vulnerability Type**: Authenticated request redirect trust-boundary violation **Risk Level**: Medium ### Vulnerable Code ```python def _http_request_json( *, method: str, url: str, api_key: str, headers: Optional[Dict[str, str]] = None, body: Optional[Dict[str, Any]] = None, timeout_s: int = 60, user_agent: str = "OpenClaw-Media-Gen/1.0", ) -> Dict[str, Any]: all_headers = { "Authorization": f"Bearer {api_key}", "Accept": "application/json", "User-Agent": user_agent, } if headers: all_headers.update(headers) data: Optional[bytes] = None if body is not None: data = json.dumps(body).encode("utf-8") all_headers.setdefault("Content-Type", "application/json") elif method.upper() in {"POST", "PUT", "PATCH"}: data = b"{}" all_headers.setdefault("Content-Type", "application/json") req = urllib.request.Request(url, data=data, headers=all_headers, method=method.upper()) try: with urllib.request.urlopen(req, timeout=timeout_s) as resp: ``` ### Technical Analysis The client places the AIsa API key in the `Authorization` header and submits the request through `urllib.request.urlopen`. Python's default URL opener automatically processes HTTP redirects. The code neither disables redirects nor verifies that a redirect remains on the expected HTTPS origin. Consequently, the authenticated request can cross from the trusted `api.aisa.one` origin to another origin without an application-level authorization check. Redirect processing may propagate sensitive request headers, including the bearer token. This behavior exceeds the minimum privilege needed for the declared functionality: the credential only needs to be disclosed to the documented AIsa API origin. Exploitation requires control over, or compromise of, an endpoint in the authenticated reque ...[truncated 1179 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Disable automatic redirects for requests carrying credentials, or use a custom `HTTPRedirectHandler`. 2. Permit authenticated requests only to an explicit allowlist containing the documented AIsa HTTPS origin. 3. If redirects are operationally required: - Resolve the new URL against the current URL. - Require the `https` scheme. - Compare the normalized hostname and effective port with the original origin. - Reject user-info components and unexpected ports. - Strip `Authorization` whenever the origin changes. - Apply a strict redirect-count limit. 4. Do not resend POST request bodies across cross-origin redirects. 5. Add tests for same-origin redirects, cross-origin redirects, HTTPS-to-HTTP downgrades, malformed destinations, and redirect loops. 6. Prefer environment-based secret handling over `--api-key`, because command-line arguments may be exposed through process listings and shell history. ]]>
