auth, headers: the examples are refused, HTTPS is insisted on

The server no longer starts with WORKSPACE_SECRET or WORKSPACE_PASSWORD
left at the value .env.example ships with. The secret was only checked
for length, and the public placeholder passed: anyone who had read the
repository could sign a session cookie for themselves. A secret under
32 characters is warned about.

With WORKSPACE_SECURE_COOKIE=1 the cookie is named __Host-diary_session,
which the browser keeps to this host and to HTTPS and which no sibling
subdomain can plant; turning it on logs everyone out once. Responses
then carry Strict-Transport-Security for a year. The traefik example
turns it on.

The password is compared as an HMAC under the secret, so the time a
comparison takes no longer depends on the configured password's length.

Every response also carries Cross-Origin-Opener-Policy and
Cross-Origin-Resource-Policy same-origin and a Permissions-Policy that
turns camera, microphone, location, payment and USB off; API answers
are no-store unless they say otherwise.
This commit is contained in:
nvrl committed 2026-10-20 11:25:16 +02:00
1 parent 6bf88e7ffd
commit f450883ac0
6 files changed
+128 -19

No files matched your search

+46 -2
View File
@@ -234,12 +234,21 @@ async fn snapshot_before_migrating(
async fn serve(bind: std::net::SocketAddr, max_upload: usize, state: AppState) -> Result<()> {
let app = Router::new()
.nest("/api", routes::api_router(max_upload))
// What the API answers is private and changes; only what says
// otherwise — the media routes — is kept by a cache.
.nest(
"/api",
routes::api_router(max_upload).layer(SetResponseHeaderLayer::if_not_present(
header::CACHE_CONTROL,
HeaderValue::from_static("no-store"),
)),
)
.fallback(static_files::handler)
.layer(axum::middleware::from_fn(auth::same_origin))
.layer(axum::middleware::from_fn(deadline))
.layer(CompressionLayer::new().compress_when(compressible))
.layer(security_headers())
.layer(strict_transport(state.config.secure_cookie))
.layer(
TraceLayer::new_for_http()
.make_span_with(|request: &axum::http::Request<axum::body::Body>| {
@@ -378,7 +387,15 @@ type HeaderLayer = SetResponseHeaderLayer<HeaderValue>;
/// Headers every response carries. `if_not_present` throughout, so a handler
/// that has already said something stricter about itself — the media routes and
/// their sandbox CSP — keeps its own answer.
fn security_headers() -> (HeaderLayer, HeaderLayer, HeaderLayer, HeaderLayer) {
fn security_headers() -> (
HeaderLayer,
HeaderLayer,
HeaderLayer,
HeaderLayer,
HeaderLayer,
HeaderLayer,
HeaderLayer,
) {
let header = |name: header::HeaderName, value: &'static str| {
SetResponseHeaderLayer::if_not_present(name, HeaderValue::from_static(value))
};
@@ -390,6 +407,33 @@ fn security_headers() -> (HeaderLayer, HeaderLayer, HeaderLayer, HeaderLayer) {
header(header::X_CONTENT_TYPE_OPTIONS, "nosniff"),
header(header::X_FRAME_OPTIONS, "DENY"),
header(header::CONTENT_SECURITY_POLICY, CSP),
// No other site's window keeps a handle on this one, nor embeds what
// it serves.
header(
header::HeaderName::from_static("cross-origin-opener-policy"),
"same-origin",
),
header(
header::HeaderName::from_static("cross-origin-resource-policy"),
"same-origin",
),
header(
header::HeaderName::from_static("permissions-policy"),
"camera=(), microphone=(), geolocation=(), payment=(), usb=()",
),
)
}
/// Over HTTPS, the browser is told to come back over nothing else for a year,
/// so a mistyped `http://` never carries the cookie in the clear.
fn strict_transport(
secure: bool,
) -> SetResponseHeaderLayer<impl Fn(&axum::response::Response) -> Option<HeaderValue> + Clone> {
SetResponseHeaderLayer::if_not_present(
header::STRICT_TRANSPORT_SECURITY,
move |_: &axum::response::Response| {
secure.then(|| HeaderValue::from_static("max-age=31536000"))
},
)
}