Skip to content

Commit

Permalink
Catch UnicodeDecodeError when passing malformed data in authorizati…
Browse files Browse the repository at this point in the history
…on header (Fixes #122)
  • Loading branch information
bastianraschke authored and miguelgrinberg committed Feb 21, 2021
1 parent 9b4659e commit 538569f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
14 changes: 10 additions & 4 deletions flask_httpauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@ def decorated(*args, **kwargs):
return login_required_internal

def username(self):
if not request.authorization:
auth = self.get_auth()
if not auth:
return ""
return request.authorization.username
return auth.username

def current_user(self):
if hasattr(g, 'flask_httpauth_user'):
Expand Down Expand Up @@ -205,9 +206,14 @@ def get_auth(self):
username, password = b64decode(credentials).split(b':', 1)
except (ValueError, TypeError):
return None
try:
username = username.decode('utf-8')
password = password.decode('utf-8')
except UnicodeDecodeError:
username = None
password = None
return Authorization(
scheme, {'username': username.decode('utf-8'),
'password': password.decode('utf-8')})
scheme, {'username': username, 'password': password})

def authenticate(self, auth, stored_password):
if auth:
Expand Down
7 changes: 7 additions & 0 deletions tests/test_basic_verify_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ def test_verify_auth_login_invalid(self):
self.assertEqual(response.status_code, 403)
self.assertTrue('WWW-Authenticate' in response.headers)

def test_verify_auth_login_malformed_password(self):
creds = 'eyJhbGciOieyJp=='
response = self.client.get('/basic-verify',
headers={'Authorization': 'Basic ' + creds})
self.assertEqual(response.status_code, 403)
self.assertTrue('WWW-Authenticate' in response.headers)


class HTTPAuthTestCaseOldStyle(HTTPAuthTestCase):
use_old_style_callback = True

0 comments on commit 538569f

Please sign in to comment.