asimov-http 25.9.1

HTTP message integrity and shared client proxy configuration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// This is free and unencumbered software released into the public domain.

use super::*;
use crate::proxy::{
    ProxyMode, ProxyOptions,
    test_support::{client_builder, tls_config},
};
use alloc::{format, string::String, sync::Arc, vec, vec::Vec};
use core::{convert::Infallible, time::Duration};
use http_body_util::{BodyExt, Full};
use hyper::{Request, Response, body::Bytes, service::service_fn};
use hyper_util::rt::TokioIo;
use tokio::{
    io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
    net::TcpListener,
    time::timeout,
};
use url::Url;

const DEADLINE: Duration = Duration::from_secs(10);

trait Io: AsyncRead + AsyncWrite + Send + Unpin {}
impl<T: AsyncRead + AsyncWrite + Send + Unpin> Io for T {}
type Stream = alloc::boxed::Box<dyn Io>;

fn tls() -> (
    rustls::ClientConfig,
    tokio_rustls::TlsAcceptor,
    tokio_rustls::TlsAcceptor,
) {
    let mut root_params = rcgen::CertificateParams::default();
    root_params.is_ca = rcgen::IsCa::Ca(rcgen::BasicConstraints::Unconstrained);
    root_params.key_usages = vec![rcgen::KeyUsagePurpose::KeyCertSign];
    let root_key = rcgen::KeyPair::generate().unwrap();
    let root = root_params.self_signed(&root_key).unwrap();
    let issuer = rcgen::Issuer::new(root_params, root_key);
    let server = |names: &[&str]| {
        let key = rcgen::KeyPair::generate().unwrap();
        let mut params = rcgen::CertificateParams::new(
            names.iter().map(|s| (*s).into()).collect::<Vec<String>>(),
        )
        .unwrap();
        params.extended_key_usages = vec![rcgen::ExtendedKeyUsagePurpose::ServerAuth];
        let cert = params.signed_by(&key, &issuer).unwrap();
        let config = rustls::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()],
            rustls::pki_types::PrivatePkcs8KeyDer::from(key.serialize_der()).into(),
        )
        .unwrap();
        tokio_rustls::TlsAcceptor::from(Arc::new(config))
    };
    let mut client = tls_config().unwrap();
    let verifier = rustls_platform_verifier::Verifier::new_with_extra_roots(
        [root.der().clone()],
        client.crypto_provider().clone(),
    )
    .unwrap();
    client
        .dangerous()
        .set_certificate_verifier(Arc::new(verifier));
    (
        client,
        server(&["proxy.invalid"]),
        server(&["service.invalid", "localhost"]),
    )
}

async fn headers(io: &mut Stream) -> String {
    let mut data = Vec::new();
    while !data.ends_with(b"\r\n\r\n") {
        assert!(data.len() < 8192);
        data.push(io.read_u8().await.unwrap());
    }
    String::from_utf8(data).unwrap()
}

async fn socks(io: &mut Stream, scheme: &str, destination: &str, port: u16) {
    if scheme.starts_with("socks5") {
        assert_eq!(io.read_u8().await.unwrap(), 5);
        let count = io.read_u8().await.unwrap();
        let mut methods = vec![0; count as usize];
        io.read_exact(&mut methods).await.unwrap();
        assert!(methods.contains(&2));
        io.write_all(&[5, 2]).await.unwrap();
        assert_eq!(io.read_u8().await.unwrap(), 1);
        let count = io.read_u8().await.unwrap();
        let mut user = vec![0; count as usize];
        io.read_exact(&mut user).await.unwrap();
        let count = io.read_u8().await.unwrap();
        let mut password = vec![0; count as usize];
        io.read_exact(&mut password).await.unwrap();
        assert_eq!(user, b"User@Realm");
        assert_eq!(password, b"p:a%ss+");
        io.write_all(&[1, 0]).await.unwrap();
        let mut request = [0; 4];
        io.read_exact(&mut request).await.unwrap();
        assert_eq!(&request[..3], &[5, 1, 0]);
        if scheme == "socks5h" {
            assert_eq!(request[3], 3);
            let count = io.read_u8().await.unwrap();
            let mut host = vec![0; count as usize];
            io.read_exact(&mut host).await.unwrap();
            assert_eq!(host, destination.as_bytes());
        } else {
            assert!(matches!(request[3], 1 | 4));
            let mut ip = vec![0; if request[3] == 1 { 4 } else { 16 }];
            io.read_exact(&mut ip).await.unwrap();
        }
        assert_eq!(io.read_u16().await.unwrap(), port);
        io.write_all(&[5, 0, 0, 1, 127, 0, 0, 1, 0, 0])
            .await
            .unwrap();
    } else {
        assert_eq!(io.read_u8().await.unwrap(), 4);
        assert_eq!(io.read_u8().await.unwrap(), 1);
        assert_eq!(io.read_u16().await.unwrap(), port);
        let mut ip = [0; 4];
        io.read_exact(&mut ip).await.unwrap();
        while io.read_u8().await.unwrap() != 0 {}
        if scheme == "socks4a" {
            assert_eq!(&ip[..3], &[0, 0, 0]);
            assert_ne!(ip[3], 0);
            let mut host = Vec::new();
            loop {
                let byte = io.read_u8().await.unwrap();
                if byte == 0 {
                    break;
                }
                host.push(byte);
            }
            assert_eq!(host, destination.as_bytes());
        } else {
            assert_eq!(ip, [127, 0, 0, 1]);
        }
        io.write_all(&[0, 90, 0, 0, 127, 0, 0, 1]).await.unwrap();
    }
}

