Skip to content

Commit 0e3e6e1

Browse files
committed
use constant-time compare on password
1 parent b680628 commit 0e3e6e1

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

aiohttp_remotes/basic_auth.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import base64
22
import binascii
3+
from hashlib import sha256
4+
from secrets import compare_digest
35
from typing import Awaitable, Callable, Iterable
46

57
from typing_extensions import NoReturn
@@ -19,7 +21,7 @@ def __init__(
1921
white_paths: Iterable[str] = (),
2022
) -> None:
2123
self._username = username
22-
self._password = password
24+
self._password_hash = sha256(password.encode("utf-8")).digest()
2325
self._realm = realm
2426
self._white_paths = set(white_paths)
2527

@@ -58,7 +60,9 @@ async def middleware(
5860

5961
username, password = credentials
6062

61-
if username != self._username or password != self._password:
63+
if username != self._username or not compare_digest(
64+
sha256(password.encode("utf-8")).digest(), self._password_hash
65+
):
6266
return await self.raise_error(request)
6367

6468
return await handler(request)

0 commit comments

Comments
 (0)