Skip to content

Commit aadaba1

Browse files
authored
Fix to only add assert_hostname for https urls (#578)
* Fix to only add assert_hostname for https urls Given an http:// server url and `'VERIFY_SERVER_CERT': False`, don't pass in assert_hostname (it will error) * Update changelog * Use url_parts and add additional test
1 parent 5905cb8 commit aadaba1

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
[Check the diff](https://github.com/elastic/apm-agent-python/compare/v5.1.1...master)
5+
6+
### Bugfixes
7+
* fixed an issue with http server_url and `'VERIFY_SERVER_CERT': False` (#570, #578)
8+
39
## v5.1.1
410
[Check the diff](https://github.com/elastic/apm-agent-python/compare/v5.1.0...v5.1.1)
511

elasticapm/transport/http.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ def __init__(self, url, **kwargs):
5151
super(Transport, self).__init__(url, **kwargs)
5252
url_parts = compat.urlparse.urlparse(url)
5353
pool_kwargs = {"cert_reqs": "CERT_REQUIRED", "ca_certs": certifi.where(), "block": True}
54-
if self._server_cert:
54+
if self._server_cert and url_parts.scheme != "http":
5555
pool_kwargs.update(
5656
{"assert_fingerprint": self.cert_fingerprint, "assert_hostname": False, "cert_reqs": ssl.CERT_NONE}
5757
)
5858
del pool_kwargs["ca_certs"]
59-
elif not self._verify_server_cert:
59+
elif not self._verify_server_cert and url_parts.scheme != "http":
6060
pool_kwargs["cert_reqs"] = ssl.CERT_NONE
6161
pool_kwargs["assert_hostname"] = False
6262
proxies = compat.getproxies_environment()

tests/transports/test_urllib3.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,38 @@ def test_ssl_verify_disable(waiting_httpsserver):
188188
transport.close()
189189

190190

191+
def test_ssl_verify_disable_http(waiting_httpserver):
192+
"""
193+
Make sure that ``assert_hostname`` isn't passed in for http requests, even
194+
with verify_server_cert=False
195+
"""
196+
waiting_httpserver.serve_content(code=202, content="", headers={"Location": "http://example.com/foo"})
197+
transport = Transport(waiting_httpserver.url, verify_server_cert=False)
198+
try:
199+
url = transport.send(compat.b("x"))
200+
assert url == "http://example.com/foo"
201+
finally:
202+
transport.close()
203+
204+
205+
def test_ssl_cert_pinning_http(waiting_httpserver):
206+
"""
207+
Won't fail, as with the other cert pinning test, since certs aren't relevant
208+
for http, only https.
209+
"""
210+
waiting_httpserver.serve_content(code=202, content="", headers={"Location": "http://example.com/foo"})
211+
transport = Transport(
212+
waiting_httpserver.url,
213+
server_cert=os.path.join(os.path.dirname(__file__), "wrong_cert.pem"),
214+
verify_server_cert=True,
215+
)
216+
try:
217+
url = transport.send(compat.b("x"))
218+
assert url == "http://example.com/foo"
219+
finally:
220+
transport.close()
221+
222+
191223
def test_ssl_cert_pinning(waiting_httpsserver):
192224
waiting_httpsserver.serve_content(code=202, content="", headers={"Location": "https://example.com/foo"})
193225
cur_dir = os.path.dirname(os.path.realpath(__file__))

0 commit comments

Comments
 (0)