T09 · Insecure Skill Coding Practices
Warning
- Location
- src/baidu_search.py:49
- Finding
- API Key Exposure Through Unsanitized HTTP Exception Output<![CDATA[ ## Vulnerability Details **File Location**: `src/baidu_search.py`, lines 49-60 and 93-94 **Vulnerability Type**: Sensitive credential exposure **Risk Level**: Medium ### Vulnerable Code ```python params = { 'query': query, 'count': count, 'ak': self.api_key } headers = { 'Content-Type': 'application/json' } response = requests.get(url, params=params, headers=headers, timeout=30) response.raise_for_status() ``` ```python except Exception as e: return f"❌ Search failed: {str(e)}" ``` ### Technical Analysis The Baidu API key is transmitted through the `ak` URL query parameter. When `response.raise_for_status()` raises a `requests.exceptions.HTTPError`, the exception message can include the complete requested URL. Because the URL contains the API key, converting the exception to a string and returning it without sanitization may expose the credential. The returned error may subsequently be displayed to users or recorded in agent transcripts, application logs, monitoring systems, or other downstream output channels. TLS protects the request while it is in transit but does not prevent credential exposure through local exception formatting, URL logging, proxy logs, or server-side request logs. ### Attack Path 1. An attacker or ordinary user submits a search request that results in an HTTP error, or waits for an upstream authentication, rate-limit, or service error. 2. The client constructs a request URL containing `ak=<BAIDU_API_KEY>`. 3. `response.raise_for_status()` raises an exception whose text may contain the requested URL. 4. The broad exception handler converts the exception to a string without redaction. 5. The resulting error text is returned to the caller and may reveal the API key. 6. Anyone with access to that output can reuse the exposed key against the Baidu API until it is revoked or expires. ### Impact Assessment Successful exploitation can disclose the configured Baidu API credential. The exposed key provides ...[truncated 378 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Use a provider-supported authorization header or protected request body instead of a URL query parameter whenever the Baidu API permits it. 2. Do not return raw exception strings to callers. Return a generic failure message and retain only sanitized diagnostics. 3. Explicitly redact sensitive query parameters such as `ak` from exception messages, request URLs, and logs. 4. Catch expected exception types separately, including `Timeout`, `ConnectionError`, and `HTTPError`. 5. If server-side diagnostics are needed, log the status code and a generated request identifier rather than the complete URL. 6. Ensure reverse proxies, HTTP debugging facilities, and monitoring platforms do not record sensitive query strings. 7. Rotate the API key if affected exception output may already have been retained. Example hardened handling: ```python try: response = requests.get(url, params=params, headers=headers, timeout=30) response.raise_for_status() except requests.exceptions.Timeout: return "❌ Search failed: the upstream service timed out." except requests.exceptions.HTTPError: return "❌ Search failed: the upstream service returned an HTTP error." except requests.exceptions.RequestException: return "❌ Search failed: unable to contact the upstream service." ``` ]]>
