diff --git a/dashboard/Cargo.lock b/dashboard/Cargo.lock index 9d75672dd53f3feee77ed3ceb86f8f43046a1d50..2b1e733ac540c52f9dba57f3e50ad440d9802447 100644 --- a/dashboard/Cargo.lock +++ b/dashboard/Cargo.lock @@ -336,6 +336,36 @@ dependencies = [ "libc", ] +[[package]] +name = "critical-section" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" + +[[package]] +name = "crossbeam-channel" +version = "0.5.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98b0cc327b5bc766e7fda9c9260cc0fa81b43a8e240440422dff70788e3f9ef1" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" + [[package]] name = "crunchy" version = "0.2.4" @@ -564,6 +594,18 @@ dependencies = [ "simdutf8", ] +[[package]] +name = "enum-as-inner" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1e6a265c649f3f5979b601d26f1d05ada116434c87741c9493cb56218f76cbc" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "equivalent" version = "1.0.2" @@ -852,12 +894,64 @@ dependencies = [ "hashbrown 0.15.5", ] +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "hex" version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hickory-proto" +version = "0.25.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8a6fe56c0038198998a6f217ca4e7ef3a5e51f46163bd6dd60b5c71ca6c6502" +dependencies = [ + "async-trait", + "cfg-if", + "data-encoding", + "enum-as-inner", + "futures-channel", + "futures-io", + "futures-util", + "idna", + "ipnet", + "once_cell", + "rand 0.9.5", + "ring", + "thiserror 2.0.21", + "tinyvec", + "tokio", + "tracing", + "url", +] + +[[package]] +name = "hickory-resolver" +version = "0.25.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc62a9a99b0bfb44d2ab95a7208ac952d31060efc16241c87eaf36406fecf87a" +dependencies = [ + "cfg-if", + "futures-util", + "hickory-proto", + "ipconfig", + "moka", + "once_cell", + "parking_lot", + "rand 0.9.5", + "resolv-conf", + "smallvec", + "thiserror 2.0.21", + "tokio", + "tracing", +] + [[package]] name = "hmac" version = "0.12.1" @@ -878,6 +972,7 @@ dependencies = [ "chrono", "futures", "globset", + "hickory-resolver", "lol_html", "openssl", "percent-encoding", @@ -1174,6 +1269,19 @@ dependencies = [ "generic-array", ] +[[package]] +name = "ipconfig" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d40460c0ce33d6ce4b0630ad68ff63d6661961c48b6dba35e5a4d81cfb48222" +dependencies = [ + "socket2", + "widestring", + "windows-registry", + "windows-result", + "windows-sys 0.61.2", +] + [[package]] name = "ipnet" version = "2.12.2" @@ -1331,6 +1439,23 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "moka" +version = "0.12.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4293f18e7567a1caf3c584855554377025c65e0aa445344d04171f5ad63d19b9" +dependencies = [ + "crossbeam-channel", + "crossbeam-epoch", + "crossbeam-utils", + "equivalent", + "parking_lot", + "portable-atomic", + "smallvec", + "tagptr", + "uuid", +] + [[package]] name = "multer" version = "3.1.0" @@ -1445,6 +1570,10 @@ name = "once_cell" version = "1.21.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" +dependencies = [ + "critical-section", + "portable-atomic", +] [[package]] name = "openssl" @@ -1668,6 +1797,12 @@ version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" +[[package]] +name = "portable-atomic" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85" + [[package]] name = "potential_utf" version = "0.1.6" @@ -1977,6 +2112,12 @@ dependencies = [ "webpki-roots", ] +[[package]] +name = "resolv-conf" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e061d1b48cb8d38042de4ae0a7a6401009d6143dc80d2e2d6f31f0bdd6470c7" + [[package]] name = "rfc6979" version = "0.4.0" @@ -2587,6 +2728,12 @@ dependencies = [ "syn 3.0.6", ] +[[package]] +name = "tagptr" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b2093cf4c8eb1e67749a6762251bc9cd836b6fc171623bd0a9d324d37af2417" + [[package]] name = "tendril" version = "0.5.1" @@ -3146,6 +3293,12 @@ dependencies = [ "rustls-pki-types", ] +[[package]] +name = "widestring" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72069c3113ab32ab29e5584db3c6ec55d416895e60715417b5b883a357c3e471" + [[package]] name = "winapi-util" version = "0.1.11" @@ -3196,6 +3349,17 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-registry" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02752bf7fbdcce7f2a27a742f798510f3e5ad88dbe84871e5168e2120c3d5720" +dependencies = [ + "windows-link", + "windows-result", + "windows-strings", +] + [[package]] name = "windows-result" version = "0.4.1" diff --git a/dashboard/Cargo.toml b/dashboard/Cargo.toml index 71de4bf47d1af4d90566169b91afb2623f2ace12..07eba1ea89b6d1242b5f06bca8f893e8fa31f18a 100644 --- a/dashboard/Cargo.toml +++ b/dashboard/Cargo.toml @@ -11,6 +11,7 @@ bytes = "1" chrono = "0.4" futures = "0.3" globset = "0.4" +hickory-resolver = "0.25" openssl = "0.10" percent-encoding = "2" rand = "0.9" diff --git a/dashboard/src/atproto.rs b/dashboard/src/atproto.rs new file mode 100644 index 0000000000000000000000000000000000000000..10bb1e22338bad81f311e25c088ddac977272707 --- /dev/null +++ b/dashboard/src/atproto.rs @@ -0,0 +1,813 @@ +use crate::*; +use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; +use openssl::{ + bn::{BigNum, BigNumContext}, + ec::{EcGroup, EcKey}, + ecdsa::EcdsaSig, + nid::Nid, +}; +use sha2::{Digest, Sha256}; +use std::net::{IpAddr, SocketAddr}; +use url::Url; + +fn reject() -> Error { + Error::new( + 502, + "Your AT Protocol provider couldn't verify sign-in. Check your handle and try again.", + ) +} + +fn public_ip(ip: IpAddr) -> bool { + match ip { + IpAddr::V4(ip) => { + let [a, b, c, _] = ip.octets(); + !(a == 0 + || a == 10 + || a == 127 + || a >= 224 + || (a == 100 && (64..=127).contains(&b)) + || (a == 169 && b == 254) + || (a == 172 && (16..=31).contains(&b)) + || (a == 192 + && ((b == 0 && (c == 0 || c == 2)) || b == 168 || (b == 88 && c == 99))) + || (a == 198 && (b == 18 || b == 19 || (b == 51 && c == 100))) + || (a == 203 && b == 0 && c == 113)) + } + IpAddr::V6(ip) => { + let s = ip.segments(); + // Only global unicast; exclude transition mechanisms and special-purpose ranges. + s[0] & 0xe000 == 0x2000 + && !(s[0] == 0x2001 && s[1] < 0x200) + && !(s[0] == 0x2001 && s[1] == 0xdb8) + && s[0] != 0x2002 + && !(s[0] == 0x3fff && s[1] < 0x1000) + } + } +} + +fn https(value: &str) -> Result { + let url = Url::parse(value).map_err(|_| reject())?; + if value.len() > 2048 + || url.scheme() != "https" + || !url.username().is_empty() + || url.password().is_some() + || url.fragment().is_some() + || url.host_str().is_none() + || value.chars().any(|c| c.is_control() || c == '\\') + { + return Err(reject()); + } + match url.host() { + Some(url::Host::Domain(host)) if valid_handle(host) => (), + Some(url::Host::Ipv4(ip)) if public_ip(ip.into()) => (), + Some(url::Host::Ipv6(ip)) if public_ip(ip.into()) => (), + _ => return Err(reject()), + } + Ok(url) +} + +fn origin(value: &str) -> Result { + let url = https(value)?; + if url.path() != "/" || url.query().is_some() { + return Err(reject()); + } + Ok(url) +} + +fn valid_handle(value: &str) -> bool { + let labels: Vec<_> = value.split('.').collect(); + value.len() <= 253 + && labels.len() >= 2 + && labels.iter().all(|label| { + !label.is_empty() + && label.len() <= 63 + && !label.starts_with('-') + && !label.ends_with('-') + && label + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b == b'-') + }) + && labels.last().is_some_and(|last| { + last.as_bytes()[0].is_ascii_alphabetic() + && ![ + "local", + "localhost", + "internal", + "arpa", + "invalid", + "test", + "onion", + ] + .contains(last) + }) +} + +/// Resolve once, validate every address, and pin the connection to prevent DNS rebinding. +async fn client(url: &Url) -> Result { + let host = url.host_str().ok_or_else(reject)?; + #[cfg(test)] + if matches!( + host, + "alice.example.com" | "pds.example.com" | "issuer.example.com" + ) { + if let Ok(client) = tests::HTTP.try_with(Clone::clone) { + return Ok(client); + } + } + let addresses: Vec = tokio::time::timeout( + Duration::from_secs(5), + tokio::net::lookup_host(( + host.trim_matches(['[', ']']), + url.port_or_known_default().ok_or_else(reject)?, + )), + ) + .await + .map_err(|_| reject())? + .map_err(|_| reject())? + .collect(); + if addresses.is_empty() || addresses.iter().any(|address| !public_ip(address.ip())) { + return Err(reject()); + } + Ok(reqwest::Client::builder() + .no_proxy() + .redirect(reqwest::redirect::Policy::none()) + .timeout(Duration::from_secs(10)) + .connect_timeout(Duration::from_secs(5)) + .resolve_to_addrs(host, &addresses) + .user_agent("Snow Globe AT Protocol sign-in") + .build()?) +} + +async fn bytes(mut response: reqwest::Response, limit: usize) -> Result> { + if response.content_length().is_some_and(|n| n > limit as u64) { + return Err(reject()); + } + let mut data = Vec::new(); + while let Some(chunk) = response.chunk().await.map_err(|_| reject())? { + if data.len() + chunk.len() > limit { + return Err(reject()); + } + data.extend_from_slice(&chunk); + } + Ok(data) +} + +async fn get(url: Url) -> Result { + let response = client(&url) + .await? + .get(url) + .header("accept", "application/json") + .send() + .await + .map_err(|_| reject())?; + if response.status() != StatusCode::OK + || response + .headers() + .get("content-type") + .and_then(|v| v.to_str().ok()) + .is_none_or(|v| v.split(';').next() != Some("application/json")) + { + return Err(reject()); + } + serde_json::from_slice(&bytes(response, 65536).await?).map_err(|_| reject()) +} + +async fn handle_did(handle: &str) -> Result { + if !valid_handle(handle) { + return Err(reject()); + } + let resolver = hickory_resolver::Resolver::builder_tokio() + .map_err(|_| reject())? + .build(); + if let Ok(Ok(records)) = tokio::time::timeout( + Duration::from_secs(5), + resolver.txt_lookup(format!("_atproto.{handle}.")), + ) + .await + { + let dids: Vec<_> = records + .iter() + .filter_map(|record| { + let text = record + .txt_data() + .iter() + .flat_map(|part| part.iter().copied()) + .collect::>(); + String::from_utf8(text) + .ok() + .and_then(|s| s.strip_prefix("did=").map(str::to_owned)) + }) + .collect(); + if dids.len() > 1 { + return Err(reject()); + } + if let Some(did) = dids.into_iter().next() { + did_url(&did)?; + return Ok(did); + } + } + let url = https(&format!("https://{handle}/.well-known/atproto-did"))?; + let response = client(&url) + .await? + .get(url) + .send() + .await + .map_err(|_| reject())?; + if response.status() != StatusCode::OK { + return Err(reject()); + } + let did = String::from_utf8(bytes(response, 65536).await?) + .map_err(|_| reject())? + .trim() + .to_owned(); + did_url(&did)?; + Ok(did) +} + +fn did_url(did: &str) -> Result { + if let Some(id) = did.strip_prefix("did:plc:") { + if id.len() == 24 + && id + .bytes() + .all(|b| b.is_ascii_lowercase() || (b'2'..=b'7').contains(&b)) + { + return https(&format!("https://plc.directory/{did}")); + } + } + if let Some(host) = did + .strip_prefix("did:web:") + .filter(|host| valid_handle(host)) + { + return https(&format!("https://{host}/.well-known/did.json")); + } + Err(reject()) +} + +async fn identity(did: &str) -> Result<(Url, Option)> { + let doc = get(did_url(did)?).await?; + if doc["id"] != did { + return Err(reject()); + } + let pds = array(&doc["service"]) + .iter() + .find(|service| { + (service["id"] == "#atproto_pds" || service["id"] == format!("{did}#atproto_pds")) + && service["type"] == "AtprotoPersonalDataServer" + }) + .ok_or_else(reject)?; + let pds = origin(string(&pds["serviceEndpoint"]))?; + let claimed = array(&doc["alsoKnownAs"]) + .iter() + .filter_map(Value::as_str) + .filter_map(|s| s.strip_prefix("at://")) + .find(|s| valid_handle(s)); + let handle = match claimed { + Some(handle) + if handle_did(handle) + .await + .is_ok_and(|resolved| resolved == did) => + { + Some(handle.to_owned()) + } + _ => None, + }; + Ok((pds, handle)) +} + +async fn server(pds: &Url) -> Result { + let resource = get(pds.join(".well-known/oauth-protected-resource")?).await?; + if resource["resource"] + .as_str() + .and_then(|v| origin(v).ok()) + .as_ref() + != Some(pds) + { + return Err(reject()); + } + let issuers = array(&resource["authorization_servers"]); + if issuers.len() != 1 { + return Err(reject()); + } + let issuer = string(&issuers[0]); + let base = origin(issuer)?; + let metadata = get(base.join(".well-known/oauth-authorization-server")?).await?; + validate_server(&metadata, issuer)?; + Ok(metadata) +} + +fn validate_server(metadata: &Value, issuer: &str) -> Result<()> { + origin(issuer)?; + if metadata["issuer"] != issuer + || metadata["authorization_response_iss_parameter_supported"] != true + || metadata["require_pushed_authorization_requests"] != true + || metadata["require_request_uri_registration"] == false + || metadata["client_id_metadata_document_supported"] != true + { + return Err(reject()); + } + for (field, value) in [ + ("response_types_supported", "code"), + ("grant_types_supported", "authorization_code"), + ("grant_types_supported", "refresh_token"), + ("code_challenge_methods_supported", "S256"), + ("token_endpoint_auth_methods_supported", "none"), + ("scopes_supported", "atproto"), + ("dpop_signing_alg_values_supported", "ES256"), + ] { + if !array(&metadata[field]).iter().any(|item| item == value) { + return Err(reject()); + } + } + for field in [ + "authorization_endpoint", + "token_endpoint", + "pushed_authorization_request_endpoint", + ] { + https(string(&metadata[field]))?; + } + Ok(()) +} + +pub fn metadata(auth: &auth::Store) -> Value { + json!({"client_id":format!("{}auth/guest/metadata/atproto",auth.origin),"client_name":"Shale", + "client_uri":auth.origin,"application_type":"web","scope":"atproto", + "grant_types":["authorization_code"],"response_types":["code"], + "redirect_uris":[format!("{}auth/guest/callback/atproto",auth.origin)], + "token_endpoint_auth_method":"none","dpop_bound_access_tokens":true}) +} + +fn proof( + key: &EcKey, + endpoint: &Url, + nonce: Option<&str>, +) -> Result { + let mut x = BigNum::new()?; + let mut y = BigNum::new()?; + let mut context = BigNumContext::new()?; + key.public_key() + .affine_coordinates_gfp(key.group(), &mut x, &mut y, &mut context)?; + let header = json!({"typ":"dpop+jwt","alg":"ES256","jwk":{"kty":"EC","crv":"P-256", + "x":URL_SAFE_NO_PAD.encode(x.to_vec_padded(32)?),"y":URL_SAFE_NO_PAD.encode(y.to_vec_padded(32)?)}}); + let mut target = endpoint.clone(); + target.set_query(None); + let mut claims = + json!({"jti":mcp::secret(),"htm":"POST","htu":target.as_str(),"iat":now() as i64}); + if let Some(nonce) = nonce { + claims["nonce"] = json!(nonce); + } + let payload = format!( + "{}.{}", + URL_SAFE_NO_PAD.encode(header.to_string()), + URL_SAFE_NO_PAD.encode(claims.to_string()) + ); + let signature = EcdsaSig::sign(&Sha256::digest(payload.as_bytes()), key)?; + let mut raw = signature.r().to_vec_padded(32)?; + raw.extend(signature.s().to_vec_padded(32)?); + Ok(format!("{payload}.{}", URL_SAFE_NO_PAD.encode(raw))) +} + +async fn post( + endpoint: &str, + key: &EcKey, + form: &[(&str, &str)], + nonce: &mut Option, + expected: StatusCode, +) -> Result { + let endpoint = https(endpoint)?; + let http = client(&endpoint).await?; + for attempt in 0..2 { + let response = http + .post(endpoint.clone()) + .header("DPoP", proof(key, &endpoint, nonce.as_deref())?) + .header("accept", "application/json") + .form(form) + .send() + .await + .map_err(|_| reject())?; + let status = response.status(); + let next_nonce = response + .headers() + .get("dpop-nonce") + .and_then(|n| n.to_str().ok()) + .filter(|n| !n.is_empty() && n.len() <= 2048) + .ok_or_else(reject)? + .to_owned(); + *nonce = Some(next_nonce); + let data: Value = + serde_json::from_slice(&bytes(response, 65536).await?).map_err(|_| reject())?; + if attempt == 0 + && (status == StatusCode::BAD_REQUEST || status == StatusCode::UNAUTHORIZED) + && data["error"] == "use_dpop_nonce" + { + continue; + } + if status != expected { + return Err(reject()); + } + return Ok(data); + } + Err(reject()) +} + +pub async fn start(auth: &auth::Store, identifier: &str, next: &str) -> Result<(String, String)> { + let identifier = identifier.trim().trim_start_matches('@'); + let did = if identifier.starts_with("did:") { + did_url(identifier)?; + identifier.to_owned() + } else { + handle_did(&identifier.to_ascii_lowercase()).await? + }; + let (pds, handle) = identity(&did).await?; + if !identifier.starts_with("did:") + && handle.as_deref() != Some(identifier.to_ascii_lowercase().as_str()) + { + return Err(reject()); + } + let server = server(&pds).await?; + let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1)?; + let key = EcKey::generate(&group)?; + let verifier = mcp::secret(); + let mut flow = json!({"provider":"atproto","next":next,"did":did,"server":server, + "verifier":verifier,"key":URL_SAFE_NO_PAD.encode(key.private_key_to_der()?)}); + let state = auth::issue(&auth.db.lock().unwrap(), "guest", flow.clone(), 300)?; + let result = async { + let metadata = metadata(auth); + let challenge = URL_SAFE_NO_PAD.encode(Sha256::digest(verifier.as_bytes())); + let mut nonce = None; + let par = post( + string(&server["pushed_authorization_request_endpoint"]), + &key, + &[ + ("client_id", string(&metadata["client_id"])), + ("redirect_uri", string(&metadata["redirect_uris"][0])), + ("response_type", "code"), + ("scope", "atproto"), + ("state", &state), + ("code_challenge", &challenge), + ("code_challenge_method", "S256"), + ("login_hint", &did), + ], + &mut nonce, + StatusCode::CREATED, + ) + .await?; + let uri = string(&par["request_uri"]); + if uri.is_empty() || uri.len() > 2048 || par["expires_in"].as_u64().is_none_or(|n| n == 0) { + return Err(reject()); + } + flow["nonce"] = json!(nonce); + auth.db.lock().unwrap().execute( + "UPDATE pending SET data=? WHERE hash=? AND kind='guest'", + rusqlite::params![flow.to_string(), mcp::hash(&state)], + )?; + let mut target = https(string(&server["authorization_endpoint"]))?; + client(&target).await?; + if target + .query_pairs() + .any(|(key, _)| matches!(key.as_ref(), "client_id" | "request_uri")) + { + return Err(reject()); + } + target.query_pairs_mut().extend_pairs([ + ("client_id", string(&metadata["client_id"])), + ("request_uri", uri), + ]); + Ok((target.to_string(), state.clone())) + } + .await; + if result.is_err() { + auth::pending(&auth.db.lock().unwrap(), &state, "guest", true)?; + } + result +} + +pub struct Profile { + pub did: String, + pub name: String, + pub picture: Option, + pub url: Option, +} + +pub async fn finish(auth: &auth::Store, flow: &Value, code: &str, issuer: &str) -> Result { + let server = &flow["server"]; + if issuer != string(&server["issuer"]) { + return Err(reject()); + } + let key = EcKey::private_key_from_der(&URL_SAFE_NO_PAD.decode(string(&flow["key"]))?)?; + let metadata = metadata(auth); + let mut nonce = flow["nonce"].as_str().map(str::to_owned); + let token = post( + string(&server["token_endpoint"]), + &key, + &[ + ("client_id", string(&metadata["client_id"])), + ("grant_type", "authorization_code"), + ("code", code), + ("redirect_uri", string(&metadata["redirect_uris"][0])), + ("code_verifier", string(&flow["verifier"])), + ], + &mut nonce, + StatusCode::OK, + ) + .await?; + if token["sub"] != flow["did"] + || token["token_type"] + .as_str() + .is_none_or(|s| !s.eq_ignore_ascii_case("DPoP")) + || string(&token["access_token"]).is_empty() + || string(&token["scope"]) != "atproto" + { + return Err(reject()); + } + let did = string(&token["sub"]); + let (pds, handle) = identity(did).await?; + if self::server(&pds).await?["issuer"] != issuer { + return Err(reject()); + } + let mut fallback = Url::parse("https://pdsls.dev/at://")?; + fallback + .path_segments_mut() + .unwrap() + .pop_if_empty() + .push(did); + let mut profile = Profile { + did: did.to_owned(), + name: handle.clone().unwrap_or_else(|| did.to_owned()), + picture: None, + url: Some(fallback.into()), + }; + for (collection, site) in [ + ("sh.tangled.actor.profile", "https://tangled.org/"), + ("app.bsky.actor.profile", "https://bsky.app/profile/"), + ] { + let mut url = pds.join("xrpc/com.atproto.repo.getRecord")?; + url.query_pairs_mut().extend_pairs([ + ("repo", did), + ("collection", collection), + ("rkey", "self"), + ]); + let Ok(record) = get(url).await else { + continue; + }; + if record["uri"] != format!("at://{did}/{collection}/self") + || record["value"]["$type"] != collection + { + continue; + } + let value = &record["value"]; + if let Some(name) = value["displayName"] + .as_str() + .filter(|s| !s.trim().is_empty() && !s.chars().any(char::is_control)) + { + profile.name = name.chars().take(128).collect(); + } + let mut link = Url::parse(site)?; + link.path_segments_mut() + .unwrap() + .pop_if_empty() + .push(handle.as_deref().unwrap_or(did)); + profile.url = Some(link.into()); + let blob = &value["avatar"]; + let cid = string(&blob["ref"]["$link"]); + if blob["$type"] == "blob" + && matches!( + string(&blob["mimeType"]), + "image/png" | "image/jpeg" | "image/webp" + ) + && !cid.is_empty() + && cid.len() <= 256 + && cid.bytes().all(|b| b.is_ascii_alphanumeric()) + { + let mut avatar = pds.join("xrpc/com.atproto.sync.getBlob")?; + avatar + .query_pairs_mut() + .extend_pairs([("did", did), ("cid", cid)]); + if let Ok(http) = client(&avatar).await + && let Ok(response) = http.get(avatar).send().await + && response.status() == StatusCode::OK + && let Ok(data) = bytes(response, 262144).await + { + let mime = if data.starts_with(b"\x89PNG\r\n\x1a\n") { + Some("image/png") + } else if data.starts_with(b"\xff\xd8\xff") { + Some("image/jpeg") + } else if data.starts_with(b"RIFF") && data.get(8..12) == Some(b"WEBP") { + Some("image/webp") + } else { + None + }; + if let Some(mime) = mime { + profile.picture = Some(format!( + "data:{mime};base64,{}", + base64::engine::general_purpose::STANDARD.encode(data) + )); + } + } + } + break; + } + Ok(profile) +} + +#[cfg(test)] +mod tests { + use super::*; + use futures::FutureExt; + use std::io::BufRead; + use std::process::{Command, Stdio}; + tokio::task_local! { pub(super) static HTTP: reqwest::Client; } + + #[test] + fn rejects_local_networks_and_malformed_identifiers() { + for ip in [ + "0.0.0.0", + "10.0.0.1", + "100.64.0.1", + "127.0.0.1", + "169.254.169.254", + "172.31.0.1", + "192.168.1.1", + "192.0.0.1", + "198.18.0.1", + "224.0.0.1", + "255.255.255.255", + "::1", + "::ffff:127.0.0.1", + "64:ff9b::7f00:1", + "2002:7f00::1", + "2001:db8::1", + "fc00::1", + "fe80::1", + ] { + assert!(!public_ip(ip.parse().unwrap()), "{ip}"); + } + for url in [ + "http://example.com", + "https://user:pass@example.com", + "https://127.1", + "https://2130706433", + "https://[::1]", + "https://example.com/#bad", + "https://x.local", + "https://localhost", + "https://example.com\\@127.0.0.1", + ] { + assert!(https(url).is_err(), "{url}"); + } + for did in [ + "did:web:localhost", + "did:web:example.com:profile", + "did:web:example.com%3A443", + "did:web:127.0.0.1", + "did:plc:../oops", + "did:plc:aaaaaaaaaaaaaaaaaaaaaaa1", + ] { + assert!(did_url(did).is_err(), "{did}"); + } + assert!(did_url("did:plc:ewvi7nxzyoun6zhxrhs64oiz").is_ok()); + assert!(did_url("did:web:example.com").is_ok()); + assert!(public_ip("8.8.8.8".parse().unwrap())); + assert!(public_ip("2606:4700:4700::1111".parse().unwrap())); + } + + #[tokio::test] + async fn synthetic_https_pds_verifies_par_pkce_dpop_and_identity() { + let root = std::env::temp_dir().join(format!("atproto-test-{}", uuid::Uuid::new_v4())); + std::fs::create_dir(&root).unwrap(); + let cert = root.join("cert.pem"); + let key = root.join("key.pem"); + assert!(Command::new("openssl").args(["req","-x509","-newkey","rsa:2048","-nodes","-days","1","-subj","/CN=fixture","-addext","subjectAltName=DNS:alice.example.com,DNS:pds.example.com,DNS:issuer.example.com","-addext","basicConstraints=critical,CA:FALSE","-keyout"]) + .arg(&key).arg("-out").arg(&cert).output().unwrap().status.success()); + let mut child = Command::new("python3") + .arg(concat!( + env!("CARGO_MANIFEST_DIR"), + "/tests/atproto-fixture.py" + )) + .arg(&cert) + .arg(&key) + .stdout(Stdio::piped()) + .spawn() + .unwrap(); + let mut line = String::new(); + std::io::BufReader::new(child.stdout.take().unwrap()) + .read_line(&mut line) + .unwrap(); + let address = format!("127.0.0.1:{}", line.trim()).parse().unwrap(); + let mut builder = reqwest::Client::builder() + .no_proxy() + .redirect(reqwest::redirect::Policy::none()) + .timeout(Duration::from_secs(5)) + .add_root_certificate( + reqwest::Certificate::from_pem(&std::fs::read(&cert).unwrap()).unwrap(), + ); + for host in ["alice.example.com", "pds.example.com", "issuer.example.com"] { + builder = builder.resolve(host, address); + } + let http = builder.build().unwrap(); + let result = std::panic::AssertUnwindSafe(HTTP.scope(http.clone(), async { + let auth = auth::Store::new( + &root.join("accounts"), + "https://snowglobe.example.com", + "https://files.example.com", + "auth.example.com", + ) + .unwrap(); + for (mode, succeeds) in [ + ("", true), + ("bluesky-only", true), + ("no-profiles", true), + ("rotating-nonce", true), + ("endpoint-query", true), + ("wrong-subject", false), + ("changed-issuer", false), + ("wrong-metadata-issuer", false), + ("missing-nonce", false), + ("private-pds", false), + ] { + http.get(format!( + "https://issuer.example.com/fixture/mode?mode={mode}" + )) + .send() + .await + .unwrap(); + let started = start(&auth, "did:web:alice.example.com", "/fixture").await; + if matches!( + mode, + "wrong-metadata-issuer" | "missing-nonce" | "private-pds" + ) { + assert!(started.is_err(), "{mode}"); + continue; + } + let (target, state) = started.unwrap(); + let code = Url::parse(&target) + .unwrap() + .query_pairs() + .find(|(key, _)| key == "request_uri") + .unwrap() + .1 + .into_owned(); + let flow = auth::pending(&auth.db.lock().unwrap(), &state, "guest", true).unwrap(); + assert!( + auth::pending(&auth.db.lock().unwrap(), &state, "guest", true) + .unwrap() + .is_null() + ); + assert!( + finish(&auth, &flow, &code, "https://wrong.example.com") + .await + .is_err() + ); + let profile = finish(&auth, &flow, &code, "https://issuer.example.com").await; + assert_eq!(profile.is_ok(), succeeds, "{mode}"); + if let Ok(profile) = profile { + assert_eq!(profile.did, "did:web:alice.example.com"); + if mode == "no-profiles" { + assert!(profile.picture.is_none()); + assert_eq!(profile.name, "did:web:alice.example.com"); + assert_eq!( + profile.url.as_deref(), + Some("https://pdsls.dev/at://did:web:alice.example.com") + ); + } else { + assert!( + profile + .picture + .as_deref() + .unwrap() + .starts_with("data:image/png;base64,") + ); + assert_eq!(profile.name, "Fixture Alice"); + assert!(profile.url.unwrap().starts_with(if mode == "bluesky-only" { + "https://bsky.app/profile/" + } else { + "https://tangled.org/" + })); + } + assert!( + finish(&auth, &flow, &code, "https://issuer.example.com") + .await + .is_err() + ); + } + } + assert!( + get(https("https://issuer.example.com/redirect").unwrap()) + .await + .is_err() + ); + assert!( + get(https("https://issuer.example.com/oversized").unwrap()) + .await + .is_err() + ); + })) + .catch_unwind() + .await; + child.kill().unwrap(); + child.wait().unwrap(); + std::fs::remove_dir_all(root).unwrap(); + if let Err(panic) = result { + std::panic::resume_unwind(panic); + } + } +} diff --git a/dashboard/src/guest.rs b/dashboard/src/guest.rs index 385973c67b2581f466cf7fdc4b650e99bf6b78f1..bb1d815244f14cf5c592626fd6155bfe8357a247 100644 --- a/dashboard/src/guest.rs +++ b/dashboard/src/guest.rs @@ -32,9 +32,10 @@ fn known(provider: &str) -> Result<&str> { match provider { "github" => Ok("GitHub"), "astheno" => Ok("Astheno"), + "atproto" => Ok("AT Protocol"), _ => Err(Error::new( 404, - "Choose GitHub or Astheno on the Shale sign-in page.", + "Choose a provider on the Shale sign-in page.", )), } } @@ -43,6 +44,12 @@ fn known(provider: &str) -> Result<&str> { pub fn provision(auth: &auth::Store, input: Value) -> Result { let provider = string(&input["provider"]); known(provider)?; + if provider == "atproto" { + return Err(Error::new( + 400, + "AT Protocol uses public client metadata; no client secret is needed.", + )); + } let mut db = auth.db.lock().unwrap(); let tx = db.transaction()?; tx.execute( @@ -80,9 +87,10 @@ pub fn providers(auth: &auth::Store, next: &str) -> Result { } let db = auth.db.lock().unwrap(); let mut query = db.prepare("SELECT id FROM guest_providers ORDER BY id")?; - let ids = query + let mut ids = query .query_map([], |r| r.get::<_, String>(0))? .collect::, _>>()?; + ids.push("atproto".into()); Ok(json!( ids.iter() .map(|id| json!({"id":id,"name":known(id).unwrap_or(id)})) @@ -522,7 +530,30 @@ async fn handle(app: &App, request: Request) -> Result { } let provider = parts[4]; known(provider)?; - let (client, secret) = registration(auth, provider)?; + if provider == "atproto" && parts[3] == "metadata" { + return Ok(axum::Json(atproto::metadata(auth)).into_response()); + } + if provider == "atproto" && parts[3] == "avatar" { + let query = fields(request.uri().query().unwrap_or_default())?; + let subject = query.get("subject").map(String::as_str).unwrap_or_default(); + let picture: Option = auth.db.lock().unwrap().query_row( + "SELECT json_extract(profile,'$.attributes.picture[0]') FROM users JOIN external_identities ON user_id=users.id WHERE provider='atproto' AND subject=? AND json_extract(profile,'$.enabled')=1", + [subject], |row| row.get(0)).optional()?.flatten(); + let (mime, data) = picture + .as_deref() + .and_then(|s| s.strip_prefix("data:")) + .and_then(|s| s.split_once(";base64,")) + .filter(|(mime, _)| matches!(*mime, "image/png" | "image/jpeg" | "image/webp")) + .ok_or_else(|| Error::new(404, "Profile picture unavailable."))?; + return Ok(( + [ + ("content-type", mime), + ("x-content-type-options", "nosniff"), + ], + base64::engine::general_purpose::STANDARD.decode(data)?, + ) + .into_response()); + } let query = fields(request.uri().query().unwrap_or_default())?; let current = auth.session(headers, "dashboard")?; if !current.is_null() && !is_guest(¤t) { @@ -538,6 +569,39 @@ async fn handle(app: &App, request: Request) -> Result { auth, query.get("next").map(String::as_str).unwrap_or_default(), )?; + if provider == "atproto" { + let result = tokio::time::timeout( + Duration::from_secs(45), + atproto::start( + auth, + query + .get("identifier") + .map(String::as_str) + .unwrap_or_default(), + &next, + ), + ) + .await; + return match result { + Ok(Ok((target, state))) => Ok(( + StatusCode::FOUND, + [ + ("location", target), + ("set-cookie", auth::set_cookie(COOKIE, &state, Some(300))), + ], + ) + .into_response()), + _ => Ok(( + StatusCode::FOUND, + [( + "location", + format!("/sign-in?next={}&atproto=1&guest_error=1", encoded(&next)), + )], + ) + .into_response()), + }; + } + let (client, _) = registration(auth, provider)?; let verifier = mcp::secret(); let nonce = mcp::secret(); let state = auth::issue( @@ -606,13 +670,43 @@ async fn handle(app: &App, request: Request) -> Result { let result = async { let code = query .get("code") - .filter(|s| !s.is_empty() && s.len() <= 4096) + .filter(|s| !s.is_empty() && s.len() <= 4096 && !query.contains_key("error")) .ok_or_else(|| { Error::new( 400, "Sign-in wasn't completed. Return to Shale and try again.", ) })?; + if provider == "atproto" { + let profile = tokio::time::timeout( + Duration::from_secs(45), + atproto::finish( + auth, + &flow, + code, + query.get("iss").map(String::as_str).unwrap_or_default(), + ), + ) + .await + .map_err(|_| Error::new(502, "Your provider took too long. Try signing in again."))??; + let id = account( + auth, + provider, + &profile.did, + &profile.name, + profile.picture.as_deref(), + )?; + auth.db.lock().unwrap().execute( + "UPDATE users SET profile=json_patch(profile,?) WHERE id=?", + sql![ + json!({"attributes":{"profileUrl":profile.url.map(|url|vec![url])}}) + .to_string(), + id + ], + )?; + return auth.create_session(&id, "dashboard", headers, None); + } + let (client, secret) = registration(auth, provider)?; let http = reqwest::Client::builder() .timeout(Duration::from_secs(10)) .redirect(reqwest::redirect::Policy::none()) @@ -675,6 +769,55 @@ pub async fn route(State(app): State>, request: Request) -> Response { mod tests { use super::*; + #[test] + fn atproto_did_is_the_account_key_and_guests_stay_separate() { + let path = + std::env::temp_dir().join(format!("atproto-guest-test-{}", uuid::Uuid::new_v4())); + let auth = auth::Store::new( + &path, + "https://snowglobe.example.com", + "https://file.example.com", + "auth.example.com", + ) + .unwrap(); + let did = "did:plc:ewvi7nxzyoun6zhxrhs64oiz"; + let first = account(&auth, "atproto", did, "alice.example.com", None).unwrap(); + assert_eq!( + first, + account(&auth, "atproto", did, "new.example.com", None).unwrap() + ); + assert_ne!( + first, + account(&auth, "github", "42", "new.example.com", None).unwrap() + ); + assert_ne!( + first, + account( + &auth, + "atproto", + "did:web:other.example.com", + "new.example.com", + None + ) + .unwrap() + ); + assert!( + auth.create_session(&first, "file", &HeaderMap::new(), None) + .is_err() + ); + assert!(auth.setup_link(&first).is_err()); + assert_eq!(providers(&auth, "/").unwrap(), json!([])); + let db = auth.db.lock().unwrap(); + let profile = auth::user(&db, &first).unwrap(); + assert!(is_guest(&profile)); + assert!(profile["email"].is_null()); + assert_eq!(profile["firstName"], "new.example.com"); + assert!(auth::set_password(&db, &first, "unused").is_err()); + drop(db); + drop(auth); + std::fs::remove_dir_all(path).unwrap(); + } + // The P-256 tokens were produced independently with Python cryptography. #[test] fn external_signature_issuer_audience_nonce_and_time_are_required() { diff --git a/dashboard/src/main.rs b/dashboard/src/main.rs index e44fa733ef8533916cb79be5c9cbd7b8059e0851..6a04af03f796036881197bce513f8cd913bc3f57 100644 --- a/dashboard/src/main.rs +++ b/dashboard/src/main.rs @@ -1,6 +1,7 @@ mod ai; mod apps; mod auth; +mod atproto; mod identity; mod cache; mod core; diff --git a/dashboard/src/shale_page.rs b/dashboard/src/shale_page.rs index eaf07b9d98287a6b0053e1e17531aa269a75735d..6b5d2bb9bd92181897f379e3f9b44c8feb609751 100644 --- a/dashboard/src/shale_page.rs +++ b/dashboard/src/shale_page.rs @@ -14,7 +14,7 @@ struct Profile { fn profiles(auth: &auth::Store) -> Result> { let db = auth.db.lock().unwrap(); - let mut query = db.prepare("SELECT json_extract(profile,'$.username'),coalesce(json_extract(profile,'$.firstName'),''),provider,subject,json_extract(profile,'$.attributes.picture[0]') FROM users JOIN external_identities ON user_id=users.id WHERE json_extract(profile,'$.kind')='guest'")?; + let mut query = db.prepare("SELECT json_extract(profile,'$.username'),coalesce(json_extract(profile,'$.firstName'),''),provider,subject,CASE WHEN provider='atproto' THEN CASE WHEN json_type(profile,'$.attributes.picture[0]')='text' THEN '' END ELSE json_extract(profile,'$.attributes.picture[0]') END,json_extract(profile,'$.attributes.profileUrl[0]') FROM users JOIN external_identities ON user_id=users.id WHERE json_extract(profile,'$.kind')='guest'")?; let rows = query.query_map([], |row| { Ok(( row.get::<_, String>(0)?, @@ -22,11 +22,12 @@ fn profiles(auth: &auth::Store) -> Result> { row.get::<_, String>(2)?, row.get::<_, String>(3)?, row.get::<_, Option>(4)?, + row.get::<_, Option>(5)?, )) })?; let mut profiles = HashMap::new(); for row in rows { - let (username, name, provider, subject, picture) = row?; + let (username, name, provider, subject, picture, profile_url) = row?; if name.is_empty() { continue; } let (url, icon) = match provider.as_str() { "github" => { @@ -36,6 +37,7 @@ fn profiles(auth: &auth::Store) -> Result> { (Some(String::from(url)), include_str!("../web/sso/github.svg")) } "astheno" => (None, include_str!("astheno.svg")), + "atproto" => (profile_url, include_str!("../web/sso/atproto.svg")), _ => continue, }; profiles.insert( @@ -49,6 +51,8 @@ fn profiles(auth: &auth::Store) -> Result> { Some(format!( "https://avatars.githubusercontent.com/u/{subject}?s=64" )) + } else if provider == "atproto" { + picture.map(|_| format!("{}auth/guest/avatar/atproto?subject={}", auth.origin, encoded(&subject))) } else { picture }, @@ -268,13 +272,13 @@ fn rewrite(html: &str, origin: &url::Url, profiles: &HashMap, a if profile.owner.is_some() || ai_profile.is_some() { element.set_tag_name("span")?; } let style = element.get_attribute("style").unwrap_or_default(); let cursor = if profile.url.is_some() { "" } else { ";cursor:default" }; - let spacing = if profile.owner.is_some() { format!(";display:inline-flex;align-items:baseline;flex-wrap:{};gap:.25em;margin-right:.25em;max-width:100%", if compact.get() { "nowrap" } else { "wrap" }) } else { String::new() }; + let spacing = if profile.owner.is_some() { format!(";display:inline-flex;align-items:baseline;flex-wrap:{};gap:.25em;margin-right:.25em;max-width:100%", if compact.get() { "nowrap" } else { "wrap" }) } else if ai_profile.is_none() { ";display:inline-flex;align-items:center;gap:.25em;max-width:100%".into() } else { String::new() }; element.set_attribute("style", &format!("{style};text-decoration:none{cursor}{spacing}"))?; if let Some(picture) = &profile.picture { element.prepend(&avatar(picture), ContentType::Html); } if profile.owner.is_some() { element.append("", ContentType::Html); } - let dimensions = if ai_profile.is_some() || profile.owner.is_some() { "width:1em;height:1em;margin-right:.25em" } else { "width:.85em;height:.85em;vertical-align:-.1em;margin-right:.05em" }; + let dimensions = if ai_profile.is_some() || profile.owner.is_some() { "width:1em;height:1em;margin-right:.25em" } else { "width:.85em;height:.85em;flex-shrink:0" }; let icon = profile.icon.trim().replace(", request: Request) -> Result { #[cfg(test)] mod tests { use super::*; + use rusqlite::params as sql; use scraper::{Html, Selector}; + #[test] + fn persisted_atproto_profiles_enrich_shale_authors_and_account_links() { + let path = std::env::temp_dir().join(format!("shale-atproto-test-{}", uuid::Uuid::new_v4())); + let store = || auth::Store::new(&path, "https://snowglobe.example.com", "https://file.example.com", "auth.example.com").unwrap(); + let accounts = [ + ("did:web:tangled.example.com", "Alice & \"friends\"", "https://tangled.org/alice.example.com", true), + ("did:web:bluesky.example.com", "Bob & \"friends\"", "https://bsky.app/profile/bob.example.com", true), + ("did:web:pds.example.com", "Carol & \"friends\"", "https://pdsls.dev/at://did:web:pds.example.com", true), + ("did:web:no-avatar.example.com", "No avatar", "https://tangled.org/no-avatar.example.com", false), + ]; + { + let auth = store(); + let db = auth.db.lock().unwrap(); + for (did, name, url, picture) in accounts { + let id = uuid::Uuid::new_v4().to_string(); + let username = format!("guest-atproto-{}", &mcp::hash(did)[..24]); + let mut profile = json!({"kind":"guest","guestProvider":"atproto","username":username,"enabled":true,"email":null,"emailVerified":false,"firstName":name,"attributes":{"profileUrl":[url]}}); + if picture { + profile["attributes"]["picture"] = json!(["data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+j3ioAAAAASUVORK5CYII="]); + } + db.execute("INSERT INTO users(id,profile) VALUES (?,?)", sql![id,profile.to_string()]).unwrap(); + db.execute("INSERT INTO external_identities VALUES ('atproto',?,?)", sql![did,id]).unwrap(); + } + } + let auth = store(); + let profiles = profiles(&auth).unwrap(); + assert_eq!(profiles.len(), accounts.len()); + let origin = url::Url::parse("https://shale.example.com/home-infra/issues/50").unwrap(); + for (did, name, url, picture) in accounts { + let username = format!("guest-atproto-{}", &mcp::hash(did)[..24]); + let local = format!("/~{username}"); + let html = format!(r#"
  1. ~{username}

    A synthetic issue.

    externalhttprepounknown
    ~{username}

~{username}

"#); + let output = rewrite(&html, &origin, &profiles, &HashMap::new(), None).unwrap(); + let document = Html::parse_document(&output); + for selector in [".n-card__header a", "#profile a"] { + let link = document.select(&Selector::parse(selector).unwrap()).next().unwrap(); + assert_eq!(link.attr("href"), Some(url)); + assert_eq!(link.attr("target"), Some("_blank")); + assert_eq!(link.attr("rel"), Some("noreferrer")); + let style = link.attr("style").unwrap(); + assert!(style.contains("display:inline-flex;align-items:center;gap:.25em")); + assert_eq!(link.children().filter_map(scraper::ElementRef::wrap).map(|e| e.value().name()).collect::>(), if picture || selector.starts_with('.') { vec!["img", "svg", "span"] } else { vec!["svg", "span"] }); + assert_eq!(link.select(&Selector::parse("span[style]").unwrap()).next().unwrap().text().collect::(), name); + let icon = link.select(&Selector::parse("svg").unwrap()).next().unwrap(); + assert_eq!(icon.attr("aria-hidden"), Some("true")); + assert_eq!(icon.attr("focusable"), Some("false")); + assert!(!icon.attr("style").unwrap().contains("margin-right")); + assert_eq!(link.select(&Selector::parse("img").unwrap()).count(), if picture { 1 } else { usize::from(selector.starts_with('.')) }); + if picture { + let avatar = link.select(&Selector::parse("img").unwrap()).next().unwrap(); + let expected = format!("https://snowglobe.example.com/auth/guest/avatar/atproto?subject=did%3Aweb%3A{}", did.strip_prefix("did:web:").unwrap()); + assert_eq!(avatar.attr("src"), Some(expected.as_str())); + assert_eq!(avatar.attr("alt"), Some("")); + assert_eq!(avatar.attr("referrerpolicy"), Some("no-referrer")); + } + } + for (id, href, label) in [("external",format!("https://other.example.com{local}"),"external"),("http",format!("http://shale.example.com{local}"),"http"),("repo",format!("{local}/repo"),"repo"),("unknown","/~guest-atproto-unknown".into(),"unknown")] { + let link = document.select(&Selector::parse(&format!("#{id}")).unwrap()).next().unwrap(); + assert_eq!(link.attr("href"), Some(href.as_str())); + assert_eq!(link.text().collect::(), label); + assert_eq!(link.attr("target"), None); + assert_eq!(link.select(&Selector::parse("svg,img").unwrap()).count(), 0); + } + assert_eq!(document.select(&Selector::parse("pre").unwrap()).next().unwrap().text().collect::(), format!("~{username}")); + assert_eq!(document.select(&Selector::parse("script,tangled,bluesky,pdsls").unwrap()).count(), 0); + assert!(!output.contains("data:image")); + assert!(!output.contains("base64")); + } + drop(auth); + std::fs::remove_dir_all(path).unwrap(); + } + #[test] fn commit_identity_keeps_entities_and_cannot_escape_its_metadata_cell() { let origin = url::Url::parse("https://shale.test/repo/commit/abc").unwrap(); diff --git a/dashboard/tests/atproto-fixture.py b/dashboard/tests/atproto-fixture.py new file mode 100644 index 0000000000000000000000000000000000000000..74820c67226e0582ec622646e5026acdc186a601 --- /dev/null +++ b/dashboard/tests/atproto-fixture.py @@ -0,0 +1,195 @@ +"""Disposable OAuth/PDS fixture. TLS files and listening address are supplied by Rust tests.""" +import base64 +import hashlib +import json +import html +import os +import secrets +import ssl +import subprocess +import sys +import tempfile +import time +from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer +from pathlib import Path +from urllib.parse import parse_qs, urlsplit, urlencode +from urllib.request import urlopen + +ISSUER = os.environ.get('ATPROTO_FIXTURE_ORIGIN', 'https://issuer.example.com') +PDS = os.environ.get('ATPROTO_FIXTURE_ORIGIN', 'https://pds.example.com') +DID = 'did:web:' + (urlsplit(os.environ['ATPROTO_FIXTURE_ORIGIN']).hostname if 'ATPROTO_FIXTURE_ORIGIN' in os.environ else 'alice.example.com') +AVATAR = base64.b64decode('iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAAQklEQVR4nGNIcziOFf3fbYsVkaqeYdSCUQuGgAXUMgiX+lELRi0YChZQyyBc6kctGLVgKFhALYNwqR+1YNSCIWABALbxyFuikyXnAAAAAElFTkSuQmCC') +AVATAR_CID = 'bafkreifixtureavatar' + + +def decode(text): + return base64.urlsafe_b64decode(text + '=' * (-len(text) % 4)) + + +def signature_der(raw): + def integer(value): + value = value.lstrip(b'\0') or b'\0' + if value[0] & 128: + value = b'\0' + value + return b'\x02' + bytes([len(value)]) + value + value = integer(raw[:32]) + integer(raw[32:]) + return b'\x30' + bytes([len(value)]) + value + + +class Fixture(BaseHTTPRequestHandler): + seen = set() + flows = {} + mode = '' + nonce = 'fixture-nonce' + exchanged = False + + def log_message(self, *_): + pass + + def send(self, code, value, nonce=False): + body = json.dumps(value).encode() + self.send_response(code) + self.send_header('Content-Type', 'application/json') + self.send_header('Content-Length', str(len(body))) + if nonce and self.mode != 'missing-nonce': + self.send_header('DPoP-Nonce', self.nonce) + self.end_headers() + self.wfile.write(body) + + def do_GET(self): + path = urlsplit(self.path).path + if path == '/.well-known/atproto-did': + body = DID.encode() + self.send_response(200) + self.send_header('Content-Length', str(len(body))) + self.end_headers() + self.wfile.write(body) + return + if path == '/authorize': + uri = parse_qs(urlsplit(self.path).query).get('request_uri', [''])[0] + if uri not in self.flows: + return self.send(400, {'error': 'expired_request'}) + body = ('Fixture account' + '

Disposable AT Protocol account

This fixture never asks for a password.

' + '
' + ' ' + '
').encode() + self.send_response(200) + self.send_header('Content-Type', 'text/html; charset=utf-8') + self.send_header('Content-Length', str(len(body))) + self.end_headers() + self.wfile.write(body) + return + if path == '/fixture/mode': + type(self).exchanged = False + type(self).mode = parse_qs(urlsplit(self.path).query).get('mode', [''])[0] + return self.send(200, {}) + if path == '/.well-known/did.json': + return self.send(200, {'id': DID, 'alsoKnownAs': ['at://'+urlsplit(PDS).hostname] if 'ATPROTO_FIXTURE_ORIGIN' in os.environ else [], 'service': [{'id': '#atproto_pds', 'type': 'AtprotoPersonalDataServer', 'serviceEndpoint': 'https://127.0.0.1' if self.mode == 'private-pds' else PDS}]}) + if path == '/.well-known/oauth-protected-resource': + return self.send(200, {'resource': PDS, 'authorization_servers': [ISSUER]}) + if path == '/.well-known/oauth-authorization-server': + return self.send(200, {'issuer': 'https://wrong.example.com' if self.mode == 'wrong-metadata-issuer' or (self.mode == 'changed-issuer' and self.exchanged) else ISSUER, + 'authorization_endpoint': ISSUER+'/authorize', 'token_endpoint': ISSUER+'/token'+('?tenant=fixture' if self.mode == 'endpoint-query' else ''), + 'pushed_authorization_request_endpoint': ISSUER+'/par', 'response_types_supported': ['code'], + 'grant_types_supported': ['authorization_code', 'refresh_token'], 'code_challenge_methods_supported': ['S256'], + 'token_endpoint_auth_methods_supported': ['none', 'private_key_jwt'], 'scopes_supported': ['atproto'], + 'dpop_signing_alg_values_supported': ['ES256'], 'require_pushed_authorization_requests': True, + 'authorization_response_iss_parameter_supported': True, 'client_id_metadata_document_supported': True}) + if path == '/xrpc/com.atproto.repo.getRecord': + query = parse_qs(urlsplit(self.path).query) + collection = query['collection'][0] + if self.mode == 'no-profiles' or (collection == 'sh.tangled.actor.profile' and self.mode == 'bluesky-only'): + return self.send(400, {'error': 'RecordNotFound'}) + return self.send(200, {'uri': f'at://{DID}/{collection}/self', 'value': {'$type': collection, 'displayName': 'Fixture Alice', 'avatar': {'$type': 'blob', 'ref': {'$link': AVATAR_CID}, 'mimeType': 'image/png', 'size': len(AVATAR)}}}) + if path == '/xrpc/com.atproto.sync.getBlob': + query = parse_qs(urlsplit(self.path).query) + if query.get('did') != [DID] or query.get('cid') != [AVATAR_CID]: + return self.send(404, {'error': 'BlobNotFound'}) + self.send_response(200) + self.send_header('Content-Type', 'image/png') + self.send_header('Content-Length', str(len(AVATAR))) + self.end_headers() + self.wfile.write(AVATAR) + return + if path == '/redirect': + self.send_response(302) + self.send_header('Location', 'https://127.0.0.1/private') + self.end_headers() + return + if path == '/oversized': + return self.send(200, {'data': 'a'*70000}) + return self.send(404, {'error': 'NotFound'}) + + def do_POST(self): + if self.path == '/decision': + form = parse_qs(self.rfile.read(min(int(self.headers['Content-Length']), 8192)).decode()) + uri = form.get('request_uri', [''])[0] + if uri not in self.flows: + return self.send(400, {'error': 'expired_request'}) + original, _ = self.flows[uri] + query = {'state': original['state'][0], 'iss': ISSUER} + query['code' if form.get('decision') == ['approve'] else 'error'] = uri if form.get('decision') == ['approve'] else 'access_denied' + self.send_response(302) + self.send_header('Location', original['redirect_uri'][0]+'?'+urlencode(query)) + self.end_headers() + return + try: + parts = self.headers['DPoP'].split('.') + header, claims = map(lambda v: json.loads(decode(v)), parts[:2]) + assert header['alg'] == 'ES256' and header['typ'] == 'dpop+jwt' + assert claims['htm'] == 'POST' and claims['htu'] == ISSUER+urlsplit(self.path).path + assert abs(claims['iat'] - time.time()) < 60 and claims['jti'] not in self.seen + self.seen.add(claims['jti']) + jwk = header['jwk'] + assert jwk['crv'] == 'P-256' and jwk['kty'] == 'EC' and 'd' not in jwk + public = bytes.fromhex('3059301306072a8648ce3d020106082a8648ce3d03010703420004') + decode(jwk['x']) + decode(jwk['y']) + with tempfile.TemporaryDirectory() as temp: + Path(temp, 'pub.der').write_bytes(public) + Path(temp, 'sig.der').write_bytes(signature_der(decode(parts[2]))) + checked = subprocess.run(['openssl', 'dgst', '-sha256', '-keyform', 'DER', '-verify', str(Path(temp, 'pub.der')), '-signature', str(Path(temp, 'sig.der'))], input='.'.join(parts[:2]).encode(), capture_output=True) + assert checked.returncode == 0 + form = parse_qs(self.rfile.read(int(self.headers['Content-Length'])).decode()) + expected_nonce = 'rotated-token-nonce' if self.mode == 'rotating-nonce' and self.path == '/token' else 'fixture-nonce' + type(self).nonce = expected_nonce + if claims.get('nonce') != expected_nonce: + return self.send(400, {'error': 'use_dpop_nonce'}, True) + if self.path == '/par': + assert form['scope'] == ['atproto'] and form['code_challenge_method'] == ['S256'] + assert form['login_hint'] == [DID] + if 'ATPROTO_FIXTURE_ORIGIN' in os.environ: + expected = os.environ['ATPROTO_FIXTURE_CLIENT_ORIGIN'].rstrip('/')+'/auth/guest/metadata/atproto' + assert form['client_id'] == [expected] + with urlopen(expected, timeout=10) as response: + metadata = json.load(response) + assert metadata['client_id'] == expected and metadata['dpop_bound_access_tokens'] is True + assert form['redirect_uri'][0] in metadata['redirect_uris'] + if len(self.flows) > 128: + self.flows.clear() + uri = 'urn:ietf:params:oauth:request_uri:'+secrets.token_hex(16) + self.flows[uri] = (form, jwk) + return self.send(201, {'request_uri': uri, 'expires_in': 300}, True) + if urlsplit(self.path).path == '/token': + original, original_jwk = self.flows.pop(form['code'][0]) + assert original_jwk == jwk + expected = base64.urlsafe_b64encode(hashlib.sha256(form['code_verifier'][0].encode()).digest()).decode().rstrip('=') + assert original['code_challenge'] == [expected] + assert original['client_id'] == form['client_id'] and original['redirect_uri'] == form['redirect_uri'] + type(self).exchanged = True + return self.send(200, {'sub': 'did:web:mallory.example.com' if self.mode == 'wrong-subject' else DID, + 'access_token': 'disposable-fixture-token', 'token_type': 'DPoP', 'scope': 'atproto'}, True) + return self.send(404, {}, True) + except Exception: + return self.send(400, {'error': 'fixture_validation_failed'}, True) + + +if __name__ == '__main__': + server = ThreadingHTTPServer((os.environ.get('ATPROTO_FIXTURE_BIND', '127.0.0.1'), int(os.environ.get('ATPROTO_FIXTURE_PORT', '0'))), Fixture) + if len(sys.argv) == 3: + tls = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) + tls.load_cert_chain(sys.argv[1], sys.argv[2]) + server.socket = tls.wrap_socket(server.socket, server_side=True) + elif 'ATPROTO_FIXTURE_ORIGIN' not in os.environ or 'ATPROTO_FIXTURE_CLIENT_ORIGIN' not in os.environ: + raise SystemExit('Configure fixture and client HTTPS origins behind a TLS proxy, or pass a TLS certificate and key.') + print(server.server_port, flush=True) + server.serve_forever() diff --git a/dashboard/web/pages/SignIn.tsx b/dashboard/web/pages/SignIn.tsx index 53cbac9789ec2c92c3372eb17e96b1d703d32569..ca55f6c0aead51840270c4a21397fda6c7e82ae1 100644 --- a/dashboard/web/pages/SignIn.tsx +++ b/dashboard/web/pages/SignIn.tsx @@ -1,6 +1,7 @@ import { createEffect, createResource, createSignal, For, onCleanup, onMount, Show } from "solid-js"; import { authReason, authRequest } from "../auth.ts"; import ArrowLeft from "lucide-solid/icons/arrow-left"; +import atprotoIcon from "../sso/atproto.svg"; import Stone from "lucide-solid/icons/stone"; import githubIcon from "../sso/github.svg"; import theme from "../sso/css/styles.css?inline"; @@ -9,6 +10,7 @@ import adjustments from "./SignIn.css?inline"; export function SignIn() { const params = new URLSearchParams(window.location.search); const setup = params.get("setup"); + const atproto = !setup && params.has("atproto"); const statusQuery = new URLSearchParams(); if (setup) statusQuery.set("setup", setup); if (params.get("flow")) statusQuery.set("flow", params.get("flow")!); @@ -20,7 +22,7 @@ export function SignIn() { const [passwordStep, setPasswordStep] = createSignal(false); const [visible, setVisible] = createSignal(false); const [busy, setBusy] = createSignal(false); - const [error, setError] = createSignal(params.has("guest_error") ? "Guest sign-in wasn't completed. Choose a provider to try again." : ""); + const [error, setError] = createSignal(params.has("guest_error") ? (atproto ? "Sign-in couldn't start. Check your handle or DID and try again." : "Guest sign-in wasn't completed. Choose a provider to try again.") : ""); const [notice, setNotice] = createSignal(""); let conditional: AbortController | undefined; let disposed = false; @@ -50,7 +52,7 @@ export function SignIn() { } finally { if (!automatic || selected) setBusy(false); } }; createEffect(() => { - if (!setup && !passwordStep() && status()) void (async () => { + if (!setup && !atproto && !passwordStep() && status()) void (async () => { if (await PublicKeyCredential.isConditionalMediationAvailable?.() && !disposed && !passwordStep()) await passkey(true); })().catch(() => {}); }); @@ -87,9 +89,21 @@ export function SignIn() {
{notice()}
refetch()}>try again}> + { if (busy()) { event.preventDefault(); return; } setBusy(true); setError(""); }}> + + +
+ + queueMicrotask(() => input.focus())} /> +

