-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Fix invalid cookie name caused by chars in path name #11526
Conversation
@jtkech If I understand correctly you used the ContentRootPath because you wanted to have the same Cookie name when trying to use the setup from different served paths. Using a GUID here might not fix that issue but it is more secure as it cannot be forged. Though, I suggested using an MD5 or any other hash to create that UID based on that ContentRootPath to at least make it a little more complex to guess the name of the cookie. Though, I think that we should leave it as a random GUID. |
Yes, if multiple instances where we can use e.g. the Redis distributed data protection provider, to not have to login again when the request is balanced to a new instance, also when submitting a form if no affinity is used, this let me think about this issue #11501 where @CrestApps tried to not use ARR affinity. I'm not a security expert so not sure, but this is only a cookie name but whose value is encrypted, otherwise I don't see how we could share a data protection mechanism accross instances. Hmm but look at this, just saw in #5803, first I just added ContentRootPath
Then @sebastienros asked to me Hmm, maybe aspnetcore would have done the right and adapted encoding, so maybe we just need to remove the Maybe worth to give it a try ;) |
Ok, a little bit of explanation here first. We can't use a method that is meant for "slugifying" or escaping a URL here because eventually, these methods could allow other chars that we don't want. We need a specific method here that is not related to a URI transformation. The best thing is to always use a Unique ID for these cookie names so I suggested something like a MD5 against the ContentRootPath that could return always the same unique chars and that would not need to be "escaped". Else, we will need to simply not use the ContentRootPath as is and remove unrestricted chars that could come from a path but these could change too from one system to another so the best way is to transform the ContentRootPath to some hash. Else, here the issue with Sebastien Ros suggested use Base64 at some point but looking at it and it is not escaping all chars we don't want. Something like this : MD5 md5Hasher = MD5.Create();
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(environment.ContentRootPath));
var guid = new Guid(data).ToString("N"); |
This is not exactly what I said, I just mentioned an old comment where I said that I saw that aspnetcore was encoding the whole cookie name, and if so maybe would be more adapted and we would just have to remove the UrlEncode() we added at some point. But yes good catch for the ContentRoot path that may be different. But just saw this breaking change in their doc saying that this is no more the case https://docs.microsoft.com/en-us/dotnet/core/compatibility/aspnet-core/5.0/security-cookie-name-encoding-removed They link to a related issue, I will take a look when I will have time. For now I saw this Recommended action |
for the new Guid(byte[]) method
@jtkech What I meant is that the ContentRootPath allowed chars could be different from Windows to Linux for example. So, basically ASCII seems safe here but hashing with a MD5 also does the trick. We could also do simply : |
Ok somehow the new Guid(byte[]) method requires a 16 byte array to generate a Guid from and the MD5 hasher returns exactly that. I'm going to keep it as it is because it produces a consistent byte array for a given string. |
Here some findings, just for info, nothing incompatible on what you're working on ;) First I was asking to me if the cookie name itself needs to be encrypted if aspnetcore uses the right secure options / policies when building the cookie. So as i saw before they were using About the new vulnerability, I saw that cookies can be prefixed with dotnet/aspnetcore#23578
That's why now they don't encode / decode anymore. So my feeling is that we don't need to encrypt the cookie name, just escape it and avoid the not allowed chars as you are working on. Just saw this data protection utility That said I remembered that when I added
But somewhere it "broke" the use case where we want the same cookie name accross instances but for the same application, as discussed in #11501 and as you said where So yes, would be great to have an application identifier to be used as a suffix for each tenant name, maybe a secret suffix from configuration whose default would be Oh yes, just remembered that we have a shell setting
|
Closes #11489