async fn serve(io: Stream, forwarded: bool, proxy_auth: bool) {
    use base64::Engine as _;
    let auth = format!(
        "Basic {}",
        base64::engine::general_purpose::STANDARD.encode("User@Realm:p:a%ss+")
    );
    let service = service_fn(move |request: Request<hyper::body::Incoming>| {
        let auth = auth.clone();
        async move {
            assert_eq!(request.uri().scheme().is_some(), forwarded);
            if proxy_auth {
                assert_eq!(request.headers()["proxy-authorization"], auth);
            } else {
                assert!(!request.headers().contains_key("proxy-authorization"));
            }
            let discovery = request.uri().path() == "/.well-known/jwks.json";
            if discovery {
                assert!(!request.headers().contains_key("authorization"));
            } else {
                assert_eq!(request.headers()["authorization"], "Bearer service-token");
                assert!(request.headers().contains_key("content-digest"));
            }
            request.into_body().collect().await.unwrap();
            let body = if discovery {
                br#"{"keys":[{"kty":"OKP","crv":"Ed25519","x":"11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo"}]}"#.as_slice()
            } else {
                b"{}\n"
            };
            Ok::<_, Infallible>(Response::new(Full::new(Bytes::copy_from_slice(body))))
        }
    });
    let result = hyper::server::conn::http1::Builder::new()
        .serve_connection(TokioIo::new(io), service)
        .await;
    if let Err(error) = result {
        // The client may close before the server writes TLS close_notify.
        assert!(error.is_shutdown(), "{error}");
    }
}

