LanguageSwitcher does not switch language (Interactive Server / Interactive Webassembly) because of httpOnly culture cookie. #4703
-
Problem: Language Switcher Not WorkingWhen I select a different language from the dropdown in the Language Switcher, nothing happens. The code behind is correctly calling // Oqtane.Client\Themes\Controls\Theme\LanguageSwitcher.razor
private async Task SetCultureAsync(string culture)
{
if (culture != CultureInfo.CurrentUICulture.Name)
{
var localizationCookieValue = CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture));
var interop = new Interop(JSRuntime);
await interop.SetCookie(CookieRequestCultureProvider.DefaultCookieName, localizationCookieValue, 360);
NavigationManager.NavigateTo(NavigationManager.Uri, forceLoad: true);
}
} also the javasript code is ok: // Oqtane.Server\wwwroot\js\interop.js
setCookie: function (name, value, days) {
var d = new Date();
d.setTime(d.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = name + "=" + value + ";" + expires + ";path=/";
}, The problem I found is that document.cookie is not writable because the language cookie '.AspNetCore.Culture' is an httpOnly cookie: it's not accessible (nor writeable) by javascript. My workaround for now is to set to false the cookie HttpOnly property: // Oqtane.Server\Components\App.razor
private void SetLocalizationCookie(string culture)
{
var cookieOptions = new Microsoft.AspNetCore.Http.CookieOptions
{
Expires = DateTimeOffset.UtcNow.AddYears(1),
SameSite = Microsoft.AspNetCore.Http.SameSiteMode.Lax, // Set SameSite attribute
Secure = true, // Ensure the cookie is only sent over HTTPS
HttpOnly = false // Optional: Helps mitigate XSS attacks <----- I CHANGED THIS to false
};
Context.Response.Cookies.Append(
CookieRequestCultureProvider.DefaultCookieName,
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
cookieOptions
); |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 21 replies
-
@thabaum this seems to be a regression error caused by the recent PR related to cookies. |
Beta Was this translation helpful? Give feedback.
-
This option changes the cookie settings to be used with HTTP only. AFAIK everything works fine |
Beta Was this translation helpful? Give feedback.
-
@sbwalker could you refer me to the PR, so I can fix the issue |
Beta Was this translation helpful? Give feedback.
-
@sbwalker #4714 This issue has been resolved setting HttpOnly=false for all modes, however I added a comment relating in the PR for static mode since it did not have an issue maybe HttpOnly would be best to have set to true for this case? |
Beta Was this translation helpful? Give feedback.
If a cookie has HttpOnly disabled it does not mean your site is vulnerable to attack. HttpOnly is only an extra layer of defense (for those browsers that actually support it). XSS requires an initial attack vector - the injection of executable JavaScript into a page. If your site allows a malicious user to inject JavaScript then they could include a script which can read the value of cookies. HttpOnly prevents scripts from reading cookie values. So it's useful as an additional safeguard for authentication cookies or cookies which store other confidential information. But if your site allows malicious scripts to be injected then you obviously have much bigger problems to worry about than j…