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

fixed cookie refresh bug #876

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions src/flask_login/login_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ def _update_remember_cookie(self, response):
self._set_cookie(response)
elif operation == "clear":
self._clear_cookie(response)
elif operation == "unset":
session["_remember"] = "unset"

return response

Expand Down
2 changes: 2 additions & 0 deletions src/flask_login/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ def login_user(user, remember=False, duration=None, force=False, fresh=True):
raise Exception(
f"duration must be a datetime.timedelta, instead got: {duration}"
) from e
else:
session["_remember"] = "unset"

current_app.login_manager._update_request_context_with_user(user)
user_logged_in.send(current_app._get_current_object(), user=_get_user())
Expand Down
99 changes: 99 additions & 0 deletions tests/test_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -1660,3 +1660,102 @@ def test_session_protection_modes(self):
self.assertEqual("Anonymous", username.data.decode("utf-8"))
is_fresh = c.get("/is-fresh")
self.assertEqual("False", is_fresh.data.decode("utf-8"))


class CookieRefreshTest(unittest.TestCase):
"""
This class tests the bug from issue #824. It makes sure that if a user is
logged in with their "rememeber" parameter as False, that user doesn't
have any cookies, even if the "REMEMBER_COOKIE_REFRESH_EACH_REQUEST"
config option is True.
"""

def setUp(self):
self.app = Flask(__name__)
self.login_manager = LoginManager()
self.login_manager.init_app(self.app)
self.app.config["SECRET_KEY"] = "deterministic"
self.app.config["LOGIN_DISABLED"] = False
self.remember_cookie_name = "remember"
self.app.config["REMEMBER_COOKIE_NAME"] = self.remember_cookie_name
self.app.test_client_class = FlaskLoginClient

@self.app.route("/login-notch0")
def login_notch0():
self.app.config["REMEMBER_COOKIE_REFRESH_EACH_REQUEST"] = True
login_user(notch, remember=True)
return "Hello"

@self.app.route("/login-notch1")
def login_notch1():
self.app.config["REMEMBER_COOKIE_REFRESH_EACH_REQUEST"] = False
login_user(notch, remember=True)
return "Hello"

@self.app.route("/login-notch2")
def login_notch2():
self.app.config["REMEMBER_COOKIE_REFRESH_EACH_REQUEST"] = True
login_user(notch, remember=False)
return "Hello"

@self.app.route("/login-notch3")
def login_notch3():
self.app.config["REMEMBER_COOKIE_REFRESH_EACH_REQUEST"] = False
login_user(notch, remember=False)
return "Hello"

# This will help us with the possibility of typos in the tests. Now
# we shouldn't have to check each response to help us set up state
# (such as login pages) to make sure it worked: we will always
# get an exception raised (rather than return a 404 response)
@self.app.errorhandler(404)
def handle_404(e):
raise e

unittest.TestCase.setUp(self)

# each of these function tests one of the ways cookie rememberance
# configuration options can be set.
def test_0(self):
with self.app.test_client() as c:
name = self.app.config["REMEMBER_COOKIE_NAME"] = "myname"
path = self.app.config["REMEMBER_COOKIE_PATH"] = "/mypath"
domain = self.app.config["REMEMBER_COOKIE_DOMAIN"] = "localhost.local"

c.get("/login-notch0")

cookie = c.get_cookie(name, domain, path)
self.assertIsNotNone(cookie)

def test_1(self):
with self.app.test_client() as c:
name = self.app.config["REMEMBER_COOKIE_NAME"] = "myname"
path = self.app.config["REMEMBER_COOKIE_PATH"] = "/mypath"
domain = self.app.config["REMEMBER_COOKIE_DOMAIN"] = "localhost.local"

c.get("/login-notch1")

cookie = c.get_cookie(name, domain, path)
self.assertIsNotNone(cookie)

def test_2(self):
with self.app.test_client() as c:
name = self.app.config["REMEMBER_COOKIE_NAME"] = "myname"
path = self.app.config["REMEMBER_COOKIE_PATH"] = "/mypath"
domain = self.app.config["REMEMBER_COOKIE_DOMAIN"] = "localhost.local"

c.get("/login-notch2")

cookie = c.get_cookie(name, domain, path)
self.assertIsNone(cookie)

def test_3(self):
with self.app.test_client() as c:
name = self.app.config["REMEMBER_COOKIE_NAME"] = "myname"
path = self.app.config["REMEMBER_COOKIE_PATH"] = "/mypath"
domain = self.app.config["REMEMBER_COOKIE_DOMAIN"] = "localhost.local"

c.get("/login-notch3")
cookie = c.get_cookie(name, domain, path)

self.assertIsNone(cookie)