Skip to content

Commit

Permalink
Fixes bugs where the globals are None
Browse files Browse the repository at this point in the history
This is the case when these functions are called directly, and where the
`inspect_domains()` function is not used.

Currently these lists are defined only if `inspect_domains()` has been
called, which may not be the case when testing (e.g. in #28 / #125), or
when using pshtt as a library. The current solution is to raise an
exception if the initialize function is not called explicitly, which is
now its own function to handle initializing all the third party data.

This begins work towards a more complete solution for #99, and allows
for the initialize function to be mocked / called separately when
working in tests.
  • Loading branch information
IanLee1521 committed Oct 26, 2017
1 parent 8e75578 commit e6a4b17
Showing 1 changed file with 51 additions and 12 deletions.
63 changes: 51 additions & 12 deletions pshtt/pshtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,16 +899,35 @@ def is_hsts_preload_ready(domain):

def is_hsts_preload_pending(domain):
"""
Whether a domain is formally pending inclusion
in Chrome's HSTS preload list.
Whether a domain is formally pending inclusion in Chrome's HSTS preload
list.
If preload_pending is None, the caches have not been initialized, so do
that.
"""
if preload_pending is None:
logging.error('`preload_pending` has not yet been initialized!')
raise RuntimeError(
'`initialize_external_data()` must be called explicitly before '
'using this function'
)

return domain.domain in preload_pending


def is_hsts_preloaded(domain):
"""
Whether a domain is contained in Chrome's HSTS preload list.
If preload_list is None, the caches have not been initialized, so do that.
"""
if preload_list is None:
logging.error('`preload_list` has not yet been initialized!')
raise RuntimeError(
'`initialize_external_data()` must be called explicitly before '
'using this function'
)

return domain.domain in preload_list


Expand All @@ -922,7 +941,16 @@ def is_parent_hsts_preloaded(domain):
def parent_domain_for(hostname):
"""
For "x.y.domain.gov", return "domain.gov".
If suffix_list is None, the caches have not been initialized, so do that.
"""
if suffix_list is None:
logging.error('`suffix_list` has not yet been initialized!')
raise RuntimeError(
'`initialize_external_data()` must be called explicitly before '
'using this function'
)

return suffix_list.get_public_suffix(hostname)


Expand Down Expand Up @@ -1089,6 +1117,26 @@ def load_suffix_list():
return suffixes


def initialize_external_data():
"""
This function serves to load all of the third party external data used
This is meant to be called explicitly by a user. Either the `pshtt` tool
itself as part of `inspect_domains()` function, or if in a library, as part
of the setup needed before using certain library functions.
"""
# Download HSTS preload list, caches locally.
global preload_list
preload_list = create_preload_list()

# Download HSTS pending preload list. Not cached.
global preload_pending
preload_pending = fetch_preload_pending()

global suffix_list
suffix_list = load_suffix_list()


def inspect_domains(domains, options):
# Override timeout, user agent, preload cache, default CA bundle
global TIMEOUT, USER_AGENT, PRELOAD_CACHE, WEB_CACHE, SUFFIX_CACHE, CA_FILE, STORE
Expand Down Expand Up @@ -1119,16 +1167,7 @@ def inspect_domains(domains, options):
# "Custom" Option from the sslyze output.
STORE = "Custom"

# Download HSTS preload list, caches locally.
global preload_list
preload_list = create_preload_list()

# Download HSTS pending preload list. Not cached.
global preload_pending
preload_pending = fetch_preload_pending()

global suffix_list
suffix_list = load_suffix_list()
initialize_external_data()

# For every given domain, get inspect data.
for domain in domains:
Expand Down

0 comments on commit e6a4b17

Please sign in to comment.