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

Replace expires cookie attribute with max-age attribute #823

Conversation

tvuotila
Copy link

@tvuotila tvuotila commented Nov 10, 2023

Internet Explorer doesn't support max-age cookie attribute. I don't know any other reason to use expires instead of max-age. I was checking wrong "max-age". Even Internet Explorer supports max-age since IE 9 (source)

Max-age works even if client or server clocks have wrong time.

@coveralls
Copy link

coveralls commented Nov 10, 2023

Coverage Status

coverage: 95.876% (-0.08%) from 95.951%
when pulling 376ea72 on tvuotila:feat/cookie-max-age-instead-of-expires
into d64e8cb on maxcountryman:main.

Repository owner deleted a comment from sebastiaguevara Nov 22, 2023
@maxcountryman
Copy link
Owner

Why not use both? That's what Django seems to do.

@tvuotila tvuotila force-pushed the feat/cookie-max-age-instead-of-expires branch from cc2bfb1 to b792120 Compare January 22, 2024 06:58
@tvuotila
Copy link
Author

Why not use both? That's what Django seems to do.

I checked the max-age support again. Seems like I was wrong and everything supports max-age.

Not using both has these benefits:

  • Simpler code
  • Doesn't reveal server time skew
  • (Smaller response size)

@tvuotila tvuotila force-pushed the feat/cookie-max-age-instead-of-expires branch from b792120 to 376ea72 Compare January 22, 2024 07:16
@tvuotila
Copy link
Author

Why not use both? That's what Django seems to do.

I investigated this more and Flask also uses both:

  • LoginManager._set_cookie calls Response.set_cookie
  • Response.set_cookie calls werkzeug.http.dump_cookie
  • The dump_cookie has default argument sync_expires: automatically set expires if max_age is defined but expires not.

Thus, in the end, this PR sets both, expires and max-age. At least, until werkzeug changes the defaults.

@@ -17,6 +17,7 @@ Unreleased
- Use `datetime.now(timezone.utc)` instead of deprecated `datetime.utcnow`. #758
- Never look at the `X-Forwarded-For` header, always use `request.remote_addr`,
requiring the developer to configure `ProxyFix` appropriately. #700
- Replace `expires` attribute with `max-age` in "remember_me" cookie. #823
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should read as additive since it's no longer replacing.

@@ -654,11 +647,8 @@ def test_remember_me_custom_duration_uses_custom_cookie(self):
c.get("/login-notch-remember-custom")
cookie = c.get_cookie(name, domain, path)
self.assertIsNotNone(cookie)
self.assertIsNotNone(cookie.expires)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be removed. Likewise below.

@@ -693,19 +680,6 @@ def login_notch_remember_custom_invalid():
result = c.get("/login-notch-remember-custom-invalid")
self.assertEqual(result.status_code, 500)

def test_set_cookie_with_invalid_duration_raises_exception(self):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this being removed?


def test_remember_me_refresh_each_request(self):
with patch("flask_login.login_manager.datetime") as mock_dt:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise here.

@tvuotila tvuotila deleted the feat/cookie-max-age-instead-of-expires branch April 1, 2024 18:02
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants