ssrf-guard 0.1.0

Fetch user-supplied URLs from a server without reaching its private network
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
mod common;

use std::future::Future;
use std::io;
use std::net::SocketAddr;
use std::pin::Pin;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::{Duration, Instant};

use common::{Reply, Table, serve};
use ssrf_guard::header::{AUTHORIZATION, CONTENT_TYPE, HeaderName, HeaderValue};
use ssrf_guard::{Error, Fetcher, Method, Policy, PolicyBuilder, Request, Url};

type Lookup<'a> = Pin<Box<dyn Future<Output = io::Result<Vec<SocketAddr>>> + Send + 'a>>;

/// The built-in list, with loopback let through so the test server is
/// reachable. Everything else private stays refused.
fn loopback() -> PolicyBuilder {
    Policy::builder().allow("127.0.0.1".parse().unwrap())
}

fn post(url: &str) -> Request {
    Request::new(Method::POST, Url::parse(url).unwrap())
        .header(
            HeaderName::from_static("x-api-key"),
            HeaderValue::from_static("k-123"),
        )
        .header(AUTHORIZATION, HeaderValue::from_static("Bearer t-456"))
        .header(CONTENT_TYPE, HeaderValue::from_static("application/json"))
        .body(r#"{"password":"hunter2"}"#)
}

#[tokio::test]
async fn loopback_is_refused_by_default() {
    let server = serve(|_| Reply::ok("hi")).await;
    let err = Fetcher::default().get(&server.url("/")).await.unwrap_err();
    assert!(
        matches!(err, Error::Denied { addr, .. } if addr == server.addr.ip()),
        "{err}"
    );
    assert!(server.seen().is_empty(), "nothing should have connected");
}

#[tokio::test]
async fn fetches_when_the_policy_allows_it() {
    let server = serve(|_| Reply::ok("hello")).await;
    let resp = Fetcher::new(loopback().build())
        .get(&server.url("/x"))
        .await
        .unwrap();
    assert_eq!(resp.status(), 200);
    assert_eq!(resp.remote_addr(), Some(server.addr));
    assert_eq!(resp.text().await.unwrap(), "hello");
}

#[tokio::test]
async fn same_origin_redirects_keep_headers_and_body() {
    let server = serve(|req| match req.path.as_str() {
        "/start" => Reply::redirect(307, "/next"),
        _ => Reply::ok("done"),
    })
    .await;
    let fetcher = Fetcher::new(loopback().build()).user_agent(HeaderValue::from_static("test/1"));
    let resp = fetcher.send(post(&server.url("/start"))).await.unwrap();
    assert_eq!(resp.url().path(), "/next");

    let seen = server.seen();
    assert_eq!(seen.len(), 2);
    let last = &seen[1];
    assert_eq!(last.method, "POST", "307 repeats the method");
    assert_eq!(last.header("x-api-key"), Some("k-123"));
    assert_eq!(last.header("authorization"), Some("Bearer t-456"));
    assert_eq!(last.body, br#"{"password":"hunter2"}"#);
}

#[tokio::test]
async fn cross_origin_redirects_drop_headers_and_body() {
    let other = serve(|_| Reply::ok("landed")).await;
    let to = other.url("/land");
    // Same host, another port: another origin.
    let first = serve(move |_| Reply::redirect(307, &to)).await;

    let fetcher = Fetcher::new(loopback().build()).user_agent(HeaderValue::from_static("test/1"));
    let resp = fetcher.send(post(&first.url("/start"))).await.unwrap();
    assert_eq!(resp.url().port(), Some(other.port()));

    assert_eq!(first.seen()[0].header("x-api-key"), Some("k-123"));
    let landed = &other.seen()[0];
    assert_eq!(landed.method, "POST");
    assert_eq!(landed.header("x-api-key"), None);
    assert_eq!(landed.header("authorization"), None);
    assert_eq!(landed.header("content-type"), None);
    assert!(landed.body.is_empty(), "the body stays on the first origin");
    assert_eq!(landed.header("user-agent"), Some("test/1"));
}

#[tokio::test]
async fn a_hop_through_another_host_name_stays_untrusted() {
    // One server answers for both names. The Host header tells them apart
    // and carries the port for the absolute redirects.
    let server = serve(|req| {
        let host = req.header("host").unwrap_or_default();
        let port = host.rsplit(':').next().unwrap_or_default();
        match (host.split(':').next(), req.path.as_str()) {
            (Some("a.test"), "/") => Reply::redirect(307, &format!("http://b.test:{port}/")),
            (Some("b.test"), _) => Reply::redirect(307, &format!("http://a.test:{port}/back")),
            _ => Reply::ok("back on a"),
        }
    })
    .await;
    let port = server.port();
    let table = Table::default()
        .with("a.test", &[&["127.0.0.1"]])
        .with("b.test", &[&["127.0.0.1"]]);
    let fetcher = Fetcher::new(loopback().build()).with_resolver(table);
    let resp = fetcher
        .send(post(&format!("http://a.test:{port}/")))
        .await
        .unwrap();
    assert_eq!(resp.text().await.unwrap(), "back on a");

    let seen = server.seen();
    assert_eq!(seen.len(), 3);
    assert_eq!(seen[0].header("x-api-key"), Some("k-123"));
    assert_eq!(seen[1].header("x-api-key"), None);
    // Back on the first origin, but the chain went through another one, which
    // chose where to send it. curl doesn't restore credentials here either.
    assert_eq!(seen[2].header("x-api-key"), None);
    assert!(seen[2].body.is_empty());
}

#[tokio::test]
async fn see_other_turns_a_post_into_a_get_without_its_body() {
    let server = serve(|req| match req.path.as_str() {
        "/form" => Reply::redirect(303, "/result"),
        _ => Reply::ok(""),
    })
    .await;
    Fetcher::new(loopback().build())
        .send(post(&server.url("/form")))
        .await
        .unwrap();
    let result = &server.seen()[1];
    assert_eq!(result.method, "GET");
    assert!(result.body.is_empty());
    assert_eq!(result.header("content-type"), None);
    assert_eq!(
        result.header("x-api-key"),
        Some("k-123"),
        "still the same origin"
    );
}

#[tokio::test]
async fn a_redirect_to_a_refused_address_is_not_followed() {
    for location in [
        "http://127.0.0.2/",
        "http://[::1]/",
        "http://169.254.169.254/latest/meta-data/",
        "http://[::ffff:10.1.2.3]/",
        "http://0.0.0.0/",
    ] {
        let to = location.to_string();
        let server = serve(move |_| Reply::redirect(302, &to)).await;
        let err = Fetcher::new(loopback().build())
            .get(&server.url("/"))
            .await
            .unwrap_err();
        assert!(matches!(err, Error::Denied { .. }), "{location}: {err}");
    }
}

#[tokio::test]
async fn one_refused_address_refuses_the_whole_name() {
    let server = serve(|_| Reply::ok("")).await;
    let table = Table::default().with("mixed.test", &[&["127.0.0.1", "10.1.2.3"]]);
    let fetcher = Fetcher::new(loopback().build()).with_resolver(table);
    let err = fetcher
        .get(&format!("http://mixed.test:{}/", server.port()))
        .await
        .unwrap_err();
    assert!(
        matches!(err, Error::Denied { addr, .. } if addr.to_string() == "10.1.2.3"),
        "{err}"
    );
    assert!(server.seen().is_empty());
}

#[tokio::test]
async fn a_name_that_rebinds_cannot_move_the_connection() {
    let server = serve(|req| match req.path.as_str() {
        "/" => Reply::redirect(302, "/again"),
        _ => Reply::ok(""),
    })
    .await;
    // The first lookup passes the check. Every lookup after it says 10.1.2.3.
    let table = Table::default().with("rebind.test", &[&["127.0.0.1"], &["10.1.2.3"]]);
    let fetcher = Fetcher::new(loopback().build()).with_resolver(table.clone());
    let err = fetcher
        .get(&format!("http://rebind.test:{}/", server.port()))
        .await
        .unwrap_err();

    // The first hop connected to the address that was checked. The client
    // never asked a resolver of its own, which would have failed on .test.
    let seen = server.seen();
    assert_eq!(seen.len(), 1);
    assert_eq!(seen[0].path, "/");
    // The redirect was looked up again, and the new answer refused.
    assert!(
        matches!(err, Error::Denied { addr, .. } if addr.to_string() == "10.1.2.3"),
        "{err}"
    );
    assert_eq!(table.lookups(), ["rebind.test", "rebind.test"]);
}

#[tokio::test]
async fn the_redirect_limit_holds() {
    let server = serve(|req| {
        let n: usize = req.path.trim_start_matches('/').parse().unwrap_or(0);
        Reply::redirect(302, &format!("/{}", n + 1))
    })
    .await;
    let err = Fetcher::new(loopback().build())
        .get(&server.url("/0"))
        .await
        .unwrap_err();
    assert!(matches!(err, Error::TooManyRedirects(3)), "{err}");
    assert_eq!(
        server.seen().len(),
        4,
        "the first request and three redirects"
    );

    // With no redirects allowed, the redirect itself is the answer.
    let none = Fetcher::new(loopback().max_redirects(0).build());
    let resp = none.get(&server.url("/0")).await.unwrap();
    assert_eq!(resp.status(), 302);
    assert_eq!(resp.headers()["location"], "/1");
}

#[tokio::test]
async fn a_redirect_without_location_is_the_answer() {
    let server = serve(|_| Reply {
        status: 302,
        headers: vec![],
        body: vec![],
        delay: Duration::ZERO,
    })
    .await;
    let resp = Fetcher::new(loopback().build())
        .get(&server.url("/"))
        .await
        .unwrap();
    assert_eq!(resp.status(), 302);
}

#[tokio::test]
async fn only_redirect_statuses_are_followed() {
    for status in [300, 304, 305] {
        let server = serve(move |req| match req.path.as_str() {
            "/" => Reply::redirect(status, "/elsewhere"),
            _ => Reply::ok("followed"),
        })
        .await;
        let resp = Fetcher::new(loopback().build())
            .get(&server.url("/"))
            .await
            .unwrap();
        assert_eq!(resp.status(), status);
        assert_eq!(server.seen().len(), 1, "{status} was followed");
    }
}

#[tokio::test]
async fn a_location_with_raw_utf8_is_followed() {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let paths = Arc::new(std::sync::Mutex::new(Vec::new()));
    let seen = paths.clone();
    tokio::spawn(async move {
        use tokio::io::{AsyncReadExt, AsyncWriteExt};
        while let Ok((mut sock, _)) = listener.accept().await {
            let mut buf = [0u8; 1024];
            let n = sock.read(&mut buf).await.unwrap_or(0);
            let head = String::from_utf8_lossy(&buf[..n]).into_owned();
            let path = head.split(' ').nth(1).unwrap_or_default().to_string();
            let reply: &[u8] = if path == "/" {
                "HTTP/1.1 302 Found\r\nLocation: /caf\u{e9}\r\nContent-Length: 0\r\n\r\n".as_bytes()
            } else {
                b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n"
            };
            seen.lock().unwrap().push(path);
            let _ = sock.write_all(reply).await;
        }
    });
    let resp = Fetcher::new(loopback().build())
        .get(&format!("http://{addr}/"))
        .await
        .unwrap();
    assert_eq!(resp.status(), 200);
    assert_eq!(resp.url().path(), "/caf%C3%A9");
    assert_eq!(paths.lock().unwrap().as_slice(), ["/", "/caf%C3%A9"]);
}

#[tokio::test]
async fn a_location_that_isnt_utf8_is_refused() {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    tokio::spawn(async move {
        use tokio::io::{AsyncReadExt, AsyncWriteExt};
        while let Ok((mut sock, _)) = listener.accept().await {
            let mut buf = [0u8; 1024];
            let _ = sock.read(&mut buf).await;
            // A Latin-1 e-acute, which HeaderValue takes and str doesn't.
            let reply = b"HTTP/1.1 302 Found\r\nLocation: /caf\xe9\r\nContent-Length: 0\r\n\r\n";
            let _ = sock.write_all(reply).await;
        }
    });
    let err = Fetcher::new(loopback().build())
        .get(&format!("http://{addr}/"))
        .await
        .unwrap_err();
    assert!(matches!(err, Error::BadRedirect(_)), "{err}");
}

#[tokio::test]
async fn errors_dont_carry_the_url() {
    // A port nothing listens on, so the connection is refused.
    let port = {
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        listener.local_addr().unwrap().port()
    };
    let fetcher = Fetcher::new(loopback().build());
    let err = fetcher
        .get(&format!("http://127.0.0.1:{port}/cb?token=q-012"))
        .await
        .unwrap_err();
    assert!(matches!(err, Error::Http(_)), "{err}");
    let mut text = format!("{err:?}");
    let mut source: Option<&dyn std::error::Error> = Some(&err);
    while let Some(e) = source {
        text.push_str(&e.to_string());
        source = e.source();
    }
    assert!(!text.contains("q-012"), "{text}");

    for url in [
        "https://user:pw-345@example.com/",
        "gopher://example.com/?token=q-012",
        "https//pw-345@example.com",
    ] {
        let err = fetcher.get(url).await.unwrap_err();
        assert!(matches!(err, Error::InvalidUrl(_)), "{url}: {err}");
        let shown = err.to_string();
        assert!(
            !shown.contains("pw-345") && !shown.contains("q-012"),
            "{shown}"
        );
    }
}

#[tokio::test]
async fn connection_headers_cannot_be_set() {
    let server = serve(|_| Reply::ok("")).await;
    let url = Url::parse(&server.url("/")).unwrap();
    for name in ["host", "content-length", "transfer-encoding", "connection"] {
        let req = Request::new(Method::POST, url.clone())
            .header(
                HeaderName::from_static(name),
                HeaderValue::from_static("internal.example"),
            )
            .body("hello");
        let err = Fetcher::new(loopback().build())
            .send(req)
            .await
            .unwrap_err();
        assert!(
            matches!(&err, Error::RefusedHeader(n) if n == name),
            "{err}"
        );
    }
    assert!(server.seen().is_empty());
}

#[tokio::test]
async fn a_resolver_cannot_choose_the_port() {
    // Answers every name with port 22, whatever port was asked for.
    struct WrongPort;
    impl ssrf_guard::Resolve for WrongPort {
        fn resolve<'a>(&'a self, _host: &'a str, _port: u16) -> Lookup<'a> {
            Box::pin(async { Ok(vec!["127.0.0.1:22".parse().unwrap()]) })
        }
    }
    let fetcher = Fetcher::new(loopback().build()).with_resolver(WrongPort);
    let addrs = fetcher.resolve("any.test", 8080).await.unwrap();
    assert_eq!(addrs, ["127.0.0.1:8080".parse().unwrap()]);
}

#[tokio::test]
async fn unfollowable_redirects_are_errors() {
    for location in [
        "ftp://example.com/",
        "http://user:pw@127.0.0.1/",
        "http://[::1",
    ] {
        let to = location.to_string();
        let server = serve(move |_| Reply::redirect(302, &to)).await;
        let err = Fetcher::new(loopback().build())
            .get(&server.url("/"))
            .await
            .unwrap_err();
        assert!(matches!(err, Error::BadRedirect(_)), "{location}: {err}");
    }
}

#[tokio::test]
async fn one_timeout_covers_every_hop() {
    // Each hop takes 300 ms, well inside the 800 ms timeout on its own. The
    // chain is four hops long and the third one runs out of time.
    let server = serve(|req| {
        let n: usize = req.path.trim_start_matches('/').parse().unwrap_or(0);
        let reply = if n < 3 {
            Reply::redirect(302, &format!("/{}", n + 1))
        } else {
            Reply::ok("")
        };
        reply.after(Duration::from_millis(300))
    })
    .await;
    let fetcher = Fetcher::new(loopback().timeout(Duration::from_millis(800)).build());
    let started = Instant::now();
    let err = fetcher.get(&server.url("/0")).await.unwrap_err();
    assert!(matches!(err, Error::Timeout(_)), "{err}");
    assert!(
        started.elapsed() < Duration::from_millis(1500),
        "took {:?}",
        started.elapsed()
    );
    assert!(server.seen().len() < 4);
}

#[tokio::test]
async fn the_timeout_covers_reading_the_body() {
    // Headers arrive, then the body never finishes.
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    tokio::spawn(async move {
        use tokio::io::{AsyncReadExt, AsyncWriteExt};
        let (mut sock, _) = listener.accept().await.unwrap();
        let mut buf = [0u8; 1024];
        let _ = sock.read(&mut buf).await;
        sock.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 1000\r\n\r\npartial")
            .await
            .unwrap();
        tokio::time::sleep(Duration::from_secs(10)).await;
    });
    let fetcher = Fetcher::new(loopback().timeout(Duration::from_millis(400)).build());
    let resp = fetcher.get(&format!("http://{addr}/")).await.unwrap();
    let started = Instant::now();
    let err = resp.bytes().await.unwrap_err();
    assert!(matches!(err, Error::Timeout(_)), "{err}");
    assert!(started.elapsed() < Duration::from_secs(2));
}

#[tokio::test]
async fn the_body_limit_holds() {
    let server = serve(|_| Reply::ok(&"x".repeat(100))).await;
    let fetcher = Fetcher::new(loopback().max_body(99).build());
    let err = fetcher
        .get(&server.url("/"))
        .await
        .unwrap()
        .bytes()
        .await
        .unwrap_err();
    assert!(matches!(err, Error::BodyTooLarge(99)), "{err}");
    let fetcher = Fetcher::new(loopback().max_body(100).build());
    assert_eq!(
        fetcher
            .get(&server.url("/"))
            .await
            .unwrap()
            .bytes()
            .await
            .unwrap()
            .len(),
        100
    );
}

#[tokio::test]
async fn a_long_body_can_be_cut_short() {
    let server = serve(|_| Reply::ok(&"x".repeat(100))).await;
    let fetcher = Fetcher::new(loopback().max_body(40).build());
    let resp = fetcher.get(&server.url("/")).await.unwrap();
    let (body, cut) = resp.bytes_truncated().await.unwrap();
    assert_eq!((body.len(), cut), (40, true));

    let fetcher = Fetcher::new(loopback().max_body(100).build());
    let resp = fetcher.get(&server.url("/")).await.unwrap();
    let (body, cut) = resp.bytes_truncated().await.unwrap();
    assert_eq!((body.len(), cut), (100, false));
}

#[tokio::test]
async fn chunks_stop_at_the_body_limit() {
    let server = serve(|_| Reply::ok(&"x".repeat(100))).await;
    let mut resp = Fetcher::new(loopback().max_body(99).build())
        .get(&server.url("/"))
        .await
        .unwrap();
    let mut read = 0;
    let err = loop {
        match resp.chunk().await {
            Ok(Some(c)) => read += c.len(),
            Ok(None) => panic!("read all {read} bytes past the limit"),
            Err(e) => break e,
        }
    };
    assert!(matches!(err, Error::BodyTooLarge(99)), "{err}");
    assert!(read <= 99);
}

#[tokio::test]
async fn a_denied_block_wins_over_an_allowed_one() {
    let server = serve(|_| Reply::ok("")).await;
    let fetcher = Fetcher::new(loopback().deny("127.0.0.0/8".parse().unwrap()).build());
    assert!(matches!(
        fetcher.get(&server.url("/")).await,
        Err(Error::Denied { .. })
    ));
}

#[tokio::test]
async fn urls_that_are_not_http_are_refused() {
    let fetcher = Fetcher::default();
    for url in [
        "file:///etc/passwd",
        "gopher://example.com/",
        "https://user:pw@example.com/",
        "not a url",
    ] {
        assert!(
            matches!(fetcher.get(url).await, Err(Error::InvalidUrl(_))),
            "{url}"
        );
    }
}

#[tokio::test]
async fn connect_checks_the_address_first() {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    let accepted = Arc::new(AtomicUsize::new(0));
    let count = accepted.clone();
    tokio::spawn(async move {
        while listener.accept().await.is_ok() {
            count.fetch_add(1, Ordering::SeqCst);
        }
    });

    let err = Fetcher::default()
        .connect("127.0.0.1", addr.port())
        .await
        .unwrap_err();
    assert!(matches!(err, Error::Denied { .. }), "{err}");

    let stream = Fetcher::new(loopback().build())
        .connect("127.0.0.1", addr.port())
        .await
        .unwrap();
    assert_eq!(stream.peer_addr().unwrap(), addr);

    let table = Table::default().with("db.test", &[&["127.0.0.1"]]);
    let fetcher = Fetcher::new(loopback().build()).with_resolver(table);
    fetcher.connect("db.test", addr.port()).await.unwrap();
    tokio::time::sleep(Duration::from_millis(50)).await;
    assert_eq!(accepted.load(Ordering::SeqCst), 2);
}

#[tokio::test]
async fn an_address_that_never_answers_leaves_time_for_the_next() {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    tokio::spawn(async move { while listener.accept().await.is_ok() {} });

    // 192.0.2.1 is documentation space. Nothing answers there, so the
    // connection hangs or fails, depending on the network.
    let table = Table::default().with("slow.test", &[&["192.0.2.1", "127.0.0.1"]]);
    let policy = loopback()
        .allow("192.0.2.1".parse().unwrap())
        .timeout(Duration::from_secs(3))
        .build();
    let started = Instant::now();
    let stream = Fetcher::new(policy)
        .with_resolver(table)
        .connect("slow.test", addr.port())
        .await
        .unwrap();
    assert_eq!(stream.peer_addr().unwrap(), addr);
    assert!(started.elapsed() < Duration::from_secs(3));
}

#[tokio::test]
async fn connections_leave_from_the_source_address() {
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let addr = listener.local_addr().unwrap();
    tokio::spawn(async move { while listener.accept().await.is_ok() {} });

    let policy = loopback().source("127.0.0.1".parse().unwrap()).build();
    let stream = Fetcher::new(policy)
        .connect("127.0.0.1", addr.port())
        .await
        .unwrap();
    assert_eq!(stream.local_addr().unwrap().ip().to_string(), "127.0.0.1");

    // A v6 source only: a v4 host has nothing to leave from, and doesn't fall
    // back to the kernel's choice.
    let v6_only = loopback().source("::1".parse().unwrap()).build();
    let err = Fetcher::new(v6_only)
        .connect("127.0.0.1", addr.port())
        .await
        .unwrap_err();
    assert!(matches!(err, Error::NoSource { .. }), "{err}");
}

#[tokio::test]
async fn fetches_leave_from_the_source_address() {
    let server = serve(|_| Reply::ok("")).await;
    let fetcher = Fetcher::new(loopback().source("127.0.0.1".parse().unwrap()).build());
    assert_eq!(fetcher.get(&server.url("/")).await.unwrap().status(), 200);
    let v6_only = Fetcher::new(loopback().source("::1".parse().unwrap()).build());
    assert!(matches!(
        v6_only.get(&server.url("/")).await,
        Err(Error::NoSource { .. })
    ));
}

#[tokio::test]
#[ignore = "needs the internet"]
async fn a_real_https_fetch() {
    let resp = Fetcher::default()
        .get("https://example.com/")
        .await
        .unwrap();
    assert_eq!(resp.status(), 200);
    assert!(resp.text().await.unwrap().contains("Example Domain"));
}

#[tokio::test]
#[ignore = "needs the internet"]
async fn a_public_name_for_the_metadata_service() {
    // nip.io answers 169.254.169.254.nip.io with 169.254.169.254, through
    // the system resolver like any other name.
    let err = Fetcher::default()
        .get("http://169.254.169.254.nip.io/latest/meta-data/")
        .await
        .unwrap_err();
    assert!(
        matches!(err, Error::Denied { addr, .. } if addr.to_string() == "169.254.169.254"),
        "{err}"
    );
}

#[tokio::test]
#[ignore = "needs the internet"]
async fn a_bad_certificate_fails() {
    let err = Fetcher::default()
        .get("https://expired.badssl.com/")
        .await
        .unwrap_err();
    assert!(matches!(err, Error::Http(_)), "{err}");
}