feat(ui): Sitzungs-Cookie hinter HTTPS-Proxy als Secure; README zum Betrieb hinter NPM oder tailscale serve
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GmHhg3bfByVgQ9z7CGCSGc
This commit is contained in:
@ -16,7 +16,9 @@
|
||||
//! mit fremdem `Origin` werden abgelehnt; jede ändernde Anfrage braucht
|
||||
//! zusätzlich das CSRF-Token der Sitzung und landet im Audit. Eine falsche Anmeldung kostet eine
|
||||
//! Sekunde Wartezeit. Über HTTP ohne TLS kann das Cookie nicht `Secure` sein;
|
||||
//! die Oberfläche ist wie das Gateway nur für das interne Netz oder Tailscale
|
||||
//! hinter einem Proxy mit HTTPS (`X-Forwarded-Proto: https`, zum Beispiel
|
||||
//! Nginx Proxy Manager oder `tailscale serve`) wird es `Secure` gesetzt. Die
|
||||
//! Oberfläche ist wie das Gateway nur für das interne Netz oder Tailscale
|
||||
//! gedacht.
|
||||
//!
|
||||
//! Statt der Bibliothek HTMX steckt das eine benötigte Muster (Teil der Seite
|
||||
@ -251,6 +253,28 @@ fn same_origin(headers: &HeaderMap) -> bool {
|
||||
same
|
||||
}
|
||||
|
||||
/// Kam die Anfrage über HTTPS (Proxy davor)? Dann bekommt das Cookie `Secure`.
|
||||
/// Ein gefälschter Header schadet nur dem, der ihn schickt: Sein Browser
|
||||
/// schickt das Cookie dann über HTTP nicht mehr mit.
|
||||
fn via_https(headers: &HeaderMap) -> bool {
|
||||
headers
|
||||
.get("x-forwarded-proto")
|
||||
.and_then(|v| v.to_str().ok())
|
||||
.is_some_and(|v| {
|
||||
v.split(',')
|
||||
.next()
|
||||
.is_some_and(|p| p.trim().eq_ignore_ascii_case("https"))
|
||||
})
|
||||
}
|
||||
|
||||
fn cookie_flags(headers: &HeaderMap) -> &'static str {
|
||||
if via_https(headers) {
|
||||
"Path=/ui; HttpOnly; Secure; SameSite=Strict"
|
||||
} else {
|
||||
"Path=/ui; HttpOnly; SameSite=Strict"
|
||||
}
|
||||
}
|
||||
|
||||
// --- Antworten ---------------------------------------------------------------
|
||||
|
||||
pub(crate) fn secure(builder: axum::http::response::Builder) -> axum::http::response::Builder {
|
||||
@ -524,7 +548,8 @@ async fn login(State(state): State<Arc<AppState>>, headers: HeaderMap, body: Byt
|
||||
}
|
||||
tracing::info!(key = %record.name, "Anmeldung an der Oberfläche");
|
||||
let cookie = format!(
|
||||
"{COOKIE_NAME}={token}; Path=/ui; HttpOnly; SameSite=Strict; Max-Age={SESSION_SECS}"
|
||||
"{COOKIE_NAME}={token}; {flags}; Max-Age={SESSION_SECS}",
|
||||
flags = cookie_flags(&headers)
|
||||
);
|
||||
let mut response = redirect("/ui");
|
||||
if let Ok(value) = HeaderValue::from_str(&cookie) {
|
||||
@ -561,10 +586,12 @@ async fn logout(State(state): State<Arc<AppState>>, headers: HeaderMap) -> Respo
|
||||
}
|
||||
}
|
||||
let mut response = redirect("/ui/login");
|
||||
response.headers_mut().insert(
|
||||
SET_COOKIE,
|
||||
HeaderValue::from_static("nexus_ui=; Path=/ui; HttpOnly; SameSite=Strict; Max-Age=0"),
|
||||
);
|
||||
if let Ok(value) = HeaderValue::from_str(&format!(
|
||||
"{COOKIE_NAME}=; {}; Max-Age=0",
|
||||
cookie_flags(&headers)
|
||||
)) {
|
||||
response.headers_mut().insert(SET_COOKIE, value);
|
||||
}
|
||||
response
|
||||
}
|
||||
|
||||
@ -947,6 +974,25 @@ mod tests {
|
||||
assert_eq!(cookie_token(&h).as_deref(), Some("abc"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cookie_ist_hinter_https_proxy_secure() {
|
||||
let mut h = HeaderMap::new();
|
||||
assert!(!cookie_flags(&h).contains("Secure"));
|
||||
h.insert("x-forwarded-proto", HeaderValue::from_static("http"));
|
||||
assert!(!cookie_flags(&h).contains("Secure"));
|
||||
h.insert("x-forwarded-proto", HeaderValue::from_static("HTTPS, http"));
|
||||
assert!(cookie_flags(&h).contains("Secure"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hinter_dem_proxy_passt_die_herkunft() {
|
||||
// NPM reicht den Host des Browsers weiter; der Browser schickt https als Origin.
|
||||
let mut h = HeaderMap::new();
|
||||
h.insert(HOST, HeaderValue::from_static("nexus.wlkns.org"));
|
||||
h.insert(ORIGIN, HeaderValue::from_static("https://nexus.wlkns.org"));
|
||||
assert!(same_origin(&h));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fremde_herkunft_wird_erkannt() {
|
||||
let mut h = HeaderMap::new();
|
||||
|
||||
Reference in New Issue
Block a user