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

Add HTTP::Cookie#destroy #14819

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

a-alhusaini
Copy link
Contributor

Reasoning

I tried to use cookies.delete("...") today and it didn't cause the cookie to expire from the browser. It sent it again on the next request.

This method sets the cookie value to an empty string and seets the expiry date to 5 minutes ago. Browsers automatically delete expired cookies.

My aim is to provide a convenience to anyone who happens to need to use HTTP::Cookie

@ysbaddaden
Copy link
Contributor

ysbaddaden commented Jul 23, 2024

Since the intent is to expire the cookie, then #expire would be a better name than #destroy.

I'd also put an expiration date far further than 5 minutes ago (clocks can get unsynced) and use a fixed date, like the UNIX timestamp (1970-01-01 00:00:00 UTC).

@a-alhusaini
Copy link
Contributor Author

@ysbaddaden Hey julian! This little function is a convenience for destroying cookies (which usually means setting their expiry to some date in the past and setting their value to blank)

I just thought others might want a convenient function to destroy cookies.

Although as you pointed out, it is superflous. I just PR'd it to promote developer happiness :3

@straight-shoota
Copy link
Member

It would be much appreciated to follow the guidelines for feature discussions and open an issue for a new proposal first, to keep the general discussion independent of a PR with a concrete implementation.
Since this is relatively trivial, I suppose we can continue the discussion here. But next time, please open an issue first please.

@straight-shoota
Copy link
Member

Actually, setting Expires to a date in the past does not fully ensure the cookie to expire. Max-Age takes precedence. If Max-Age is set, the value of Expires doesn't matter.

So I think the functionality should be implemented as self.max_age = Time::Span.zero.
We could also consider self.expires = Time::UNIX_EPOCH on top, just for good measure, but that should not really be necessary.

@a-alhusaini
Copy link
Contributor Author

Sorry about the typo. I can't run the test suite locally so I'm relying on the CI.

Comment on lines +195 to +203
# tell the browser to delete the cookie.
# Set its value to an empty string
# and set its expiration time to the past
# Browsers delete cookies that have expired.
def destroy
self.value = ""
self.expires = Time::UNIX_EPOCH
self.max_age = Time::Span.zero
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
# tell the browser to delete the cookie.
# Set its value to an empty string
# and set its expiration time to the past
# Browsers delete cookies that have expired.
def destroy
self.value = ""
self.expires = Time::UNIX_EPOCH
self.max_age = Time::Span.zero
end
# Tells the browser to expire the cookie by setting its
# max age time to zero and its expiration time to the past.
def expire
self.expires = Time::UNIX_EPOCH
self.max_age = Time::Span.zero
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you elaborate on why you suggest that we remove setting the value to a blank string?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, my reasoning was twofold:

  1. expire method name IMO implies only cookie expiration, not emptying its value
  2. Spec doesn't mention (or I've not seen it does) clearing out the value as the requirement for a cookie expiration

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe destroy will better represent what the function does. And there are many methods with a name similar to #expire I think calling it #destroy would be more clear.

Copy link
Member

Choose a reason for hiding this comment

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

Clearing the value is not a requirement for expiration. But I think it makes a lot of sense not to send the value you want gone to the client again.

Comment on lines +195 to +198
# tell the browser to delete the cookie.
# Set its value to an empty string
# and set its expiration time to the past
# Browsers delete cookies that have expired.
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: Improve wording.

Suggested change
# tell the browser to delete the cookie.
# Set its value to an empty string
# and set its expiration time to the past
# Browsers delete cookies that have expired.
# Destroys the cookie.
# This is done by causing the cookie to expire. Also clears its value.

@straight-shoota straight-shoota changed the title added HTTP::Cookie#destroy. It expires cookie and sets its value to b… Add HTTP::Cookie#destroy Aug 5, 2024
@straight-shoota
Copy link
Member

IMO the name #destroy makes sense. Expiration is just the mechanism used to signal to the client to forget this cookie.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants