T09 · Insecure Skill Coding Practices
- Location
scripts/search_novels.py:73- Finding
TLS Certificate Verification Disabled for External API Requests
- Content
View full analysis
Vulnerability Details
File Location:
scripts/search_novels.py, lines 73–84 and 104–110
Vulnerability Type: Improper certificate validation
Risk Level: HighVulnerable Code
python def search_gutenberg(query, limit=20): """Search Project Gutenberg via Gutendex API.""" try: import urllib.request import ssl # Disable SSL verification for gutendex context = ssl._create_unverified_context() url = f"{PLATFORMS['gutenberg']['base_url']}?search={quote_plus(query)}" url += f"&languages=en" req = urllib.request.Request(url) req.add_header('Accept', 'application/json') with urllib.request.urlopen(req, context=context, timeout=10) as response:The same insecure pattern is used for Open Library:
python context = ssl._create_unverified_context() req = urllib.request.Request(url) with urllib.request.urlopen(req, context=context, timeout=10) as response: data = json.loads(response.read().decode()) docs = data.get("docs", [])[:limit] return [format_openlibrary_result(doc) for doc in docs]Technical Analysis
ssl._create_unverified_context()disables certificate-chain and hostname verification. Although the endpoints use HTTPS, the client does not authenticate the remote servers. Any certificate presented by an intermediary will be accepted.The affected responses are parsed as JSON and used to construct search results and URLs. A network-positioned attacker can therefore impersonate Gutendex or Open Library and provide attacker-controlled metadata.
Attack Path
- A user runs
search_novels.pyagainst Gutendex or Open Library. - An attacker gains a network interception position, such as through a malicious Wi-Fi access point, compromised proxy, DNS manipulation, or upstream network compromise.
- The attacker redirects or intercepts the HTTPS request and presents an untrusted certific ...[truncated 965 chars]
- A user runs
- Remediation
View remediation
Remediation Suggestions
Remove every use of
ssl._create_unverified_context()and rely on Python’s verified default TLS context:python with urllib.request.urlopen(req, timeout=10) as response: data = json.loads(response.read().decode("utf-8"))If an explicit context is required, create a verified one:
python import ssl context = ssl.create_default_context() with urllib.request.urlopen(req, context=context, timeout=10) as response: data = json.loads(response.read().decode("utf-8"))Additional hardening should include:
- Do not fall back to insecure TLS when verification fails.
- Keep the operating system and Python CA certificate stores current.
- Validate the response
Content-Typebefore parsing it as JSON. - Apply a reasonable maximum response size to reduce resource-exhaustion risk.
- Validate returned fields and generated URLs before exposing them to users or downstream systems.
- Add automated tests confirming that self-signed certificates and hostname mismatches are rejected.
