certbot/certbot-compatibility-test/certbot_compatibility_test/validator.py

107 lines
3.9 KiB
Python
Raw Normal View History

2015-07-22 16:47:09 -04:00
"""Validators to determine the current webserver configuration"""
2015-07-22 21:25:09 -04:00
import logging
import socket
from typing import cast
from typing import Mapping
from typing import Optional
from typing import Union
2015-07-22 16:47:09 -04:00
from OpenSSL import crypto
import requests
2015-07-22 21:25:09 -04:00
from acme import crypto_util
from acme import errors as acme_errors
2015-07-22 16:47:09 -04:00
2015-07-22 21:25:09 -04:00
logger = logging.getLogger(__name__)
class Validator:
2015-07-22 16:47:09 -04:00
"""Collection of functions to test a live webserver's configuration"""
def certificate(self, cert: crypto.X509, name: Union[str, bytes],
alt_host: Optional[str] = None, port: int = 443) -> bool:
2015-07-22 21:25:09 -04:00
"""Verifies the certificate presented at name is cert"""
if alt_host is None:
# In fact, socket.gethostbyname accepts both bytes and str, but types do not know that.
host = socket.gethostbyname(cast(str, name)).encode()
elif isinstance(alt_host, bytes):
host = alt_host
else:
host = alt_host.encode()
name = name if isinstance(name, bytes) else name.encode()
2015-07-22 21:25:09 -04:00
try:
presented_cert = crypto_util.probe_sni(name, host, port)
except acme_errors.Error as error:
logger.exception(str(error))
2015-07-22 21:25:09 -04:00
return False
# Despite documentation saying that bytes are expected for digest(), we must provide a str.
return presented_cert.digest(cast(bytes, "sha256")) == cert.digest(cast(bytes, "sha256"))
2015-07-22 21:25:09 -04:00
def redirect(self, name: str, port: int = 80,
headers: Optional[Mapping[str, str]] = None) -> bool:
2015-07-22 16:47:09 -04:00
"""Test whether webserver redirects to secure connection."""
2015-07-22 21:25:09 -04:00
url = "http://{0}:{1}".format(name, port)
if headers:
response = requests.get(url, headers=headers, allow_redirects=False)
else:
response = requests.get(url, allow_redirects=False)
2015-07-22 16:47:09 -04:00
redirect_location = response.headers.get("location", "")
# We're checking that the redirect we added behaves correctly.
# It's okay for some server configuration to redirect to an
# http URL, as long as it's on some other domain.
2015-07-22 16:47:09 -04:00
if not redirect_location.startswith("https://"):
return False
2015-07-22 16:47:09 -04:00
if response.status_code != 301:
2015-07-22 21:25:09 -04:00
logger.error("Server did not redirect with permanent code")
return False
2015-07-22 16:47:09 -04:00
return True
def any_redirect(self, name: str, port: int = 80,
headers: Optional[Mapping[str, str]] = None) -> bool:
"""Test whether webserver redirects."""
url = "http://{0}:{1}".format(name, port)
if headers:
response = requests.get(url, headers=headers, allow_redirects=False)
else:
response = requests.get(url, allow_redirects=False)
return response.status_code in range(300, 309)
def hsts(self, name: str) -> bool:
2015-07-22 16:47:09 -04:00
"""Test for HTTP Strict Transport Security header"""
2015-07-22 21:25:09 -04:00
headers = requests.get("https://" + name).headers
2015-07-22 16:47:09 -04:00
hsts_header = headers.get("strict-transport-security")
if not hsts_header:
return False
# Split directives following RFC6797, section 6.1
directives = [d.split("=") for d in hsts_header.split(";")]
max_age = [d for d in directives if d[0] == "max-age"]
if not max_age:
2015-07-22 21:25:09 -04:00
logger.error("Server responded with invalid HSTS header field")
return False
2015-07-22 16:47:09 -04:00
try:
max_age_value = int(max_age[0][1])
2015-07-22 16:47:09 -04:00
except ValueError:
2015-07-22 21:25:09 -04:00
logger.error("Server responded with invalid HSTS header field")
return False
2015-07-22 16:47:09 -04:00
# Test whether HSTS does not expire for at least two weeks.
if max_age_value <= (2 * 7 * 24 * 3600):
2015-07-22 21:25:09 -04:00
logger.error("HSTS should not expire in less than two weeks")
return False
2015-07-22 16:47:09 -04:00
return True
def ocsp_stapling(self, name: str) -> None:
2015-07-22 16:47:09 -04:00
"""Verify ocsp stapling for domain."""
raise NotImplementedError()