T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/search.py:139
- Finding
- HTTPS Certificate and Hostname Verification Disabled## Vulnerability Details **File Location**: `scripts/search.py`, lines 139–147 **Vulnerability Type**: Improper TLS certificate validation **Risk Level**: High ### Vulnerable Code ```python # 禁用 SSL 验证 ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE try: with urllib.request.urlopen(req, timeout=60, context=ctx) as response: result = json.loads(response.read().decode("utf-8")) ``` ### Technical Analysis The script creates a TLS context but explicitly disables both hostname validation and certificate-chain verification. Consequently, HTTPS encryption is used without authenticating that the remote endpoint is actually `qianfan.baidubce.com`. The request carries the Baidu API key in the `Authorization: Bearer` header. Its body may contain financial queries, custom instructions, and caller-supplied conversation history. A network-positioned attacker can present an arbitrary certificate, impersonate the Baidu endpoint, decrypt the request, and provide a forged response because the client accepts certificates that are untrusted or issued for a different hostname. ### Attack Path 1. A user invokes the Skill to submit a financial search. 2. An attacker obtains a network interception position, such as through a malicious proxy, compromised router, hostile Wi-Fi network, or DNS manipulation. 3. The attacker redirects or intercepts the connection intended for `qianfan.baidubce.com`. 4. The attacker's server presents an arbitrary TLS certificate. 5. Because `check_hostname` is `False` and `verify_mode` is `ssl.CERT_NONE`, the client accepts the certificate. 6. The client transmits the Bearer API key and request body to the impersonated endpoint. 7. The attacker captures the credential and potentially sensitive request data. 8. The attacker can return fabricated JSON containing manipulated financial analysis or references, which the script formats and presents as a legitimate result. ### Impact Assessment Su ...[truncated 616 chars]
- Remediation
- ## Remediation Suggestions Remove the custom TLS context and allow `urllib` to use certificate and hostname verification through the operating system's trusted CA store: ```python try: with urllib.request.urlopen(req, timeout=60) as response: result = json.loads(response.read().decode("utf-8")) ``` Alternatively, create a secure context without weakening its defaults: ```python ctx = ssl.create_default_context() try: with urllib.request.urlopen(req, timeout=60, context=ctx) as response: result = json.loads(response.read().decode("utf-8")) ``` If a private CA is required, load only the necessary trusted CA bundle with `cafile` or `load_verify_locations()`. Keep `check_hostname = True` and `verify_mode = ssl.CERT_REQUIRED`. Additional hardening should include: - Fail closed on certificate or hostname validation errors. - Never add an option that silently disables TLS verification in production. - Rotate the Baidu API key if the vulnerable implementation has been used over untrusted networks. - Restrict the API key's permissions and quota to the minimum required. - Add a regression test confirming that self-signed, expired, and hostname-mismatched certificates are rejected.
