oxedyne/fe2o3/fe2o3_steel/tests/proxy_hsts.rs
10.2 KiB, 3 runs
created by r1870400018:72582, 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 | //! A proxied response over HTTPS carries the front's HSTS exactly once, as Steel's own responses do. |
| 2 | //! |
| 3 | //! TLS ends at the front, so the front is where `Strict-Transport-Security` belongs; the upstream |
| 4 | //! behind it speaks plain loopback HTTP and rightly sends none. The front fills the field only where |
| 5 | //! the upstream left it empty: an upstream's own policy is relayed as sent and never joined by a |
| 6 | //! second. A visitor on the plain loopback listener is given none, the header meaning nothing there. |
| 7 | //! |
| 8 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 9 | //! Anthropic Claude |
| 10 | |
| 11 | use oxedyne_fe2o3_steel::{ |
| 12 | app::https::AppWebHandler, |
| 13 | srv::{ |
| 14 | api::ApiHandlerRegistry, |
| 15 | cfg::{ |
| 16 | ProxyRoute, |
| 17 | ServerConfig, |
| 18 | }, |
| 19 | context::{ |
| 20 | self, |
| 21 | Protocol, |
| 22 | ServerContext, |
| 23 | VhostRuntime, |
| 24 | }, |
| 25 | id, |
| 26 | webhook::WebhookRegistry, |
| 27 | ws::{ |
| 28 | handler::AppWebSocketHandler, |
| 29 | syntax::WebSocketSyntax, |
| 30 | }, |
| 31 | }, |
| 32 | }; |
| 33 | |
| 34 | use oxedyne_fe2o3_core::{ |
| 35 | file::OsPath, |
| 36 | path::NormalPath, |
| 37 | prelude::*, |
| 38 | }; |
| 39 | use oxedyne_fe2o3_jdat::version::SemVer; |
| 40 | |
| 41 | use std::{ |
| 42 | collections::HashMap, |
| 43 | path::Path, |
| 44 | sync::{ |
| 45 | Arc, |
| 46 | RwLock, |
| 47 | }, |
| 48 | }; |
| 49 | |
| 50 | use tokio::{ |
| 51 | io::{ |
| 52 | AsyncRead, |
| 53 | AsyncReadExt, |
| 54 | AsyncWrite, |
| 55 | AsyncWriteExt, |
| 56 | }, |
| 57 | net::{ |
| 58 | TcpListener, |
| 59 | TcpStream, |
| 60 | }, |
| 61 | }; |
| 62 | use tokio_rustls::{ |
| 63 | rustls::{ |
| 64 | pki_types::{ |
| 65 | CertificateDer, |
| 66 | PrivateKeyDer, |
| 67 | PrivatePkcs8KeyDer, |
| 68 | ServerName, |
| 69 | }, |
| 70 | ClientConfig, |
| 71 | RootCertStore, |
| 72 | ServerConfig as TlsServerConfig, |
| 73 | }, |
| 74 | TlsAcceptor, |
| 75 | TlsConnector, |
| 76 | }; |
| 77 | |
| 78 | const HOST: &str = "proxied.test"; |
| 79 | const OWN: &str = "max-age=60"; // the upstream's own policy |
| 80 | const FRONT: &str = "max-age=31536000; includeSubDomains"; // the front's |
| 81 | const MAX_AGE: u32 = 31_536_000; |
| 82 | |
| 83 | fn no_dbs<T>(_: fn(&Path, &[u8], bool) -> Outcome<T>) |
| 84 | -> Arc<RwLock<HashMap<String, (Arc<RwLock<T>>, id::Uid)>>> |
| 85 | { |
| 86 | Arc::new(RwLock::new(HashMap::new())) |
| 87 | } |
| 88 | |
| 89 | /// A one-shot-per-connection upstream that sends its own HSTS only when the path holds `own`. |
| 90 | async fn upstream() -> Outcome<u16> { |
| 91 | let listener = res!(TcpListener::bind("127.0.0.1:0").await, Network, Init); |
| 92 | let port = res!(listener.local_addr(), Network, Init).port(); |
| 93 | tokio::spawn(async move { |
| 94 | loop { |
| 95 | let (mut tcp, _) = match listener.accept().await { |
| 96 | Ok(c) => c, |
| 97 | Err(_) => return, |
| 98 | }; |
| 99 | tokio::spawn(async move { |
| 100 | let mut buf = Vec::new(); |
| 101 | let mut chunk = [0u8; 4096]; |
| 102 | while !buf.windows(4).any(|w| w == b"\r\n\r\n") { |
| 103 | match tcp.read(&mut chunk).await { |
| 104 | Ok(0) | Err(_) => return, |
| 105 | Ok(n) => buf.extend_from_slice(&chunk[..n]), |
| 106 | } |
| 107 | } |
| 108 | let head = String::from_utf8_lossy(&buf).to_string(); |
| 109 | let line = head.lines().next().unwrap_or("").to_string(); |
| 110 | let own = match line.contains("own") { |
| 111 | true => fmt!("Strict-Transport-Security: {}\r\n", OWN), |
| 112 | false => String::new(), |
| 113 | }; |
| 114 | let out = fmt!("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n{}\ |
| 115 | Content-Length: 2\r\nConnection: close\r\n\r\nok", own); |
| 116 | let _ = tcp.write_all(out.as_bytes()).await; |
| 117 | let _ = tcp.shutdown().await; |
| 118 | }); |
| 119 | } |
| 120 | }); |
| 121 | Ok(port) |
| 122 | } |
| 123 | |
| 124 | /// Sends one GET on `stream` and reads one whole response, framed by its Content-Length: the head, |
| 125 | /// lower cased. |
| 126 | async fn ask<S>(stream: &mut S, path: &str) -> Outcome<String> |
| 127 | where S: AsyncRead + AsyncWrite + Unpin |
| 128 | { |
| 129 | let request = fmt!("GET {} HTTP/1.1\r\nHost: {}\r\nConnection: close\r\n\r\n", path, HOST); |
| 130 | res!(stream.write_all(request.as_bytes()).await, Network, Write); |
| 131 | res!(stream.flush().await, Network, Write); |
| 132 | let mut reply = Vec::new(); |
| 133 | let mut chunk = [0u8; 4096]; |
| 134 | loop { |
| 135 | if let Some(i) = reply.windows(4).position(|w| w == b"\r\n\r\n") { |
| 136 | let head = String::from_utf8_lossy(&reply[..i]).to_lowercase(); |
| 137 | let want = head.lines() |
| 138 | .filter_map(|l| l.strip_prefix("content-length:")) |
| 139 | .filter_map(|v| v.trim().parse::<usize>().ok()) |
| 140 | .next() |
| 141 | .unwrap_or(0); |
| 142 | if reply.len() >= i + 4 + want { |
| 143 | return Ok(head); |
| 144 | } |
| 145 | } |
| 146 | match stream.read(&mut chunk).await { |
| 147 | Ok(0) | Err(_) => break, |
| 148 | Ok(n) => reply.extend_from_slice(&chunk[..n]), |
| 149 | } |
| 150 | } |
| 151 | Err(err!("GET {} answered with no complete response: {:?}.", |
| 152 | path, String::from_utf8_lossy(&reply); Test)) |
| 153 | } |
| 154 | |
| 155 | /// Every `strict-transport-security` value in a lower-cased head. |
| 156 | fn hsts(head: &str) -> Vec<String> { |
| 157 | head.lines() |
| 158 | .skip(1) |
| 159 | .filter_map(|l| l.strip_prefix("strict-transport-security:")) |
| 160 | .map(|v| v.trim().to_string()) |
| 161 | .collect() |
| 162 | } |
| 163 | |
| 164 | #[test] |
| 165 | fn a_proxied_response_carries_the_fronts_hsts_once() -> Outcome<()> { |
| 166 | let runtime = res!(tokio::runtime::Runtime::new(), Init); |
| 167 | runtime.block_on(async { drive().await }) |
| 168 | } |
| 169 | |
| 170 | async fn drive() -> Outcome<()> { |
| 171 | oxedyne_fe2o3_net::tls::ensure_crypto_provider(); |
| 172 | let cert = res!(rcgen::generate_simple_self_signed(vec![HOST.to_string()]), Init); |
| 173 | let der = res!(cert.serialize_der(), Init); |
| 174 | let key = PrivateKeyDer::Pkcs8(PrivatePkcs8KeyDer::from(cert.serialize_private_key_der())); |
| 175 | let server_tls = Arc::new(res!(TlsServerConfig::builder() |
| 176 | .with_no_client_auth() |
| 177 | .with_single_cert(vec![CertificateDer::from(der.clone())], key), Init)); |
| 178 | let mut roots = RootCertStore::empty(); |
| 179 | res!(roots.add(CertificateDer::from(der)), Init); |
| 180 | let client_tls = Arc::new(ClientConfig::builder() |
| 181 | .with_root_certificates(roots) |
| 182 | .with_no_client_auth()); |
| 183 | |
| 184 | let up_port = res!(upstream().await); |
| 185 | let mut cfg = ServerConfig::default(); |
| 186 | cfg.hsts_max_age_secs = MAX_AGE; |
| 187 | let web = AppWebHandler::<HashMap<String, OsPath>>::new( |
| 188 | cfg.clone(), |
| 189 | std::env::temp_dir().join(fmt!("steel_proxy_hsts_{}", std::process::id())), |
| 190 | HashMap::new(), |
| 191 | vec![fmt!("index.html")], |
| 192 | true, |
| 193 | Vec::new(), |
| 194 | Vec::new(), |
| 195 | Arc::new(WebhookRegistry::new()), |
| 196 | Arc::new(ApiHandlerRegistry::new()), |
| 197 | None, |
| 198 | None, |
| 199 | None, |
| 200 | None, |
| 201 | None, |
| 202 | Arc::new(Vec::new()), |
| 203 | ); |
| 204 | let runtime = Arc::new(VhostRuntime { |
| 205 | hostnames: vec![HOST.to_string()], |
| 206 | web_handler: web, |
| 207 | ws_handler: AppWebSocketHandler::new(None), |
| 208 | ws_syntax: res!(WebSocketSyntax::new("steel_ws", &SemVer::new(0, 1, 0), "Proxy HSTS")), |
| 209 | redirects: Vec::new(), |
| 210 | proxy_routes: vec![ProxyRoute { |
| 211 | path_prefix: fmt!("/"), |
| 212 | exact: false, |
| 213 | upstream_host: fmt!("127.0.0.1"), |
| 214 | upstream_port: up_port, |
| 215 | upstream_tls: false, |
| 216 | strip_prefix: false, |
| 217 | }], |
| 218 | ws_routes: Vec::new(), |
| 219 | term_manager: None, |
| 220 | uses_sessions: false, |
| 221 | permissions_policy: None, |
| 222 | tiles: None, |
| 223 | access_log: false, |
| 224 | response_headers: Vec::new(), |
| 225 | path_headers: Vec::new(), |
| 226 | }); |
| 227 | let mut vhosts = HashMap::new(); |
| 228 | vhosts.insert(HOST.to_string(), runtime); |
| 229 | let protocol = Protocol::Web { |
| 230 | vhosts: Arc::new(vhosts), |
| 231 | default_vhost: HOST.to_string(), |
| 232 | dev_mode: true, |
| 233 | }; |
| 234 | let root = std::env::temp_dir().normalise().absolute(); |
| 235 | let context = ServerContext::new(cfg, root, no_dbs(context::new_db), Vec::new(), protocol, |
| 236 | None, None); |
| 237 | |
| 238 | // One request on a fresh connection, over TLS or, with `plain`, as the loopback listener has it. |
| 239 | let fetch = async |path: &str, plain: bool| -> Outcome<String> { |
| 240 | let listener = res!(TcpListener::bind("127.0.0.1:0").await, Network, Init); |
| 241 | let addr = res!(listener.local_addr(), Network, Init); |
| 242 | let ctx = context.clone(); |
| 243 | let acceptor = TlsAcceptor::from(server_tls.clone()); |
| 244 | let server = tokio::spawn(async move { |
| 245 | let (tcp, peer) = match listener.accept().await { |
| 246 | Ok(c) => c, |
| 247 | Err(e) => return Err(err!(e, "Accept failed."; Network)), |
| 248 | }; |
| 249 | match plain { |
| 250 | true => ctx.handle_https(tcp, None, peer, true).await, |
| 251 | false => { |
| 252 | let stream = match acceptor.accept(tcp).await { |
| 253 | Ok(s) => s, |
| 254 | Err(e) => return Err(err!(e, "TLS accept failed."; Network)), |
| 255 | }; |
| 256 | ctx.handle_https(stream, Some(HOST.to_string()), peer, false).await |
| 257 | } |
| 258 | } |
| 259 | }); |
| 260 | let mut tcp = res!(TcpStream::connect(addr).await, Network, Init); |
| 261 | let head = match plain { |
| 262 | true => res!(ask(&mut tcp, path).await), |
| 263 | false => { |
| 264 | let name = res!(ServerName::try_from(HOST.to_string()), Invalid); |
| 265 | let mut stream = res!(TlsConnector::from(client_tls.clone()).connect(name, tcp).await, |
| 266 | Network); |
| 267 | res!(ask(&mut stream, path).await) |
| 268 | } |
| 269 | }; |
| 270 | let _ = server.await; |
| 271 | Ok(head) |
| 272 | }; |
| 273 | |
| 274 | // Over HTTPS, an upstream that sends none is given the front's, once. |
| 275 | let head = res!(fetch("/page", false).await); |
| 276 | assert!(head.starts_with("http/1.1 200"), "{}", head); |
| 277 | assert_eq!(hsts(&head), vec![FRONT.to_lowercase()], "{}", head); |
| 278 | |
| 279 | // An upstream's own policy is relayed as sent, never joined by a second. |
| 280 | let head = res!(fetch("/own", false).await); |
| 281 | assert!(head.starts_with("http/1.1 200"), "{}", head); |
| 282 | assert_eq!(hsts(&head), vec![OWN.to_string()], "{}", head); |
| 283 | |
| 284 | // A plain-HTTP visitor is given none. |
| 285 | let head = res!(fetch("/page", true).await); |
| 286 | assert!(head.starts_with("http/1.1 200"), "{}", head); |
| 287 | assert!(hsts(&head).is_empty(), "{}", head); |
| 288 | Ok(()) |
| 289 | } |