test(worker): the HTTPS client refuses a self-signed server certificate (in-process rustls server, rcgen dev-dependency)
This commit is contained in:
1 parent
c9dab637e4
commit
a604c07f50
3 files changed
+286
-3
No files matched your search
@@ -34,6 +34,14 @@ crc32fast = { version = "1.4", optional = true }
|
||||
# Already in the tree through png: used by the iCCP-bomb fixture only.
|
||||
flate2 = { version = "1.1", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
# tests/tls.rs: an in-process TLS server with a self-signed certificate that the
|
||||
# worker's client must refuse (certificate validation proven active, R4.11).
|
||||
# rustls is already in the tree through ureq, the same features; rcgen is the
|
||||
# rustls project's certificate generator, ring provider like the client.
|
||||
rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] }
|
||||
rcgen = { version = "0.14", default-features = false, features = ["ring"] }
|
||||
|
||||
# The worker owns I/O (it is the process boundary), so the core's disallowed_methods lint is
|
||||
# off here. unsafe stays denied crate-wide; the libwebp FFI and the prctl call carry a local
|
||||
# #[allow(unsafe_code)] with a SAFETY comment each (forbid would not allow the local override).
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
//! 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:<port>` 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<ServerConfig> {
|
||||
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<ServerConfig>) -> 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:?}"),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user