NDOJ/judge/utils/pwned.py

138 lines
4.8 KiB
Python
Raw Normal View History

2020-01-21 06:35:58 +00:00
"""
Based on https://github.com/ubernostrum/pwned-passwords-django.
Original license:
Copyright (c) 2018, James Bennett
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.
* Neither the name of the author nor the names of other
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."""
import hashlib
import logging
import requests
from django.conf import settings
from django.contrib.auth.password_validation import CommonPasswordValidator
from django.core.exceptions import ValidationError
from django.utils.translation import gettext as _, ungettext
from judge.utils.unicode import utf8bytes
log = logging.getLogger(__name__)
2022-05-14 17:57:27 +00:00
API_ENDPOINT = "https://api.pwnedpasswords.com/range/{}"
2020-01-21 06:35:58 +00:00
REQUEST_TIMEOUT = 2.0 # 2 seconds
def _get_pwned(prefix):
"""
Fetches a dict of all hash suffixes from Pwned Passwords for a
given SHA-1 prefix.
"""
try:
response = requests.get(
url=API_ENDPOINT.format(prefix),
timeout=getattr(
settings,
2022-05-14 17:57:27 +00:00
"PWNED_PASSWORDS_API_TIMEOUT",
2020-01-21 06:35:58 +00:00
REQUEST_TIMEOUT,
),
)
response.raise_for_status()
except requests.RequestException:
# Gracefully handle timeouts and HTTP error response codes.
2022-05-14 17:57:27 +00:00
log.warning("Skipped Pwned Passwords check due to error", exc_info=True)
2020-01-21 06:35:58 +00:00
return None
results = {}
for line in response.text.splitlines():
2022-05-14 17:57:27 +00:00
line_suffix, _, times = line.partition(":")
2020-01-21 06:35:58 +00:00
results[line_suffix] = int(times)
return results
def pwned_password(password):
"""
Checks a password against the Pwned Passwords database.
"""
2022-11-01 01:43:06 +00:00
if not isinstance(password, str):
2022-05-14 17:57:27 +00:00
raise TypeError("Password values to check must be strings.")
2020-01-21 06:35:58 +00:00
password_hash = hashlib.sha1(utf8bytes(password)).hexdigest().upper()
prefix, suffix = password_hash[:5], password_hash[5:]
results = _get_pwned(prefix)
if results is None:
# Gracefully handle timeouts and HTTP error response codes.
return None
return results.get(suffix, 0)
class PwnedPasswordsValidator(object):
"""
Password validator which checks the Pwned Passwords database.
"""
2022-05-14 17:57:27 +00:00
2020-01-21 06:35:58 +00:00
DEFAULT_HELP_MESSAGE = _("Your password can't be a commonly used password.")
2022-05-14 17:57:27 +00:00
DEFAULT_PWNED_MESSAGE = _("This password is too common.")
2020-01-21 06:35:58 +00:00
def __init__(self, error_message=None, help_message=None):
self.help_message = help_message or self.DEFAULT_HELP_MESSAGE
error_message = error_message or self.DEFAULT_PWNED_MESSAGE
# If there is no plural, use the same message for both forms.
2022-11-01 01:43:06 +00:00
if isinstance(error_message, str):
2020-01-21 06:35:58 +00:00
singular, plural = error_message, error_message
else:
singular, plural = error_message
self.error_message = {
2022-05-14 17:57:27 +00:00
"singular": singular,
"plural": plural,
2020-01-21 06:35:58 +00:00
}
def validate(self, password, user=None):
amount = pwned_password(password)
if amount is None:
# HIBP API failure. Instead of allowing a potentially compromised
# password, check Django's list of common passwords generated from
# the same database.
CommonPasswordValidator().validate(password, user)
elif amount:
raise ValidationError(
ungettext(
2022-05-14 17:57:27 +00:00
self.error_message["singular"],
self.error_message["plural"],
2020-01-21 06:35:58 +00:00
amount,
),
2022-05-14 17:57:27 +00:00
params={"amount": amount},
code="pwned_password",
2020-01-21 06:35:58 +00:00
)
def get_help_text(self):
return self.help_message