//! R4.11 proof that the worker's HTTPS client validates server certificates: an //! in-process TLS server presents a self-signed certificate for 127.0.0.1, and the //! `HttpBackend` built from a plain `https://127.0.0.1:` configuration must //! refuse the handshake before any request byte leaves. If validation were off //! (or the bundled roots trusted anything), the server would answer the RPC with //! an empty job list and the call would succeed. The proof covers validation //! itself, not a specific distrusted root: the bundled store is webpki-roots'. #![allow(clippy::unwrap_used, clippy::expect_used)] use std::io::{Read, Write}; use std::net::{Ipv4Addr, TcpListener}; use std::sync::Arc; use std::thread; use faunosteo_worker::backend::{Backend, BackendError}; use faunosteo_worker::config::Config; use faunosteo_worker::http::HttpBackend; use rcgen::{CertifiedKey, generate_simple_self_signed}; use rustls::pki_types::PrivateKeyDer; use rustls::{ServerConfig, ServerConnection, Stream}; /// What the server saw: a completed handshake with the request's first bytes, or /// the connection dropped during the handshake (the client's refusal). enum Seen { Request(String), Dropped, } fn self_signed_server() -> Arc { let CertifiedKey { cert, signing_key } = generate_simple_self_signed(vec!["127.0.0.1".to_owned()]).unwrap(); let key = PrivateKeyDer::try_from(signing_key.serialize_der()).unwrap(); let config = ServerConfig::builder_with_provider(Arc::new(rustls::crypto::ring::default_provider())) .with_safe_default_protocol_versions() .unwrap() .with_no_client_auth() .with_single_cert(vec![cert.der().clone()], key) .unwrap(); Arc::new(config) } /// Accepts one connection, completes the handshake if the client lets it, then /// answers an empty PostgREST array to whatever request arrives. fn serve_once(listener: TcpListener, config: Arc) -> Seen { let (mut tcp, _) = listener.accept().unwrap(); let mut tls = ServerConnection::new(config).unwrap(); let mut stream = Stream::new(&mut tls, &mut tcp); let mut head = [0u8; 256]; let n = match stream.read(&mut head) { Ok(n) => n, Err(_) => return Seen::Dropped, }; let _ = stream.write_all( b"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: 2\r\n\r\n[]", ); Seen::Request(String::from_utf8_lossy(&head[..n]).into_owned()) } #[test] fn a_self_signed_server_certificate_is_refused_before_any_request() { let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0)).unwrap(); let port = listener.local_addr().unwrap().port(); let server = thread::spawn({ let config = self_signed_server(); move || serve_once(listener, config) }); let url = format!("https://127.0.0.1:{port}"); let config = Config::from_vars(|name| match name { "SUPABASE_URL" => Some(url.clone()), "SUPABASE_SERVICE_ROLE_KEY" => Some("service-key-under-test".to_owned()), _ => None, }) .expect("config"); let backend = HttpBackend::new(&config); let outcome = backend.claim_jobs(1); let message = match outcome { Err(BackendError::Network(message)) => message, other => panic!("expected a network error, got {other:?}"), }; assert!( message.to_ascii_lowercase().contains("certificate"), "the refusal names the certificate: {message}" ); // The service key never reaches a server the client does not trust. assert!(!message.contains("service-key-under-test")); match server.join().unwrap() { Seen::Dropped => {} Seen::Request(head) => panic!("the server received a request: {head:?}"), } }