| author | |
| committer | |
| log | a061380e5790ff217adfe1d0ca9dd0426d126739 |
| tree | adef8534a8f4a282f3234ab7fd34a9bda4f10e8e |
| parent | 41a098feadb154575407c9c8fc162ca22bc0caf5 |
| signature | Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU |
Allow Shale guests to authenticate through their AT Protocol PDS with PKCE, PAR and DPoP. Resolve public profile names and avatars, center provider marks beside author names, and retain trusted preview gateway routing. Update Shale to r1821.
Verified web check/build, 57 Rust tests, actual Linux Nix build and Caddy proxy integration, routing tests, and staged author spacing.
Assisted-by: gpt-6.1-sol15 files changed, 1558 insertions(+), 37 deletions(-)
dashboard/Cargo.lock+164| ... | @@ -336,6 +336,36 @@ dependencies = [ | ... | @@ -336,6 +336,36 @@ dependencies = [ |
| 336 | "libc", | 336 | "libc", |
| 337 | ] | 337 | ] |
| 338 | 338 | ||
| 339 | [[package]] | ||
| 340 | name = "critical-section" | ||
| 341 | version = "1.2.0" | ||
| 342 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 343 | checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" | ||
| 344 | |||
| 345 | [[package]] | ||
| 346 | name = "crossbeam-channel" | ||
| 347 | version = "0.5.17" | ||
| 348 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 349 | checksum = "98b0cc327b5bc766e7fda9c9260cc0fa81b43a8e240440422dff70788e3f9ef1" | ||
| 350 | dependencies = [ | ||
| 351 | "crossbeam-utils", | ||
| 352 | ] | ||
| 353 | |||
| 354 | [[package]] | ||
| 355 | name = "crossbeam-epoch" | ||
| 356 | version = "0.9.21" | ||
| 357 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 358 | checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d" | ||
| 359 | dependencies = [ | ||
| 360 | "crossbeam-utils", | ||
| 361 | ] | ||
| 362 | |||
| 363 | [[package]] | ||
| 364 | name = "crossbeam-utils" | ||
| 365 | version = "0.8.23" | ||
| 366 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 367 | checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" | ||
| 368 | |||
| 339 | [[package]] | 369 | [[package]] |
| 340 | name = "crunchy" | 370 | name = "crunchy" |
| 341 | version = "0.2.4" | 371 | version = "0.2.4" |
| ... | @@ -564,6 +594,18 @@ dependencies = [ | ... | @@ -564,6 +594,18 @@ dependencies = [ |
| 564 | "simdutf8", | 594 | "simdutf8", |
| 565 | ] | 595 | ] |
| 566 | 596 | ||
| 597 | [[package]] | ||
| 598 | name = "enum-as-inner" | ||
| 599 | version = "0.6.1" | ||
| 600 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 601 | checksum = "a1e6a265c649f3f5979b601d26f1d05ada116434c87741c9493cb56218f76cbc" | ||
| 602 | dependencies = [ | ||
| 603 | "heck", | ||
| 604 | "proc-macro2", | ||
| 605 | "quote", | ||
| 606 | "syn 2.0.119", | ||
| 607 | ] | ||
| 608 | |||
| 567 | [[package]] | 609 | [[package]] |
| 568 | name = "equivalent" | 610 | name = "equivalent" |
| 569 | version = "1.0.2" | 611 | version = "1.0.2" |
| ... | @@ -852,12 +894,64 @@ dependencies = [ | ... | @@ -852,12 +894,64 @@ dependencies = [ |
| 852 | "hashbrown 0.15.5", | 894 | "hashbrown 0.15.5", |
| 853 | ] | 895 | ] |
| 854 | 896 | ||
| 897 | [[package]] | ||
| 898 | name = "heck" | ||
| 899 | version = "0.5.0" | ||
| 900 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 901 | checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" | ||
| 902 | |||
| 855 | [[package]] | 903 | [[package]] |
| 856 | name = "hex" | 904 | name = "hex" |
| 857 | version = "0.4.3" | 905 | version = "0.4.3" |
| 858 | source = "registry+https://github.com/rust-lang/crates.io-index" | 906 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 859 | checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" | 907 | checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" |
| 860 | 908 | ||
| 909 | [[package]] | ||
| 910 | name = "hickory-proto" | ||
| 911 | version = "0.25.2" | ||
| 912 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 913 | checksum = "f8a6fe56c0038198998a6f217ca4e7ef3a5e51f46163bd6dd60b5c71ca6c6502" | ||
| 914 | dependencies = [ | ||
| 915 | "async-trait", | ||
| 916 | "cfg-if", | ||
| 917 | "data-encoding", | ||
| 918 | "enum-as-inner", | ||
| 919 | "futures-channel", | ||
| 920 | "futures-io", | ||
| 921 | "futures-util", | ||
| 922 | "idna", | ||
| 923 | "ipnet", | ||
| 924 | "once_cell", | ||
| 925 | "rand 0.9.5", | ||
| 926 | "ring", | ||
| 927 | "thiserror 2.0.21", | ||
| 928 | "tinyvec", | ||
| 929 | "tokio", | ||
| 930 | "tracing", | ||
| 931 | "url", | ||
| 932 | ] | ||
| 933 | |||
| 934 | [[package]] | ||
| 935 | name = "hickory-resolver" | ||
| 936 | version = "0.25.2" | ||
| 937 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 938 | checksum = "dc62a9a99b0bfb44d2ab95a7208ac952d31060efc16241c87eaf36406fecf87a" | ||
| 939 | dependencies = [ | ||
| 940 | "cfg-if", | ||
| 941 | "futures-util", | ||
| 942 | "hickory-proto", | ||
| 943 | "ipconfig", | ||
| 944 | "moka", | ||
| 945 | "once_cell", | ||
| 946 | "parking_lot", | ||
| 947 | "rand 0.9.5", | ||
| 948 | "resolv-conf", | ||
| 949 | "smallvec", | ||
| 950 | "thiserror 2.0.21", | ||
| 951 | "tokio", | ||
| 952 | "tracing", | ||
| 953 | ] | ||
| 954 | |||
| 861 | [[package]] | 955 | [[package]] |
| 862 | name = "hmac" | 956 | name = "hmac" |
| 863 | version = "0.12.1" | 957 | version = "0.12.1" |
| ... | @@ -878,6 +972,7 @@ dependencies = [ | ... | @@ -878,6 +972,7 @@ dependencies = [ |
| 878 | "chrono", | 972 | "chrono", |
| 879 | "futures", | 973 | "futures", |
| 880 | "globset", | 974 | "globset", |
| 975 | "hickory-resolver", | ||
| 881 | "lol_html", | 976 | "lol_html", |
| 882 | "openssl", | 977 | "openssl", |
| 883 | "percent-encoding", | 978 | "percent-encoding", |
| ... | @@ -1174,6 +1269,19 @@ dependencies = [ | ... | @@ -1174,6 +1269,19 @@ dependencies = [ |
| 1174 | "generic-array", | 1269 | "generic-array", |
| 1175 | ] | 1270 | ] |
| 1176 | 1271 | ||
| 1272 | [[package]] | ||
| 1273 | name = "ipconfig" | ||
| 1274 | version = "0.3.4" | ||
| 1275 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 1276 | checksum = "4d40460c0ce33d6ce4b0630ad68ff63d6661961c48b6dba35e5a4d81cfb48222" | ||
| 1277 | dependencies = [ | ||
| 1278 | "socket2", | ||
| 1279 | "widestring", | ||
| 1280 | "windows-registry", | ||
| 1281 | "windows-result", | ||
| 1282 | "windows-sys 0.61.2", | ||
| 1283 | ] | ||
| 1284 | |||
| 1177 | [[package]] | 1285 | [[package]] |
| 1178 | name = "ipnet" | 1286 | name = "ipnet" |
| 1179 | version = "2.12.2" | 1287 | version = "2.12.2" |
| ... | @@ -1331,6 +1439,23 @@ dependencies = [ | ... | @@ -1331,6 +1439,23 @@ dependencies = [ |
| 1331 | "windows-sys 0.61.2", | 1439 | "windows-sys 0.61.2", |
| 1332 | ] | 1440 | ] |
| 1333 | 1441 | ||
| 1442 | [[package]] | ||
| 1443 | name = "moka" | ||
| 1444 | version = "0.12.16" | ||
| 1445 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 1446 | checksum = "4293f18e7567a1caf3c584855554377025c65e0aa445344d04171f5ad63d19b9" | ||
| 1447 | dependencies = [ | ||
| 1448 | "crossbeam-channel", | ||
| 1449 | "crossbeam-epoch", | ||
| 1450 | "crossbeam-utils", | ||
| 1451 | "equivalent", | ||
| 1452 | "parking_lot", | ||
| 1453 | "portable-atomic", | ||
| 1454 | "smallvec", | ||
| 1455 | "tagptr", | ||
| 1456 | "uuid", | ||
| 1457 | ] | ||
| 1458 | |||
| 1334 | [[package]] | 1459 | [[package]] |
| 1335 | name = "multer" | 1460 | name = "multer" |
| 1336 | version = "3.1.0" | 1461 | version = "3.1.0" |
| ... | @@ -1445,6 +1570,10 @@ name = "once_cell" | ... | @@ -1445,6 +1570,10 @@ name = "once_cell" |
| 1445 | version = "1.21.4" | 1570 | version = "1.21.4" |
| 1446 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1571 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 1447 | checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" | 1572 | checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" |
| 1573 | dependencies = [ | ||
| 1574 | "critical-section", | ||
| 1575 | "portable-atomic", | ||
| 1576 | ] | ||
| 1448 | 1577 | ||
| 1449 | [[package]] | 1578 | [[package]] |
| 1450 | name = "openssl" | 1579 | name = "openssl" |
| ... | @@ -1668,6 +1797,12 @@ version = "0.3.34" | ... | @@ -1668,6 +1797,12 @@ version = "0.3.34" |
| 1668 | source = "registry+https://github.com/rust-lang/crates.io-index" | 1797 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 1669 | checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" | 1798 | checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" |
| 1670 | 1799 | ||
| 1800 | [[package]] | ||
| 1801 | name = "portable-atomic" | ||
| 1802 | version = "1.15.0" | ||
| 1803 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 1804 | checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85" | ||
| 1805 | |||
| 1671 | [[package]] | 1806 | [[package]] |
| 1672 | name = "potential_utf" | 1807 | name = "potential_utf" |
| 1673 | version = "0.1.6" | 1808 | version = "0.1.6" |
| ... | @@ -1977,6 +2112,12 @@ dependencies = [ | ... | @@ -1977,6 +2112,12 @@ dependencies = [ |
| 1977 | "webpki-roots", | 2112 | "webpki-roots", |
| 1978 | ] | 2113 | ] |
| 1979 | 2114 | ||
| 2115 | [[package]] | ||
| 2116 | name = "resolv-conf" | ||
| 2117 | version = "0.7.6" | ||
| 2118 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 2119 | checksum = "1e061d1b48cb8d38042de4ae0a7a6401009d6143dc80d2e2d6f31f0bdd6470c7" | ||
| 2120 | |||
| 1980 | [[package]] | 2121 | [[package]] |
| 1981 | name = "rfc6979" | 2122 | name = "rfc6979" |
| 1982 | version = "0.4.0" | 2123 | version = "0.4.0" |
| ... | @@ -2587,6 +2728,12 @@ dependencies = [ | ... | @@ -2587,6 +2728,12 @@ dependencies = [ |
| 2587 | "syn 3.0.6", | 2728 | "syn 3.0.6", |
| 2588 | ] | 2729 | ] |
| 2589 | 2730 | ||
| 2731 | [[package]] | ||
| 2732 | name = "tagptr" | ||
| 2733 | version = "0.2.0" | ||
| 2734 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 2735 | checksum = "7b2093cf4c8eb1e67749a6762251bc9cd836b6fc171623bd0a9d324d37af2417" | ||
| 2736 | |||
| 2590 | [[package]] | 2737 | [[package]] |
| 2591 | name = "tendril" | 2738 | name = "tendril" |
| 2592 | version = "0.5.1" | 2739 | version = "0.5.1" |
| ... | @@ -3146,6 +3293,12 @@ dependencies = [ | ... | @@ -3146,6 +3293,12 @@ dependencies = [ |
| 3146 | "rustls-pki-types", | 3293 | "rustls-pki-types", |
| 3147 | ] | 3294 | ] |
| 3148 | 3295 | ||
| 3296 | [[package]] | ||
| 3297 | name = "widestring" | ||
| 3298 | version = "1.2.1" | ||
| 3299 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 3300 | checksum = "72069c3113ab32ab29e5584db3c6ec55d416895e60715417b5b883a357c3e471" | ||
| 3301 | |||
| 3149 | [[package]] | 3302 | [[package]] |
| 3150 | name = "winapi-util" | 3303 | name = "winapi-util" |
| 3151 | version = "0.1.11" | 3304 | version = "0.1.11" |
| ... | @@ -3196,6 +3349,17 @@ version = "0.2.1" | ... | @@ -3196,6 +3349,17 @@ version = "0.2.1" |
| 3196 | source = "registry+https://github.com/rust-lang/crates.io-index" | 3349 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 3197 | checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" | 3350 | checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" |
| 3198 | 3351 | ||
| 3352 | [[package]] | ||
| 3353 | name = "windows-registry" | ||
| 3354 | version = "0.6.1" | ||
| 3355 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 3356 | checksum = "02752bf7fbdcce7f2a27a742f798510f3e5ad88dbe84871e5168e2120c3d5720" | ||
| 3357 | dependencies = [ | ||
| 3358 | "windows-link", | ||
| 3359 | "windows-result", | ||
| 3360 | "windows-strings", | ||
| 3361 | ] | ||
| 3362 | |||
| 3199 | [[package]] | 3363 | [[package]] |
| 3200 | name = "windows-result" | 3364 | name = "windows-result" |
| 3201 | version = "0.4.1" | 3365 | version = "0.4.1" |
dashboard/Cargo.toml+1| ... | @@ -11,6 +11,7 @@ bytes = "1" | ... | @@ -11,6 +11,7 @@ bytes = "1" |
| 11 | chrono = "0.4" | 11 | chrono = "0.4" |
| 12 | futures = "0.3" | 12 | futures = "0.3" |
| 13 | globset = "0.4" | 13 | globset = "0.4" |
| 14 | hickory-resolver = "0.25" | ||
| 14 | openssl = "0.10" | 15 | openssl = "0.10" |
| 15 | percent-encoding = "2" | 16 | percent-encoding = "2" |
| 16 | rand = "0.9" | 17 | rand = "0.9" |
dashboard/src/atproto.rs created+813| ... | @@ -0,0 +1,813 @@ | ||
| 1 | use crate::*; | ||
| 2 | use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD}; | ||
| 3 | use openssl::{ | ||
| 4 | bn::{BigNum, BigNumContext}, | ||
| 5 | ec::{EcGroup, EcKey}, | ||
| 6 | ecdsa::EcdsaSig, | ||
| 7 | nid::Nid, | ||
| 8 | }; | ||
| 9 | use sha2::{Digest, Sha256}; | ||
| 10 | use std::net::{IpAddr, SocketAddr}; | ||
| 11 | use url::Url; | ||
| 12 | |||
| 13 | fn reject() -> Error { | ||
| 14 | Error::new( | ||
| 15 | 502, | ||
| 16 | "Your AT Protocol provider couldn't verify sign-in. Check your handle and try again.", | ||
| 17 | ) | ||
| 18 | } | ||
| 19 | |||
| 20 | fn public_ip(ip: IpAddr) -> bool { | ||
| 21 | match ip { | ||
| 22 | IpAddr::V4(ip) => { | ||
| 23 | let [a, b, c, _] = ip.octets(); | ||
| 24 | !(a == 0 | ||
| 25 | || a == 10 | ||
| 26 | || a == 127 | ||
| 27 | || a >= 224 | ||
| 28 | || (a == 100 && (64..=127).contains(&b)) | ||
| 29 | || (a == 169 && b == 254) | ||
| 30 | || (a == 172 && (16..=31).contains(&b)) | ||
| 31 | || (a == 192 | ||
| 32 | && ((b == 0 && (c == 0 || c == 2)) || b == 168 || (b == 88 && c == 99))) | ||
| 33 | || (a == 198 && (b == 18 || b == 19 || (b == 51 && c == 100))) | ||
| 34 | || (a == 203 && b == 0 && c == 113)) | ||
| 35 | } | ||
| 36 | IpAddr::V6(ip) => { | ||
| 37 | let s = ip.segments(); | ||
| 38 | // Only global unicast; exclude transition mechanisms and special-purpose ranges. | ||
| 39 | s[0] & 0xe000 == 0x2000 | ||
| 40 | && !(s[0] == 0x2001 && s[1] < 0x200) | ||
| 41 | && !(s[0] == 0x2001 && s[1] == 0xdb8) | ||
| 42 | && s[0] != 0x2002 | ||
| 43 | && !(s[0] == 0x3fff && s[1] < 0x1000) | ||
| 44 | } | ||
| 45 | } | ||
| 46 | } | ||
| 47 | |||
| 48 | fn https(value: &str) -> Result<Url> { | ||
| 49 | let url = Url::parse(value).map_err(|_| reject())?; | ||
| 50 | if value.len() > 2048 | ||
| 51 | || url.scheme() != "https" | ||
| 52 | || !url.username().is_empty() | ||
| 53 | || url.password().is_some() | ||
| 54 | || url.fragment().is_some() | ||
| 55 | || url.host_str().is_none() | ||
| 56 | || value.chars().any(|c| c.is_control() || c == '\\') | ||
| 57 | { | ||
| 58 | return Err(reject()); | ||
| 59 | } | ||
| 60 | match url.host() { | ||
| 61 | Some(url::Host::Domain(host)) if valid_handle(host) => (), | ||
| 62 | Some(url::Host::Ipv4(ip)) if public_ip(ip.into()) => (), | ||
| 63 | Some(url::Host::Ipv6(ip)) if public_ip(ip.into()) => (), | ||
| 64 | _ => return Err(reject()), | ||
| 65 | } | ||
| 66 | Ok(url) | ||
| 67 | } | ||
| 68 | |||
| 69 | fn origin(value: &str) -> Result<Url> { | ||
| 70 | let url = https(value)?; | ||
| 71 | if url.path() != "/" || url.query().is_some() { | ||
| 72 | return Err(reject()); | ||
| 73 | } | ||
| 74 | Ok(url) | ||
| 75 | } | ||
| 76 | |||
| 77 | fn valid_handle(value: &str) -> bool { | ||
| 78 | let labels: Vec<_> = value.split('.').collect(); | ||
| 79 | value.len() <= 253 | ||
| 80 | && labels.len() >= 2 | ||
| 81 | && labels.iter().all(|label| { | ||
| 82 | !label.is_empty() | ||
| 83 | && label.len() <= 63 | ||
| 84 | && !label.starts_with('-') | ||
| 85 | && !label.ends_with('-') | ||
| 86 | && label | ||
| 87 | .bytes() | ||
| 88 | .all(|b| b.is_ascii_alphanumeric() || b == b'-') | ||
| 89 | }) | ||
| 90 | && labels.last().is_some_and(|last| { | ||
| 91 | last.as_bytes()[0].is_ascii_alphabetic() | ||
| 92 | && ![ | ||
| 93 | "local", | ||
| 94 | "localhost", | ||
| 95 | "internal", | ||
| 96 | "arpa", | ||
| 97 | "invalid", | ||
| 98 | "test", | ||
| 99 | "onion", | ||
| 100 | ] | ||
| 101 | .contains(last) | ||
| 102 | }) | ||
| 103 | } | ||
| 104 | |||
| 105 | /// Resolve once, validate every address, and pin the connection to prevent DNS rebinding. | ||
| 106 | async fn client(url: &Url) -> Result<reqwest::Client> { | ||
| 107 | let host = url.host_str().ok_or_else(reject)?; | ||
| 108 | #[cfg(test)] | ||
| 109 | if matches!( | ||
| 110 | host, | ||
| 111 | "alice.example.com" | "pds.example.com" | "issuer.example.com" | ||
| 112 | ) { | ||
| 113 | if let Ok(client) = tests::HTTP.try_with(Clone::clone) { | ||
| 114 | return Ok(client); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | let addresses: Vec<SocketAddr> = tokio::time::timeout( | ||
| 118 | Duration::from_secs(5), | ||
| 119 | tokio::net::lookup_host(( | ||
| 120 | host.trim_matches(['[', ']']), | ||
| 121 | url.port_or_known_default().ok_or_else(reject)?, | ||
| 122 | )), | ||
| 123 | ) | ||
| 124 | .await | ||
| 125 | .map_err(|_| reject())? | ||
| 126 | .map_err(|_| reject())? | ||
| 127 | .collect(); | ||
| 128 | if addresses.is_empty() || addresses.iter().any(|address| !public_ip(address.ip())) { | ||
| 129 | return Err(reject()); | ||
| 130 | } | ||
| 131 | Ok(reqwest::Client::builder() | ||
| 132 | .no_proxy() | ||
| 133 | .redirect(reqwest::redirect::Policy::none()) | ||
| 134 | .timeout(Duration::from_secs(10)) | ||
| 135 | .connect_timeout(Duration::from_secs(5)) | ||
| 136 | .resolve_to_addrs(host, &addresses) | ||
| 137 | .user_agent("Snow Globe AT Protocol sign-in") | ||
| 138 | .build()?) | ||
| 139 | } | ||
| 140 | |||
| 141 | async fn bytes(mut response: reqwest::Response, limit: usize) -> Result<Vec<u8>> { | ||
| 142 | if response.content_length().is_some_and(|n| n > limit as u64) { | ||
| 143 | return Err(reject()); | ||
| 144 | } | ||
| 145 | let mut data = Vec::new(); | ||
| 146 | while let Some(chunk) = response.chunk().await.map_err(|_| reject())? { | ||
| 147 | if data.len() + chunk.len() > limit { | ||
| 148 | return Err(reject()); | ||
| 149 | } | ||
| 150 | data.extend_from_slice(&chunk); | ||
| 151 | } | ||
| 152 | Ok(data) | ||
| 153 | } | ||
| 154 | |||
| 155 | async fn get(url: Url) -> Result<Value> { | ||
| 156 | let response = client(&url) | ||
| 157 | .await? | ||
| 158 | .get(url) | ||
| 159 | .header("accept", "application/json") | ||
| 160 | .send() | ||
| 161 | .await | ||
| 162 | .map_err(|_| reject())?; | ||
| 163 | if response.status() != StatusCode::OK | ||
| 164 | || response | ||
| 165 | .headers() | ||
| 166 | .get("content-type") | ||
| 167 | .and_then(|v| v.to_str().ok()) | ||
| 168 | .is_none_or(|v| v.split(';').next() != Some("application/json")) | ||
| 169 | { | ||
| 170 | return Err(reject()); | ||
| 171 | } | ||
| 172 | serde_json::from_slice(&bytes(response, 65536).await?).map_err(|_| reject()) | ||
| 173 | } | ||
| 174 | |||
| 175 | async fn handle_did(handle: &str) -> Result<String> { | ||
| 176 | if !valid_handle(handle) { | ||
| 177 | return Err(reject()); | ||
| 178 | } | ||
| 179 | let resolver = hickory_resolver::Resolver::builder_tokio() | ||
| 180 | .map_err(|_| reject())? | ||
| 181 | .build(); | ||
| 182 | if let Ok(Ok(records)) = tokio::time::timeout( | ||
| 183 | Duration::from_secs(5), | ||
| 184 | resolver.txt_lookup(format!("_atproto.{handle}.")), | ||
| 185 | ) | ||
| 186 | .await | ||
| 187 | { | ||
| 188 | let dids: Vec<_> = records | ||
| 189 | .iter() | ||
| 190 | .filter_map(|record| { | ||
| 191 | let text = record | ||
| 192 | .txt_data() | ||
| 193 | .iter() | ||
| 194 | .flat_map(|part| part.iter().copied()) | ||
| 195 | .collect::<Vec<_>>(); | ||
| 196 | String::from_utf8(text) | ||
| 197 | .ok() | ||
| 198 | .and_then(|s| s.strip_prefix("did=").map(str::to_owned)) | ||
| 199 | }) | ||
| 200 | .collect(); | ||
| 201 | if dids.len() > 1 { | ||
| 202 | return Err(reject()); | ||
| 203 | } | ||
| 204 | if let Some(did) = dids.into_iter().next() { | ||
| 205 | did_url(&did)?; | ||
| 206 | return Ok(did); | ||
| 207 | } | ||
| 208 | } | ||
| 209 | let url = https(&format!("https://{handle}/.well-known/atproto-did"))?; | ||
| 210 | let response = client(&url) | ||
| 211 | .await? | ||
| 212 | .get(url) | ||
| 213 | .send() | ||
| 214 | .await | ||
| 215 | .map_err(|_| reject())?; | ||
| 216 | if response.status() != StatusCode::OK { | ||
| 217 | return Err(reject()); | ||
| 218 | } | ||
| 219 | let did = String::from_utf8(bytes(response, 65536).await?) | ||
| 220 | .map_err(|_| reject())? | ||
| 221 | .trim() | ||
| 222 | .to_owned(); | ||
| 223 | did_url(&did)?; | ||
| 224 | Ok(did) | ||
| 225 | } | ||
| 226 | |||
| 227 | fn did_url(did: &str) -> Result<Url> { | ||
| 228 | if let Some(id) = did.strip_prefix("did:plc:") { | ||
| 229 | if id.len() == 24 | ||
| 230 | && id | ||
| 231 | .bytes() | ||
| 232 | .all(|b| b.is_ascii_lowercase() || (b'2'..=b'7').contains(&b)) | ||
| 233 | { | ||
| 234 | return https(&format!("https://plc.directory/{did}")); | ||
| 235 | } | ||
| 236 | } | ||
| 237 | if let Some(host) = did | ||
| 238 | .strip_prefix("did:web:") | ||
| 239 | .filter(|host| valid_handle(host)) | ||
| 240 | { | ||
| 241 | return https(&format!("https://{host}/.well-known/did.json")); | ||
| 242 | } | ||
| 243 | Err(reject()) | ||
| 244 | } | ||
| 245 | |||
| 246 | async fn identity(did: &str) -> Result<(Url, Option<String>)> { | ||
| 247 | let doc = get(did_url(did)?).await?; | ||
| 248 | if doc["id"] != did { | ||
| 249 | return Err(reject()); | ||
| 250 | } | ||
| 251 | let pds = array(&doc["service"]) | ||
| 252 | .iter() | ||
| 253 | .find(|service| { | ||
| 254 | (service["id"] == "#atproto_pds" || service["id"] == format!("{did}#atproto_pds")) | ||
| 255 | && service["type"] == "AtprotoPersonalDataServer" | ||
| 256 | }) | ||
| 257 | .ok_or_else(reject)?; | ||
| 258 | let pds = origin(string(&pds["serviceEndpoint"]))?; | ||
| 259 | let claimed = array(&doc["alsoKnownAs"]) | ||
| 260 | .iter() | ||
| 261 | .filter_map(Value::as_str) | ||
| 262 | .filter_map(|s| s.strip_prefix("at://")) | ||
| 263 | .find(|s| valid_handle(s)); | ||
| 264 | let handle = match claimed { | ||
| 265 | Some(handle) | ||
| 266 | if handle_did(handle) | ||
| 267 | .await | ||
| 268 | .is_ok_and(|resolved| resolved == did) => | ||
| 269 | { | ||
| 270 | Some(handle.to_owned()) | ||
| 271 | } | ||
| 272 | _ => None, | ||
| 273 | }; | ||
| 274 | Ok((pds, handle)) | ||
| 275 | } | ||
| 276 | |||
| 277 | async fn server(pds: &Url) -> Result<Value> { | ||
| 278 | let resource = get(pds.join(".well-known/oauth-protected-resource")?).await?; | ||
| 279 | if resource["resource"] | ||
| 280 | .as_str() | ||
| 281 | .and_then(|v| origin(v).ok()) | ||
| 282 | .as_ref() | ||
| 283 | != Some(pds) | ||
| 284 | { | ||
| 285 | return Err(reject()); | ||
| 286 | } | ||
| 287 | let issuers = array(&resource["authorization_servers"]); | ||
| 288 | if issuers.len() != 1 { | ||
| 289 | return Err(reject()); | ||
| 290 | } | ||
| 291 | let issuer = string(&issuers[0]); | ||
| 292 | let base = origin(issuer)?; | ||
| 293 | let metadata = get(base.join(".well-known/oauth-authorization-server")?).await?; | ||
| 294 | validate_server(&metadata, issuer)?; | ||
| 295 | Ok(metadata) | ||
| 296 | } | ||
| 297 | |||
| 298 | fn validate_server(metadata: &Value, issuer: &str) -> Result<()> { | ||
| 299 | origin(issuer)?; | ||
| 300 | if metadata["issuer"] != issuer | ||
| 301 | || metadata["authorization_response_iss_parameter_supported"] != true | ||
| 302 | || metadata["require_pushed_authorization_requests"] != true | ||
| 303 | || metadata["require_request_uri_registration"] == false | ||
| 304 | || metadata["client_id_metadata_document_supported"] != true | ||
| 305 | { | ||
| 306 | return Err(reject()); | ||
| 307 | } | ||
| 308 | for (field, value) in [ | ||
| 309 | ("response_types_supported", "code"), | ||
| 310 | ("grant_types_supported", "authorization_code"), | ||
| 311 | ("grant_types_supported", "refresh_token"), | ||
| 312 | ("code_challenge_methods_supported", "S256"), | ||
| 313 | ("token_endpoint_auth_methods_supported", "none"), | ||
| 314 | ("scopes_supported", "atproto"), | ||
| 315 | ("dpop_signing_alg_values_supported", "ES256"), | ||
| 316 | ] { | ||
| 317 | if !array(&metadata[field]).iter().any(|item| item == value) { | ||
| 318 | return Err(reject()); | ||
| 319 | } | ||
| 320 | } | ||
| 321 | for field in [ | ||
| 322 | "authorization_endpoint", | ||
| 323 | "token_endpoint", | ||
| 324 | "pushed_authorization_request_endpoint", | ||
| 325 | ] { | ||
| 326 | https(string(&metadata[field]))?; | ||
| 327 | } | ||
| 328 | Ok(()) | ||
| 329 | } | ||
| 330 | |||
| 331 | pub fn metadata(auth: &auth::Store) -> Value { | ||
| 332 | json!({"client_id":format!("{}auth/guest/metadata/atproto",auth.origin),"client_name":"Shale", | ||
| 333 | "client_uri":auth.origin,"application_type":"web","scope":"atproto", | ||
| 334 | "grant_types":["authorization_code"],"response_types":["code"], | ||
| 335 | "redirect_uris":[format!("{}auth/guest/callback/atproto",auth.origin)], | ||
| 336 | "token_endpoint_auth_method":"none","dpop_bound_access_tokens":true}) | ||
| 337 | } | ||
| 338 | |||
| 339 | fn proof( | ||
| 340 | key: &EcKey<openssl::pkey::Private>, | ||
| 341 | endpoint: &Url, | ||
| 342 | nonce: Option<&str>, | ||
| 343 | ) -> Result<String> { | ||
| 344 | let mut x = BigNum::new()?; | ||
| 345 | let mut y = BigNum::new()?; | ||
| 346 | let mut context = BigNumContext::new()?; | ||
| 347 | key.public_key() | ||
| 348 | .affine_coordinates_gfp(key.group(), &mut x, &mut y, &mut context)?; | ||
| 349 | let header = json!({"typ":"dpop+jwt","alg":"ES256","jwk":{"kty":"EC","crv":"P-256", | ||
| 350 | "x":URL_SAFE_NO_PAD.encode(x.to_vec_padded(32)?),"y":URL_SAFE_NO_PAD.encode(y.to_vec_padded(32)?)}}); | ||
| 351 | let mut target = endpoint.clone(); | ||
| 352 | target.set_query(None); | ||
| 353 | let mut claims = | ||
| 354 | json!({"jti":mcp::secret(),"htm":"POST","htu":target.as_str(),"iat":now() as i64}); | ||
| 355 | if let Some(nonce) = nonce { | ||
| 356 | claims["nonce"] = json!(nonce); | ||
| 357 | } | ||
| 358 | let payload = format!( | ||
| 359 | "{}.{}", | ||
| 360 | URL_SAFE_NO_PAD.encode(header.to_string()), | ||
| 361 | URL_SAFE_NO_PAD.encode(claims.to_string()) | ||
| 362 | ); | ||
| 363 | let signature = EcdsaSig::sign(&Sha256::digest(payload.as_bytes()), key)?; | ||
| 364 | let mut raw = signature.r().to_vec_padded(32)?; | ||
| 365 | raw.extend(signature.s().to_vec_padded(32)?); | ||
| 366 | Ok(format!("{payload}.{}", URL_SAFE_NO_PAD.encode(raw))) | ||
| 367 | } | ||
| 368 | |||
| 369 | async fn post( | ||
| 370 | endpoint: &str, | ||
| 371 | key: &EcKey<openssl::pkey::Private>, | ||
| 372 | form: &[(&str, &str)], | ||
| 373 | nonce: &mut Option<String>, | ||
| 374 | expected: StatusCode, | ||
| 375 | ) -> Result<Value> { | ||
| 376 | let endpoint = https(endpoint)?; | ||
| 377 | let http = client(&endpoint).await?; | ||
| 378 | for attempt in 0..2 { | ||
| 379 | let response = http | ||
| 380 | .post(endpoint.clone()) | ||
| 381 | .header("DPoP", proof(key, &endpoint, nonce.as_deref())?) | ||
| 382 | .header("accept", "application/json") | ||
| 383 | .form(form) | ||
| 384 | .send() | ||
| 385 | .await | ||
| 386 | .map_err(|_| reject())?; | ||
| 387 | let status = response.status(); | ||
| 388 | let next_nonce = response | ||
| 389 | .headers() | ||
| 390 | .get("dpop-nonce") | ||
| 391 | .and_then(|n| n.to_str().ok()) | ||
| 392 | .filter(|n| !n.is_empty() && n.len() <= 2048) | ||
| 393 | .ok_or_else(reject)? | ||
| 394 | .to_owned(); | ||
| 395 | *nonce = Some(next_nonce); | ||
| 396 | let data: Value = | ||
| 397 | serde_json::from_slice(&bytes(response, 65536).await?).map_err(|_| reject())?; | ||
| 398 | if attempt == 0 | ||
| 399 | && (status == StatusCode::BAD_REQUEST || status == StatusCode::UNAUTHORIZED) | ||
| 400 | && data["error"] == "use_dpop_nonce" | ||
| 401 | { | ||
| 402 | continue; | ||
| 403 | } | ||
| 404 | if status != expected { | ||
| 405 | return Err(reject()); | ||
| 406 | } | ||
| 407 | return Ok(data); | ||
| 408 | } | ||
| 409 | Err(reject()) | ||
| 410 | } | ||
| 411 | |||
| 412 | pub async fn start(auth: &auth::Store, identifier: &str, next: &str) -> Result<(String, String)> { | ||
| 413 | let identifier = identifier.trim().trim_start_matches('@'); | ||
| 414 | let did = if identifier.starts_with("did:") { | ||
| 415 | did_url(identifier)?; | ||
| 416 | identifier.to_owned() | ||
| 417 | } else { | ||
| 418 | handle_did(&identifier.to_ascii_lowercase()).await? | ||
| 419 | }; | ||
| 420 | let (pds, handle) = identity(&did).await?; | ||
| 421 | if !identifier.starts_with("did:") | ||
| 422 | && handle.as_deref() != Some(identifier.to_ascii_lowercase().as_str()) | ||
| 423 | { | ||
| 424 | return Err(reject()); | ||
| 425 | } | ||
| 426 | let server = server(&pds).await?; | ||
| 427 | let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1)?; | ||
| 428 | let key = EcKey::generate(&group)?; | ||
| 429 | let verifier = mcp::secret(); | ||
| 430 | let mut flow = json!({"provider":"atproto","next":next,"did":did,"server":server, | ||
| 431 | "verifier":verifier,"key":URL_SAFE_NO_PAD.encode(key.private_key_to_der()?)}); | ||
| 432 | let state = auth::issue(&auth.db.lock().unwrap(), "guest", flow.clone(), 300)?; | ||
| 433 | let result = async { | ||
| 434 | let metadata = metadata(auth); | ||
| 435 | let challenge = URL_SAFE_NO_PAD.encode(Sha256::digest(verifier.as_bytes())); | ||
| 436 | let mut nonce = None; | ||
| 437 | let par = post( | ||
| 438 | string(&server["pushed_authorization_request_endpoint"]), | ||
| 439 | &key, | ||
| 440 | &[ | ||
| 441 | ("client_id", string(&metadata["client_id"])), | ||
| 442 | ("redirect_uri", string(&metadata["redirect_uris"][0])), | ||
| 443 | ("response_type", "code"), | ||
| 444 | ("scope", "atproto"), | ||
| 445 | ("state", &state), | ||
| 446 | ("code_challenge", &challenge), | ||
| 447 | ("code_challenge_method", "S256"), | ||
| 448 | ("login_hint", &did), | ||
| 449 | ], | ||
| 450 | &mut nonce, | ||
| 451 | StatusCode::CREATED, | ||
| 452 | ) | ||
| 453 | .await?; | ||
| 454 | let uri = string(&par["request_uri"]); | ||
| 455 | if uri.is_empty() || uri.len() > 2048 || par["expires_in"].as_u64().is_none_or(|n| n == 0) { | ||
| 456 | return Err(reject()); | ||
| 457 | } | ||
| 458 | flow["nonce"] = json!(nonce); | ||
| 459 | auth.db.lock().unwrap().execute( | ||
| 460 | "UPDATE pending SET data=? WHERE hash=? AND kind='guest'", | ||
| 461 | rusqlite::params![flow.to_string(), mcp::hash(&state)], | ||
| 462 | )?; | ||
| 463 | let mut target = https(string(&server["authorization_endpoint"]))?; | ||
| 464 | client(&target).await?; | ||
| 465 | if target | ||
| 466 | .query_pairs() | ||
| 467 | .any(|(key, _)| matches!(key.as_ref(), "client_id" | "request_uri")) | ||
| 468 | { | ||
| 469 | return Err(reject()); | ||
| 470 | } | ||
| 471 | target.query_pairs_mut().extend_pairs([ | ||
| 472 | ("client_id", string(&metadata["client_id"])), | ||
| 473 | ("request_uri", uri), | ||
| 474 | ]); | ||
| 475 | Ok((target.to_string(), state.clone())) | ||
| 476 | } | ||
| 477 | .await; | ||
| 478 | if result.is_err() { | ||
| 479 | auth::pending(&auth.db.lock().unwrap(), &state, "guest", true)?; | ||
| 480 | } | ||
| 481 | result | ||
| 482 | } | ||
| 483 | |||
| 484 | pub struct Profile { | ||
| 485 | pub did: String, | ||
| 486 | pub name: String, | ||
| 487 | pub picture: Option<String>, | ||
| 488 | pub url: Option<String>, | ||
| 489 | } | ||
| 490 | |||
| 491 | pub async fn finish(auth: &auth::Store, flow: &Value, code: &str, issuer: &str) -> Result<Profile> { | ||
| 492 | let server = &flow["server"]; | ||
| 493 | if issuer != string(&server["issuer"]) { | ||
| 494 | return Err(reject()); | ||
| 495 | } | ||
| 496 | let key = EcKey::private_key_from_der(&URL_SAFE_NO_PAD.decode(string(&flow["key"]))?)?; | ||
| 497 | let metadata = metadata(auth); | ||
| 498 | let mut nonce = flow["nonce"].as_str().map(str::to_owned); | ||
| 499 | let token = post( | ||
| 500 | string(&server["token_endpoint"]), | ||
| 501 | &key, | ||
| 502 | &[ | ||
| 503 | ("client_id", string(&metadata["client_id"])), | ||
| 504 | ("grant_type", "authorization_code"), | ||
| 505 | ("code", code), | ||
| 506 | ("redirect_uri", string(&metadata["redirect_uris"][0])), | ||
| 507 | ("code_verifier", string(&flow["verifier"])), | ||
| 508 | ], | ||
| 509 | &mut nonce, | ||
| 510 | StatusCode::OK, | ||
| 511 | ) | ||
| 512 | .await?; | ||
| 513 | if token["sub"] != flow["did"] | ||
| 514 | || token["token_type"] | ||
| 515 | .as_str() | ||
| 516 | .is_none_or(|s| !s.eq_ignore_ascii_case("DPoP")) | ||
| 517 | || string(&token["access_token"]).is_empty() | ||
| 518 | || string(&token["scope"]) != "atproto" | ||
| 519 | { | ||
| 520 | return Err(reject()); | ||
| 521 | } | ||
| 522 | let did = string(&token["sub"]); | ||
| 523 | let (pds, handle) = identity(did).await?; | ||
| 524 | if self::server(&pds).await?["issuer"] != issuer { | ||
| 525 | return Err(reject()); | ||
| 526 | } | ||
| 527 | let mut fallback = Url::parse("https://pdsls.dev/at://")?; | ||
| 528 | fallback | ||
| 529 | .path_segments_mut() | ||
| 530 | .unwrap() | ||
| 531 | .pop_if_empty() | ||
| 532 | .push(did); | ||
| 533 | let mut profile = Profile { | ||
| 534 | did: did.to_owned(), | ||
| 535 | name: handle.clone().unwrap_or_else(|| did.to_owned()), | ||
| 536 | picture: None, | ||
| 537 | url: Some(fallback.into()), | ||
| 538 | }; | ||
| 539 | for (collection, site) in [ | ||
| 540 | ("sh.tangled.actor.profile", "https://tangled.org/"), | ||
| 541 | ("app.bsky.actor.profile", "https://bsky.app/profile/"), | ||
| 542 | ] { | ||
| 543 | let mut url = pds.join("xrpc/com.atproto.repo.getRecord")?; | ||
| 544 | url.query_pairs_mut().extend_pairs([ | ||
| 545 | ("repo", did), | ||
| 546 | ("collection", collection), | ||
| 547 | ("rkey", "self"), | ||
| 548 | ]); | ||
| 549 | let Ok(record) = get(url).await else { | ||
| 550 | continue; | ||
| 551 | }; | ||
| 552 | if record["uri"] != format!("at://{did}/{collection}/self") | ||
| 553 | || record["value"]["$type"] != collection | ||
| 554 | { | ||
| 555 | continue; | ||
| 556 | } | ||
| 557 | let value = &record["value"]; | ||
| 558 | if let Some(name) = value["displayName"] | ||
| 559 | .as_str() | ||
| 560 | .filter(|s| !s.trim().is_empty() && !s.chars().any(char::is_control)) | ||
| 561 | { | ||
| 562 | profile.name = name.chars().take(128).collect(); | ||
| 563 | } | ||
| 564 | let mut link = Url::parse(site)?; | ||
| 565 | link.path_segments_mut() | ||
| 566 | .unwrap() | ||
| 567 | .pop_if_empty() | ||
| 568 | .push(handle.as_deref().unwrap_or(did)); | ||
| 569 | profile.url = Some(link.into()); | ||
| 570 | let blob = &value["avatar"]; | ||
| 571 | let cid = string(&blob["ref"]["$link"]); | ||
| 572 | if blob["$type"] == "blob" | ||
| 573 | && matches!( | ||
| 574 | string(&blob["mimeType"]), | ||
| 575 | "image/png" | "image/jpeg" | "image/webp" | ||
| 576 | ) | ||
| 577 | && !cid.is_empty() | ||
| 578 | && cid.len() <= 256 | ||
| 579 | && cid.bytes().all(|b| b.is_ascii_alphanumeric()) | ||
| 580 | { | ||
| 581 | let mut avatar = pds.join("xrpc/com.atproto.sync.getBlob")?; | ||
| 582 | avatar | ||
| 583 | .query_pairs_mut() | ||
| 584 | .extend_pairs([("did", did), ("cid", cid)]); | ||
| 585 | if let Ok(http) = client(&avatar).await | ||
| 586 | && let Ok(response) = http.get(avatar).send().await | ||
| 587 | && response.status() == StatusCode::OK | ||
| 588 | && let Ok(data) = bytes(response, 262144).await | ||
| 589 | { | ||
| 590 | let mime = if data.starts_with(b"\x89PNG\r\n\x1a\n") { | ||
| 591 | Some("image/png") | ||
| 592 | } else if data.starts_with(b"\xff\xd8\xff") { | ||
| 593 | Some("image/jpeg") | ||
| 594 | } else if data.starts_with(b"RIFF") && data.get(8..12) == Some(b"WEBP") { | ||
| 595 | Some("image/webp") | ||
| 596 | } else { | ||
| 597 | None | ||
| 598 | }; | ||
| 599 | if let Some(mime) = mime { | ||
| 600 | profile.picture = Some(format!( | ||
| 601 | "data:{mime};base64,{}", | ||
| 602 | base64::engine::general_purpose::STANDARD.encode(data) | ||
| 603 | )); | ||
| 604 | } | ||
| 605 | } | ||
| 606 | } | ||
| 607 | break; | ||
| 608 | } | ||
| 609 | Ok(profile) | ||
| 610 | } | ||
| 611 | |||
| 612 | #[cfg(test)] | ||
| 613 | mod tests { | ||
| 614 | use super::*; | ||
| 615 | use futures::FutureExt; | ||
| 616 | use std::io::BufRead; | ||
| 617 | use std::process::{Command, Stdio}; | ||
| 618 | tokio::task_local! { pub(super) static HTTP: reqwest::Client; } | ||
| 619 | |||
| 620 | #[test] | ||
| 621 | fn rejects_local_networks_and_malformed_identifiers() { | ||
| 622 | for ip in [ | ||
| 623 | "0.0.0.0", | ||
| 624 | "10.0.0.1", | ||
| 625 | "100.64.0.1", | ||
| 626 | "127.0.0.1", | ||
| 627 | "169.254.169.254", | ||
| 628 | "172.31.0.1", | ||
| 629 | "192.168.1.1", | ||
| 630 | "192.0.0.1", | ||
| 631 | "198.18.0.1", | ||
| 632 | "224.0.0.1", | ||
| 633 | "255.255.255.255", | ||
| 634 | "::1", | ||
| 635 | "::ffff:127.0.0.1", | ||
| 636 | "64:ff9b::7f00:1", | ||
| 637 | "2002:7f00::1", | ||
| 638 | "2001:db8::1", | ||
| 639 | "fc00::1", | ||
| 640 | "fe80::1", | ||
| 641 | ] { | ||
| 642 | assert!(!public_ip(ip.parse().unwrap()), "{ip}"); | ||
| 643 | } | ||
| 644 | for url in [ | ||
| 645 | "http://example.com", | ||
| 646 | "https://user:pass@example.com", | ||
| 647 | "https://127.1", | ||
| 648 | "https://2130706433", | ||
| 649 | "https://[::1]", | ||
| 650 | "https://example.com/#bad", | ||
| 651 | "https://x.local", | ||
| 652 | "https://localhost", | ||
| 653 | "https://example.com\\@127.0.0.1", | ||
| 654 | ] { | ||
| 655 | assert!(https(url).is_err(), "{url}"); | ||
| 656 | } | ||
| 657 | for did in [ | ||
| 658 | "did:web:localhost", | ||
| 659 | "did:web:example.com:profile", | ||
| 660 | "did:web:example.com%3A443", | ||
| 661 | "did:web:127.0.0.1", | ||
| 662 | "did:plc:../oops", | ||
| 663 | "did:plc:aaaaaaaaaaaaaaaaaaaaaaa1", | ||
| 664 | ] { | ||
| 665 | assert!(did_url(did).is_err(), "{did}"); | ||
| 666 | } | ||
| 667 | assert!(did_url("did:plc:ewvi7nxzyoun6zhxrhs64oiz").is_ok()); | ||
| 668 | assert!(did_url("did:web:example.com").is_ok()); | ||
| 669 | assert!(public_ip("8.8.8.8".parse().unwrap())); | ||
| 670 | assert!(public_ip("2606:4700:4700::1111".parse().unwrap())); | ||
| 671 | } | ||
| 672 | |||
| 673 | #[tokio::test] | ||
| 674 | async fn synthetic_https_pds_verifies_par_pkce_dpop_and_identity() { | ||
| 675 | let root = std::env::temp_dir().join(format!("atproto-test-{}", uuid::Uuid::new_v4())); | ||
| 676 | std::fs::create_dir(&root).unwrap(); | ||
| 677 | let cert = root.join("cert.pem"); | ||
| 678 | let key = root.join("key.pem"); | ||
| 679 | 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"]) | ||
| 680 | .arg(&key).arg("-out").arg(&cert).output().unwrap().status.success()); | ||
| 681 | let mut child = Command::new("python3") | ||
| 682 | .arg(concat!( | ||
| 683 | env!("CARGO_MANIFEST_DIR"), | ||
| 684 | "/tests/atproto-fixture.py" | ||
| 685 | )) | ||
| 686 | .arg(&cert) | ||
| 687 | .arg(&key) | ||
| 688 | .stdout(Stdio::piped()) | ||
| 689 | .spawn() | ||
| 690 | .unwrap(); | ||
| 691 | let mut line = String::new(); | ||
| 692 | std::io::BufReader::new(child.stdout.take().unwrap()) | ||
| 693 | .read_line(&mut line) | ||
| 694 | .unwrap(); | ||
| 695 | let address = format!("127.0.0.1:{}", line.trim()).parse().unwrap(); | ||
| 696 | let mut builder = reqwest::Client::builder() | ||
| 697 | .no_proxy() | ||
| 698 | .redirect(reqwest::redirect::Policy::none()) | ||
| 699 | .timeout(Duration::from_secs(5)) | ||
| 700 | .add_root_certificate( | ||
| 701 | reqwest::Certificate::from_pem(&std::fs::read(&cert).unwrap()).unwrap(), | ||
| 702 | ); | ||
| 703 | for host in ["alice.example.com", "pds.example.com", "issuer.example.com"] { | ||
| 704 | builder = builder.resolve(host, address); | ||
| 705 | } | ||
| 706 | let http = builder.build().unwrap(); | ||
| 707 | let result = std::panic::AssertUnwindSafe(HTTP.scope(http.clone(), async { | ||
| 708 | let auth = auth::Store::new( | ||
| 709 | &root.join("accounts"), | ||
| 710 | "https://snowglobe.example.com", | ||
| 711 | "https://files.example.com", | ||
| 712 | "auth.example.com", | ||
| 713 | ) | ||
| 714 | .unwrap(); | ||
| 715 | for (mode, succeeds) in [ | ||
| 716 | ("", true), | ||
| 717 | ("bluesky-only", true), | ||
| 718 | ("no-profiles", true), | ||
| 719 | ("rotating-nonce", true), | ||
| 720 | ("endpoint-query", true), | ||
| 721 | ("wrong-subject", false), | ||
| 722 | ("changed-issuer", false), | ||
| 723 | ("wrong-metadata-issuer", false), | ||
| 724 | ("missing-nonce", false), | ||
| 725 | ("private-pds", false), | ||
| 726 | ] { | ||
| 727 | http.get(format!( | ||
| 728 | "https://issuer.example.com/fixture/mode?mode={mode}" | ||
| 729 | )) | ||
| 730 | .send() | ||
| 731 | .await | ||
| 732 | .unwrap(); | ||
| 733 | let started = start(&auth, "did:web:alice.example.com", "/fixture").await; | ||
| 734 | if matches!( | ||
| 735 | mode, | ||
| 736 | "wrong-metadata-issuer" | "missing-nonce" | "private-pds" | ||
| 737 | ) { | ||
| 738 | assert!(started.is_err(), "{mode}"); | ||
| 739 | continue; | ||
| 740 | } | ||
| 741 | let (target, state) = started.unwrap(); | ||
| 742 | let code = Url::parse(&target) | ||
| 743 | .unwrap() | ||
| 744 | .query_pairs() | ||
| 745 | .find(|(key, _)| key == "request_uri") | ||
| 746 | .unwrap() | ||
| 747 | .1 | ||
| 748 | .into_owned(); | ||
| 749 | let flow = auth::pending(&auth.db.lock().unwrap(), &state, "guest", true).unwrap(); | ||
| 750 | assert!( | ||
| 751 | auth::pending(&auth.db.lock().unwrap(), &state, "guest", true) | ||
| 752 | .unwrap() | ||
| 753 | .is_null() | ||
| 754 | ); | ||
| 755 | assert!( | ||
| 756 | finish(&auth, &flow, &code, "https://wrong.example.com") | ||
| 757 | .await | ||
| 758 | .is_err() | ||
| 759 | ); | ||
| 760 | let profile = finish(&auth, &flow, &code, "https://issuer.example.com").await; | ||
| 761 | assert_eq!(profile.is_ok(), succeeds, "{mode}"); | ||
| 762 | if let Ok(profile) = profile { | ||
| 763 | assert_eq!(profile.did, "did:web:alice.example.com"); | ||
| 764 | if mode == "no-profiles" { | ||
| 765 | assert!(profile.picture.is_none()); | ||
| 766 | assert_eq!(profile.name, "did:web:alice.example.com"); | ||
| 767 | assert_eq!( | ||
| 768 | profile.url.as_deref(), | ||
| 769 | Some("https://pdsls.dev/at://did:web:alice.example.com") | ||
| 770 | ); | ||
| 771 | } else { | ||
| 772 | assert!( | ||
| 773 | profile | ||
| 774 | .picture | ||
| 775 | .as_deref() | ||
| 776 | .unwrap() | ||
| 777 | .starts_with("data:image/png;base64,") | ||
| 778 | ); | ||
| 779 | assert_eq!(profile.name, "Fixture Alice"); | ||
| 780 | assert!(profile.url.unwrap().starts_with(if mode == "bluesky-only" { | ||
| 781 | "https://bsky.app/profile/" | ||
| 782 | } else { | ||
| 783 | "https://tangled.org/" | ||
| 784 | })); | ||
| 785 | } | ||
| 786 | assert!( | ||
| 787 | finish(&auth, &flow, &code, "https://issuer.example.com") | ||
| 788 | .await | ||
| 789 | .is_err() | ||
| 790 | ); | ||
| 791 | } | ||
| 792 | } | ||
| 793 | assert!( | ||
| 794 | get(https("https://issuer.example.com/redirect").unwrap()) | ||
| 795 | .await | ||
| 796 | .is_err() | ||
| 797 | ); | ||
| 798 | assert!( | ||
| 799 | get(https("https://issuer.example.com/oversized").unwrap()) | ||
| 800 | .await | ||
| 801 | .is_err() | ||
| 802 | ); | ||
| 803 | })) | ||
| 804 | .catch_unwind() | ||
| 805 | .await; | ||
| 806 | child.kill().unwrap(); | ||
| 807 | child.wait().unwrap(); | ||
| 808 | std::fs::remove_dir_all(root).unwrap(); | ||
| 809 | if let Err(panic) = result { | ||
| 810 | std::panic::resume_unwind(panic); | ||
| 811 | } | ||
| 812 | } | ||
| 813 | } | ||
dashboard/src/guest.rs+147-4| ... | @@ -32,9 +32,10 @@ fn known(provider: &str) -> Result<&str> { | ... | @@ -32,9 +32,10 @@ fn known(provider: &str) -> Result<&str> { |
| 32 | match provider { | 32 | match provider { |
| 33 | "github" => Ok("GitHub"), | 33 | "github" => Ok("GitHub"), |
| 34 | "astheno" => Ok("Astheno"), | 34 | "astheno" => Ok("Astheno"), |
| 35 | "atproto" => Ok("AT Protocol"), | ||
| 35 | _ => Err(Error::new( | 36 | _ => Err(Error::new( |
| 36 | 404, | 37 | 404, |
| 37 | "Choose GitHub or Astheno on the Shale sign-in page.", | 38 | "Choose a provider on the Shale sign-in page.", |
| 38 | )), | 39 | )), |
| 39 | } | 40 | } |
| 40 | } | 41 | } |
| ... | @@ -43,6 +44,12 @@ fn known(provider: &str) -> Result<&str> { | ... | @@ -43,6 +44,12 @@ fn known(provider: &str) -> Result<&str> { |
| 43 | pub fn provision(auth: &auth::Store, input: Value) -> Result<Value> { | 44 | pub fn provision(auth: &auth::Store, input: Value) -> Result<Value> { |
| 44 | let provider = string(&input["provider"]); | 45 | let provider = string(&input["provider"]); |
| 45 | known(provider)?; | 46 | known(provider)?; |
| 47 | if provider == "atproto" { | ||
| 48 | return Err(Error::new( | ||
| 49 | 400, | ||
| 50 | "AT Protocol uses public client metadata; no client secret is needed.", | ||
| 51 | )); | ||
| 52 | } | ||
| 46 | let mut db = auth.db.lock().unwrap(); | 53 | let mut db = auth.db.lock().unwrap(); |
| 47 | let tx = db.transaction()?; | 54 | let tx = db.transaction()?; |
| 48 | tx.execute( | 55 | tx.execute( |
| ... | @@ -80,9 +87,10 @@ pub fn providers(auth: &auth::Store, next: &str) -> Result<Value> { | ... | @@ -80,9 +87,10 @@ pub fn providers(auth: &auth::Store, next: &str) -> Result<Value> { |
| 80 | } | 87 | } |
| 81 | let db = auth.db.lock().unwrap(); | 88 | let db = auth.db.lock().unwrap(); |
| 82 | let mut query = db.prepare("SELECT id FROM guest_providers ORDER BY id")?; | 89 | let mut query = db.prepare("SELECT id FROM guest_providers ORDER BY id")?; |
| 83 | let ids = query | 90 | let mut ids = query |
| 84 | .query_map([], |r| r.get::<_, String>(0))? | 91 | .query_map([], |r| r.get::<_, String>(0))? |
| 85 | .collect::<std::result::Result<Vec<_>, _>>()?; | 92 | .collect::<std::result::Result<Vec<_>, _>>()?; |
| 93 | ids.push("atproto".into()); | ||
| 86 | Ok(json!( | 94 | Ok(json!( |
| 87 | ids.iter() | 95 | ids.iter() |
| 88 | .map(|id| json!({"id":id,"name":known(id).unwrap_or(id)})) | 96 | .map(|id| json!({"id":id,"name":known(id).unwrap_or(id)})) |
| ... | @@ -522,7 +530,30 @@ async fn handle(app: &App, request: Request) -> Result<Response> { | ... | @@ -522,7 +530,30 @@ async fn handle(app: &App, request: Request) -> Result<Response> { |
| 522 | } | 530 | } |
| 523 | let provider = parts[4]; | 531 | let provider = parts[4]; |
| 524 | known(provider)?; | 532 | known(provider)?; |
| 525 | let (client, secret) = registration(auth, provider)?; | 533 | if provider == "atproto" && parts[3] == "metadata" { |
| 534 | return Ok(axum::Json(atproto::metadata(auth)).into_response()); | ||
| 535 | } | ||
| 536 | if provider == "atproto" && parts[3] == "avatar" { | ||
| 537 | let query = fields(request.uri().query().unwrap_or_default())?; | ||
| 538 | let subject = query.get("subject").map(String::as_str).unwrap_or_default(); | ||
| 539 | let picture: Option<String> = auth.db.lock().unwrap().query_row( | ||
| 540 | "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", | ||
| 541 | [subject], |row| row.get(0)).optional()?.flatten(); | ||
| 542 | let (mime, data) = picture | ||
| 543 | .as_deref() | ||
| 544 | .and_then(|s| s.strip_prefix("data:")) | ||
| 545 | .and_then(|s| s.split_once(";base64,")) | ||
| 546 | .filter(|(mime, _)| matches!(*mime, "image/png" | "image/jpeg" | "image/webp")) | ||
| 547 | .ok_or_else(|| Error::new(404, "Profile picture unavailable."))?; | ||
| 548 | return Ok(( | ||
| 549 | [ | ||
| 550 | ("content-type", mime), | ||
| 551 | ("x-content-type-options", "nosniff"), | ||
| 552 | ], | ||
| 553 | base64::engine::general_purpose::STANDARD.decode(data)?, | ||
| 554 | ) | ||
| 555 | .into_response()); | ||
| 556 | } | ||
| 526 | let query = fields(request.uri().query().unwrap_or_default())?; | 557 | let query = fields(request.uri().query().unwrap_or_default())?; |
| 527 | let current = auth.session(headers, "dashboard")?; | 558 | let current = auth.session(headers, "dashboard")?; |
| 528 | if !current.is_null() && !is_guest(&current) { | 559 | if !current.is_null() && !is_guest(&current) { |
| ... | @@ -538,6 +569,39 @@ async fn handle(app: &App, request: Request) -> Result<Response> { | ... | @@ -538,6 +569,39 @@ async fn handle(app: &App, request: Request) -> Result<Response> { |
| 538 | auth, | 569 | auth, |
| 539 | query.get("next").map(String::as_str).unwrap_or_default(), | 570 | query.get("next").map(String::as_str).unwrap_or_default(), |
| 540 | )?; | 571 | )?; |
| 572 | if provider == "atproto" { | ||
| 573 | let result = tokio::time::timeout( | ||
| 574 | Duration::from_secs(45), | ||
| 575 | atproto::start( | ||
| 576 | auth, | ||
| 577 | query | ||
| 578 | .get("identifier") | ||
| 579 | .map(String::as_str) | ||
| 580 | .unwrap_or_default(), | ||
| 581 | &next, | ||
| 582 | ), | ||
| 583 | ) | ||
| 584 | .await; | ||
| 585 | return match result { | ||
| 586 | Ok(Ok((target, state))) => Ok(( | ||
| 587 | StatusCode::FOUND, | ||
| 588 | [ | ||
| 589 | ("location", target), | ||
| 590 | ("set-cookie", auth::set_cookie(COOKIE, &state, Some(300))), | ||
| 591 | ], | ||
| 592 | ) | ||
| 593 | .into_response()), | ||
| 594 | _ => Ok(( | ||
| 595 | StatusCode::FOUND, | ||
| 596 | [( | ||
| 597 | "location", | ||
| 598 | format!("/sign-in?next={}&atproto=1&guest_error=1", encoded(&next)), | ||
| 599 | )], | ||
| 600 | ) | ||
| 601 | .into_response()), | ||
| 602 | }; | ||
| 603 | } | ||
| 604 | let (client, _) = registration(auth, provider)?; | ||
| 541 | let verifier = mcp::secret(); | 605 | let verifier = mcp::secret(); |
| 542 | let nonce = mcp::secret(); | 606 | let nonce = mcp::secret(); |
| 543 | let state = auth::issue( | 607 | let state = auth::issue( |
| ... | @@ -606,13 +670,43 @@ async fn handle(app: &App, request: Request) -> Result<Response> { | ... | @@ -606,13 +670,43 @@ async fn handle(app: &App, request: Request) -> Result<Response> { |
| 606 | let result = async { | 670 | let result = async { |
| 607 | let code = query | 671 | let code = query |
| 608 | .get("code") | 672 | .get("code") |
| 609 | .filter(|s| !s.is_empty() && s.len() <= 4096) | 673 | .filter(|s| !s.is_empty() && s.len() <= 4096 && !query.contains_key("error")) |
| 610 | .ok_or_else(|| { | 674 | .ok_or_else(|| { |
| 611 | Error::new( | 675 | Error::new( |
| 612 | 400, | 676 | 400, |
| 613 | "Sign-in wasn't completed. Return to Shale and try again.", | 677 | "Sign-in wasn't completed. Return to Shale and try again.", |
| 614 | ) | 678 | ) |
| 615 | })?; | 679 | })?; |
| 680 | if provider == "atproto" { | ||
| 681 | let profile = tokio::time::timeout( | ||
| 682 | Duration::from_secs(45), | ||
| 683 | atproto::finish( | ||
| 684 | auth, | ||
| 685 | &flow, | ||
| 686 | code, | ||
| 687 | query.get("iss").map(String::as_str).unwrap_or_default(), | ||
| 688 | ), | ||
| 689 | ) | ||
| 690 | .await | ||
| 691 | .map_err(|_| Error::new(502, "Your provider took too long. Try signing in again."))??; | ||
| 692 | let id = account( | ||
| 693 | auth, | ||
| 694 | provider, | ||
| 695 | &profile.did, | ||
| 696 | &profile.name, | ||
| 697 | profile.picture.as_deref(), | ||
| 698 | )?; | ||
| 699 | auth.db.lock().unwrap().execute( | ||
| 700 | "UPDATE users SET profile=json_patch(profile,?) WHERE id=?", | ||
| 701 | sql![ | ||
| 702 | json!({"attributes":{"profileUrl":profile.url.map(|url|vec![url])}}) | ||
| 703 | .to_string(), | ||
| 704 | id | ||
| 705 | ], | ||
| 706 | )?; | ||
| 707 | return auth.create_session(&id, "dashboard", headers, None); | ||
| 708 | } | ||
| 709 | let (client, secret) = registration(auth, provider)?; | ||
| 616 | let http = reqwest::Client::builder() | 710 | let http = reqwest::Client::builder() |
| 617 | .timeout(Duration::from_secs(10)) | 711 | .timeout(Duration::from_secs(10)) |
| 618 | .redirect(reqwest::redirect::Policy::none()) | 712 | .redirect(reqwest::redirect::Policy::none()) |
| ... | @@ -675,6 +769,55 @@ pub async fn route(State(app): State<Arc<App>>, request: Request) -> Response { | ... | @@ -675,6 +769,55 @@ pub async fn route(State(app): State<Arc<App>>, request: Request) -> Response { |
| 675 | mod tests { | 769 | mod tests { |
| 676 | use super::*; | 770 | use super::*; |
| 677 | 771 | ||
| 772 | #[test] | ||
| 773 | fn atproto_did_is_the_account_key_and_guests_stay_separate() { | ||
| 774 | let path = | ||
| 775 | std::env::temp_dir().join(format!("atproto-guest-test-{}", uuid::Uuid::new_v4())); | ||
| 776 | let auth = auth::Store::new( | ||
| 777 | &path, | ||
| 778 | "https://snowglobe.example.com", | ||
| 779 | "https://file.example.com", | ||
| 780 | "auth.example.com", | ||
| 781 | ) | ||
| 782 | .unwrap(); | ||
| 783 | let did = "did:plc:ewvi7nxzyoun6zhxrhs64oiz"; | ||
| 784 | let first = account(&auth, "atproto", did, "alice.example.com", None).unwrap(); | ||
| 785 | assert_eq!( | ||
| 786 | first, | ||
| 787 | account(&auth, "atproto", did, "new.example.com", None).unwrap() | ||
| 788 | ); | ||
| 789 | assert_ne!( | ||
| 790 | first, | ||
| 791 | account(&auth, "github", "42", "new.example.com", None).unwrap() | ||
| 792 | ); | ||
| 793 | assert_ne!( | ||
| 794 | first, | ||
| 795 | account( | ||
| 796 | &auth, | ||
| 797 | "atproto", | ||
| 798 | "did:web:other.example.com", | ||
| 799 | "new.example.com", | ||
| 800 | None | ||
| 801 | ) | ||
| 802 | .unwrap() | ||
| 803 | ); | ||
| 804 | assert!( | ||
| 805 | auth.create_session(&first, "file", &HeaderMap::new(), None) | ||
| 806 | .is_err() | ||
| 807 | ); | ||
| 808 | assert!(auth.setup_link(&first).is_err()); | ||
| 809 | assert_eq!(providers(&auth, "/").unwrap(), json!([])); | ||
| 810 | let db = auth.db.lock().unwrap(); | ||
| 811 | let profile = auth::user(&db, &first).unwrap(); | ||
| 812 | assert!(is_guest(&profile)); | ||
| 813 | assert!(profile["email"].is_null()); | ||
| 814 | assert_eq!(profile["firstName"], "new.example.com"); | ||
| 815 | assert!(auth::set_password(&db, &first, "unused").is_err()); | ||
| 816 | drop(db); | ||
| 817 | drop(auth); | ||
| 818 | std::fs::remove_dir_all(path).unwrap(); | ||
| 819 | } | ||
| 820 | |||
| 678 | // The P-256 tokens were produced independently with Python cryptography. | 821 | // The P-256 tokens were produced independently with Python cryptography. |
| 679 | #[test] | 822 | #[test] |
| 680 | fn external_signature_issuer_audience_nonce_and_time_are_required() { | 823 | fn external_signature_issuer_audience_nonce_and_time_are_required() { |
dashboard/src/main.rs+1| ... | @@ -1,6 +1,7 @@ | ... | @@ -1,6 +1,7 @@ |
| 1 | mod ai; | 1 | mod ai; |
| 2 | mod apps; | 2 | mod apps; |
| 3 | mod auth; | 3 | mod auth; |
| 4 | mod atproto; | ||
| 4 | mod identity; | 5 | mod identity; |
| 5 | mod cache; | 6 | mod cache; |
| 6 | mod core; | 7 | mod core; |
dashboard/src/shale_page.rs+81-4| ... | @@ -14,7 +14,7 @@ struct Profile { | ... | @@ -14,7 +14,7 @@ struct Profile { |
| 14 | 14 | ||
| 15 | fn profiles(auth: &auth::Store) -> Result<HashMap<String, Profile>> { | 15 | fn profiles(auth: &auth::Store) -> Result<HashMap<String, Profile>> { |
| 16 | let db = auth.db.lock().unwrap(); | 16 | let db = auth.db.lock().unwrap(); |
| 17 | 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'")?; | 17 | 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'")?; |
| 18 | let rows = query.query_map([], |row| { | 18 | let rows = query.query_map([], |row| { |
| 19 | Ok(( | 19 | Ok(( |
| 20 | row.get::<_, String>(0)?, | 20 | row.get::<_, String>(0)?, |
| ... | @@ -22,11 +22,12 @@ fn profiles(auth: &auth::Store) -> Result<HashMap<String, Profile>> { | ... | @@ -22,11 +22,12 @@ fn profiles(auth: &auth::Store) -> Result<HashMap<String, Profile>> { |
| 22 | row.get::<_, String>(2)?, | 22 | row.get::<_, String>(2)?, |
| 23 | row.get::<_, String>(3)?, | 23 | row.get::<_, String>(3)?, |
| 24 | row.get::<_, Option<String>>(4)?, | 24 | row.get::<_, Option<String>>(4)?, |
| 25 | row.get::<_, Option<String>>(5)?, | ||
| 25 | )) | 26 | )) |
| 26 | })?; | 27 | })?; |
| 27 | let mut profiles = HashMap::new(); | 28 | let mut profiles = HashMap::new(); |
| 28 | for row in rows { | 29 | for row in rows { |
| 29 | let (username, name, provider, subject, picture) = row?; | 30 | let (username, name, provider, subject, picture, profile_url) = row?; |
| 30 | if name.is_empty() { continue; } | 31 | if name.is_empty() { continue; } |
| 31 | let (url, icon) = match provider.as_str() { | 32 | let (url, icon) = match provider.as_str() { |
| 32 | "github" => { | 33 | "github" => { |
| ... | @@ -36,6 +37,7 @@ fn profiles(auth: &auth::Store) -> Result<HashMap<String, Profile>> { | ... | @@ -36,6 +37,7 @@ fn profiles(auth: &auth::Store) -> Result<HashMap<String, Profile>> { |
| 36 | (Some(String::from(url)), include_str!("../web/sso/github.svg")) | 37 | (Some(String::from(url)), include_str!("../web/sso/github.svg")) |
| 37 | } | 38 | } |
| 38 | "astheno" => (None, include_str!("astheno.svg")), | 39 | "astheno" => (None, include_str!("astheno.svg")), |
| 40 | "atproto" => (profile_url, include_str!("../web/sso/atproto.svg")), | ||
| 39 | _ => continue, | 41 | _ => continue, |
| 40 | }; | 42 | }; |
| 41 | profiles.insert( | 43 | profiles.insert( |
| ... | @@ -49,6 +51,8 @@ fn profiles(auth: &auth::Store) -> Result<HashMap<String, Profile>> { | ... | @@ -49,6 +51,8 @@ fn profiles(auth: &auth::Store) -> Result<HashMap<String, Profile>> { |
| 49 | Some(format!( | 51 | Some(format!( |
| 50 | "https://avatars.githubusercontent.com/u/{subject}?s=64" | 52 | "https://avatars.githubusercontent.com/u/{subject}?s=64" |
| 51 | )) | 53 | )) |
| 54 | } else if provider == "atproto" { | ||
| 55 | picture.map(|_| format!("{}auth/guest/avatar/atproto?subject={}", auth.origin, encoded(&subject))) | ||
| 52 | } else { | 56 | } else { |
| 53 | picture | 57 | picture |
| 54 | }, | 58 | }, |
| ... | @@ -268,13 +272,13 @@ fn rewrite(html: &str, origin: &url::Url, profiles: &HashMap<String, Profile>, a | ... | @@ -268,13 +272,13 @@ fn rewrite(html: &str, origin: &url::Url, profiles: &HashMap<String, Profile>, a |
| 268 | if profile.owner.is_some() || ai_profile.is_some() { element.set_tag_name("span")?; } | 272 | if profile.owner.is_some() || ai_profile.is_some() { element.set_tag_name("span")?; } |
| 269 | let style = element.get_attribute("style").unwrap_or_default(); | 273 | let style = element.get_attribute("style").unwrap_or_default(); |
| 270 | let cursor = if profile.url.is_some() { "" } else { ";cursor:default" }; | 274 | let cursor = if profile.url.is_some() { "" } else { ";cursor:default" }; |
| 271 | 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() }; | 275 | 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() }; |
| 272 | element.set_attribute("style", &format!("{style};text-decoration:none{cursor}{spacing}"))?; | 276 | element.set_attribute("style", &format!("{style};text-decoration:none{cursor}{spacing}"))?; |
| 273 | if let Some(picture) = &profile.picture { | 277 | if let Some(picture) = &profile.picture { |
| 274 | element.prepend(&avatar(picture), ContentType::Html); | 278 | element.prepend(&avatar(picture), ContentType::Html); |
| 275 | } | 279 | } |
| 276 | if profile.owner.is_some() { element.append("<span class=\"va-middle-childs\" style=\"white-space:nowrap\">", ContentType::Html); } | 280 | if profile.owner.is_some() { element.append("<span class=\"va-middle-childs\" style=\"white-space:nowrap\">", ContentType::Html); } |
| 277 | 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" }; | 281 | 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" }; |
| 278 | let icon = profile.icon.trim().replace("<svg ", &format!("<svg aria-hidden=\"true\" focusable=\"false\" style=\"color:inherit;stroke:currentColor;{dimensions}\" ")); | 282 | let icon = profile.icon.trim().replace("<svg ", &format!("<svg aria-hidden=\"true\" focusable=\"false\" style=\"color:inherit;stroke:currentColor;{dimensions}\" ")); |
| 279 | let icon = if profile.url.is_some() { icon.replace("<path ", "<path style=\"fill:currentColor;stroke:none\" ") } else { icon }; | 283 | let icon = if profile.url.is_some() { icon.replace("<path ", "<path style=\"fill:currentColor;stroke:none\" ") } else { icon }; |
| 280 | element.append(&icon, ContentType::Html); | 284 | element.append(&icon, ContentType::Html); |
| ... | @@ -502,8 +506,81 @@ pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> { | ... | @@ -502,8 +506,81 @@ pub async fn proxy(app: Arc<App>, request: Request) -> Result<Response> { |
| 502 | #[cfg(test)] | 506 | #[cfg(test)] |
| 503 | mod tests { | 507 | mod tests { |
| 504 | use super::*; | 508 | use super::*; |
| 509 | use rusqlite::params as sql; | ||
| 505 | use scraper::{Html, Selector}; | 510 | use scraper::{Html, Selector}; |
| 506 | 511 | ||
| 512 | #[test] | ||
| 513 | fn persisted_atproto_profiles_enrich_shale_authors_and_account_links() { | ||
| 514 | let path = std::env::temp_dir().join(format!("shale-atproto-test-{}", uuid::Uuid::new_v4())); | ||
| 515 | let store = || auth::Store::new(&path, "https://snowglobe.example.com", "https://file.example.com", "auth.example.com").unwrap(); | ||
| 516 | let accounts = [ | ||
| 517 | ("did:web:tangled.example.com", "Alice <Tangled> & \"friends\"", "https://tangled.org/alice.example.com", true), | ||
| 518 | ("did:web:bluesky.example.com", "Bob <Bluesky> & \"friends\"", "https://bsky.app/profile/bob.example.com", true), | ||
| 519 | ("did:web:pds.example.com", "Carol <PDSLS> & \"friends\"", "https://pdsls.dev/at://did:web:pds.example.com", true), | ||
| 520 | ("did:web:no-avatar.example.com", "No avatar", "https://tangled.org/no-avatar.example.com", false), | ||
| 521 | ]; | ||
| 522 | { | ||
| 523 | let auth = store(); | ||
| 524 | let db = auth.db.lock().unwrap(); | ||
| 525 | for (did, name, url, picture) in accounts { | ||
| 526 | let id = uuid::Uuid::new_v4().to_string(); | ||
| 527 | let username = format!("guest-atproto-{}", &mcp::hash(did)[..24]); | ||
| 528 | let mut profile = json!({"kind":"guest","guestProvider":"atproto","username":username,"enabled":true,"email":null,"emailVerified":false,"firstName":name,"attributes":{"profileUrl":[url]}}); | ||
| 529 | if picture { | ||
| 530 | profile["attributes"]["picture"] = json!(["data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+j3ioAAAAASUVORK5CYII="]); | ||
| 531 | } | ||
| 532 | db.execute("INSERT INTO users(id,profile) VALUES (?,?)", sql![id,profile.to_string()]).unwrap(); | ||
| 533 | db.execute("INSERT INTO external_identities VALUES ('atproto',?,?)", sql![did,id]).unwrap(); | ||
| 534 | } | ||
| 535 | } | ||
| 536 | let auth = store(); | ||
| 537 | let profiles = profiles(&auth).unwrap(); | ||
| 538 | assert_eq!(profiles.len(), accounts.len()); | ||
| 539 | let origin = url::Url::parse("https://shale.example.com/home-infra/issues/50").unwrap(); | ||
| 540 | for (did, name, url, picture) in accounts { | ||
| 541 | let username = format!("guest-atproto-{}", &mcp::hash(did)[..24]); | ||
| 542 | let local = format!("/~{username}"); | ||
| 543 | let html = format!(r#"<body id="page-issue"><ol class="timeline"><li class="spanspacer"><div class="n-card__header"><div class="spanspacer"><a class="usa-link va-middle-childs" href="{local}"><img src="old-avatar" height="16"><span>~{username}</span></a><time>opened this issue</time></div></div><div class="markdown"><p>A synthetic issue.</p><a id="external" href="https://other.example.com{local}">external</a><a id="http" href="http://shale.example.com{local}">http</a><a id="repo" href="{local}/repo">repo</a><a id="unknown" href="/~guest-atproto-unknown">unknown</a><pre>~{username}</pre></div></li></ol><h1 id="profile"><a href="https://shale.example.com{local}/"><span>~{username}</span></a></h1></body>"#); | ||
| 544 | let output = rewrite(&html, &origin, &profiles, &HashMap::new(), None).unwrap(); | ||
| 545 | let document = Html::parse_document(&output); | ||
| 546 | for selector in [".n-card__header a", "#profile a"] { | ||
| 547 | let link = document.select(&Selector::parse(selector).unwrap()).next().unwrap(); | ||
| 548 | assert_eq!(link.attr("href"), Some(url)); | ||
| 549 | assert_eq!(link.attr("target"), Some("_blank")); | ||
| 550 | assert_eq!(link.attr("rel"), Some("noreferrer")); | ||
| 551 | let style = link.attr("style").unwrap(); | ||
| 552 | assert!(style.contains("display:inline-flex;align-items:center;gap:.25em")); | ||
| 553 | assert_eq!(link.children().filter_map(scraper::ElementRef::wrap).map(|e| e.value().name()).collect::<Vec<_>>(), if picture || selector.starts_with('.') { vec!["img", "svg", "span"] } else { vec!["svg", "span"] }); | ||
| 554 | assert_eq!(link.select(&Selector::parse("span[style]").unwrap()).next().unwrap().text().collect::<String>(), name); | ||
| 555 | let icon = link.select(&Selector::parse("svg").unwrap()).next().unwrap(); | ||
| 556 | assert_eq!(icon.attr("aria-hidden"), Some("true")); | ||
| 557 | assert_eq!(icon.attr("focusable"), Some("false")); | ||
| 558 | assert!(!icon.attr("style").unwrap().contains("margin-right")); | ||
| 559 | assert_eq!(link.select(&Selector::parse("img").unwrap()).count(), if picture { 1 } else { usize::from(selector.starts_with('.')) }); | ||
| 560 | if picture { | ||
| 561 | let avatar = link.select(&Selector::parse("img").unwrap()).next().unwrap(); | ||
| 562 | let expected = format!("https://snowglobe.example.com/auth/guest/avatar/atproto?subject=did%3Aweb%3A{}", did.strip_prefix("did:web:").unwrap()); | ||
| 563 | assert_eq!(avatar.attr("src"), Some(expected.as_str())); | ||
| 564 | assert_eq!(avatar.attr("alt"), Some("")); | ||
| 565 | assert_eq!(avatar.attr("referrerpolicy"), Some("no-referrer")); | ||
| 566 | } | ||
| 567 | } | ||
| 568 | 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")] { | ||
| 569 | let link = document.select(&Selector::parse(&format!("#{id}")).unwrap()).next().unwrap(); | ||
| 570 | assert_eq!(link.attr("href"), Some(href.as_str())); | ||
| 571 | assert_eq!(link.text().collect::<String>(), label); | ||
| 572 | assert_eq!(link.attr("target"), None); | ||
| 573 | assert_eq!(link.select(&Selector::parse("svg,img").unwrap()).count(), 0); | ||
| 574 | } | ||
| 575 | assert_eq!(document.select(&Selector::parse("pre").unwrap()).next().unwrap().text().collect::<String>(), format!("~{username}")); | ||
| 576 | assert_eq!(document.select(&Selector::parse("script,tangled,bluesky,pdsls").unwrap()).count(), 0); | ||
| 577 | assert!(!output.contains("data:image")); | ||
| 578 | assert!(!output.contains("base64")); | ||
| 579 | } | ||
| 580 | drop(auth); | ||
| 581 | std::fs::remove_dir_all(path).unwrap(); | ||
| 582 | } | ||
| 583 | |||
| 507 | #[test] | 584 | #[test] |
| 508 | fn commit_identity_keeps_entities_and_cannot_escape_its_metadata_cell() { | 585 | fn commit_identity_keeps_entities_and_cannot_escape_its_metadata_cell() { |
| 509 | let origin = url::Url::parse("https://shale.test/repo/commit/abc").unwrap(); | 586 | let origin = url::Url::parse("https://shale.test/repo/commit/abc").unwrap(); |
dashboard/tests/atproto-fixture.py created+195| ... | @@ -0,0 +1,195 @@ | ||
| 1 | """Disposable OAuth/PDS fixture. TLS files and listening address are supplied by Rust tests.""" | ||
| 2 | import base64 | ||
| 3 | import hashlib | ||
| 4 | import json | ||
| 5 | import html | ||
| 6 | import os | ||
| 7 | import secrets | ||
| 8 | import ssl | ||
| 9 | import subprocess | ||
| 10 | import sys | ||
| 11 | import tempfile | ||
| 12 | import time | ||
| 13 | from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer | ||
| 14 | from pathlib import Path | ||
| 15 | from urllib.parse import parse_qs, urlsplit, urlencode | ||
| 16 | from urllib.request import urlopen | ||
| 17 | |||
| 18 | ISSUER = os.environ.get('ATPROTO_FIXTURE_ORIGIN', 'https://issuer.example.com') | ||
| 19 | PDS = os.environ.get('ATPROTO_FIXTURE_ORIGIN', 'https://pds.example.com') | ||
| 20 | DID = 'did:web:' + (urlsplit(os.environ['ATPROTO_FIXTURE_ORIGIN']).hostname if 'ATPROTO_FIXTURE_ORIGIN' in os.environ else 'alice.example.com') | ||
| 21 | AVATAR = base64.b64decode('iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAAQklEQVR4nGNIcziOFf3fbYsVkaqeYdSCUQuGgAXUMgiX+lELRi0YChZQyyBc6kctGLVgKFhALYNwqR+1YNSCIWABALbxyFuikyXnAAAAAElFTkSuQmCC') | ||
| 22 | AVATAR_CID = 'bafkreifixtureavatar' | ||
| 23 | |||
| 24 | |||
| 25 | def decode(text): | ||
| 26 | return base64.urlsafe_b64decode(text + '=' * (-len(text) % 4)) | ||
| 27 | |||
| 28 | |||
| 29 | def signature_der(raw): | ||
| 30 | def integer(value): | ||
| 31 | value = value.lstrip(b'\0') or b'\0' | ||
| 32 | if value[0] & 128: | ||
| 33 | value = b'\0' + value | ||
| 34 | return b'\x02' + bytes([len(value)]) + value | ||
| 35 | value = integer(raw[:32]) + integer(raw[32:]) | ||
| 36 | return b'\x30' + bytes([len(value)]) + value | ||
| 37 | |||
| 38 | |||
| 39 | class Fixture(BaseHTTPRequestHandler): | ||
| 40 | seen = set() | ||
| 41 | flows = {} | ||
| 42 | mode = '' | ||
| 43 | nonce = 'fixture-nonce' | ||
| 44 | exchanged = False | ||
| 45 | |||
| 46 | def log_message(self, *_): | ||
| 47 | pass | ||
| 48 | |||
| 49 | def send(self, code, value, nonce=False): | ||
| 50 | body = json.dumps(value).encode() | ||
| 51 | self.send_response(code) | ||
| 52 | self.send_header('Content-Type', 'application/json') | ||
| 53 | self.send_header('Content-Length', str(len(body))) | ||
| 54 | if nonce and self.mode != 'missing-nonce': | ||
| 55 | self.send_header('DPoP-Nonce', self.nonce) | ||
| 56 | self.end_headers() | ||
| 57 | self.wfile.write(body) | ||
| 58 | |||
| 59 | def do_GET(self): | ||
| 60 | path = urlsplit(self.path).path | ||
| 61 | if path == '/.well-known/atproto-did': | ||
| 62 | body = DID.encode() | ||
| 63 | self.send_response(200) | ||
| 64 | self.send_header('Content-Length', str(len(body))) | ||
| 65 | self.end_headers() | ||
| 66 | self.wfile.write(body) | ||
| 67 | return | ||
| 68 | if path == '/authorize': | ||
| 69 | uri = parse_qs(urlsplit(self.path).query).get('request_uri', [''])[0] | ||
| 70 | if uri not in self.flows: | ||
| 71 | return self.send(400, {'error': 'expired_request'}) | ||
| 72 | body = ('<!doctype html><meta name="viewport" content="width=device-width"><title>Fixture account</title>' | ||
| 73 | '<h1>Disposable AT Protocol account</h1><p>This fixture never asks for a password.</p>' | ||
| 74 | '<form method="post" action="/decision"><input type="hidden" name="request_uri" value="'+html.escape(uri, quote=True)+'">' | ||
| 75 | '<button name="decision" value="approve">Continue as Fixture Alice</button> ' | ||
| 76 | '<button name="decision" value="cancel">Cancel</button></form>').encode() | ||
| 77 | self.send_response(200) | ||
| 78 | self.send_header('Content-Type', 'text/html; charset=utf-8') | ||
| 79 | self.send_header('Content-Length', str(len(body))) | ||
| 80 | self.end_headers() | ||
| 81 | self.wfile.write(body) | ||
| 82 | return | ||
| 83 | if path == '/fixture/mode': | ||
| 84 | type(self).exchanged = False | ||
| 85 | type(self).mode = parse_qs(urlsplit(self.path).query).get('mode', [''])[0] | ||
| 86 | return self.send(200, {}) | ||
| 87 | if path == '/.well-known/did.json': | ||
| 88 | 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}]}) | ||
| 89 | if path == '/.well-known/oauth-protected-resource': | ||
| 90 | return self.send(200, {'resource': PDS, 'authorization_servers': [ISSUER]}) | ||
| 91 | if path == '/.well-known/oauth-authorization-server': | ||
| 92 | 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, | ||
| 93 | 'authorization_endpoint': ISSUER+'/authorize', 'token_endpoint': ISSUER+'/token'+('?tenant=fixture' if self.mode == 'endpoint-query' else ''), | ||
| 94 | 'pushed_authorization_request_endpoint': ISSUER+'/par', 'response_types_supported': ['code'], | ||
| 95 | 'grant_types_supported': ['authorization_code', 'refresh_token'], 'code_challenge_methods_supported': ['S256'], | ||
| 96 | 'token_endpoint_auth_methods_supported': ['none', 'private_key_jwt'], 'scopes_supported': ['atproto'], | ||
| 97 | 'dpop_signing_alg_values_supported': ['ES256'], 'require_pushed_authorization_requests': True, | ||
| 98 | 'authorization_response_iss_parameter_supported': True, 'client_id_metadata_document_supported': True}) | ||
| 99 | if path == '/xrpc/com.atproto.repo.getRecord': | ||
| 100 | query = parse_qs(urlsplit(self.path).query) | ||
| 101 | collection = query['collection'][0] | ||
| 102 | if self.mode == 'no-profiles' or (collection == 'sh.tangled.actor.profile' and self.mode == 'bluesky-only'): | ||
| 103 | return self.send(400, {'error': 'RecordNotFound'}) | ||
| 104 | 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)}}}) | ||
| 105 | if path == '/xrpc/com.atproto.sync.getBlob': | ||
| 106 | query = parse_qs(urlsplit(self.path).query) | ||
| 107 | if query.get('did') != [DID] or query.get('cid') != [AVATAR_CID]: | ||
| 108 | return self.send(404, {'error': 'BlobNotFound'}) | ||
| 109 | self.send_response(200) | ||
| 110 | self.send_header('Content-Type', 'image/png') | ||
| 111 | self.send_header('Content-Length', str(len(AVATAR))) | ||
| 112 | self.end_headers() | ||
| 113 | self.wfile.write(AVATAR) | ||
| 114 | return | ||
| 115 | if path == '/redirect': | ||
| 116 | self.send_response(302) | ||
| 117 | self.send_header('Location', 'https://127.0.0.1/private') | ||
| 118 | self.end_headers() | ||
| 119 | return | ||
| 120 | if path == '/oversized': | ||
| 121 | return self.send(200, {'data': 'a'*70000}) | ||
| 122 | return self.send(404, {'error': 'NotFound'}) | ||
| 123 | |||
| 124 | def do_POST(self): | ||
| 125 | if self.path == '/decision': | ||
| 126 | form = parse_qs(self.rfile.read(min(int(self.headers['Content-Length']), 8192)).decode()) | ||
| 127 | uri = form.get('request_uri', [''])[0] | ||
| 128 | if uri not in self.flows: | ||
| 129 | return self.send(400, {'error': 'expired_request'}) | ||
| 130 | original, _ = self.flows[uri] | ||
| 131 | query = {'state': original['state'][0], 'iss': ISSUER} | ||
| 132 | query['code' if form.get('decision') == ['approve'] else 'error'] = uri if form.get('decision') == ['approve'] else 'access_denied' | ||
| 133 | self.send_response(302) | ||
| 134 | self.send_header('Location', original['redirect_uri'][0]+'?'+urlencode(query)) | ||
| 135 | self.end_headers() | ||
| 136 | return | ||
| 137 | try: | ||
| 138 | parts = self.headers['DPoP'].split('.') | ||
| 139 | header, claims = map(lambda v: json.loads(decode(v)), parts[:2]) | ||
| 140 | assert header['alg'] == 'ES256' and header['typ'] == 'dpop+jwt' | ||
| 141 | assert claims['htm'] == 'POST' and claims['htu'] == ISSUER+urlsplit(self.path).path | ||
| 142 | assert abs(claims['iat'] - time.time()) < 60 and claims['jti'] not in self.seen | ||
| 143 | self.seen.add(claims['jti']) | ||
| 144 | jwk = header['jwk'] | ||
| 145 | assert jwk['crv'] == 'P-256' and jwk['kty'] == 'EC' and 'd' not in jwk | ||
| 146 | public = bytes.fromhex('3059301306072a8648ce3d020106082a8648ce3d03010703420004') + decode(jwk['x']) + decode(jwk['y']) | ||
| 147 | with tempfile.TemporaryDirectory() as temp: | ||
| 148 | Path(temp, 'pub.der').write_bytes(public) | ||
| 149 | Path(temp, 'sig.der').write_bytes(signature_der(decode(parts[2]))) | ||
| 150 | 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) | ||
| 151 | assert checked.returncode == 0 | ||
| 152 | form = parse_qs(self.rfile.read(int(self.headers['Content-Length'])).decode()) | ||
| 153 | expected_nonce = 'rotated-token-nonce' if self.mode == 'rotating-nonce' and self.path == '/token' else 'fixture-nonce' | ||
| 154 | type(self).nonce = expected_nonce | ||
| 155 | if claims.get('nonce') != expected_nonce: | ||
| 156 | return self.send(400, {'error': 'use_dpop_nonce'}, True) | ||
| 157 | if self.path == '/par': | ||
| 158 | assert form['scope'] == ['atproto'] and form['code_challenge_method'] == ['S256'] | ||
| 159 | assert form['login_hint'] == [DID] | ||
| 160 | if 'ATPROTO_FIXTURE_ORIGIN' in os.environ: | ||
| 161 | expected = os.environ['ATPROTO_FIXTURE_CLIENT_ORIGIN'].rstrip('/')+'/auth/guest/metadata/atproto' | ||
| 162 | assert form['client_id'] == [expected] | ||
| 163 | with urlopen(expected, timeout=10) as response: | ||
| 164 | metadata = json.load(response) | ||
| 165 | assert metadata['client_id'] == expected and metadata['dpop_bound_access_tokens'] is True | ||
| 166 | assert form['redirect_uri'][0] in metadata['redirect_uris'] | ||
| 167 | if len(self.flows) > 128: | ||
| 168 | self.flows.clear() | ||
| 169 | uri = 'urn:ietf:params:oauth:request_uri:'+secrets.token_hex(16) | ||
| 170 | self.flows[uri] = (form, jwk) | ||
| 171 | return self.send(201, {'request_uri': uri, 'expires_in': 300}, True) | ||
| 172 | if urlsplit(self.path).path == '/token': | ||
| 173 | original, original_jwk = self.flows.pop(form['code'][0]) | ||
| 174 | assert original_jwk == jwk | ||
| 175 | expected = base64.urlsafe_b64encode(hashlib.sha256(form['code_verifier'][0].encode()).digest()).decode().rstrip('=') | ||
| 176 | assert original['code_challenge'] == [expected] | ||
| 177 | assert original['client_id'] == form['client_id'] and original['redirect_uri'] == form['redirect_uri'] | ||
| 178 | type(self).exchanged = True | ||
| 179 | return self.send(200, {'sub': 'did:web:mallory.example.com' if self.mode == 'wrong-subject' else DID, | ||
| 180 | 'access_token': 'disposable-fixture-token', 'token_type': 'DPoP', 'scope': 'atproto'}, True) | ||
| 181 | return self.send(404, {}, True) | ||
| 182 | except Exception: | ||
| 183 | return self.send(400, {'error': 'fixture_validation_failed'}, True) | ||
| 184 | |||
| 185 | |||
| 186 | if __name__ == '__main__': | ||
| 187 | server = ThreadingHTTPServer((os.environ.get('ATPROTO_FIXTURE_BIND', '127.0.0.1'), int(os.environ.get('ATPROTO_FIXTURE_PORT', '0'))), Fixture) | ||
| 188 | if len(sys.argv) == 3: | ||
| 189 | tls = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) | ||
| 190 | tls.load_cert_chain(sys.argv[1], sys.argv[2]) | ||
| 191 | server.socket = tls.wrap_socket(server.socket, server_side=True) | ||
| 192 | elif 'ATPROTO_FIXTURE_ORIGIN' not in os.environ or 'ATPROTO_FIXTURE_CLIENT_ORIGIN' not in os.environ: | ||
| 193 | raise SystemExit('Configure fixture and client HTTPS origins behind a TLS proxy, or pass a TLS certificate and key.') | ||
| 194 | print(server.server_port, flush=True) | ||
| 195 | server.serve_forever() | ||
dashboard/web/pages/SignIn.tsx+18-3| ... | @@ -1,6 +1,7 @@ | ... | @@ -1,6 +1,7 @@ |
| 1 | import { createEffect, createResource, createSignal, For, onCleanup, onMount, Show } from "solid-js"; | 1 | import { createEffect, createResource, createSignal, For, onCleanup, onMount, Show } from "solid-js"; |
| 2 | import { authReason, authRequest } from "../auth.ts"; | 2 | import { authReason, authRequest } from "../auth.ts"; |
| 3 | import ArrowLeft from "lucide-solid/icons/arrow-left"; | 3 | import ArrowLeft from "lucide-solid/icons/arrow-left"; |
| 4 | import atprotoIcon from "../sso/atproto.svg"; | ||
| 4 | import Stone from "lucide-solid/icons/stone"; | 5 | import Stone from "lucide-solid/icons/stone"; |
| 5 | import githubIcon from "../sso/github.svg"; | 6 | import githubIcon from "../sso/github.svg"; |
| 6 | import theme from "../sso/css/styles.css?inline"; | 7 | import theme from "../sso/css/styles.css?inline"; |
| ... | @@ -9,6 +10,7 @@ import adjustments from "./SignIn.css?inline"; | ... | @@ -9,6 +10,7 @@ import adjustments from "./SignIn.css?inline"; |
| 9 | export function SignIn() { | 10 | export function SignIn() { |
| 10 | const params = new URLSearchParams(window.location.search); | 11 | const params = new URLSearchParams(window.location.search); |
| 11 | const setup = params.get("setup"); | 12 | const setup = params.get("setup"); |
| 13 | const atproto = !setup && params.has("atproto"); | ||
| 12 | const statusQuery = new URLSearchParams(); | 14 | const statusQuery = new URLSearchParams(); |
| 13 | if (setup) statusQuery.set("setup", setup); | 15 | if (setup) statusQuery.set("setup", setup); |
| 14 | if (params.get("flow")) statusQuery.set("flow", params.get("flow")!); | 16 | if (params.get("flow")) statusQuery.set("flow", params.get("flow")!); |
| ... | @@ -20,7 +22,7 @@ export function SignIn() { | ... | @@ -20,7 +22,7 @@ export function SignIn() { |
| 20 | const [passwordStep, setPasswordStep] = createSignal(false); | 22 | const [passwordStep, setPasswordStep] = createSignal(false); |
| 21 | const [visible, setVisible] = createSignal(false); | 23 | const [visible, setVisible] = createSignal(false); |
| 22 | const [busy, setBusy] = createSignal(false); | 24 | const [busy, setBusy] = createSignal(false); |
| 23 | const [error, setError] = createSignal(params.has("guest_error") ? "Guest sign-in wasn't completed. Choose a provider to try again." : ""); | 25 | 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.") : ""); |
| 24 | const [notice, setNotice] = createSignal(""); | 26 | const [notice, setNotice] = createSignal(""); |
| 25 | let conditional: AbortController | undefined; | 27 | let conditional: AbortController | undefined; |
| 26 | let disposed = false; | 28 | let disposed = false; |
| ... | @@ -50,7 +52,7 @@ export function SignIn() { | ... | @@ -50,7 +52,7 @@ export function SignIn() { |
| 50 | } finally { if (!automatic || selected) setBusy(false); } | 52 | } finally { if (!automatic || selected) setBusy(false); } |
| 51 | }; | 53 | }; |
| 52 | createEffect(() => { | 54 | createEffect(() => { |
| 53 | if (!setup && !passwordStep() && status()) void (async () => { | 55 | if (!setup && !atproto && !passwordStep() && status()) void (async () => { |
| 54 | if (await PublicKeyCredential.isConditionalMediationAvailable?.() && !disposed && !passwordStep()) await passkey(true); | 56 | if (await PublicKeyCredential.isConditionalMediationAvailable?.() && !disposed && !passwordStep()) await passkey(true); |
| 55 | })().catch(() => {}); | 57 | })().catch(() => {}); |
| 56 | }); | 58 | }); |
| ... | @@ -87,9 +89,21 @@ export function SignIn() { | ... | @@ -87,9 +89,21 @@ export function SignIn() { |
| 87 | <Show when={status.error || error()}><div class="alert-error pf-v5-c-alert" role="alert"><span class="pf-v5-c-alert__title">{status.error ? authReason(status.error) : error()}</span></div></Show> | 89 | <Show when={status.error || error()}><div class="alert-error pf-v5-c-alert" role="alert"><span class="pf-v5-c-alert__title">{status.error ? authReason(status.error) : error()}</span></div></Show> |
| 88 | <Show when={notice()}><div class="alert-info pf-v5-c-alert" role="status"><span class="pf-v5-c-alert__title">{notice()}</span></div></Show> | 90 | <Show when={notice()}><div class="alert-info pf-v5-c-alert" role="status"><span class="pf-v5-c-alert__title">{notice()}</span></div></Show> |
| 89 | <Show when={!status.error} fallback={<button class="pf-v5-c-button pf-m-primary pf-m-block" onClick={() => refetch()}>try again</button>}> | 91 | <Show when={!status.error} fallback={<button class="pf-v5-c-button pf-m-primary pf-m-block" onClick={() => refetch()}>try again</button>}> |
| 92 | <Show when={!atproto} fallback={ | ||
| 93 | <form action="/auth/guest/start/atproto" method="get" onSubmit={(event) => { if (busy()) { event.preventDefault(); return; } setBusy(true); setError(""); }}> | ||
| 94 | <div class="sign-in-account"><a rel="external" href={`/sign-in?${statusQuery}`}><ArrowLeft size={18} aria-hidden="true" />back</a></div> | ||
| 95 | <input type="hidden" name="next" value={params.get("next") ?? ""} /> | ||
| 96 | <div class="pf-v5-c-form__group"> | ||
| 97 | <label for="atproto-identifier" class="pf-v5-c-form__label">handle or DID</label> | ||
| 98 | <input id="atproto-identifier" name="identifier" class="pf-v5-c-form-control" required maxlength={2048} autocomplete="username" autocapitalize="none" spellcheck={false} placeholder="you.example.com" aria-describedby="atproto-hint" autofocus ref={(input) => queueMicrotask(() => input.focus())} /> | ||
| 99 | <p id="atproto-hint">Your provider will ask you to approve sign-in.</p> | ||
| 100 | </div> | ||
| 101 | <div id="kc-form-buttons" class="pf-v5-c-form__group"><button class="pf-v5-c-button pf-m-primary pf-m-block" type="submit" disabled={busy() || !status()} aria-busy={busy()}>{busy() ? "connecting…" : "continue"}</button></div> | ||
| 102 | </form> | ||
| 103 | }> | ||
| 90 | <Show when={!setup && !passwordStep() && status()?.providers?.length}> | 104 | <Show when={!setup && !passwordStep() && status()?.providers?.length}> |
| 91 | <div id="kc-social-providers" class="guest-providers"> | 105 | <div id="kc-social-providers" class="guest-providers"> |
| 92 | <For each={status()?.providers}>{(provider) => <a rel="external" data-provider={provider.id} class="pf-v5-c-button pf-m-primary pf-m-block" href={`/auth/guest/start/${provider.id}?next=${encodeURIComponent(params.get("next") ?? "")}`}>{provider.id === "astheno" ? <Stone size={18} aria-hidden="true" /> : <img src={githubIcon} width="18" height="18" alt="" />}<span>continue with {provider.name}</span></a>}</For> | 106 | <For each={status()?.providers}>{(provider) => <a rel="external" data-provider={provider.id} class="pf-v5-c-button pf-m-primary pf-m-block" href={provider.id === "atproto" ? `/sign-in?${statusQuery}&atproto=1` : `/auth/guest/start/${provider.id}?next=${encodeURIComponent(params.get("next") ?? "")}`}>{provider.id === "atproto" ? <img src={atprotoIcon} width="18" height="18" alt="" /> : provider.id === "astheno" ? <Stone size={18} aria-hidden="true" /> : <img src={githubIcon} width="18" height="18" alt="" />}<span>continue with {provider.name}</span></a>}</For> |
| 93 | </div> | 107 | </div> |
| 94 | </Show> | 108 | </Show> |
| 95 | <div id="kc-form"><div id="kc-form-wrapper"> | 109 | <div id="kc-form"><div id="kc-form-wrapper"> |
| ... | @@ -127,6 +141,7 @@ export function SignIn() { | ... | @@ -127,6 +141,7 @@ export function SignIn() { |
| 127 | </form> | 141 | </form> |
| 128 | </div></div> | 142 | </div></div> |
| 129 | <Show when={!setup}><a id="authenticateWebAuthnButton" href="#" class="pf-v5-c-button pf-m-secondary pf-m-block" aria-disabled={busy() || !status()} onClick={(event) => { event.preventDefault(); void passkey(); }}>sign in with passkey</a></Show> | 143 | <Show when={!setup}><a id="authenticateWebAuthnButton" href="#" class="pf-v5-c-button pf-m-secondary pf-m-block" aria-disabled={busy() || !status()} onClick={(event) => { event.preventDefault(); void passkey(); }}>sign in with passkey</a></Show> |
| 144 | </Show> | ||
| 130 | 145 | ||
| 131 | </Show> | 146 | </Show> |
| 132 | </div></div> | 147 | </div></div> |
dashboard/web/sso/atproto.svg created+1| ... | @@ -0,0 +1 @@ | ||
| 1 | <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><text x="12" y="18" text-anchor="middle" font-family="sans-serif" font-size="22" fill="currentColor" stroke="none">@</text></svg> | ||
nixos/dashboard.nix+2-1| ... | @@ -36,12 +36,13 @@ let | ... | @@ -36,12 +36,13 @@ let |
| 36 | src = lib.fileset.toSource { | 36 | src = lib.fileset.toSource { |
| 37 | root = ../dashboard; | 37 | root = ../dashboard; |
| 38 | fileset = lib.fileset.unions [ | 38 | fileset = lib.fileset.unions [ |
| 39 | ../dashboard/src ../dashboard/web/sso/github.svg ../dashboard/ai ../dashboard/tests ../dashboard/Cargo.toml ../dashboard/Cargo.lock | 39 | ../dashboard/src ../dashboard/web/sso ../dashboard/ai ../dashboard/tests ../dashboard/Cargo.toml ../dashboard/Cargo.lock |
| 40 | ../dashboard/agent/install.sh ../dashboard/agent/install.ps1 | 40 | ../dashboard/agent/install.sh ../dashboard/agent/install.ps1 |
| 41 | ]; | 41 | ]; |
| 42 | }; | 42 | }; |
| 43 | cargoLock.lockFile = ../dashboard/Cargo.lock; | 43 | cargoLock.lockFile = ../dashboard/Cargo.lock; |
| 44 | nativeBuildInputs = [ pkg-config ]; | 44 | nativeBuildInputs = [ pkg-config ]; |
| 45 | nativeCheckInputs = [ python3 openssl ]; | ||
| 45 | buildInputs = [ sqlite openssl ]; | 46 | buildInputs = [ sqlite openssl ]; |
| 46 | LIBSQLITE3_SYS_USE_PKG_CONFIG = "1"; | 47 | LIBSQLITE3_SYS_USE_PKG_CONFIG = "1"; |
| 47 | }; | 48 | }; |
service/shale/service.pkl+1-1| ... | @@ -18,7 +18,7 @@ prepare = "prepare.py" | ... | @@ -18,7 +18,7 @@ prepare = "prepare.py" |
| 18 | 18 | ||
| 19 | container { | 19 | container { |
| 20 | memory = 4096 | 20 | memory = 4096 |
| 21 | image = "docker.io/astheno/shale@sha256:b55cc42c4eb2c1e21733405bec795e49ddd7c71fb1755d2aa2ee4361f750cebd" | 21 | image = "docker.io/astheno/shale@sha256:4aa7db75ab5e47e9fef68bc88357e2fef014c8cfd8a6fb7e14203f23e8aa80de" |
| 22 | 22 | ||
| 23 | http { | 23 | http { |
| 24 | containerPort = 8000 | 24 | containerPort = 8000 |
tools/dashboard-shale-page-test.py+3-1| ... | @@ -130,7 +130,9 @@ def main(): | ... | @@ -130,7 +130,9 @@ def main(): |
| 130 | profile = {'kind': 'guest', 'enabled': True, 'username': username, 'guestProvider': provider, 'firstName': name} | 130 | profile = {'kind': 'guest', 'enabled': True, 'username': username, 'guestProvider': provider, 'firstName': name} |
| 131 | db.execute('INSERT INTO users(id,profile) VALUES (?,?)', [username, json.dumps(profile)]) | 131 | db.execute('INSERT INTO users(id,profile) VALUES (?,?)', [username, json.dumps(profile)]) |
| 132 | db.execute('INSERT INTO external_identities VALUES (?,?,?)', [provider, subject, username]) | 132 | db.execute('INSERT INTO external_identities VALUES (?,?,?)', [provider, subject, username]) |
| 133 | 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)) | 133 | (root / 'stages').mkdir() |
| 134 | (root / 'stages/shale-preview-12345678.json').write_text(json.dumps({'sourceId': 'shale'})) | ||
| 135 | 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)) | ||
| 134 | router.ROUTE_DIR = str(root / 'routes') | 136 | router.ROUTE_DIR = str(root / 'routes') |
| 135 | Path(router.ROUTE_DIR).mkdir() | 137 | Path(router.ROUTE_DIR).mkdir() |
| 136 | (Path(router.ROUTE_DIR) / 'shale.json').write_text(json.dumps({'headHtml': {'shale.snowglobe.test': {'/snowbound/*': '<meta name="rewrite-test" content="kept">'}}})) | 138 | (Path(router.ROUTE_DIR) / 'shale.json').write_text(json.dumps({'headHtml': {'shale.snowglobe.test': {'/snowbound/*': '<meta name="rewrite-test" content="kept">'}}})) |
tools/router-cookie-test.py+48-3| ... | @@ -20,6 +20,7 @@ class Cookies(BaseHTTPRequestHandler): | ... | @@ -20,6 +20,7 @@ class Cookies(BaseHTTPRequestHandler): |
| 20 | self.send_response(204 if self.path == '/logout' else 200) | 20 | self.send_response(204 if self.path == '/logout' else 200) |
| 21 | self.send_header('Set-Cookie', 'session=fixture; HttpOnly; Path=/') | 21 | self.send_header('Set-Cookie', 'session=fixture; HttpOnly; Path=/') |
| 22 | self.send_header('Set-Cookie', 'other=fixture; SameSite=Lax') | 22 | self.send_header('Set-Cookie', 'other=fixture; SameSite=Lax') |
| 23 | self.send_header('X-Fixture-Referer', self.headers.get('Referer', '')) | ||
| 23 | self.end_headers() | 24 | self.end_headers() |
| 24 | 25 | ||
| 25 | def log_message(self, *_): | 26 | def log_message(self, *_): |
| ... | @@ -32,16 +33,27 @@ def main(): | ... | @@ -32,16 +33,27 @@ def main(): |
| 32 | args = parser.parse_args() | 33 | args = parser.parse_args() |
| 33 | with tempfile.TemporaryDirectory(prefix='snowglobe-router-cookies-') as temporary, ThreadingHTTPServer(('127.0.0.1', 0), Cookies) as backend: | 34 | with tempfile.TemporaryDirectory(prefix='snowglobe-router-cookies-') as temporary, ThreadingHTTPServer(('127.0.0.1', 0), Cookies) as backend: |
| 34 | root = Path(temporary) | 35 | root = Path(temporary) |
| 36 | (root / 'stages').mkdir() | ||
| 37 | (root / 'stages/shale-preview-1234abcd.json').write_text(json.dumps({'sourceId': 'shale'})) | ||
| 38 | static = root / 'static' | ||
| 39 | static.mkdir() | ||
| 40 | (static / 'nsv.woff2').write_bytes(b'public font fixture') | ||
| 41 | (static / 'font.css').write_text('@font-face { font-family: fixture; }') | ||
| 42 | routes = root / 'routes' | ||
| 43 | routes.mkdir() | ||
| 44 | for service in ('copyparty', 'copyparty-preview-1234abcd', 'plain'): | ||
| 45 | (routes / (service + '.json')).write_text(json.dumps({'dirs': {'/.static/': str(static)}})) | ||
| 35 | proof = root / 'proof' | 46 | proof = root / 'proof' |
| 36 | proof.write_text('a' * 64) | 47 | proof.write_text('a' * 64) |
| 37 | environment = {**os.environ, 'SNOWGLOBE_DOMAIN': 'snowglobe.test', 'SNOWGLOBE_DASHBOARD_PORT': '3000', | 48 | environment = {**os.environ, 'SNOWGLOBE_DOMAIN': 'snowglobe.test', 'SNOWGLOBE_DASHBOARD_PORT': str(backend.server_port), |
| 49 | 'SNOWGLOBE_STATE': str(root), | ||
| 38 | 'SNOWGLOBE_PROXY_TOKEN_FILE': str(proof), 'XDG_DATA_HOME': str(root / 'data'), | 50 | 'SNOWGLOBE_PROXY_TOKEN_FILE': str(proof), 'XDG_DATA_HOME': str(root / 'data'), |
| 39 | 'XDG_CONFIG_HOME': str(root / 'config')} | 51 | 'XDG_CONFIG_HOME': str(root / 'config')} |
| 40 | environment.pop('SNOWGLOBE_INTERNAL_PORT', None) | 52 | environment.pop('SNOWGLOBE_INTERNAL_PORT', None) |
| 41 | 53 | ||
| 42 | def nomad(path, _): | 54 | def nomad(path, _): |
| 43 | if path == '/v1/services': | 55 | if path == '/v1/services': |
| 44 | return [{'Namespace': 'default', 'Services': [{'ServiceName': 'knife'}, {'ServiceName': 'plain'}]}] | 56 | return [{'Namespace': 'default', 'Services': [{'ServiceName': name} for name in ('knife', 'plain', 'shale', 'shale-preview-1234abcd', 'copyparty', 'copyparty-preview-1234abcd')]}] |
| 45 | if path.startswith('/v1/service/'): | 57 | if path.startswith('/v1/service/'): |
| 46 | service = path.rsplit('/', 1)[1] | 58 | service = path.rsplit('/', 1)[1] |
| 47 | return [{'ServiceName': service, 'JobID': service, 'AllocID': service, 'Address': '127.0.0.1', | 59 | return [{'ServiceName': service, 'JobID': service, 'AllocID': service, 'Address': '127.0.0.1', |
| ... | @@ -63,6 +75,8 @@ def main(): | ... | @@ -63,6 +75,8 @@ def main(): |
| 63 | runtime = root / 'Caddyfile' | 75 | runtime = root / 'Caddyfile' |
| 64 | sites = rendered.split('auth.snowglobe.test {', 1)[0] | 76 | sites = rendered.split('auth.snowglobe.test {', 1)[0] |
| 65 | 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', '') | 77 | 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', '') |
| 78 | sites = sites.replace('shale.snowglobe.test {', f'http://shale.localhost:{port} {{').replace('shale-preview-1234abcd.snowglobe.test {', f'http://preview.localhost:{port} {{') | ||
| 79 | sites = sites.replace('copyparty.snowglobe.test {', f'http://files.localhost:{port} {{').replace('copyparty-preview-1234abcd.snowglobe.test {', f'http://preview-files.localhost:{port} {{') | ||
| 66 | runtime.write_text('{\n admin off\n auto_https off\n}\n' + sites) | 80 | runtime.write_text('{\n admin off\n auto_https off\n}\n' + sites) |
| 67 | threading.Thread(target=backend.serve_forever, daemon=True).start() | 81 | threading.Thread(target=backend.serve_forever, daemon=True).start() |
| 68 | with (root / 'caddy.log').open('w') as log: | 82 | with (root / 'caddy.log').open('w') as log: |
| ... | @@ -91,11 +105,42 @@ def main(): | ... | @@ -91,11 +105,42 @@ def main(): |
| 91 | 'other=fixture; SameSite=Lax' + ('; Secure' if host == '127.0.0.1' else '')], cookies | 105 | 'other=fixture; SameSite=Lax' + ('; Secure' if host == '127.0.0.1' else '')], cookies |
| 92 | response.read() | 106 | response.read() |
| 93 | connection.close() | 107 | connection.close() |
| 108 | for host in ('files.localhost', 'preview-files.localhost', 'localhost'): | ||
| 109 | for path in ('/.static/nsv.woff2', '/.static/font.css', '/media/nsv.woff2'): | ||
| 110 | for method in ('GET', 'HEAD') if path.startswith('/.static/') else ('GET',): | ||
| 111 | connection = HTTPConnection('127.0.0.1', port, timeout=2) | ||
| 112 | connection.request(method, path, headers={'Host': f'{host}:{port}', 'Origin': 'https://sign-in.example'}) | ||
| 113 | response = connection.getresponse() | ||
| 114 | assert response.status == 200 | ||
| 115 | expected = '*' if host != 'localhost' and path == '/.static/nsv.woff2' else None | ||
| 116 | assert response.getheader('Access-Control-Allow-Origin') == expected, (host, path, method) | ||
| 117 | assert response.getheader('Access-Control-Allow-Credentials') is None | ||
| 118 | body = response.read() | ||
| 119 | if path == '/.static/nsv.woff2': | ||
| 120 | assert body == (b'public font fixture' if method == 'GET' else b'') | ||
| 121 | connection.close() | ||
| 122 | for service, host in (('shale', 'shale.localhost'), ('shale-preview-1234abcd', 'preview.localhost')): | ||
| 123 | origin = f'https://{service}.snowglobe.test/' | ||
| 124 | 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'): | ||
| 125 | for path in ('/-/login', '/-/settings'): | ||
| 126 | headers = {'Host': f'{host}:{port}', 'Accept': 'application/json'} | ||
| 127 | if referer is not None: | ||
| 128 | headers['Referer'] = referer | ||
| 129 | connection = HTTPConnection('127.0.0.1', port, timeout=2) | ||
| 130 | connection.request('GET', path, headers=headers) | ||
| 131 | response = connection.getresponse() | ||
| 132 | expected = referer or '' | ||
| 133 | if path == '/-/login' and referer not in (origin, origin + 'home-infra/issues/50?q=is%3Aopen&limit=10'): | ||
| 134 | expected = origin | ||
| 135 | assert response.status == 200 | ||
| 136 | assert response.getheader('X-Fixture-Referer') == expected, (service, path, referer) | ||
| 137 | response.read() | ||
| 138 | connection.close() | ||
| 94 | finally: | 139 | finally: |
| 95 | caddy.terminate() | 140 | caddy.terminate() |
| 96 | caddy.wait(timeout=10) | 141 | caddy.wait(timeout=10) |
| 97 | backend.shutdown() | 142 | backend.shutdown() |
| 98 | print(json.dumps({'generated_config': 'validated', 'multiple_cookies': 'secure', 'logout_cookies': 'secure', 'default_route': 'unchanged'})) | 143 | 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'})) |
| 99 | 144 | ||
| 100 | 145 | ||
| 101 | if __name__ == '__main__': | 146 | if __name__ == '__main__': |
tools/router-jellyfin-test.py+36-1| ... | @@ -89,8 +89,10 @@ class GatewayAssociation(unittest.TestCase): | ... | @@ -89,8 +89,10 @@ class GatewayAssociation(unittest.TestCase): |
| 89 | stages = root / 'stages' | 89 | stages = root / 'stages' |
| 90 | stages.mkdir() | 90 | stages.mkdir() |
| 91 | jellyfin = 'jellyfin-preview-1234abcd' | 91 | jellyfin = 'jellyfin-preview-1234abcd' |
| 92 | shale = 'shale-preview-5678abcd' | ||
| 92 | dashboard = 'dashboard-preview-1234abcd' | 93 | dashboard = 'dashboard-preview-1234abcd' |
| 93 | (stages / (jellyfin + '.json')).write_text(json.dumps({'sourceId': 'jellyfin', 'gatewayStage': dashboard})) | 94 | (stages / (jellyfin + '.json')).write_text(json.dumps({'sourceId': 'jellyfin', 'gatewayStage': dashboard})) |
| 95 | (stages / (shale + '.json')).write_text(json.dumps({'sourceId': 'shale', 'gatewayStage': dashboard})) | ||
| 94 | mount = root / 'staging' / dashboard | 96 | mount = root / 'staging' / dashboard |
| 95 | mount.mkdir(parents=True) | 97 | mount.mkdir(parents=True) |
| 96 | proxy_proof = mount / 'proxy.token' | 98 | proxy_proof = mount / 'proxy.token' |
| ... | @@ -103,6 +105,7 @@ class GatewayAssociation(unittest.TestCase): | ... | @@ -103,6 +105,7 @@ class GatewayAssociation(unittest.TestCase): |
| 103 | 'jellyfin': (1111, ['caddy-host=jelly.paperclover.net']), | 105 | 'jellyfin': (1111, ['caddy-host=jelly.paperclover.net']), |
| 104 | 'copyparty': (1112, ['caddy-host=file.paperclover.net']), | 106 | 'copyparty': (1112, ['caddy-host=file.paperclover.net']), |
| 105 | jellyfin: (2222, ['caddy-host=' + jellyfin + '.paperclover.net', 'caddy-jellyfin-auth=true']), | 107 | jellyfin: (2222, ['caddy-host=' + jellyfin + '.paperclover.net', 'caddy-jellyfin-auth=true']), |
| 108 | shale: (6666, ['caddy-host=' + shale + '.paperclover.net']), | ||
| 106 | dashboard: (3333, ['caddy-host=' + dashboard + '.paperclover.net']), | 109 | dashboard: (3333, ['caddy-host=' + dashboard + '.paperclover.net']), |
| 107 | } | 110 | } |
| 108 | 111 | ||
| ... | @@ -130,8 +133,23 @@ class GatewayAssociation(unittest.TestCase): | ... | @@ -130,8 +133,23 @@ class GatewayAssociation(unittest.TestCase): |
| 130 | dashboard_route = rendered.split(dashboard + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | 133 | dashboard_route = rendered.split(dashboard + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] |
| 131 | self.assertIn('request_header Snowglobe-Proxy-Token ' + 'b' * 64, dashboard_route) | 134 | self.assertIn('request_header Snowglobe-Proxy-Token ' + 'b' * 64, dashboard_route) |
| 132 | self.assertNotIn('tracing', preview) | 135 | self.assertNotIn('tracing', preview) |
| 136 | shale_route = rendered.split(shale + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | ||
| 137 | self.assertIn('reverse_proxy 127.0.0.1:3333', shale_route) | ||
| 138 | self.assertIn('Snowglobe-Shale-Upstream "127.0.0.1:6666"', shale_route) | ||
| 139 | self.assertIn('b' * 64, shale_route) | ||
| 140 | self.assertNotIn('a' * 64, shale_route) | ||
| 141 | self.assertNotIn('reverse_proxy 127.0.0.1:4444', shale_route) | ||
| 142 | (stages / (shale + '.json')).write_text(json.dumps({'sourceId': 'jellyfin', 'gatewayStage': dashboard})) | ||
| 143 | denied = router.render('fixture').split(shale + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | ||
| 144 | self.assertIn('respond 503', denied) | ||
| 145 | self.assertNotIn('/auth/shale/page', denied) | ||
| 146 | (stages / (shale + '.json')).write_text(json.dumps({'sourceId': 'shale'})) | ||
| 147 | unbound = router.render('fixture').split(shale + '.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | ||
| 148 | self.assertIn('reverse_proxy 127.0.0.1:4444', unbound) | ||
| 149 | self.assertIn('a' * 64, unbound) | ||
| 133 | files = rendered.split('file.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | 150 | files = rendered.split('file.paperclover.net {', 1)[1].split('\n}\n', 1)[0] |
| 134 | self.assertNotIn('@zig_mirror', files) | 151 | self.assertIn('handle @mirror_private {\n respond 404', files) |
| 152 | self.assertIn('handle @zig_mirror {\n respond 503', files) | ||
| 135 | services['zig-mirror'] = (5555, ['caddy-host=zig.paperclover.net']) | 153 | services['zig-mirror'] = (5555, ['caddy-host=zig.paperclover.net']) |
| 136 | enabled = router.render('fixture') | 154 | enabled = router.render('fixture') |
| 137 | files = enabled.split('file.paperclover.net {', 1)[1].split('\n}\n', 1)[0] | 155 | files = enabled.split('file.paperclover.net {', 1)[1].split('\n}\n', 1)[0] |
| ... | @@ -219,6 +237,17 @@ class JellyfinRouter(unittest.TestCase): | ... | @@ -219,6 +237,17 @@ class JellyfinRouter(unittest.TestCase): |
| 219 | self.assertEqual(Backend.requests, []) | 237 | self.assertEqual(Backend.requests, []) |
| 220 | self.assertEqual(self.request('GET', '/QuickConnect/Initiate')[0], 401) | 238 | self.assertEqual(self.request('GET', '/QuickConnect/Initiate')[0], 401) |
| 221 | 239 | ||
| 240 | def test_legacy_public_route_case_keeps_exact_method_and_path_boundary(self): | ||
| 241 | for path in ['/users/public', '/branding/configuration', '/qUiCkCoNnEcT/eNaBlEd']: | ||
| 242 | self.assertEqual(self.request('GET', path)[0], 200) | ||
| 243 | self.assertEqual(Gateway.requests[-1][1], '/auth/jellyfin/public') | ||
| 244 | self.assertEqual(self.request('POST', '/quickconnect/initiate', body='{}')[0], 200) | ||
| 245 | for path in ['/users/public/extra', '/quickconnect/initiate/extra', '/users/private', '/items/known']: | ||
| 246 | self.assertEqual(self.request('GET', path)[0], 401) | ||
| 247 | self.assertEqual(self.request('POST', '/users/public', body='{}')[0], 401) | ||
| 248 | self.assertEqual(self.request('POST', '/qUiCkCoNnEcT/aUtHoRiZe', body='{}')[0], 404) | ||
| 249 | self.assertEqual(Backend.requests, []) | ||
| 250 | |||
| 222 | def test_authenticated_ranges_head_websocket_and_normalized_credential(self): | 251 | def test_authenticated_ranges_head_websocket_and_normalized_credential(self): |
| 223 | headers = {'Authorization': 'allow', 'Cookie': 'private=never-upstream', 'X-Emby-Token': 'legacy', 'Snowglobe-Jellyfin-Upstream': 'attacker'} | 252 | headers = {'Authorization': 'allow', 'Cookie': 'private=never-upstream', 'X-Emby-Token': 'legacy', 'Snowglobe-Jellyfin-Upstream': 'attacker'} |
| 224 | 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'}) | 253 | 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): | ... | @@ -230,6 +259,12 @@ class JellyfinRouter(unittest.TestCase): |
| 230 | self.assertEqual((method, path), ('GET', '/Videos/fixture/stream?static=true')) | 259 | self.assertEqual((method, path), ('GET', '/Videos/fixture/stream?static=true')) |
| 231 | self.assertEqual(upstream['Authorization'], 'MediaBrowser Token="normalized"') | 260 | self.assertEqual(upstream['Authorization'], 'MediaBrowser Token="normalized"') |
| 232 | self.assertFalse(any(k.lower().startswith(('snowglobe-', 'x-snowglobe-')) or k.lower() in {'cookie', 'x-emby-token'} for k in upstream)) | 261 | self.assertFalse(any(k.lower().startswith(('snowglobe-', 'x-snowglobe-')) or k.lower() in {'cookie', 'x-emby-token'} for k in upstream)) |
| 262 | for path, expected in [ | ||
| 263 | ('/Videos/fixture/stream?ApiKey=old', '/Videos/fixture/stream'), | ||
| 264 | ('/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'), | ||
| 265 | ]: | ||
| 266 | self.assertEqual(self.request('GET', path, headers=headers)[0], 200) | ||
| 267 | self.assertEqual(Backend.requests[-1][1], expected) | ||
| 233 | status, _, body = self.request('HEAD', '/Videos/fixture/stream', headers=headers) | 268 | status, _, body = self.request('HEAD', '/Videos/fixture/stream', headers=headers) |
| 234 | self.assertEqual((status, body), (200, b'')) | 269 | self.assertEqual((status, body), (200, b'')) |
| 235 | self.assertEqual(Backend.requests[-1][0], 'HEAD') | 270 | self.assertEqual(Backend.requests[-1][0], 'HEAD') |
tools/router.py+47-19| ... | @@ -99,6 +99,14 @@ def shale_git_routes(upstreams): | ... | @@ -99,6 +99,14 @@ def shale_git_routes(upstreams): |
| 99 | return [*lines, " }", " }"] | 99 | return [*lines, " }", " }"] |
| 100 | 100 | ||
| 101 | 101 | ||
| 102 | def shale_login_routes(host): | ||
| 103 | # Shale parses the login Referer as an absolute URL after OAuth. | ||
| 104 | return_url = rf"^https://{re.escape(host)}/(?:[A-Za-z0-9._~!$&'()*+,;=:@/?#\[\]-]|%[0-9A-Fa-f]{{2}})*$" | ||
| 105 | return [" @shale_login_return {", " path /-/login", | ||
| 106 | f" not header_regexp Referer `{return_url}`", " }", | ||
| 107 | f" request_header @shale_login_return Referer https://{host}/"] | ||
| 108 | |||
| 109 | |||
| 102 | def zig_mirror_routes(service, routes): | 110 | def zig_mirror_routes(service, routes): |
| 103 | if service == "copyparty": | 111 | if service == "copyparty": |
| 104 | mirror = "zig-mirror" | 112 | mirror = "zig-mirror" |
| ... | @@ -155,21 +163,33 @@ def dashboard_preview_proof(service): | ... | @@ -155,21 +163,33 @@ def dashboard_preview_proof(service): |
| 155 | return proof | 163 | return proof |
| 156 | 164 | ||
| 157 | 165 | ||
| 158 | def jellyfin_gateway(service, routes, proof): | 166 | def service_gateway(service, routes, proof): |
| 159 | if service == "jellyfin": | 167 | source = service.split("-preview-", 1)[0] |
| 168 | if source not in {"jellyfin", "shale"}: | ||
| 169 | return None, None | ||
| 170 | stages = os.path.join(os.environ.get("SNOWGLOBE_STATE", "/var/lib/snowglobe"), "stages") | ||
| 171 | gateway = None | ||
| 172 | if service != source: | ||
| 173 | try: | ||
| 174 | with open(os.path.join(stages, service + ".json")) as file: | ||
| 175 | stage = json.load(file) | ||
| 176 | except FileNotFoundError: | ||
| 177 | return None, None | ||
| 178 | if stage.get("sourceId") != source: | ||
| 179 | return None, None | ||
| 180 | gateway = stage.get("gatewayStage") | ||
| 181 | if gateway is None and source != "shale": | ||
| 182 | return None, None | ||
| 183 | if gateway is not None and (not isinstance(gateway, str) or not re.fullmatch(r"dashboard-preview-[0-9a-f]{8}", gateway)): | ||
| 184 | return None, None | ||
| 185 | if gateway is None: | ||
| 160 | port = int(os.environ["SNOWGLOBE_DASHBOARD_PORT"]) | 186 | port = int(os.environ["SNOWGLOBE_DASHBOARD_PORT"]) |
| 161 | if not 1 <= port <= 65535: | 187 | if not 1 <= port <= 65535: |
| 162 | raise ValueError("invalid dashboard port for Jellyfin") | 188 | raise ValueError("invalid dashboard port") |
| 163 | return f"127.0.0.1:{port}", proof | 189 | return f"127.0.0.1:{port}", proof |
| 164 | stages = os.path.join(os.environ.get("SNOWGLOBE_STATE", "/var/lib/snowglobe"), "stages") | ||
| 165 | try: | 190 | try: |
| 166 | with open(os.path.join(stages, service + ".json")) as source: | 191 | with open(os.path.join(stages, gateway + ".json")) as file: |
| 167 | stage = json.load(source) | 192 | if json.load(file).get("sourceId") != "dashboard": |
| 168 | gateway = stage.get("gatewayStage", "") | ||
| 169 | if stage.get("sourceId") != "jellyfin" or not isinstance(gateway, str) or not re.fullmatch(r"dashboard-preview-[0-9a-f]{8}", gateway): | ||
| 170 | return None, None | ||
| 171 | with open(os.path.join(stages, gateway + ".json")) as source: | ||
| 172 | if json.load(source).get("sourceId") != "dashboard": | ||
| 173 | return None, None | 193 | return None, None |
| 174 | except FileNotFoundError: | 194 | except FileNotFoundError: |
| 175 | return None, None | 195 | return None, None |
| ... | @@ -178,7 +198,7 @@ def jellyfin_gateway(service, routes, proof): | ... | @@ -178,7 +198,7 @@ def jellyfin_gateway(service, routes, proof): |
| 178 | return None, None | 198 | return None, None |
| 179 | upstream = next(iter(upstreams)) | 199 | upstream = next(iter(upstreams)) |
| 180 | if not re.fullmatch(r"127\.0\.0\.1:[0-9]+|\[::1\]:[0-9]+", upstream): | 200 | if not re.fullmatch(r"127\.0\.0\.1:[0-9]+|\[::1\]:[0-9]+", upstream): |
| 181 | raise ValueError("Jellyfin preview gateway must bind loopback") | 201 | raise ValueError("preview gateway must bind loopback") |
| 182 | proof = dashboard_preview_proof(gateway) | 202 | proof = dashboard_preview_proof(gateway) |
| 183 | return (upstream, proof) if proof else (None, None) | 203 | return (upstream, proof) if proof else (None, None) |
| 184 | 204 | ||
| ... | @@ -204,7 +224,8 @@ def jellyfin_routes(service, upstreams, gateway, proof): | ... | @@ -204,7 +224,8 @@ def jellyfin_routes(service, upstreams, gateway, proof): |
| 204 | } | 224 | } |
| 205 | for method, allowed in paths.items(): | 225 | for method, allowed in paths.items(): |
| 206 | name = "@jellyfin_public_" + method.lower() | 226 | name = "@jellyfin_public_" + method.lower() |
| 207 | lines += [f" {name} {{", f" method {method}", f" path {allowed}", " }", | 227 | pattern = "(?i)^(?:" + "|".join(re.escape(path) for path in allowed.split()) + ")$" |
| 228 | lines += [f" {name} {{", f" method {method}", f" path_regexp {name[1:]} {pattern}", " }", | ||
| 208 | f" handle {name} {{", f" reverse_proxy {gateway} {{", " rewrite /auth/jellyfin/public?", | 229 | f" handle {name} {{", f" reverse_proxy {gateway} {{", " rewrite /auth/jellyfin/public?", |
| 209 | *[" " + line for line in trusted], " }", " }"] | 230 | *[" " + line for line in trusted], " }", " }"] |
| 210 | lines += [" @jellyfin_options method OPTIONS", " handle @jellyfin_options {", | 231 | lines += [" @jellyfin_options method OPTIONS", " handle @jellyfin_options {", |
| ... | @@ -216,7 +237,8 @@ def jellyfin_routes(service, upstreams, gateway, proof): | ... | @@ -216,7 +237,8 @@ def jellyfin_routes(service, upstreams, gateway, proof): |
| 216 | " request_header -X-Emby-Token", " request_header -X-MediaBrowser-Token", | 237 | " request_header -X-Emby-Token", " request_header -X-MediaBrowser-Token", |
| 217 | " request_header -X-Emby-Authorization", " request_header -Cookie", | 238 | " request_header -X-Emby-Authorization", " request_header -Cookie", |
| 218 | " request_header Authorization {rp.header.X-Snowglobe-Jellyfin-Authorization}", | 239 | " request_header Authorization {rp.header.X-Snowglobe-Jellyfin-Authorization}", |
| 219 | " rewrite * {rp.header.X-Snowglobe-Jellyfin-Uri}", f" reverse_proxy {upstream} {{", | 240 | # Caddy needs a literal query delimiter to replace a query from a response header. |
| 241 | " rewrite * {rp.header.X-Snowglobe-Jellyfin-Uri}?", f" reverse_proxy {upstream} {{", | ||
| 220 | " header_up -Snowglobe-*", " header_up -X-Snowglobe-*", " }", | 242 | " header_up -Snowglobe-*", " header_up -X-Snowglobe-*", " }", |
| 221 | " }", " }", " }", " }", " handle_errors {", " respond 503", " }"] | 243 | " }", " }", " }", " }", " handle_errors {", " respond 503", " }"] |
| 222 | return lines | 244 | return lines |
| ... | @@ -325,6 +347,7 @@ def render(token): | ... | @@ -325,6 +347,7 @@ def render(token): |
| 325 | # Keep the Origin guard before every static/proxy handle; | 347 | # Keep the Origin guard before every static/proxy handle; |
| 326 | # otherwise Caddy sorts those handles ahead of respond. | 348 | # otherwise Caddy sorts those handles ahead of respond. |
| 327 | lines += [" route {", *shale_write_routes(host), | 349 | lines += [" route {", *shale_write_routes(host), |
| 350 | *shale_login_routes(host), | ||
| 328 | *shale_git_routes(" ".join(sorted(route["upstreams"])))] | 351 | *shale_git_routes(" ".join(sorted(route["upstreams"])))] |
| 329 | if re.fullmatch(r"shale-preview-[0-9a-f]{8}", service): | 352 | if re.fullmatch(r"shale-preview-[0-9a-f]{8}", service): |
| 330 | lines += shale_attachment_routes(" ".join(sorted(route["upstreams"])), | 353 | lines += shale_attachment_routes(" ".join(sorted(route["upstreams"])), |
| ... | @@ -342,7 +365,7 @@ def render(token): | ... | @@ -342,7 +365,7 @@ def render(token): |
| 342 | raise ValueError("invalid dashboard port for Shale linking") | 365 | raise ValueError("invalid dashboard port for Shale linking") |
| 343 | lines += shale_mcp_routes(port) | 366 | lines += shale_mcp_routes(port) |
| 344 | if route["jellyfinAuth"]: | 367 | if route["jellyfinAuth"]: |
| 345 | gateway, proof = jellyfin_gateway(service, routes, dashboard_proof) | 368 | gateway, proof = service_gateway(service, routes, dashboard_proof) |
| 346 | if gateway: | 369 | if gateway: |
| 347 | lines += jellyfin_routes(service, route["upstreams"], gateway, proof) | 370 | lines += jellyfin_routes(service, route["upstreams"], gateway, proof) |
| 348 | else: | 371 | else: |
| ... | @@ -391,8 +414,10 @@ def render(token): | ... | @@ -391,8 +414,10 @@ def render(token): |
| 391 | for request, path in assets.get("dirs", {}).items(): | 414 | for request, path in assets.get("dirs", {}).items(): |
| 392 | if not request.startswith("/") or " " in request or "\n" in request: | 415 | if not request.startswith("/") or " " in request or "\n" in request: |
| 393 | raise ValueError(f"invalid static prefix: {request}") | 416 | raise ValueError(f"invalid static prefix: {request}") |
| 394 | lines += [f" handle_path {request}* {{", f" root * {json.dumps(path)}", | 417 | lines += [f" handle_path {request}* {{", f" root * {json.dumps(path)}"] |
| 395 | " file_server", " }"] | 418 | if request == "/.static/" and (service == "copyparty" or re.fullmatch(r"copyparty-preview-[0-9a-f]{8}", service)): |
| 419 | lines += [" @public_font path *.woff2", ' header @public_font Access-Control-Allow-Origin "*"'] | ||
| 420 | lines += [" file_server", " }"] | ||
| 396 | metrics_path = assets.get("metricsPaths", {}).get(host) | 421 | metrics_path = assets.get("metricsPaths", {}).get(host) |
| 397 | if metrics_path: | 422 | if metrics_path: |
| 398 | if not re.fullmatch(r"/[A-Za-z0-9/._-]+", metrics_path): | 423 | if not re.fullmatch(r"/[A-Za-z0-9/._-]+", metrics_path): |
| ... | @@ -448,8 +473,11 @@ def render(token): | ... | @@ -448,8 +473,11 @@ def render(token): |
| 448 | shale_page = service == "shale" or re.fullmatch(r"shale-preview-[0-9a-f]{8}", service) | 473 | shale_page = service == "shale" or re.fullmatch(r"shale-preview-[0-9a-f]{8}", service) |
| 449 | page_headers = [] | 474 | page_headers = [] |
| 450 | if shale_page: | 475 | if shale_page: |
| 451 | page_upstream = f"127.0.0.1:{int(os.environ['SNOWGLOBE_DASHBOARD_PORT'])}" | 476 | page_upstream, proof = service_gateway(service, routes, dashboard_proof) |
| 452 | page_headers = [f" request_header Snowglobe-Proxy-Token {dashboard_proof}", | 477 | if not page_upstream: |
| 478 | lines += [" respond 503", " }", "}"] | ||
| 479 | continue | ||
| 480 | page_headers = [f" request_header Snowglobe-Proxy-Token {proof}", | ||
| 453 | f" request_header Snowglobe-Shale-Upstream {json.dumps(sorted(route['upstreams'])[0])}", | 481 | f" request_header Snowglobe-Shale-Upstream {json.dumps(sorted(route['upstreams'])[0])}", |
| 454 | " request_header Snowglobe-Shale-Uri {uri}", | 482 | " request_header Snowglobe-Shale-Uri {uri}", |
| 455 | " rewrite * /auth/shale/page"] | 483 | " rewrite * /auth/shale/page"] |