-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Clarify behavior of CookieAuthenticationOptions.Cookie.Expiration #4671
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
Comments
Where? In doc comments? Samples? Or make it throw? |
For others new to this thread, let me add a little context. The reason for the Example: we haven't figure out how to handle something like this. Cookie.Expiration = TimeSpan.FromDays(9999);
ExpiresTimeSpan = TimeSpan.FromMinutes(30); This would create an auth ticket valid for the next 30 minutes, but the cookie will be stored by the browser 9999 days. That means in a month, the browser will still send the cookie, but auth won't accept the ticket contained in the cookie. Until 2.0.0, this wasn't a problem because users couldn't configure cookie expiration manually. We always inferred cookie expiration from ticket expiration. |
@Tratcher I think it would be useful to assert that Cookie.Expiration >= ExpireTimeSpan. If not, throw. There is nothing wrong with Cookie.Expiration >= ExpireTimeSpan, it's just means you have a cookie that will never work and doesn't get cleaned up right away. But on the other hand, if someone configures Cookie.Expiration but not ExpireTimeSpan, they will get an error. |
Cookie.Expiration is currently ignored. It never makes it to the browser. https://github.com/aspnet/Security/blob/a53bf093a7d86b35e019c80515c92d7626982325/src/Microsoft.AspNetCore.Authentication.Cookies/CookieAuthenticationHandler.cs#L181 Auth cookies have a separate flag for persisting cookies (via setting their expiration) and it's intentionally only available per request because it requires user consent. Setting Cookie.Expiration is an error in the current design, we just don't validate it. |
So in 2.1, we should just update CookieAuthenticationHandler to throw if expiration is ever set before using cookies? |
Yes, that's an option. I don't know if @Eilon will take kindly to throwing new exceptions though. |
The way I see it, we have 3 options:
|
I like option 1, throwing an exception (pointing them at the right setting) instead of just ignoring it seems way better. It would be a really easy breaking change for people to fix, and its not like setting it actually did what they wanted anyways... |
Random user here but my vote in order of preference is:
Sidenote - It'd be cool if MS or the community provided upgrade Codemods in the way of Roslyn analyzers to autofix your code on upgrades. The React team at Facebook does this for instance.. |
I of course don't like the idea of throwing from the setter because of the breaking change aspect. But, worse than that, property setters that throw on condition of another property's value are a Very Bad Idea™️ because you can only set the properties in a particular order, and that's a serious usability pattern. So, that leaves throwing at "runtime" (either in some sort of property validation phase, or during a request), but that's still a breaking change, so I'm still not a fan. I'm ok with marking something as obsolete if we think that's an appropriate choice, then we can delete in 3.0. Is that reasonable? |
On a related note, that property is coming from a shared library (RequestPathBaseCookieBuilder), CookieAuth doesn't actually own it. We'd have to derive and override it to add an exception. I don't think an obsolete attribute would even surface to the user, we're not obsoleting it on the base type. And it can't be removed from the base type as it's valid for other uses. Back to the drawing board. https://github.com/aspnet/Security/blob/a53bf093a7d86b35e019c80515c92d7626982325/src/Microsoft.AspNetCore.Authentication.Cookies/CookieAuthenticationOptions.cs#L65 |
There is a virtual Not sure how many people would actually call that... but... it's there. Edit: Still a breaking change though I guess. |
Indeed, more fundamentally, the question is: How bad is this really? We can very easily document this in the |
You could also argue that as far as breaking changes go, its not too bad, since this is actually better behavior. Setting the property does nothing, and is basically always developer error Anyone who sets this really needed to set cookie.ExpiresTimeSpan instead, so the exception would let them know their cookie actually isn't expiring when they expect, instead of silently being ignored... |
For what it's worth, from an outside perspective I think what lead to my confusion was mostly the fact that the same property when set on the After I noticed the auth cookie's expiration was a year out I looked more closely at the
vs
I guess my question is why not let developers use the As it stands today is there any way to set the auth cookie itself to expire in 20 minutes while |
@RickBlouch It's funny you compare it to Session as that has most of the same issues but possibly worse. Prior to 2.0 there was no way to set the Session cookie expiration. It was only added because it was part of the shared CookieBuilder infrastructure and we weren't sure it was worth blocking. Now I think the sliding expiration issue makes it worth blocking. And no, you cannot directly control the auth cookie's expiration value without enabling IsPersistent (per login). That's because you're supposed to get user consent before persisting auth cookies beyond the current browser session (e.g. "remember me"). |
Marking as a breaking change for 3.0. We should add options validation that throws if the incorrect cookie property was set. |
So is this going to be resolved in .NET 3.0? I just spent an hour trying to figure out why my cookies were always expiring in 2 weeks even though I was setting options.Cookie.Expiration to another timespan. I searched for a while, and then finally decided to search the source code where I finally found a clue: |
aspnet/Security#1285 added
CookieAuthenticationOptions.Cookie.Expiration
, a nullableTimeSpan
, but we still haveCookieAuthenticationOptions.ExpireTimeSpan
. Cookie.Expiration is ignored. We should clarify what Cookie.Expiration is meant to do and how it interacts with ExpireTimeSpanThe text was updated successfully, but these errors were encountered: