fix(dashboard): validate __Host- cookie vs RequireHttpsCookie; surface login flags (mxaccessgw#196)

O19: reject a __Host- CookieName with RequireHttpsCookie=false at startup.
O20: project DisableLogin, AutoLoginUser, RequireHttpsCookie and CookieName into
EffectiveDashboardConfiguration and show them on the Settings page.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
Joseph Doherty
2026-11-09 12:28:21 -05:00
parent 417b9e5a19
commit f3a452810e
6 changed files with 72 additions and 2 deletions
@@ -9,4 +9,8 @@ public sealed record EffectiveDashboardConfiguration(
bool ShowTagValues,
IReadOnlyDictionary<string, string> GroupToRole,
IReadOnlyDictionary<string, IReadOnlyList<string>> GroupToTag,
UntaggedSessionVisibility UntaggedSessionVisibility);
UntaggedSessionVisibility UntaggedSessionVisibility,
bool DisableLogin = false,
string? AutoLoginUser = null,
bool RequireHttpsCookie = true,
string? CookieName = null);
@@ -71,7 +71,11 @@ public sealed class GatewayConfigurationProvider(IOptions<GatewayOptions> option
pair => pair.Key,
pair => (IReadOnlyList<string>)pair.Value,
value.Dashboard.GroupToTag.Comparer),
UntaggedSessionVisibility: value.Dashboard.UntaggedSessionVisibility),
UntaggedSessionVisibility: value.Dashboard.UntaggedSessionVisibility,
DisableLogin: value.Dashboard.DisableLogin,
AutoLoginUser: value.Dashboard.AutoLoginUser,
RequireHttpsCookie: value.Dashboard.RequireHttpsCookie,
CookieName: value.Dashboard.CookieName),
Protocol: new EffectiveProtocolConfiguration(
value.Protocol.WorkerProtocolVersion,
value.Protocol.MaxGrpcMessageBytes));
@@ -386,6 +386,17 @@ public sealed class GatewayOptionsValidator : OptionsValidatorBase<GatewayOption
"MxGateway:Dashboard:DisableLogin must not be true in the Production environment; it disables all dashboard authentication.");
}
// A __Host- cookie must carry Secure; with RequireHttpsCookie=false the cookie is only
// Secure on HTTPS requests, so over plain HTTP browsers silently drop it and login
// appears to succeed yet never sticks. Reject the combination at startup (R21-O19).
if (!options.RequireHttpsCookie
&& options.CookieName is { } cookieName
&& cookieName.StartsWith("__Host-", StringComparison.OrdinalIgnoreCase))
{
builder.Add(
"MxGateway:Dashboard:CookieName must not use the '__Host-' prefix when MxGateway:Dashboard:RequireHttpsCookie is false; browsers silently drop a __Host- cookie that is not Secure.");
}
// GroupToRole shape is validated even when the dashboard is disabled so
// misconfiguration surfaces at startup; emptiness is allowed, with the
// consequence that no LDAP user can sign in (login returns "no roles
@@ -102,6 +102,10 @@ else
<tr><th scope="row">Recent session limit</th><td>@Snapshot.Configuration.Dashboard.RecentSessionLimit</td></tr>
<tr><th scope="row">Show tag values</th><td>@Snapshot.Configuration.Dashboard.ShowTagValues</td></tr>
<tr><th scope="row">Untagged session visibility</th><td>@Snapshot.Configuration.Dashboard.UntaggedSessionVisibility</td></tr>
<tr><th scope="row">Login disabled</th><td>@Snapshot.Configuration.Dashboard.DisableLogin</td></tr>
<tr><th scope="row">Auto-login user</th><td>@(Snapshot.Configuration.Dashboard.AutoLoginUser ?? "(default)")</td></tr>
<tr><th scope="row">Require HTTPS cookie</th><td>@Snapshot.Configuration.Dashboard.RequireHttpsCookie</td></tr>
<tr><th scope="row">Cookie name</th><td>@(Snapshot.Configuration.Dashboard.CookieName ?? "(default)")</td></tr>
<tr><th scope="row">Worker protocol</th><td>@Snapshot.Configuration.Protocol.WorkerProtocolVersion</td></tr>
</tbody>
</table>
@@ -100,6 +100,29 @@ public sealed class GatewayConfigurationProviderTests
Project(widened).Dashboard.UntaggedSessionVisibility);
}
/// <summary>The login/cookie flags are projected so the Settings page can show them (R21-O20).</summary>
[Fact]
public void GetEffectiveConfiguration_ProjectsLoginAndCookieFlags()
{
GatewayOptions options = new()
{
Dashboard = new DashboardOptions
{
DisableLogin = true,
AutoLoginUser = "dev",
RequireHttpsCookie = false,
CookieName = "gw-a",
},
};
EffectiveDashboardConfiguration dashboard = Project(options).Dashboard;
Assert.True(dashboard.DisableLogin);
Assert.Equal("dev", dashboard.AutoLoginUser);
Assert.False(dashboard.RequireHttpsCookie);
Assert.Equal("gw-a", dashboard.CookieName);
}
/// <summary>The pre-existing dashboard members keep coming from the options they always did.</summary>
[Fact]
public void GetEffectiveConfiguration_CopiesTheOtherDashboardMembers()
@@ -952,6 +952,30 @@ public sealed class GatewayOptionsValidatorTests
Assert.True(result.Succeeded);
}
/// <summary>A __Host- cookie name with RequireHttpsCookie=false is a cookie browsers drop (R21-O19).</summary>
[Fact]
public void Validate_Fails_WhenHostPrefixedCookieNameWithoutRequireHttpsCookie()
{
GatewayOptions options = CloneWithDashboard(
ValidOptions(),
new DashboardOptions { CookieName = "__Host-Custom", RequireHttpsCookie = false });
ValidateOptionsResult result = new GatewayOptionsValidator().Validate(null, options);
Assert.True(result.Failed);
Assert.Contains(result.Failures!, f => f.Contains("__Host-") && f.Contains("RequireHttpsCookie"));
}
/// <summary>__Host- is fine when the cookie is always Secure; a plain name is fine without it.</summary>
[Fact]
public void Validate_Succeeds_ForValidCookieNameCombinations()
{
Assert.True(new GatewayOptionsValidator().Validate(null, CloneWithDashboard(
ValidOptions(),
new DashboardOptions { CookieName = "__Host-Custom", RequireHttpsCookie = true })).Succeeded);
Assert.True(new GatewayOptionsValidator().Validate(null, CloneWithDashboard(
ValidOptions(),
new DashboardOptions { CookieName = "MyCookie", RequireHttpsCookie = false })).Succeeded);
}
/// <summary>Verifies a populated GroupToTag map with well-formed tags passes validation.</summary>
[Fact]
public void Validate_Succeeds_WhenGroupToTagWellFormed()