test(worker): the HTTPS client refuses a self-signed server certificate (in-process rustls server, rcgen dev-dependency)

This commit is contained in:
bchanot committed 2026-10-21 17:23:38 +02:00
1 parent c9dab637e4
commit a604c07f50
3 files changed
+286 -3

No files matched your search

Generated
+184 -3
View File
@@ -34,6 +34,45 @@ dependencies = [
"alloc-no-stdlib",
]
[[package]]
name = "asn1-rs"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7f43a50ac4fdca5df8e885c21b835997f0a1cdee65494a6847694a98652d9d8"
dependencies = [
"asn1-rs-derive",
"asn1-rs-impl",
"displaydoc",
"nom 7.1.3",
"num-traits",
"rusticata-macros",
"thiserror",
"time",
]
[[package]]
name = "asn1-rs-derive"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3109e49b1e4909e9db6515a30c633684d68cdeaa252f215214cb4fa1a5bfee2c"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.119",
"synstructure 0.13.2",
]
[[package]]
name = "asn1-rs-impl"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b18050c2cd6fe86c3a76584ef5e0baf286d038cda203eb6223df2cc413565f7"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.119",
]
[[package]]
name = "autocfg"
version = "1.5.1"
@@ -46,6 +85,15 @@ version = "0.23.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5"
[[package]]
name = "bit-vec"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b71798fca2c1fe1086445a7258a4bc81e6e49dcd24c8d0dd9a1e57395b603f51"
dependencies = [
"serde",
]
[[package]]
name = "bitflags"
version = "2.13.2"
@@ -226,6 +274,26 @@ dependencies = [
"hybrid-array",
]
[[package]]
name = "data-encoding"
version = "2.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4583a4551df46e2792f82ceeac45e850d2e2d5debba0b91f102385cda5b11f06"
[[package]]
name = "der-parser"
version = "10.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07da5016415d5a3c4dd39b11ed26f915f52fc4e0dc197d87908bc916e51bc1a6"
dependencies = [
"asn1-rs",
"displaydoc",
"nom 7.1.3",
"num-bigint",
"num-traits",
"rusticata-macros",
]
[[package]]
name = "deranged"
version = "0.5.9"
@@ -324,7 +392,9 @@ dependencies = [
"image",
"libc",
"libwebp-sys",
"rcgen",
"rlimit",
"rustls",
"seccompiler",
"serde",
"serde_json",
@@ -599,6 +669,12 @@ dependencies = [
"libc",
]
[[package]]
name = "lazy_static"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb"
[[package]]
name = "libc"
version = "0.2.190"
@@ -652,7 +728,7 @@ dependencies = [
"itoa",
"log",
"md-5",
"nom",
"nom 8.0.0",
"rand",
"rangemap",
"sha2",
@@ -677,6 +753,12 @@ version = "2.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
[[package]]
name = "minimal-lexical"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "miniz_oxide"
version = "0.8.9"
@@ -713,6 +795,16 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "743fb55ba31b18fb1ecef6bdc9aa2743314978ac084044301a7eee33fb99a20d"
[[package]]
name = "nom"
version = "7.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
dependencies = [
"memchr",
"minimal-lexical",
]
[[package]]
name = "nom"
version = "8.0.0"
@@ -722,12 +814,31 @@ dependencies = [
"memchr",
]
[[package]]
name = "num-bigint"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c89e69e7e0f03bea5ef08013795c25018e101932225a656383bd384495ecc367"
dependencies = [
"num-integer",
"num-traits",
]
[[package]]
name = "num-conv"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441"
[[package]]
name = "num-integer"
version = "0.1.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ce2d95d4b3734dc35aa2f45e1aa22cd416814592a4f9d9205e11affd5b8e10b"
dependencies = [
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.19"
@@ -737,6 +848,15 @@ dependencies = [
"autocfg",
]
[[package]]
name = "oid-registry"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12f40cff3dde1b6087cc5d5f5d4d65712f34016a03ed60e9c08dcc392736b5b7"
dependencies = [
"asn1-rs",
]
[[package]]
name = "once_cell"
version = "1.21.4"
@@ -842,6 +962,19 @@ version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a611d15b50743feb4c76b7d03edcb0e64f399c26961e4efe6975bc398be6aa3d"
[[package]]
name = "rcgen"
version = "0.14.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8774e05a7d0de114588e6a28fe7e71694b82614ed569d86d8b389dfbc98b8ad8"
dependencies = [
"ring",
"rustls-pki-types",
"time",
"x509-parser",
"yasna",
]
[[package]]
name = "ring"
version = "0.17.14"
@@ -874,6 +1007,15 @@ dependencies = [
"memchr",
]
[[package]]
name = "rusticata-macros"
version = "4.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "faf0c4a6ece9950b9abdb62b1cfcf2a68b3b67a10ba445b3bb85be2a293d0632"
dependencies = [
"nom 7.1.3",
]
[[package]]
name = "rustls"
version = "0.23.46"
@@ -1055,6 +1197,17 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "synstructure"
version = "0.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.119",
]
[[package]]
name = "synstructure"
version = "0.14.0"
@@ -1432,6 +1585,34 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc"
[[package]]
name = "x509-parser"
version = "0.18.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d43b0f71ce057da06bc0851b23ee24f3f86190b07203dd8f567d0b706a185202"
dependencies = [
"asn1-rs",
"data-encoding",
"der-parser",
"lazy_static",
"nom 7.1.3",
"oid-registry",
"ring",
"rusticata-macros",
"thiserror",
"time",
]
[[package]]
name = "yasna"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5f6765e852b9b4dc8e2a76843e4d64d1cea8e79bcde0b6901aea8e7c7f08282"
dependencies = [
"bit-vec",
"time",
]
[[package]]
name = "yoke"
version = "0.8.3"
@@ -1452,7 +1633,7 @@ dependencies = [
"proc-macro2",
"quote",
"syn 3.0.9",
"synstructure",
"synstructure 0.14.0",
]
[[package]]
@@ -1473,7 +1654,7 @@ dependencies = [
"proc-macro2",
"quote",
"syn 3.0.9",
"synstructure",
"synstructure 0.14.0",
]
[[package]]
+8
View File
@@ -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).
+94
View File
@@ -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:?}"),
}
}