Optionally enable HSTS using env vars. Useful for testing

This commit is contained in:
rossmill76 committed 2026-03-07 11:15:42 +00:00
1 parent 6dc5a0faf8
commit 779d493513
3 files changed
+22 -1

No files matched your search

+12
View File
@@ -82,6 +82,18 @@ builder.Services.AddScoped<AppState>();
builder.Services.AddCascadingAuthenticationState(); builder.Services.AddCascadingAuthenticationState();
// HSTS
if (bool.Parse(Environment.GetEnvironmentVariable("ENABLE_HSTS") ?? "false"))
{
builder.Services.AddHsts(options =>
{
var hstsSection = builder.Configuration.GetSection("Hsts");
options.MaxAge = TimeSpan.FromDays(hstsSection.GetValue<int>("MaxAgeDays", 365));
options.IncludeSubDomains = hstsSection.GetValue<bool>("IncludeSubDomains", true);
options.Preload = hstsSection.GetValue<bool>("Preload", false);
});
}
// Add services to the container. // Add services to the container.
builder.Services.AddRazorComponents() builder.Services.AddRazorComponents()
.AddInteractiveServerComponents(); .AddInteractiveServerComponents();
+6
View File
@@ -16,6 +16,12 @@
"Twitter": "", "Twitter": "",
"Mastodon": "" "Mastodon": ""
}, },
"Hsts": {
"Domain": "nas.oneputleygreen.com",
"MaxAgeDays": 365,
"IncludeSubDomains": true,
"Preload": false
},
"Authentication": { "Authentication": {
"Google": { "Google": {
"ClientId": "", "ClientId": "",
+4 -1
View File
@@ -32,6 +32,9 @@ LABEL org.opencontainers.image.title="Budgeteer" \
# Ensure the SQLite data directory exists and is writable # Ensure the SQLite data directory exists and is writable
RUN mkdir -p /app/data RUN mkdir -p /app/data
# HSTS domain — used as the certificate CN and read by the app via Hsts:Domain config
ENV Hsts__Domain=nas.oneputleygreen.com
# Generate a self-signed TLS certificate for HTTPS # Generate a self-signed TLS certificate for HTTPS
RUN mkdir -p /app/certs && \ RUN mkdir -p /app/certs && \
openssl req -x509 -newkey rsa:4096 \ openssl req -x509 -newkey rsa:4096 \
@@ -39,7 +42,7 @@ RUN mkdir -p /app/certs && \
-out /app/certs/cert.pem \ -out /app/certs/cert.pem \
-days 3650 \ -days 3650 \
-nodes \ -nodes \
-subj "/C=GB/ST=England/O=Budgeteer/CN=localhost" && \ -subj "/C=GB/ST=England/O=Budgeteer/CN=${Hsts__Domain}" && \
openssl pkcs12 -export \ openssl pkcs12 -export \
-out /app/certs/cert.pfx \ -out /app/certs/cert.pfx \
-inkey /app/certs/key.pem \ -inkey /app/certs/key.pem \