Interactive Playground
Experiment with the vulnerable code and security rule below. Edit the code to see how the rule detects different vulnerability patterns.
pathfinder scan --ruleset python/PYTHON-LANG-SEC-062 --project .About This Rule
Understanding the vulnerability and how it is detected
urllib.request.Request() creates a request object that encapsulates a URL, headers, and request body. When the URL in the Request object uses http:// rather than https://, all data transmitted including authentication headers, API keys, request bodies, and cookies is sent in plaintext over the network.
urllib.request.OpenerDirector (obtained via urllib.request.build_opener()) provides a customizable request opener. When configured without proper HTTPS handlers or used with HTTP URLs, it has the same cleartext transmission risk.
This rule audits urllib.request.Request() and urllib.request.build_opener() usage to ensure HTTPS URLs are used and no insecure handlers are installed.
Security Implications
Potential attack scenarios if this vulnerability is exploited
Authentication Header Exposure
Authentication headers added to Request objects (Authorization, X-API-Key, Cookie) are transmitted in plaintext when HTTP URLs are used, exposing credentials to network observers.
Request Body Disclosure
POST request bodies containing passwords, form data, file contents, or API parameters are transmitted without encryption, enabling data theft by network observers.
Custom Handler Security Bypass
OpenerDirector with custom handlers can install insecure protocols or disable default security behaviors. Handlers that bypass certificate verification or add HTTP Basic Auth to HTTP (not HTTPS) URLs create additional security risks.
Redirect Security
Custom openers may handle redirects differently from urlopen(), potentially following redirects from HTTPS to HTTP URLs or not handling security-sensitive redirect scenarios correctly.
How to Fix
Recommended remediation steps
- 1Validate that all URLs in urllib.request.Request() objects use the https:// scheme before the request is executed.
- 2When using OpenerDirector, audit all installed handlers to ensure no insecure HTTP handlers or certificate bypass handlers are used.
- 3Pass an explicit ssl.create_default_context() to urlopen() when executing HTTPS Request objects for clarity and security.
- 4Consider migrating from urllib.request.Request/OpenerDirector to the requests library for simpler, more secure HTTP client code.
- 5Add URL scheme validation in any code that accepts URLs from configuration or user input before using them in urllib.request.Request().
Detection Scope
How Code Pathfinder analyzes your code for this vulnerability
This rule detects calls to urllib.request.Request(), urllib.request.build_opener(), and urllib.request.install_opener() in Python source code. All call sites are flagged to prompt review of the URL scheme and handler configuration for security.
Compliance & Standards
Industry frameworks and regulations that require detection of this vulnerability
References
External resources and documentation
Similar Rules
Explore related security rules for Python
HTTP Request Without TLS (requests library)
HTTP URLs in requests calls transmit data in plaintext without encryption. Use HTTPS URLs for sensitive data transmission.
Insecure urllib.request.urlopen() Usage
urllib.request.urlopen() over HTTP transmits data in plaintext. Verify HTTPS URLs are used and SSL context is properly configured.
Certificate Validation Disabled (verify=False)
TLS certificate validation is explicitly disabled via verify=False or CERT_NONE, making connections vulnerable to man-in-the-middle attacks.
Frequently Asked Questions
Common questions about Insecure urllib Request Object Usage
New feature
Get these findings posted directly on your GitHub pull requests
The Insecure urllib Request Object Usage rule runs in CI and posts inline review comments on the exact lines — no dashboard, no SARIF viewer.