| ... | @@ -10,6 +10,7 @@ use webauthn_rs::prelude::*; | ... | @@ -10,6 +10,7 @@ use webauthn_rs::prelude::*; |
| 10 | | 10 | |
| 11 | const COOKIE: &str = "__Host-snow-session"; | 11 | const COOKIE: &str = "__Host-snow-session"; |
| 12 | const FLOW_COOKIE: &str = "__Host-snow-flow"; | 12 | const FLOW_COOKIE: &str = "__Host-snow-flow"; |
| | 13 | const CSRF_COOKIE: &str = "__Host-snow-csrf"; |
| 13 | const SESSION_TTL: i64 = 30 * 86400; | 14 | const SESSION_TTL: i64 = 30 * 86400; |
| 14 | const GROUPS: &[&str] = &["infra-admin", "media", "media-manage", "metrics", "vm", "ai"]; | 15 | const GROUPS: &[&str] = &["infra-admin", "media", "media-manage", "metrics", "vm", "ai"]; |
| 15 | | 16 | |
| ... | @@ -32,8 +33,12 @@ pub fn cookie(headers: &HeaderMap, name: &str) -> Option<String> { | ... | @@ -32,8 +33,12 @@ pub fn cookie(headers: &HeaderMap, name: &str) -> Option<String> { |
| 32 | (key == name).then(|| value.to_owned()) | 33 | (key == name).then(|| value.to_owned()) |
| 33 | }) | 34 | }) |
| 34 | } | 35 | } |
| 35 | pub(crate) fn set_cookie(name: &str, value: &str, ttl: i64) -> String { | 36 | pub(crate) fn set_cookie(name: &str, value: &str, ttl: Option<i64>) -> String { |
| 36 | format!("{name}={value}; Path=/; Secure; HttpOnly; SameSite=Lax; Max-Age={ttl}") | 37 | let mut cookie = format!("{name}={value}; Path=/; Secure; HttpOnly; SameSite=Lax"); |
| | 38 | if let Some(ttl) = ttl { |
| | 39 | cookie.push_str(&format!("; Max-Age={ttl}")); |
| | 40 | } |
| | 41 | cookie |
| 37 | } | 42 | } |
| 38 | fn row(db: &Connection, statement: &str, key: &str) -> Result<Value> { | 43 | fn row(db: &Connection, statement: &str, key: &str) -> Result<Value> { |
| 39 | let value: Option<String> = db.query_row(statement, [key], |r| r.get(0)).optional()?; | 44 | let value: Option<String> = db.query_row(statement, [key], |r| r.get(0)).optional()?; |
| ... | @@ -430,7 +435,7 @@ impl Store { | ... | @@ -430,7 +435,7 @@ impl Store { |
| 430 | now() as i64 | 435 | now() as i64 |
| 431 | ], | 436 | ], |
| 432 | )?; | 437 | )?; |
| 433 | Ok(set_cookie(COOKIE, &token, SESSION_TTL)) | 438 | Ok(set_cookie(COOKIE, &token, Some(SESSION_TTL))) |
| 434 | } | 439 | } |
| 435 | pub(crate) fn limit(&self, headers: &HeaderMap, name: &str) -> Result<()> { | 440 | pub(crate) fn limit(&self, headers: &HeaderMap, name: &str) -> Result<()> { |
| 436 | let ip = headers | 441 | let ip = headers |
| ... | @@ -476,15 +481,11 @@ impl Store { | ... | @@ -476,15 +481,11 @@ impl Store { |
| 476 | { | 481 | { |
| 477 | return Err(Error::new(403, "Open sign-in on Snowglobe and try again.")); | 482 | return Err(Error::new(403, "Open sign-in on Snowglobe and try again.")); |
| 478 | } | 483 | } |
| 479 | let cookie = cookie(headers, FLOW_COOKIE).unwrap_or_default(); | 484 | let cookie = cookie(headers, CSRF_COOKIE).unwrap_or_default(); |
| 480 | if cookie.is_empty() | 485 | if cookie.is_empty() |
| 481 | || !bool::from(cookie.as_bytes().ct_eq(string(&body["csrf"]).as_bytes())) | 486 | || !bool::from(cookie.as_bytes().ct_eq(string(&body["csrf"]).as_bytes())) |
| 482 | || pending(&self.db.lock().unwrap(), &cookie, "csrf", false)?.is_null() | | |
| 483 | { | 487 | { |
| 484 | return Err(Error::new( | 488 | return Err(Error::new(403, "Reload the page to sign in.")); |
| 485 | 403, | | |
| 486 | "Sign-in expired. Reload the page and try again.", | | |
| 487 | )); | | |
| 488 | } | 489 | } |
| 489 | Ok(()) | 490 | Ok(()) |
| 490 | } | 491 | } |
| ... | @@ -635,7 +636,7 @@ pub async fn route(State(app): State<Arc<App>>, request: Request) -> Result<Resp | ... | @@ -635,7 +636,7 @@ pub async fn route(State(app): State<Arc<App>>, request: Request) -> Result<Resp |
| 635 | "location", | 636 | "location", |
| 636 | format!("{}auth/continue?flow={flow}", auth.origin), | 637 | format!("{}auth/continue?flow={flow}", auth.origin), |
| 637 | ), | 638 | ), |
| 638 | ("set-cookie", set_cookie(FLOW_COOKIE, &flow, 300)), | 639 | ("set-cookie", set_cookie(FLOW_COOKIE, &flow, Some(300))), |
| 639 | ], | 640 | ], |
| 640 | ) | 641 | ) |
| 641 | .into_response()); | 642 | .into_response()); |
| ... | @@ -693,7 +694,9 @@ pub async fn route(State(app): State<Arc<App>>, request: Request) -> Result<Resp | ... | @@ -693,7 +694,9 @@ pub async fn route(State(app): State<Arc<App>>, request: Request) -> Result<Resp |
| 693 | .into_response()); | 694 | .into_response()); |
| 694 | } | 695 | } |
| 695 | if path == "/auth/status" && method == Method::GET { | 696 | if path == "/auth/status" && method == Method::GET { |
| 696 | let csrf = issue(&auth.db.lock().unwrap(), "csrf", json!({}), 900)?; | 697 | let csrf = cookie(&headers, CSRF_COOKIE) |
| | 698 | .filter(|token| !token.is_empty()) |
| | 699 | .unwrap_or_else(mcp::secret); |
| 697 | let mut value = json!({"csrf":csrf,"account":auth.session(&headers,"dashboard")?}); | 700 | let mut value = json!({"csrf":csrf,"account":auth.session(&headers,"dashboard")?}); |
| 698 | value["service"] = json!("snow globe"); | 701 | value["service"] = json!("snow globe"); |
| 699 | if let Some(flow) = query.get("flow") { | 702 | if let Some(flow) = query.get("flow") { |
| ... | @@ -754,7 +757,7 @@ pub async fn route(State(app): State<Arc<App>>, request: Request) -> Result<Resp | ... | @@ -754,7 +757,7 @@ pub async fn route(State(app): State<Arc<App>>, request: Request) -> Result<Resp |
| 754 | } | 757 | } |
| 755 | return Ok(( | 758 | return Ok(( |
| 756 | [ | 759 | [ |
| 757 | ("set-cookie", set_cookie(FLOW_COOKIE, &csrf, 900)), | 760 | ("set-cookie", set_cookie(CSRF_COOKIE, &csrf, None)), |
| 758 | ("cache-control", "no-store".into()), | 761 | ("cache-control", "no-store".into()), |
| 759 | ], | 762 | ], |
| 760 | axum::Json(value), | 763 | axum::Json(value), |
| ... | @@ -807,7 +810,7 @@ pub async fn route(State(app): State<Arc<App>>, request: Request) -> Result<Resp | ... | @@ -807,7 +810,7 @@ pub async fn route(State(app): State<Arc<App>>, request: Request) -> Result<Resp |
| 807 | return Ok(( | 810 | return Ok(( |
| 808 | StatusCode::SEE_OTHER, | 811 | StatusCode::SEE_OTHER, |
| 809 | [ | 812 | [ |
| 810 | ("set-cookie", set_cookie(COOKIE, "", 0)), | 813 | ("set-cookie", set_cookie(COOKIE, "", Some(0))), |
| 811 | ( | 814 | ( |
| 812 | "location", | 815 | "location", |
| 813 | if shale_logout { | 816 | if shale_logout { |
| ... | @@ -821,7 +824,7 @@ pub async fn route(State(app): State<Arc<App>>, request: Request) -> Result<Resp | ... | @@ -821,7 +824,7 @@ pub async fn route(State(app): State<Arc<App>>, request: Request) -> Result<Resp |
| 821 | .into_response()); | 824 | .into_response()); |
| 822 | } | 825 | } |
| 823 | return Ok(( | 826 | return Ok(( |
| 824 | [("set-cookie", set_cookie(COOKIE, "", 0))], | 827 | [("set-cookie", set_cookie(COOKIE, "", Some(0)))], |
| 825 | axum::Json(json!({"next":"/sign-in"})), | 828 | axum::Json(json!({"next":"/sign-in"})), |
| 826 | ) | 829 | ) |
| 827 | .into_response()); | 830 | .into_response()); |