oxedyne/fe2o3/fe2o3_net/tests/ws_client_url.rs
16.6 KiB, 13 runs
created by r1870400018:76911, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | #![cfg(feature = "async")] |
| 2 | //! `WsClient::connect_url` against hand-written loopback servers, plain and TLS: the scheme |
| 3 | //! chooses the transport, the query and further fields reach the upgrade request as given, a |
| 4 | //! refusal quotes its reason, and ping, pong and the close handshake work over TLS as over TCP. |
| 5 | |
| 6 | use oxedyne_fe2o3_core::prelude::*; |
| 7 | use oxedyne_fe2o3_net::{ |
| 8 | http::status::{ |
| 9 | HttpStatus, |
| 10 | UpgradeRefused, |
| 11 | }, |
| 12 | tls::ensure_crypto_provider, |
| 13 | ws::{ |
| 14 | accept_key, |
| 15 | connect_request_with, |
| 16 | encode_message, |
| 17 | read_frame, |
| 18 | status::WebSocketStatusCode, |
| 19 | WebSocketFrame, |
| 20 | WebSocketLimits, |
| 21 | WebSocketMessage, |
| 22 | WsClient, |
| 23 | WsConnect, |
| 24 | }, |
| 25 | }; |
| 26 | |
| 27 | use std::{ |
| 28 | future::Future, |
| 29 | sync::Arc, |
| 30 | time::Duration, |
| 31 | }; |
| 32 | |
| 33 | use tokio::{ |
| 34 | io::{ |
| 35 | AsyncRead, |
| 36 | AsyncReadExt, |
| 37 | AsyncWrite, |
| 38 | AsyncWriteExt, |
| 39 | }, |
| 40 | net::{ |
| 41 | TcpListener, |
| 42 | TcpStream, |
| 43 | }, |
| 44 | task::JoinHandle, |
| 45 | }; |
| 46 | use tokio_rustls::{ |
| 47 | rustls::{ |
| 48 | pki_types::{ |
| 49 | CertificateDer, |
| 50 | PrivateKeyDer, |
| 51 | PrivatePkcs8KeyDer, |
| 52 | }, |
| 53 | ClientConfig, |
| 54 | RootCertStore, |
| 55 | ServerConfig, |
| 56 | }, |
| 57 | server::TlsStream, |
| 58 | TlsAcceptor, |
| 59 | }; |
| 60 | |
| 61 | |
| 62 | const HOST: &str = "localhost"; |
| 63 | const WAIT: Duration = Duration::from_secs(5); |
| 64 | const TARGET: &str = "/api/chat/relay?v=1&device=d0&ts=1793352150&nonce=n_0-x&sigv=2&sig=YWJj-_w"; |
| 65 | |
| 66 | /// A certificate for [`HOST`], a server holding it, and a client whose one root it is. |
| 67 | fn tls_pair() -> Outcome<(ServerConfig, ClientConfig)> { |
| 68 | ensure_crypto_provider(); |
| 69 | let cert = res!(rcgen::generate_simple_self_signed(vec![HOST.to_string()]), Init); |
| 70 | let der = CertificateDer::from(res!(cert.serialize_der(), Init)); |
| 71 | let key = PrivateKeyDer::Pkcs8(PrivatePkcs8KeyDer::from(cert.serialize_private_key_der())); |
| 72 | let srv = res!(ServerConfig::builder() |
| 73 | .with_no_client_auth() |
| 74 | .with_single_cert(vec![der.clone()], key), Init); |
| 75 | let mut roots = RootCertStore::empty(); |
| 76 | res!(roots.add(der), Init); |
| 77 | let cli = ClientConfig::builder().with_root_certificates(roots).with_no_client_auth(); |
| 78 | Ok((srv, cli)) |
| 79 | } |
| 80 | |
| 81 | /// Binds a loopback listener and runs `serve` on the first connection, returning the port and |
| 82 | /// the server task. |
| 83 | async fn spawn_plain<F, Fut>(serve: F) -> Outcome<(u16, JoinHandle<Outcome<()>>)> |
| 84 | where |
| 85 | F: FnOnce(TcpStream, u16) -> Fut + Send + 'static, |
| 86 | Fut: Future<Output = Outcome<()>> + Send + 'static, |
| 87 | { |
| 88 | let listener = res!(TcpListener::bind("127.0.0.1:0").await); |
| 89 | let port = res!(listener.local_addr()).port(); |
| 90 | let task = tokio::spawn(async move { |
| 91 | let (tcp, _) = res!(listener.accept().await); |
| 92 | serve(tcp, port).await |
| 93 | }); |
| 94 | Ok((port, task)) |
| 95 | } |
| 96 | |
| 97 | /// As [`spawn_plain`], with the connection TLS-wrapped under `cfg`. |
| 98 | async fn spawn_tls<F, Fut>(cfg: ServerConfig, serve: F) -> Outcome<(u16, JoinHandle<Outcome<()>>)> |
| 99 | where |
| 100 | F: FnOnce(TlsStream<TcpStream>, u16) -> Fut + Send + 'static, |
| 101 | Fut: Future<Output = Outcome<()>> + Send + 'static, |
| 102 | { |
| 103 | spawn_plain(move |tcp, port| async move { |
| 104 | let tls = res!(TlsAcceptor::from(Arc::new(cfg)).accept(tcp).await); |
| 105 | serve(tls, port).await |
| 106 | }).await |
| 107 | } |
| 108 | |
| 109 | async fn read_request<S: AsyncRead + Unpin>(stream: &mut S) -> Outcome<String> { |
| 110 | let mut head = Vec::new(); |
| 111 | while !head.ends_with(b"\r\n\r\n") { |
| 112 | let mut b = [0u8; 1]; |
| 113 | res!(stream.read_exact(&mut b).await); |
| 114 | head.push(b[0]); |
| 115 | } |
| 116 | Ok(res!(String::from_utf8(head))) |
| 117 | } |
| 118 | |
| 119 | fn field<'a>(request: &'a str, name: &str) -> Option<&'a str> { |
| 120 | for line in request.lines() { |
| 121 | if let Some((n, v)) = line.split_once(':') { |
| 122 | if n.trim().eq_ignore_ascii_case(name) { |
| 123 | return Some(v.trim()); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | None |
| 128 | } |
| 129 | |
| 130 | /// Completes the handshake, returning the request text. |
| 131 | async fn accept<S: AsyncRead + AsyncWrite + Unpin>(stream: &mut S) -> Outcome<String> { |
| 132 | let request = res!(read_request(stream).await); |
| 133 | let key = res!(field(&request, "sec-websocket-key") |
| 134 | .ok_or_else(|| err!("No Sec-WebSocket-Key:\n{}", request; Test, Missing))); |
| 135 | res!(stream.write_all(fmt!("HTTP/1.1 101 Switching Protocols\r\n\ |
| 136 | Upgrade: websocket\r\n\ |
| 137 | Connection: Upgrade\r\n\ |
| 138 | Sec-WebSocket-Accept: {}\r\n\r\n", accept_key(key)).as_bytes()).await); |
| 139 | Ok(request) |
| 140 | } |
| 141 | |
| 142 | async fn read_masked<S: AsyncRead + Unpin>(stream: &mut S) -> Outcome<WebSocketFrame> { |
| 143 | match res!(read_frame(stream, 1_024, WebSocketLimits::default(), 0).await) { |
| 144 | Some(frame) if frame.masked => Ok(frame), |
| 145 | Some(frame) => Err(err!("Unmasked client frame {:#x}.", frame.opcode; Test, Invalid)), |
| 146 | None => Err(err!("The client closed before sending a frame."; Test, Missing)), |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | async fn join(task: JoinHandle<Outcome<()>>) -> Outcome<()> { |
| 151 | match task.await { |
| 152 | Ok(outcome) => outcome, |
| 153 | Err(e) => Err(err!(e, "The test server task failed."; Test)), |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /// Over `wss`: the query and a further field arrive verbatim, a text echoes, a ping is answered |
| 158 | /// with its pong, and a close from the client completes when the server echoes it. |
| 159 | #[tokio::test] |
| 160 | async fn test_wss_url_carries_query_and_fields_and_closes_cleanly_00() -> Outcome<()> { |
| 161 | let (srv, cli) = res!(tls_pair()); |
| 162 | let (port, server) = res!(spawn_tls(srv, move |mut s, port| async move { |
| 163 | let request = res!(accept(&mut s).await); |
| 164 | if !request.starts_with(&fmt!("GET {} HTTP/1.1\r\n", TARGET)) |
| 165 | || field(&request, "host") != Some(&fmt!("{}:{}", HOST, port)) |
| 166 | || field(&request, "x-probe") != Some("one two") |
| 167 | || field(&request, "origin") != Some("https://next.example") |
| 168 | { |
| 169 | return Err(err!("Unexpected upgrade request:\n{}", request; Test, Mismatch)); |
| 170 | } |
| 171 | let text = res!(read_masked(&mut s).await); |
| 172 | res!(s.write_all(&res!(encode_message( |
| 173 | &WebSocketMessage::Text(res!(String::from_utf8(text.payload))), false, 1_024, 1_024, |
| 174 | ))).await); |
| 175 | res!(s.write_all(&res!(encode_message( |
| 176 | &WebSocketMessage::Ping(b"hb".to_vec()), false, 125, 125))).await); |
| 177 | let pong = res!(read_masked(&mut s).await); |
| 178 | if pong.opcode != 0xA || pong.payload != b"hb" { |
| 179 | return Err(err!("Expected a pong echoing 'hb', got {:?}.", pong; Test, Mismatch)); |
| 180 | } |
| 181 | let close = res!(read_masked(&mut s).await); |
| 182 | if close.opcode != 0x8 || close.payload[..2] != 1000u16.to_be_bytes() { |
| 183 | return Err(err!("Expected a normal close, got {:?}.", close; Test, Mismatch)); |
| 184 | } |
| 185 | res!(s.write_all(&res!(encode_message( |
| 186 | &WebSocketMessage::Close(Some(WebSocketStatusCode::NormalClosure), None), |
| 187 | false, 125, 125))).await); |
| 188 | Ok(()) |
| 189 | }).await); |
| 190 | |
| 191 | let opts = WsConnect::default() |
| 192 | .origin("https://next.example") |
| 193 | .field("X-Probe", "one two") |
| 194 | .tls(Arc::new(cli)); |
| 195 | let url = fmt!("wss://{}:{}{}", HOST, port, TARGET); |
| 196 | let mut client = res!(WsClient::connect_url(&url, &opts).await); |
| 197 | res!(client.send_text("over tls").await); |
| 198 | match res!(client.recv(WAIT).await) { |
| 199 | Some(WebSocketMessage::Text(txt)) => req!(txt, fmt!("over tls")), |
| 200 | other => return Err(err!("Expected the echo, got {:?}.", other; Test, Mismatch)), |
| 201 | } |
| 202 | // The ping is answered inside recv, which then waits for a data message that never comes. |
| 203 | match res!(client.recv(Duration::from_millis(300)).await) { |
| 204 | None => (), |
| 205 | Some(msg) => return Err(err!("Expected nothing after the ping, got {:?}.", msg; |
| 206 | Test, Mismatch)), |
| 207 | } |
| 208 | res!(client.close(WAIT).await); |
| 209 | if client.is_open() { |
| 210 | return Err(err!("The client stayed open after its close."; Test, Unexpected)); |
| 211 | } |
| 212 | join(server).await |
| 213 | } |
| 214 | |
| 215 | /// A `ws` URL is plain TCP, the default port is left out of `Host` only when it is the scheme's, |
| 216 | /// and the target goes out unchanged. |
| 217 | #[tokio::test] |
| 218 | async fn test_ws_url_is_plain_and_keeps_its_target_00() -> Outcome<()> { |
| 219 | let (port, server) = res!(spawn_plain(move |mut s, port| async move { |
| 220 | let request = res!(accept(&mut s).await); |
| 221 | if !request.starts_with(&fmt!("GET {} HTTP/1.1\r\n", TARGET)) |
| 222 | || field(&request, "host") != Some(&fmt!("127.0.0.1:{}", port)) |
| 223 | { |
| 224 | return Err(err!("Unexpected upgrade request:\n{}", request; Test, Mismatch)); |
| 225 | } |
| 226 | res!(s.write_all(&res!(encode_message( |
| 227 | &WebSocketMessage::Text(fmt!("plain")), false, 1_024, 1_024))).await); |
| 228 | Ok(()) |
| 229 | }).await); |
| 230 | |
| 231 | let url = fmt!("ws://127.0.0.1:{}{}", port, TARGET); |
| 232 | let mut client = res!(WsClient::connect_url(&url, &WsConnect::default()).await); |
| 233 | match res!(client.recv(WAIT).await) { |
| 234 | Some(WebSocketMessage::Text(txt)) => req!(txt, fmt!("plain")), |
| 235 | other => return Err(err!("Expected the greeting, got {:?}.", other; Test, Mismatch)), |
| 236 | } |
| 237 | join(server).await |
| 238 | } |
| 239 | |
| 240 | /// A server refusing before its 101 is reported with the status and the reason it gave. |
| 241 | #[tokio::test] |
| 242 | async fn test_a_refusal_quotes_its_reason_00() -> Outcome<()> { |
| 243 | let (srv, cli) = res!(tls_pair()); |
| 244 | let (port, server) = res!(spawn_tls(srv, |mut s, _| async move { |
| 245 | res!(read_request(&mut s).await); |
| 246 | res!(s.write_all(b"HTTP/1.1 403 Forbidden\r\nContent-Length: 20\r\n\ |
| 247 | Connection: close\r\n\r\ndevice not approved.").await); |
| 248 | res!(s.shutdown().await); |
| 249 | Ok(()) |
| 250 | }).await); |
| 251 | |
| 252 | let url = fmt!("https://{}:{}/api/chat/relay?v=1", HOST, port); |
| 253 | match WsClient::connect_url(&url, &WsConnect::default().tls(Arc::new(cli))).await { |
| 254 | Ok(_) => return Err(err!("A 403 was taken as a handshake."; Test, Unexpected)), |
| 255 | Err(e) => { |
| 256 | let msg = fmt!("{}", e); |
| 257 | if !msg.contains("403") || !msg.contains("device not approved") { |
| 258 | return Err(err!("The refusal lacks its status or reason: {}", msg; |
| 259 | Test, Mismatch)); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | join(server).await |
| 264 | } |
| 265 | |
| 266 | /// A refusal's status reaches the caller typed, as the error's cause; a transport failure has none. |
| 267 | #[tokio::test] |
| 268 | async fn test_a_refusal_carries_its_status_typed_00() -> Outcome<()> { |
| 269 | for (line, want) in [ |
| 270 | ("401 Unauthorized", HttpStatus::Unauthorized), |
| 271 | ("410 Gone", HttpStatus::Gone), |
| 272 | ] { |
| 273 | let (port, server) = res!(spawn_plain(move |mut s, _| async move { |
| 274 | res!(read_request(&mut s).await); |
| 275 | res!(s.write_all(fmt!("HTTP/1.1 {}\r\nContent-Length: 0\r\n\ |
| 276 | Connection: close\r\n\r\n", line).as_bytes()).await); |
| 277 | res!(s.shutdown().await); |
| 278 | Ok(()) |
| 279 | }).await); |
| 280 | let url = fmt!("ws://127.0.0.1:{}/relay", port); |
| 281 | match WsClient::connect_url(&url, &WsConnect::default()).await { |
| 282 | Ok(_) => return Err(err!("A {} was taken as a handshake.", line; Test, Unexpected)), |
| 283 | Err(e) => { |
| 284 | let got = e.find_cause::<UpgradeRefused>().map(|r| r.status); |
| 285 | if got != Some(want) { |
| 286 | return Err(err!("A {} refusal gave the cause {:?}.", line, got; |
| 287 | Test, Mismatch)); |
| 288 | } |
| 289 | let msg = e.plain(); |
| 290 | let said = fmt!("with status {}, not 101.", want); |
| 291 | if !msg.contains(&said) { |
| 292 | return Err(err!("The refusal no longer says '{}': {}", said, msg; |
| 293 | Test, Mismatch)); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | res!(join(server).await); |
| 298 | } |
| 299 | |
| 300 | // A refusal keeps its fields: a 429's Retry-After reaches the caller as a wait. |
| 301 | let (port, server) = res!(spawn_plain(move |mut s, _| async move { |
| 302 | res!(read_request(&mut s).await); |
| 303 | res!(s.write_all(b"HTTP/1.1 429 Too Many Requests\r\nRetry-After: 7\r\n\ |
| 304 | Content-Length: 0\r\nConnection: close\r\n\r\n").await); |
| 305 | res!(s.shutdown().await); |
| 306 | Ok(()) |
| 307 | }).await); |
| 308 | match WsClient::connect_url(&fmt!("ws://127.0.0.1:{}/relay", port), &WsConnect::default()) |
| 309 | .await |
| 310 | { |
| 311 | Ok(_) => return Err(err!("A 429 was taken as a handshake."; Test, Unexpected)), |
| 312 | Err(e) => { |
| 313 | let got = e.find_cause::<UpgradeRefused>() |
| 314 | .map(|r| (r.status, r.fields.retry_after())); |
| 315 | if got != Some((HttpStatus::TooManyRequests, Some(Duration::from_secs(7)))) { |
| 316 | return Err(err!("A 429 with Retry-After: 7 gave the cause {:?}.", got; |
| 317 | Test, Mismatch)); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | res!(join(server).await); |
| 322 | |
| 323 | // A port with nothing listening: the dial fails, and no status is claimed. |
| 324 | let port = { |
| 325 | let listener = res!(TcpListener::bind("127.0.0.1:0").await); |
| 326 | res!(listener.local_addr()).port() |
| 327 | }; |
| 328 | match WsClient::connect_url(&fmt!("ws://127.0.0.1:{}/relay", port), &WsConnect::default()) |
| 329 | .await |
| 330 | { |
| 331 | Ok(_) => Err(err!("A closed port gave a websocket."; Test, Unexpected)), |
| 332 | Err(e) => match e.find_cause::<UpgradeRefused>() { |
| 333 | None => Ok(()), |
| 334 | Some(r) => Err(err!("A transport failure claimed the status {}.", r.status; |
| 335 | Test, Mismatch)), |
| 336 | }, |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /// A `wss` server whose certificate the client does not trust is refused in the TLS handshake. |
| 341 | #[tokio::test] |
| 342 | async fn test_wss_refuses_an_untrusted_certificate_00() -> Outcome<()> { |
| 343 | let (srv, _) = res!(tls_pair()); |
| 344 | let listener = res!(TcpListener::bind("127.0.0.1:0").await); |
| 345 | let port = res!(listener.local_addr()).port(); |
| 346 | let server = tokio::spawn(async move { |
| 347 | let (tcp, _): (TcpStream, _) = res!(listener.accept().await); |
| 348 | // The client gives up part way, so how this side ends does not matter. |
| 349 | let _ = TlsAcceptor::from(Arc::new(srv)).accept(tcp).await; |
| 350 | Ok(()) |
| 351 | }); |
| 352 | let none = ClientConfig::builder() |
| 353 | .with_root_certificates(RootCertStore::empty()) |
| 354 | .with_no_client_auth(); |
| 355 | let url = fmt!("wss://{}:{}/", HOST, port); |
| 356 | match WsClient::connect_url(&url, &WsConnect::default().tls(Arc::new(none))).await { |
| 357 | Ok(_) => return Err(err!("An untrusted certificate was accepted."; Test, Unexpected)), |
| 358 | Err(e) => if !e.tags().contains(&ErrTag::Security) { |
| 359 | return Err(err!(e, "The refusal is not tagged as the certificate's."; Test, Mismatch)); |
| 360 | }, |
| 361 | } |
| 362 | join(server).await |
| 363 | } |
| 364 | |
| 365 | #[test] |
| 366 | fn test_a_scheme_that_is_not_a_websocket_one_is_refused_00() { |
| 367 | let rt = match tokio::runtime::Runtime::new() { |
| 368 | Ok(rt) => rt, |
| 369 | Err(e) => panic!("No runtime: {}", e), |
| 370 | }; |
| 371 | for url in ["ftp://localhost/", "file:///etc/passwd", "localhost:80/"] { |
| 372 | assert!(rt.block_on(WsClient::connect_url(url, &WsConnect::default())).is_err(), |
| 373 | "{} was accepted", url); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | #[test] |
| 378 | fn test_upgrade_fields_that_could_forge_the_request_are_refused_00() -> Outcome<()> { |
| 379 | let ok = res!(connect_request_with("h.example", "/p?a=1", None, |
| 380 | &[(fmt!("Authorization"), fmt!("Bearer abc"))])); |
| 381 | let text = fmt!("{}", ok.0.header); |
| 382 | if !text.to_lowercase().contains("authorization: bearer abc") { |
| 383 | return Err(err!("The field did not reach the request:\n{}", text; Test, Missing)); |
| 384 | } |
| 385 | for (name, value) in [ |
| 386 | ("Host", "elsewhere"), |
| 387 | ("sec-websocket-key", "AAAA"), |
| 388 | ("X-Split", "a\r\nHost: elsewhere"), |
| 389 | ("Bad Name", "x"), |
| 390 | ("", "x"), |
| 391 | ("Sec-WebSocket-Extensions","permessage-deflate"), |
| 392 | ] { |
| 393 | if connect_request_with("h.example", "/", None, &[(fmt!("{}", name), fmt!("{}", value))]) |
| 394 | .is_ok() |
| 395 | { |
| 396 | return Err(err!("The field {:?}: {:?} was accepted.", name, value; Test, Unexpected)); |
| 397 | } |
| 398 | } |
| 399 | // The path, host and origin go into the request as written, so none may break a line. |
| 400 | for (host, path, origin) in [ |
| 401 | ("h.example", "/p\r\nX-Evil: 1", None), |
| 402 | ("h.example", "/p q", None), |
| 403 | // The request line is split on any white space, so these would cut the path short. |
| 404 | ("h.example", "/a\u{3000}HTTP/1.1\u{3000}x", None), |
| 405 | ("h.example", "/a\u{a0}b", None), |
| 406 | ("h.example", "/a\u{2028}b", None), |
| 407 | ("h.example", "/caf\u{e9}", None), |
| 408 | ("h.example\r\nX-Evil: 1", "/", None), |
| 409 | ("h.example", "/", Some("a\r\nHost: b")), |
| 410 | ] { |
| 411 | if connect_request_with(host, path, origin, &[]).is_ok() { |
| 412 | return Err(err!("The request for {:?} {:?} {:?} was accepted.", host, path, origin; |
| 413 | Test, Unexpected)); |
| 414 | } |
| 415 | } |
| 416 | Ok(()) |
| 417 | } |