slightly refactor origin validity checks

This commit is contained in:
Ryan Petrello
2018-07-24 15:06:42 -04:00
parent 21568f74c0
commit c81d2f53c5

View File

@@ -27,7 +27,8 @@ def origin_is_valid(message, trusted_values):
client = urlparse(origin) client = urlparse(origin)
trusted = urlparse(trusted) trusted = urlparse(trusted)
except (AttributeError, ValueError): except (AttributeError, ValueError):
# if we can't parse the origin header, fall back to the else block # if we can't parse a hostname, consider it invalid and try the
# next one
pass pass
else: else:
# if we _can_ parse the origin header, verify that it's trusted # if we _can_ parse the origin header, verify that it's trusted
@@ -36,15 +37,13 @@ def origin_is_valid(message, trusted_values):
is_same_domain(client.netloc, trusted.netloc) is_same_domain(client.netloc, trusted.netloc)
): ):
# the provided Origin matches at least _one_ whitelisted host, # the provided Origin matches at least _one_ whitelisted host,
# break out and accept the connection # return True
break return True
else: logger.error((
logger.error(( "ws:// origin header mismatch {} not in {}; consider adding {} to "
"ws:// origin header mismatch {} not in {}; consider adding {} to " "settings.WEBSOCKET_ORIGIN_WHITELIST if it's a trusted host."
"settings.WEBSOCKET_ORIGIN_WHITELIST if it's a trusted host." ).format(origin, trusted_values, origin))
).format(origin, trusted_values, origin)) return False
return False
return True