T09 · Insecure Skill Coding Practices
- Location
scripts/compare_price.py:399- Finding
Authenticated Baidu Cloud API Requests Disable TLS Certificate Verification
- Content
View full analysis
Vulnerability Details
File Location:
scripts/compare_price.py:399-414
Vulnerability Type: Improper TLS certificate validation
Risk Level: HighVulnerable Code
python def _bcc_http_post(host, path, payload, ak, sk): """向百度云发起签名 POST,返回解析后的 JSON""" timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") headers = { "Host": host, "Content-Type": "application/json;charset=utf-8", "Authorization": _bcc_sign("POST", path, timestamp, ak, sk, host), } req = urllib.request.Request( f"https://{host}{path}", data=json.dumps(payload).encode(), headers=headers, method="POST", ) ctx = ssl._create_unverified_context() return json.loads(urllib.request.urlopen(req, timeout=15, context=ctx).read().decode())Technical Analysis
The function explicitly creates an unverified TLS context with
ssl._create_unverified_context(). This disables certificate-chain and hostname validation for all signed Baidu BCC, CDS, and EIP pricing requests made through this function.Although HTTPS is used, the client cannot authenticate the remote server when verification is disabled. An attacker capable of controlling DNS resolution, a proxy, the local network, or another point along the network path can present an arbitrary certificate and establish a man-in-the-middle connection.
Requests include a BCE
Authorizationheader generated from the access-key identifier, timestamp, host, method, and request path. The authorization value uses a one-hour validity period. The signing implementation does not bind the JSON request body into the canonical request:python canonical_request = f"{method}\n{canonical_uri}\n\n{canonical_headers}"A network attacker could therefore capture the authorization material, inspect request parameters, manipulate returned pricing data, and potentially replay a captured ...[truncated 1558 chars]
- Remediation
View remediation
Remediation Suggestions
Remove the unverified SSL context and rely on Python's verified default context:
python return json.loads( urllib.request.urlopen(req, timeout=15).read().decode() )If an explicit context is required, use the platform trust store while retaining certificate and hostname validation:
python ctx = ssl.create_default_context() return json.loads( urllib.request.urlopen(req, timeout=15, context=ctx).read().decode() )Additional hardening should include:
- Never set
check_hostnametoFalseorverify_modetoCERT_NONE. - Fail closed on certificate validation errors rather than retrying insecurely.
- Ensure the runtime trust store is current.
- If supported by the Baidu signing protocol, bind a digest of the request body into the signature.
- Reduce authorization validity from one hour where protocol configuration permits.
- Use cloud credentials restricted to the minimum read-only pricing permissions.
- Never set
