Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use hmac.compare_digest() to compare HTTP basic auth creds #1166

Merged
merged 5 commits into from
May 13, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
use hmac.compare_digest() to check http basic auth
  • Loading branch information
pbartyik committed Nov 22, 2021
commit 7f398f7eeb9d95399b6bf1905e0704646d0c4ece
24 changes: 15 additions & 9 deletions flower/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import traceback
import copy
import logging
import hmac

from distutils.util import strtobool
from base64 import b64decode
Expand All @@ -19,7 +20,7 @@ def set_default_headers(self):
self.set_header("Access-Control-Allow-Origin", "*")
self.set_header("Access-Control-Allow-Headers", "x-requested-with")
self.set_header('Access-Control-Allow-Methods',
' PUT, DELETE, OPTIONS')
' PUT, DELETE, OPTIONS')

def options(self):
self.set_status(204)
Expand All @@ -45,10 +46,10 @@ def write_error(self, status_code, **kwargs):
error_trace += line

self.render('error.html',
debug=self.application.options.debug,
status_code=status_code,
error_trace=error_trace,
bugreport=bugreport())
debug=self.application.options.debug,
status_code=status_code,
error_trace=error_trace,
bugreport=bugreport())
elif status_code == 401:
self.set_status(status_code)
self.set_header('WWW-Authenticate', 'Basic realm="flower"')
Expand All @@ -70,7 +71,12 @@ def get_current_user(self):
try:
basic, credentials = auth_header.split()
credentials = b64decode(credentials.encode()).decode()
if basic != 'Basic' or credentials not in basic_auth:
if basic != 'Basic':
raise tornado.web.HTTPError(401)
for stored_credential in basic_auth:
if hmac.compare_digest(stored_credential, credentials):
break
else:
raise tornado.web.HTTPError(401)
except ValueError:
raise tornado.web.HTTPError(401)
Expand Down Expand Up @@ -100,9 +106,9 @@ def get_argument(self, name, default=[], strip=True, type=None):
if arg is None and default is None:
return arg
raise tornado.web.HTTPError(
400,
"Invalid argument '%s' of type '%s'" % (
arg, type.__name__))
400,
"Invalid argument '%s' of type '%s'" % (
arg, type.__name__))
return arg

@property
Expand Down