Your provider will ask you to approve sign-in.

+
+
+ + }>
@@ -127,6 +141,7 @@ export function SignIn() {
{ event.preventDefault(); void passkey(); }}>sign in with passkey +
diff --git a/dashboard/web/sso/atproto.svg b/dashboard/web/sso/atproto.svg new file mode 100644 index 0000000000000000000000000000000000000000..e834fdff4f6e2dcde2884c0a22c79d7ed584cac1 --- /dev/null +++ b/dashboard/web/sso/atproto.svg @@ -0,0 +1 @@ +@ diff --git a/nixos/dashboard.nix b/nixos/dashboard.nix index e9f66b3a307867ede64b3ab9c1eb68bb09a9af62..da3ea8233fc1b5eef4c0c692a9266bbd84135c11 100644 --- a/nixos/dashboard.nix +++ b/nixos/dashboard.nix @@ -36,12 +36,13 @@ let src = lib.fileset.toSource { root = ../dashboard; fileset = lib.fileset.unions [ - ../dashboard/src ../dashboard/web/sso/github.svg ../dashboard/ai ../dashboard/tests ../dashboard/Cargo.toml ../dashboard/Cargo.lock + ../dashboard/src ../dashboard/web/sso ../dashboard/ai ../dashboard/tests ../dashboard/Cargo.toml ../dashboard/Cargo.lock ../dashboard/agent/install.sh ../dashboard/agent/install.ps1 ]; }; cargoLock.lockFile = ../dashboard/Cargo.lock; nativeBuildInputs = [ pkg-config ]; + nativeCheckInputs = [ python3 openssl ]; buildInputs = [ sqlite openssl ]; LIBSQLITE3_SYS_USE_PKG_CONFIG = "1"; }; diff --git a/service/shale/service.pkl b/service/shale/service.pkl index 556de423d541818c4d70775eb97bfd274856dd95..352daaf30756d5221c6b2c136d9c8ecdcefec114 100644 --- a/service/shale/service.pkl +++ b/service/shale/service.pkl @@ -18,7 +18,7 @@ prepare = "prepare.py" container { memory = 4096 - image = "docker.io/astheno/shale@sha256:b55cc42c4eb2c1e21733405bec795e49ddd7c71fb1755d2aa2ee4361f750cebd" + image = "docker.io/astheno/shale@sha256:4aa7db75ab5e47e9fef68bc88357e2fef014c8cfd8a6fb7e14203f23e8aa80de" http { containerPort = 8000 diff --git a/tools/dashboard-shale-page-test.py b/tools/dashboard-shale-page-test.py index cff199376d419dcc6801bebf9e371396a4cd4485..cb699ed5864c899021e15a62806a453a7094efe2 100644 --- a/tools/dashboard-shale-page-test.py +++ b/tools/dashboard-shale-page-test.py @@ -130,7 +130,9 @@ def main(): profile = {'kind': 'guest', 'enabled': True, 'username': username, 'guestProvider': provider, 'firstName': name} db.execute('INSERT INTO users(id,profile) VALUES (?,?)', [username, json.dumps(profile)]) db.execute('INSERT INTO external_identities VALUES (?,?,?)', [provider, subject, username]) - os.environ.update(SNOWGLOBE_DOMAIN='snowglobe.test', SNOWGLOBE_DASHBOARD_PORT=str(dashboard_port), SNOWGLOBE_PROXY_TOKEN_FILE=str(token), SNOWGLOBE_INTERNAL_PORT=str(internal_port)) + (root / 'stages').mkdir() + (root / 'stages/shale-preview-12345678.json').write_text(json.dumps({'sourceId': 'shale'})) + os.environ.update(SNOWGLOBE_DOMAIN='snowglobe.test', SNOWGLOBE_DASHBOARD_PORT=str(dashboard_port), SNOWGLOBE_PROXY_TOKEN_FILE=str(token), SNOWGLOBE_INTERNAL_PORT=str(internal_port), SNOWGLOBE_STATE=str(root)) router.ROUTE_DIR = str(root / 'routes') Path(router.ROUTE_DIR).mkdir() (Path(router.ROUTE_DIR) / 'shale.json').write_text(json.dumps({'headHtml': {'shale.snowglobe.test': {'/snowbound/*': ''}}})) diff --git a/tools/router-cookie-test.py b/tools/router-cookie-test.py index e69d98ac90c90268d819acf30b85051c4e5e2b28..7875cddcc373cf060df59789313d7a78f92517be 100644 --- a/tools/router-cookie-test.py +++ b/tools/router-cookie-test.py @@ -20,6 +20,7 @@ class Cookies(BaseHTTPRequestHandler): self.send_response(204 if self.path == '/logout' else 200) self.send_header('Set-Cookie', 'session=fixture; HttpOnly; Path=/') self.send_header('Set-Cookie', 'other=fixture; SameSite=Lax') + self.send_header('X-Fixture-Referer', self.headers.get('Referer', '')) self.end_headers() def log_message(self, *_): @@ -32,16 +33,27 @@ def main(): args = parser.parse_args() with tempfile.TemporaryDirectory(prefix='snowglobe-router-cookies-') as temporary, ThreadingHTTPServer(('127.0.0.1', 0), Cookies) as backend: root = Path(temporary) + (root / 'stages').mkdir() + (root / 'stages/shale-preview-1234abcd.json').write_text(json.dumps({'sourceId': 'shale'})) + static = root / 'static' + static.mkdir() + (static / 'nsv.woff2').write_bytes(b'public font fixture') + (static / 'font.css').write_text('@font-face { font-family: fixture; }') + routes = root / 'routes' + routes.mkdir() + for service in ('copyparty', 'copyparty-preview-1234abcd', 'plain'): + (routes / (service + '.json')).write_text(json.dumps({'dirs': {'/.static/': str(static)}})) proof = root / 'proof' proof.write_text('a' * 64) - environment = {**os.environ, 'SNOWGLOBE_DOMAIN': 'snowglobe.test', 'SNOWGLOBE_DASHBOARD_PORT': '3000', + environment = {**os.environ, 'SNOWGLOBE_DOMAIN': 'snowglobe.test', 'SNOWGLOBE_DASHBOARD_PORT': str(backend.server_port), + 'SNOWGLOBE_STATE': str(root), 'SNOWGLOBE_PROXY_TOKEN_FILE': str(proof), 'XDG_DATA_HOME': str(root / 'data'), 'XDG_CONFIG_HOME': str(root / 'config')} environment.pop('SNOWGLOBE_INTERNAL_PORT', None) def nomad(path, _): if path == '/v1/services': - return [{'Namespace': 'default', 'Services': [{'ServiceName': 'knife'}, {'ServiceName': 'plain'}]}] + return [{'Namespace': 'default', 'Services': [{'ServiceName': name} for name in ('knife', 'plain', 'shale', 'shale-preview-1234abcd', 'copyparty', 'copyparty-preview-1234abcd')]}] if path.startswith('/v1/service/'): service = path.rsplit('/', 1)[1] return [{'ServiceName': service, 'JobID': service, 'AllocID': service, 'Address': '127.0.0.1', @@ -63,6 +75,8 @@ def main(): runtime = root / 'Caddyfile' sites = rendered.split('auth.snowglobe.test {', 1)[0] sites = sites.replace('knife.snowglobe.test {', f'http://127.0.0.1:{port} {{').replace('plain.snowglobe.test {', f'http://localhost:{port} {{').replace(' tls internal\n', '') + sites = sites.replace('shale.snowglobe.test {', f'http://shale.localhost:{port} {{').replace('shale-preview-1234abcd.snowglobe.test {', f'http://preview.localhost:{port} {{') + sites = sites.replace('copyparty.snowglobe.test {', f'http://files.localhost:{port} {{').replace('copyparty-preview-1234abcd.snowglobe.test {', f'http://preview-files.localhost:{port} {{') runtime.write_text('{\n admin off\n auto_https off\n}\n' + sites) threading.Thread(target=backend.serve_forever, daemon=True).start() with (root / 'caddy.log').open('w') as log: @@ -91,11 +105,42 @@ def main(): 'other=fixture; SameSite=Lax' + ('; Secure' if host == '127.0.0.1' else '')], cookies response.read() connection.close() + for host in ('files.localhost', 'preview-files.localhost', 'localhost'): + for path in ('/.static/nsv.woff2', '/.static/font.css', '/media/nsv.woff2'): + for method in ('GET', 'HEAD') if path.startswith('/.static/') else ('GET',): + connection = HTTPConnection('127.0.0.1', port, timeout=2) + connection.request(method, path, headers={'Host': f'{host}:{port}', 'Origin': 'https://sign-in.example'}) + response = connection.getresponse() + assert response.status == 200 + expected = '*' if host != 'localhost' and path == '/.static/nsv.woff2' else None + assert response.getheader('Access-Control-Allow-Origin') == expected, (host, path, method) + assert response.getheader('Access-Control-Allow-Credentials') is None + body = response.read() + if path == '/.static/nsv.woff2': + assert body == (b'public font fixture' if method == 'GET' else b'') + connection.close() + for service, host in (('shale', 'shale.localhost'), ('shale-preview-1234abcd', 'preview.localhost')): + origin = f'https://{service}.snowglobe.test/' + for referer in (None, '', '/', 'https://foreign.invalid/', origin[:-1] + '.foreign.invalid/', origin + 'bad%escape', origin, origin + 'home-infra/issues/50?q=is%3Aopen&limit=10'): + for path in ('/-/login', '/-/settings'): + headers = {'Host': f'{host}:{port}', 'Accept': 'application/json'} + if referer is not None: + headers['Referer'] = referer + connection = HTTPConnection('127.0.0.1', port, timeout=2) + connection.request('GET', path, headers=headers) + response = connection.getresponse() + expected = referer or '' + if path == '/-/login' and referer not in (origin, origin + 'home-infra/issues/50?q=is%3Aopen&limit=10'): + expected = origin + assert response.status == 200 + assert response.getheader('X-Fixture-Referer') == expected, (service, path, referer) + response.read() + connection.close() finally: caddy.terminate() caddy.wait(timeout=10) backend.shutdown() - print(json.dumps({'generated_config': 'validated', 'multiple_cookies': 'secure', 'logout_cookies': 'secure', 'default_route': 'unchanged'})) + print(json.dumps({'generated_config': 'validated', 'multiple_cookies': 'secure', 'logout_cookies': 'secure', 'default_route': 'unchanged', 'shale_login_return': 'same-origin URL or home', 'font_cors': 'public Copyparty static fonts only'})) if __name__ == '__main__': diff --git a/tools/router-jellyfin-test.py b/tools/router-jellyfin-test.py index 6c42be794b70c633b06bebbefabab48c4e7a200b..e758f3700540977e7456ef134a71464a296b930d 100644 --- a/tools/router-jellyfin-test.py +++ b/tools/router-jellyfin-test.py @@ -89,8 +89,10 @@ class GatewayAssociation(unittest.TestCase): stages = root / 'stages' stages.mkdir() jellyfin = 'jellyfin-preview-1234abcd' + shale = 'shale-preview-5678abcd' dashboard = 'dashboard-preview-1234abcd' (stages / (jellyfin + '.json')).write_text(json.dumps({'sourceId': 'jellyfin', 'gatewayStage': dashboard})) + (stages / (shale + '.json')).write_text(json.dumps({'sourceId': 'shale', 'gatewayStage': dashboard})) mount = root / 'staging' / dashboard mount.mkdir(parents=True) proxy_proof = mount / 'proxy.token' @@ -103,6 +105,7 @@ class GatewayAssociation(unittest.TestCase): 'jellyfin': (1111, ['caddy-host=jelly.paperclover.net']), 'copyparty': (1112, ['caddy-host=file.paperclover.net']), jellyfin: (2222, ['caddy-host=' + jellyfin + '.paperclover.net', 'caddy-jellyfin-auth=true']), + shale: (6666, ['caddy-host=' + shale + '.paperclover.net']), dashboard: (3333, ['caddy-host=' + dashboard + '.paperclover.net']), } @@ -130,8 +133,23 @@ class GatewayAssociation(unittest.TestCase): dashboard_route = rendered.split(dashboard + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] self.assertIn('request_header Snowglobe-Proxy-Token ' + 'b' * 64, dashboard_route) self.assertNotIn('tracing', preview) + shale_route = rendered.split(shale + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] + self.assertIn('reverse_proxy 127.0.0.1:3333', shale_route) + self.assertIn('Snowglobe-Shale-Upstream "127.0.0.1:6666"', shale_route) + self.assertIn('b' * 64, shale_route) + self.assertNotIn('a' * 64, shale_route) + self.assertNotIn('reverse_proxy 127.0.0.1:4444', shale_route) + (stages / (shale + '.json')).write_text(json.dumps({'sourceId': 'jellyfin', 'gatewayStage': dashboard})) + denied = router.render('fixture').split(shale + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] + self.assertIn('respond 503', denied) + self.assertNotIn('/auth/shale/page', denied) + (stages / (shale + '.json')).write_text(json.dumps({'sourceId': 'shale'})) + unbound = router.render('fixture').split(shale + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] + self.assertIn('reverse_proxy 127.0.0.1:4444', unbound) + self.assertIn('a' * 64, unbound) files = rendered.split('file.paperclover.net {', 1)[1].split('\n}\n', 1)[0] - self.assertNotIn('@zig_mirror', files) + self.assertIn('handle @mirror_private {\n respond 404', files) + self.assertIn('handle @zig_mirror {\n respond 503', files) services['zig-mirror'] = (5555, ['caddy-host=zig.paperclover.net']) enabled = router.render('fixture') files = enabled.split('file.paperclover.net {', 1)[1].split('\n}\n', 1)[0] @@ -219,6 +237,17 @@ class JellyfinRouter(unittest.TestCase): self.assertEqual(Backend.requests, []) self.assertEqual(self.request('GET', '/QuickConnect/Initiate')[0], 401) + def test_legacy_public_route_case_keeps_exact_method_and_path_boundary(self): + for path in ['/users/public', '/branding/configuration', '/qUiCkCoNnEcT/eNaBlEd']: + self.assertEqual(self.request('GET', path)[0], 200) + self.assertEqual(Gateway.requests[-1][1], '/auth/jellyfin/public') + self.assertEqual(self.request('POST', '/quickconnect/initiate', body='{}')[0], 200) + for path in ['/users/public/extra', '/quickconnect/initiate/extra', '/users/private', '/items/known']: + self.assertEqual(self.request('GET', path)[0], 401) + self.assertEqual(self.request('POST', '/users/public', body='{}')[0], 401) + self.assertEqual(self.request('POST', '/qUiCkCoNnEcT/aUtHoRiZe', body='{}')[0], 404) + self.assertEqual(Backend.requests, []) + def test_authenticated_ranges_head_websocket_and_normalized_credential(self): headers = {'Authorization': 'allow', 'Cookie': 'private=never-upstream', 'X-Emby-Token': 'legacy', 'Snowglobe-Jellyfin-Upstream': 'attacker'} status, response_headers, body = self.request('GET', '/Videos/fixture/stream?ApiKey=old&api_key=old&aPiKeY=old&static=true', headers={**headers, 'Range': 'bytes=0-4'}) @@ -230,6 +259,12 @@ class JellyfinRouter(unittest.TestCase): self.assertEqual((method, path), ('GET', '/Videos/fixture/stream?static=true')) self.assertEqual(upstream['Authorization'], 'MediaBrowser Token="normalized"') self.assertFalse(any(k.lower().startswith(('snowglobe-', 'x-snowglobe-')) or k.lower() in {'cookie', 'x-emby-token'} for k in upstream)) + for path, expected in [ + ('/Videos/fixture/stream?ApiKey=old', '/Videos/fixture/stream'), + ('/Videos/fixture/stream?api_key=old&static=true&signature=abc%3D%3D&name=a%26b', '/Videos/fixture/stream?static=true&signature=abc%3D%3D&name=a%26b'), + ]: + self.assertEqual(self.request('GET', path, headers=headers)[0], 200) + self.assertEqual(Backend.requests[-1][1], expected) status, _, body = self.request('HEAD', '/Videos/fixture/stream', headers=headers) self.assertEqual((status, body), (200, b'')) self.assertEqual(Backend.requests[-1][0], 'HEAD') diff --git a/tools/router.py b/tools/router.py index 72831004ba4e38a044b568cbb0e67c72265249d5..a7a14d783d9126ae572ab607a7672d9b72c3d423 100644 --- a/tools/router.py +++ b/tools/router.py @@ -99,6 +99,14 @@ def shale_git_routes(upstreams): return [*lines, " }", " }"] +def shale_login_routes(host): + # Shale parses the login Referer as an absolute URL after OAuth. + return_url = rf"^https://{re.escape(host)}/(?:[A-Za-z0-9._~!$&'()*+,;=:@/?#\[\]-]|%[0-9A-Fa-f]{{2}})*$" + return [" @shale_login_return {", " path /-/login", + f" not header_regexp Referer `{return_url}`", " }", + f" request_header @shale_login_return Referer https://{host}/"] + + def zig_mirror_routes(service, routes): if service == "copyparty": mirror = "zig-mirror" @@ -155,21 +163,33 @@ def dashboard_preview_proof(service): return proof -def jellyfin_gateway(service, routes, proof): - if service == "jellyfin": +def service_gateway(service, routes, proof): + source = service.split("-preview-", 1)[0] + if source not in {"jellyfin", "shale"}: + return None, None + stages = os.path.join(os.environ.get("SNOWGLOBE_STATE", "/var/lib/snowglobe"), "stages") + gateway = None + if service != source: + try: + with open(os.path.join(stages, service + ".json")) as file: + stage = json.load(file) + except FileNotFoundError: + return None, None + if stage.get("sourceId") != source: + return None, None + gateway = stage.get("gatewayStage") + if gateway is None and source != "shale": + return None, None + if gateway is not None and (not isinstance(gateway, str) or not re.fullmatch(r"dashboard-preview-[0-9a-f]{8}", gateway)): + return None, None + if gateway is None: port = int(os.environ["SNOWGLOBE_DASHBOARD_PORT"]) if not 1 <= port <= 65535: - raise ValueError("invalid dashboard port for Jellyfin") + raise ValueError("invalid dashboard port") return f"127.0.0.1:{port}", proof - stages = os.path.join(os.environ.get("SNOWGLOBE_STATE", "/var/lib/snowglobe"), "stages") try: - with open(os.path.join(stages, service + ".json")) as source: - stage = json.load(source) - gateway = stage.get("gatewayStage", "") - if stage.get("sourceId") != "jellyfin" or not isinstance(gateway, str) or not re.fullmatch(r"dashboard-preview-[0-9a-f]{8}", gateway): - return None, None - with open(os.path.join(stages, gateway + ".json")) as source: - if json.load(source).get("sourceId") != "dashboard": + with open(os.path.join(stages, gateway + ".json")) as file: + if json.load(file).get("sourceId") != "dashboard": return None, None except FileNotFoundError: return None, None @@ -178,7 +198,7 @@ def jellyfin_gateway(service, routes, proof): return None, None upstream = next(iter(upstreams)) if not re.fullmatch(r"127\.0\.0\.1:[0-9]+|\[::1\]:[0-9]+", upstream): - raise ValueError("Jellyfin preview gateway must bind loopback") + raise ValueError("preview gateway must bind loopback") proof = dashboard_preview_proof(gateway) return (upstream, proof) if proof else (None, None) @@ -204,7 +224,8 @@ def jellyfin_routes(service, upstreams, gateway, proof): } for method, allowed in paths.items(): name = "@jellyfin_public_" + method.lower() - lines += [f" {name} {{", f" method {method}", f" path {allowed}", " }", + pattern = "(?i)^(?:" + "|".join(re.escape(path) for path in allowed.split()) + ")$" + lines += [f" {name} {{", f" method {method}", f" path_regexp {name[1:]} {pattern}", " }", f" handle {name} {{", f" reverse_proxy {gateway} {{", " rewrite /auth/jellyfin/public?", *[" " + line for line in trusted], " }", " }"] lines += [" @jellyfin_options method OPTIONS", " handle @jellyfin_options {", @@ -216,7 +237,8 @@ def jellyfin_routes(service, upstreams, gateway, proof): " request_header -X-Emby-Token", " request_header -X-MediaBrowser-Token", " request_header -X-Emby-Authorization", " request_header -Cookie", " request_header Authorization {rp.header.X-Snowglobe-Jellyfin-Authorization}", - " rewrite * {rp.header.X-Snowglobe-Jellyfin-Uri}", f" reverse_proxy {upstream} {{", + # Caddy needs a literal query delimiter to replace a query from a response header. + " rewrite * {rp.header.X-Snowglobe-Jellyfin-Uri}?", f" reverse_proxy {upstream} {{", " header_up -Snowglobe-*", " header_up -X-Snowglobe-*", " }", " }", " }", " }", " }", " handle_errors {", " respond 503", " }"] return lines @@ -325,6 +347,7 @@ def render(token): # Keep the Origin guard before every static/proxy handle; # otherwise Caddy sorts those handles ahead of respond. lines += [" route {", *shale_write_routes(host), + *shale_login_routes(host), *shale_git_routes(" ".join(sorted(route["upstreams"])))] if re.fullmatch(r"shale-preview-[0-9a-f]{8}", service): lines += shale_attachment_routes(" ".join(sorted(route["upstreams"])), @@ -342,7 +365,7 @@ def render(token): raise ValueError("invalid dashboard port for Shale linking") lines += shale_mcp_routes(port) if route["jellyfinAuth"]: - gateway, proof = jellyfin_gateway(service, routes, dashboard_proof) + gateway, proof = service_gateway(service, routes, dashboard_proof) if gateway: lines += jellyfin_routes(service, route["upstreams"], gateway, proof) else: @@ -391,8 +414,10 @@ def render(token): for request, path in assets.get("dirs", {}).items(): if not request.startswith("/") or " " in request or "\n" in request: raise ValueError(f"invalid static prefix: {request}") - lines += [f" handle_path {request}* {{", f" root * {json.dumps(path)}", - " file_server", " }"] + lines += [f" handle_path {request}* {{", f" root * {json.dumps(path)}"] + if request == "/.static/" and (service == "copyparty" or re.fullmatch(r"copyparty-preview-[0-9a-f]{8}", service)): + lines += [" @public_font path *.woff2", ' header @public_font Access-Control-Allow-Origin "*"'] + lines += [" file_server", " }"] metrics_path = assets.get("metricsPaths", {}).get(host) if metrics_path: if not re.fullmatch(r"/[A-Za-z0-9/._-]+", metrics_path): @@ -448,8 +473,11 @@ def render(token): shale_page = service == "shale" or re.fullmatch(r"shale-preview-[0-9a-f]{8}", service) page_headers = [] if shale_page: - page_upstream = f"127.0.0.1:{int(os.environ['SNOWGLOBE_DASHBOARD_PORT'])}" - page_headers = [f" request_header Snowglobe-Proxy-Token {dashboard_proof}", + page_upstream, proof = service_gateway(service, routes, dashboard_proof) + if not page_upstream: + lines += [" respond 503", " }", "}"] + continue + page_headers = [f" request_header Snowglobe-Proxy-Token {proof}", f" request_header Snowglobe-Shale-Upstream {json.dumps(sorted(route['upstreams'])[0])}", " request_header Snowglobe-Shale-Uri {uri}", " rewrite * /auth/shale/page"]