#[tokio::test]
async fn authenticated_http_https_and_socks_proxies_carry_requests() {
    use base64::Engine as _;
    for scheme in ["http", "https", "socks4", "socks4a", "socks5", "socks5h"] {
        for secure in [false, true] {
            let (tls, proxy_tls, origin_tls) = tls();
            let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
            let address = listener.local_addr().unwrap();
            let destination = if matches!(scheme, "socks4" | "socks5") {
                "localhost"
            } else {
                "service.invalid"
            };
            let port = if secure { 443 } else { 80 };
            let forwarded = !secure && matches!(scheme, "http" | "https");
            let task = tokio::spawn(async move {
                let (socket, _) = listener.accept().await.unwrap();
                let mut io: Stream = if scheme == "https" {
                    alloc::boxed::Box::new(proxy_tls.accept(socket).await.unwrap())
                } else {
                    alloc::boxed::Box::new(socket)
                };
                if scheme.starts_with("socks") {
                    socks(&mut io, scheme, destination, port).await;
                } else if secure {
                    let connect = headers(&mut io).await;
                    assert!(
                        connect.starts_with(&format!("CONNECT {destination}:443 HTTP/1.1\r\n"))
                    );
                    let auth =
                        base64::engine::general_purpose::STANDARD.encode("User@Realm:p:a%ss+");
                    let authorization = connect.lines().find_map(|line| {
                        let (name, value) = line.split_once(':')?;
                        name.eq_ignore_ascii_case("proxy-authorization")
                            .then_some(value.trim())
                    });
                    assert_eq!(authorization, Some(format!("Basic {auth}").as_str()));
                    assert!(!connect.contains("service-token"));
                    io.write_all(b"HTTP/1.1 200 Connection Established\r\n\r\n")
                        .await
                        .unwrap();
                }
                if secure {
                    io = alloc::boxed::Box::new(origin_tls.accept(io).await.unwrap());
                }
                serve(io, forwarded, forwarded).await;
            });
            let url = format!(
                "{}://{destination}/api/",
                if secure { "https" } else { "http" }
            );
            let proxy = Url::parse(&format!(
                "{scheme}://User%40Realm:p%3Aa%25ss+@proxy.invalid:{}",
                address.port()
            ))
            .unwrap();
            let url = Url::parse(&url).unwrap();
            let client = OriginProxy::resolve(
                &url,
                &ProxyOptions {
                    mode: ProxyMode::Explicit(proxy.as_str().into()),
                    no_proxy: Some(String::new()),
                },
            )
            .unwrap()
            .configure_reqwest(
                client_builder()
                    .unwrap()
                    .tls_backend_preconfigured(tls)
                    .resolve("proxy.invalid", address),
            )
            .unwrap()
            .build()
            .unwrap();
            timeout(DEADLINE, async {
                client
                    .get(url.join("/.well-known/jwks.json").unwrap())
                    .send()
                    .await
                    .unwrap()
                    .bytes()
                    .await
                    .unwrap();
                let body = client
                    .post(url.join("fetch").unwrap())
                    .bearer_auth("service-token")
                    .header(
                        "content-digest",
                        crate::ContentDigest::new(b"{}").to_field_value(),
                    )
                    .body("{}")
                    .send()
                    .await
                    .unwrap()
                    .bytes()
                    .await
                    .unwrap();
                assert_eq!(body, b"{}\n".as_slice());
            })
            .await
            .unwrap_or_else(|_| panic!("{scheme}, secure={secure} timed out"));
            drop(client);
            timeout(DEADLINE, task).await.unwrap().unwrap();
        }
    }
}

#[tokio::test]
async fn proxy_and_tunneled_origin_tls_validate_their_own_hostnames() {
    for outer_tls in [false, true] {
        let (tls, proxy_tls, origin_tls) = tls();
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let task = tokio::spawn(async move {
            let (socket, _) = listener.accept().await.unwrap();
            let mut io: Stream = Box::new(socket);
            if outer_tls {
                // A certificate valid for the service cannot identify the proxy.
                assert!(origin_tls.accept(io).await.is_err());
            } else {
                let connect = headers(&mut io).await;
                assert!(connect.starts_with("CONNECT service.invalid:443 HTTP/1.1\r\n"));
                io.write_all(b"HTTP/1.1 200 Connection Established\r\n\r\n")
                    .await
                    .unwrap();
                // And a proxy's certificate cannot identify the tunneled service.
                assert!(proxy_tls.accept(io).await.is_err());
            }
        });
        let proxy = Url::parse(&format!(
            "{}://proxy.invalid:{}",
            if outer_tls { "https" } else { "http" },
            address.port()
        ))
        .unwrap();
        let policy = OriginProxy::resolve(
            &Url::parse("https://service.invalid/").unwrap(),
            &ProxyOptions {
                mode: ProxyMode::Explicit(proxy.as_str().into()),
                no_proxy: Some(String::new()),
            },
        )
        .unwrap();
        let client = policy
            .configure_reqwest(
                client_builder()
                    .unwrap()
                    .tls_backend_preconfigured(tls)
                    .resolve("proxy.invalid", address),
            )
            .unwrap()
            .build()
            .unwrap();
        let error = timeout(DEADLINE, client.get("https://service.invalid/").send())
            .await
            .unwrap()
            .unwrap_err();
        assert!(error.is_connect() && !error.is_timeout(), "{error}");
        timeout(DEADLINE, task).await.unwrap().unwrap();
    }
}

#[tokio::test]
async fn origin_scoped_configuration_disables_redirects() {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let url = Url::parse(&format!("http://{}/", listener.local_addr().unwrap())).unwrap();
    let policy = OriginProxy::resolve(
        &url,
        &ProxyOptions {
            mode: ProxyMode::Direct,
            no_proxy: None,
        },
    )
    .unwrap();
    let client = policy
        .configure_reqwest(
            reqwest::Client::builder().redirect(reqwest::redirect::Policy::limited(10)),
        )
        .unwrap()
        .build()
        .unwrap();
    let task = tokio::spawn(async move {
        let (socket, _) = listener.accept().await.unwrap();
        let mut io: Stream = Box::new(socket);
        headers(&mut io).await;
        io.write_all(b"HTTP/1.1 302 Found\r\nLocation: http://unresolved.invalid/\r\nContent-Length: 0\r\n\r\n").await.unwrap();
    });
    policy.check_origin(&url).unwrap();
    assert_eq!(
        timeout(DEADLINE, client.get(url).send())
            .await
            .unwrap()
            .unwrap()
            .status(),
        302
    );
    timeout(DEADLINE, task).await.unwrap().unwrap();
}

