Bifrost/src/Bifrost.Web/Features/ConceptVotes/VoterKeyResolver.cs
Heimdall c757acf525 Make every cookie host-only: __Host- names, Secure, Path=/, no Domain
Session, OAuth correlation, antiforgery, voter and Heimdall attempt cookies all take the
__Host- prefix. The prefix forbids a narrower Path, so the voter (was /mill) and attempt
(was /auth/heimdall) cookies move to Path=/.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-07 18:42:43 +00:00

56 lines
2 KiB
C#

using System.Security.Cryptography;
using Bifrost.Web.Features.Shared;
using Microsoft.AspNetCore.DataProtection;
namespace Bifrost.Web.Features.ConceptVotes;
// The only reader and writer of the bifrost_voter cookie. It supplies a device id and nothing else: VoterIdentity.For
// decides the voter key and class. The cookie holds a protected random id, never a count, class or account.
public sealed class VoterKeyResolver(IDataProtectionProvider dataProtection)
{
public const string CookieName = BifrostCookies.Voter;
public const string CookiePath = BifrostCookies.Path;
public static readonly TimeSpan CookieLifetime = TimeSpan.FromDays(365);
private const string Purpose = "Bifrost.ConceptVotes.DeviceToken.v1";
private readonly IDataProtector protector = dataProtection.CreateProtector(Purpose);
// The device id, or null when there is no cookie or it does not unprotect under this key ring and purpose
// (tampered, truncated, a bare id, or minted elsewhere). Nothing about a refused value is kept or reported.
public string? TryRead(HttpContext context)
{
if (!context.Request.Cookies.TryGetValue(CookieName, out var token) || string.IsNullOrEmpty(token))
{
return null;
}
try
{
return protector.Unprotect(token);
}
catch (CryptographicException)
{
return null;
}
}
// Sets a fresh device cookie on this response and returns its device id. Called only from a cast.
public string Mint(HttpContext context)
{
var deviceId = Guid.NewGuid().ToString("N");
context.Response.Cookies.Append(
CookieName,
protector.Protect(deviceId),
new CookieOptions
{
HttpOnly = true,
Secure = true,
SameSite = SameSiteMode.Lax,
Path = CookiePath,
MaxAge = CookieLifetime,
IsEssential = true
});
return deviceId;
}
}