Skip to content

Commit c6846cb

Browse files
committed
Fix bug in issue #1566 based on ideas in #1567.
This fixes a bug where changing the password still allowed login by a previous cookie even if the server was restarted.
1 parent 27b8578 commit c6846cb

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

jupyter_server/auth/identity.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import binascii
1212
import datetime
13+
import hashlib
1314
import json
1415
import os
1516
import re
@@ -610,6 +611,18 @@ def logout_available(self):
610611
"""Whether a LogoutHandler is needed."""
611612
return True
612613

614+
def cookie_secret_hook(self, h: hashlib._Hash) -> hashlib._Hash:
615+
"""Update cookie secret input
616+
617+
Subclasses may call `h.update()` with any credentials that,
618+
when changed, should invalidate existing cookies, such as a
619+
password.
620+
621+
The updated hashlib object should be returned.
622+
623+
"""
624+
return h
625+
613626

614627
class PasswordIdentityProvider(IdentityProvider):
615628
"""A password identity provider."""
@@ -740,6 +753,14 @@ def validate_security(
740753
self.log.critical(_i18n("\t$ python -m jupyter_server.auth password"))
741754
sys.exit(1)
742755

756+
def cookie_secret_hook(self, h: hashlib._Hash) -> hashlib._Hash:
757+
"""Include password in cookie secret.
758+
759+
This makes it so changing the password invalidates cookies.
760+
"""
761+
h.update(self.hashed_password.encode())
762+
return h
763+
743764

744765
class LegacyIdentityProvider(PasswordIdentityProvider):
745766
"""Legacy IdentityProvider for use with custom LoginHandlers

jupyter_server/serverapp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,7 @@ def _default_cookie_secret(self) -> bytes:
11691169
self._write_cookie_secret_file(key)
11701170
h = hmac.new(key, digestmod=hashlib.sha256)
11711171
h.update(self.password.encode())
1172+
h = self.identity_provider.cookie_secret_hook(h)
11721173
return h.digest()
11731174

11741175
def _write_cookie_secret_file(self, secret: bytes) -> None:

0 commit comments

Comments
 (0)