#[tokio::test]
async fn client_builder_limits_stalled_reads() {
    for respond in [false, true] {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let url = Url::parse(&format!("http://{}/", listener.local_addr().unwrap())).unwrap();
        let options = ProxyOptions {
            mode: ProxyMode::Direct,
            no_proxy: None,
        };
        let policy = OriginProxy::resolve(&url, &options).unwrap();
        let client = policy
            .client_builder_with(&ClientOptions {
                connect_timeout: Some(DEADLINE),
                read_timeout: Some(Duration::from_millis(50)),
                dns_resolver: None,
            })
            .unwrap()
            .build()
            .unwrap();
        let task = tokio::spawn(async move {
            let (socket, _) = listener.accept().await.unwrap();
            let mut io: Stream = Box::new(socket);
            headers(&mut io).await;
            if respond {
                io.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\n")
                    .await
                    .unwrap();
            }
            core::future::pending::<()>().await;
        });
        let result = timeout(DEADLINE, async {
            client.get(url).send().await?.bytes().await
        })
        .await
        .expect("the read limit must fire");
        task.abort();
        assert!(result.unwrap_err().is_timeout(), "headers sent: {respond}");
    }
}

/// Resolves every name to fixed addresses, so that `service.invalid` resolves
/// only through this resolver.
struct Fixed(Vec<std::net::SocketAddr>);

impl Resolve for Fixed {
    fn resolve(&self, _: reqwest::dns::Name) -> reqwest::dns::Resolving {
        let addresses = self.0.clone();
        Box::pin(async move { Ok(Box::new(addresses.into_iter()) as reqwest::dns::Addrs) })
    }
}

async fn respond(mut io: Stream) {
    headers(&mut io).await;
    io.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\nConnection: close\r\n\r\n")
        .await
        .unwrap();
}

#[tokio::test]
async fn client_builder_uses_the_given_resolver() {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let address = listener.local_addr().unwrap();
    let url = Url::parse(&format!("http://service.invalid:{}/", address.port())).unwrap();
    let options = ProxyOptions {
        mode: ProxyMode::Direct,
        no_proxy: None,
    };
    let client = OriginProxy::resolve(&url, &options)
        .unwrap()
        .client_builder_with(&ClientOptions {
            dns_resolver: Some(Arc::new(Fixed(vec![address]))),
            ..ClientOptions::default()
        })
        .unwrap()
        .build()
        .unwrap();
    let task = tokio::spawn(async move {
        let (socket, _) = listener.accept().await.unwrap();
        respond(Box::new(socket)).await;
    });
    let response = timeout(DEADLINE, client.get(url).send()).await.unwrap();
    assert!(response.unwrap().status().is_success());
    timeout(DEADLINE, task).await.unwrap().unwrap();
}

#[tokio::test]
async fn socks4_prefers_ipv4_from_the_given_resolver() {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let proxy = format!("socks4://{}", listener.local_addr().unwrap());
    let url = Url::parse("http://service.invalid/").unwrap();
    let options = ProxyOptions {
        mode: ProxyMode::Explicit(proxy),
        no_proxy: Some(String::new()),
    };
    // IPv6 first: the SOCKS4 wrapper must still send the IPv4 address.
    let resolver = Fixed(vec![
        "[::1]:0".parse().unwrap(),
        "127.0.0.1:0".parse().unwrap(),
    ]);
    let client = OriginProxy::resolve(&url, &options)
        .unwrap()
        .client_builder_with(&ClientOptions {
            dns_resolver: Some(Arc::new(resolver)),
            ..ClientOptions::default()
        })
        .unwrap()
        .build()
        .unwrap();
    let task = tokio::spawn(async move {
        let (socket, _) = listener.accept().await.unwrap();
        let mut io: Stream = Box::new(socket);
        socks(&mut io, "socks4", "service.invalid", 80).await;
        respond(io).await;
    });
    let response = timeout(DEADLINE, client.get(url).send()).await.unwrap();
    assert!(response.unwrap().status().is_success());
    timeout(DEADLINE, task).await.unwrap().unwrap();
}