abuse-contact 0.2.0

Find the abuse contact for an IP address or a domain name
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
//! The RDAP client, against a server this test starts.
//!
//! The tests need no network and no live registry. They check what the client does
//! with each answer a registry can give.

#![cfg(feature = "http")]

use abuse_contact::bootstrap::{Bootstrap, Registry};
use abuse_contact::{Client, Destinations, Error, MAX_RECORD_BYTES, Scope, Source};
use wiremock::matchers::{header, method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

/// An answer with one abuse contact, in the shape ARIN uses.
const RECORD: &str = r#"{
  "entities": [{
    "roles": ["registrant"],
    "entities": [{
      "roles": ["abuse"],
      "vcardArray": ["vcard", [["email", {}, "text", "network-abuse@example.com"]]]
    }]
  }]
}"#;

/// Returns a client whose registries send every lookup to this server.
///
/// The server listens on 127.0.0.1, which a public-only client refuses, so these
/// clients take [`Destinations::Any`].
fn client_for(server: &MockServer) -> Client {
    let services = format!(
        r#"{{"services":[[["0.0.0.0/0"],["{uri}"]],[["::/0"],["{uri}"]],[["com"],["{uri}"]]]}}"#,
        uri = server.uri()
    );
    let registry = Registry::from_slice(services.as_bytes()).unwrap();

    Client::with_bootstrap(
        Bootstrap {
            ipv4: registry.clone(),
            ipv6: registry.clone(),
            dns: registry,
        },
        Destinations::Any,
    )
    .unwrap()
}

#[tokio::test]
async fn fetches_and_reads_an_ip_record() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/ip/8.8.8.8"))
        .and(header("accept", "application/rdap+json"))
        .respond_with(ResponseTemplate::new(200).set_body_string(RECORD))
        .expect(1)
        .mount(&server)
        .await;

    let record = client_for(&server)
        .lookup_ip("8.8.8.8".parse().unwrap())
        .await
        .unwrap()
        .expect("the server answered with a record");

    let contacts = record.abuse_contacts(Scope::Network);

    assert_eq!(contacts.len(), 1);
    assert_eq!(contacts[0].email.as_str(), "network-abuse@example.com");
    // The source names the server that answered, not a value the caller made up.
    assert_eq!(
        contacts[0].source,
        Source::Rdap {
            server: host_and_port(&server)
        }
    );
}

/// Returns the server part of a test server URL, such as `127.0.0.1:41234`.
fn host_and_port(server: &MockServer) -> String {
    server.uri().trim_start_matches("http://").to_owned()
}

#[tokio::test]
async fn the_record_names_the_server_a_redirect_ended_at() {
    // A registry that no longer holds a range redirects to the one that does. The
    // contacts come from the second server, so the record must name it.
    let first = MockServer::start().await;
    let second = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/ip/8.8.8.8"))
        .respond_with(
            ResponseTemplate::new(301)
                .insert_header("location", format!("{}/registry/ip/8.8.8.8", second.uri())),
        )
        .expect(1)
        .mount(&first)
        .await;
    Mock::given(method("GET"))
        .and(path("/registry/ip/8.8.8.8"))
        .respond_with(ResponseTemplate::new(200).set_body_string(RECORD))
        .expect(1)
        .mount(&second)
        .await;

    let record = client_for(&first)
        .lookup_ip("8.8.8.8".parse().unwrap())
        .await
        .unwrap()
        .expect("the second server answered with a record");

    assert_eq!(record.server, host_and_port(&second));
    assert_eq!(record.url, format!("{}/registry/ip/8.8.8.8", second.uri()));
    assert_eq!(
        record.abuse_contacts(Scope::Network)[0].source,
        Source::Rdap {
            server: host_and_port(&second)
        }
    );
}

#[tokio::test]
async fn fetches_a_domain_record() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/domain/example.com"))
        .respond_with(ResponseTemplate::new(200).set_body_string(RECORD))
        .expect(1)
        .mount(&server)
        .await;

    let found = client_for(&server)
        .lookup_domain(&"Example.COM.".parse().unwrap())
        .await
        .unwrap();

    assert!(found.is_some(), "the server answered with a record");
}

#[tokio::test]
async fn a_registry_that_holds_no_record_gives_nothing() {
    // 404 is an answer: the registry has no record. It must not stop a caller that
    // is asking several sources.
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .respond_with(ResponseTemplate::new(404))
        .mount(&server)
        .await;

    let found = client_for(&server)
        .lookup_ip("8.8.8.8".parse().unwrap())
        .await
        .unwrap();

    assert!(found.is_none());
}

#[tokio::test]
async fn a_server_that_refuses_gives_the_status() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .respond_with(ResponseTemplate::new(429))
        .mount(&server)
        .await;

    let error = client_for(&server)
        .lookup_ip("8.8.8.8".parse().unwrap())
        .await
        .unwrap_err();

    match error {
        Error::Status { status, target, .. } => {
            assert_eq!(status, 429);
            assert_eq!(target, "8.8.8.8");
        }
        other => panic!("expected a status error, got {other:?}"),
    }
}

#[tokio::test]
async fn a_body_that_is_not_rdap_gives_a_decode_error() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .respond_with(ResponseTemplate::new(200).set_body_string("<html>down for maintenance"))
        .mount(&server)
        .await;

    let error = client_for(&server)
        .lookup_ip("8.8.8.8".parse().unwrap())
        .await
        .unwrap_err();

    assert!(
        matches!(error, Error::Decode { .. }),
        "expected a decode error, got {error:?}"
    );
}

#[tokio::test]
async fn an_address_no_registry_holds_names_itself_in_the_error() {
    let empty = Client::with_bootstrap(Bootstrap::default(), Destinations::Public).unwrap();

    let error = empty
        .lookup_ip("8.8.8.8".parse().unwrap())
        .await
        .unwrap_err();

    match &error {
        Error::NoServer { target } => assert_eq!(target, "8.8.8.8"),
        other => panic!("expected no server, got {other:?}"),
    }

    let message = error.to_string();
    assert!(message.contains("8.8.8.8"), "message was: {message}");
}

#[tokio::test]
async fn a_private_address_is_refused_before_a_request_goes_out() {
    // A registry holds a record for the block a private address sits in, and that
    // record names IANA. Answering with it sends reports to somebody who cannot act.
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .respond_with(ResponseTemplate::new(200).set_body_string(RECORD))
        .expect(0)
        .mount(&server)
        .await;

    for target in ["192.168.1.1", "127.0.0.1", "10.0.0.1", "fe80::1", "0.1.2.3"] {
        let error = client_for(&server)
            .lookup_ip(target.parse().unwrap())
            .await
            .unwrap_err();

        match &error {
            Error::NotPublic { target: named } => assert_eq!(named, target),
            other => panic!("expected {target} to be refused, got {other:?}"),
        }

        // The message must say what to do instead.
        let message = error.to_string();
        assert!(message.contains(target), "message was: {message}");
        assert!(message.contains("public address"), "message was: {message}");
    }
}

#[tokio::test]
async fn an_ipv4_address_written_as_ipv6_is_looked_up_as_ipv4() {
    // The IPv6 registry holds no record for the mapped form, so the request must go
    // out for the IPv4 address it carries.
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/ip/8.8.8.8"))
        .respond_with(ResponseTemplate::new(200).set_body_string(RECORD))
        .expect(1)
        .mount(&server)
        .await;

    let found = client_for(&server)
        .lookup_ip("::ffff:8.8.8.8".parse().unwrap())
        .await
        .unwrap();

    assert!(found.is_some(), "the lookup went out for 8.8.8.8");
}

#[tokio::test]
async fn the_ipv6_spelling_of_a_private_address_is_refused() {
    // Without unmapping, ::ffff:10.0.0.1 passes a check that only reads IPv6 ranges.
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .respond_with(ResponseTemplate::new(200).set_body_string(RECORD))
        .expect(0)
        .mount(&server)
        .await;

    let error = client_for(&server)
        .lookup_ip("::ffff:10.0.0.1".parse().unwrap())
        .await
        .unwrap_err();

    match error {
        // The error names the IPv4 form, which is the address that was judged.
        Error::NotPublic { target } => assert_eq!(target, "10.0.0.1"),
        other => panic!("expected the mapped private address to be refused, got {other:?}"),
    }
}

#[tokio::test]
async fn follows_a_link_out_of_a_record() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .and(path("/rdap/v1/domain/EXAMPLE.COM"))
        .respond_with(ResponseTemplate::new(200).set_body_string(RECORD))
        .expect(1)
        .mount(&server)
        .await;

    let url = format!("{}/rdap/v1/domain/EXAMPLE.COM", server.uri());

    let found = client_for(&server)
        .fetch(&url, "example.com")
        .await
        .unwrap();

    assert!(found.is_some());
}

/// Returns a public-only client with no registries, for requests made by URL.
fn public_client() -> Client {
    Client::with_bootstrap(Bootstrap::default(), Destinations::Public).unwrap()
}

/// Returns the reason when the error is a refusal, and fails the test otherwise.
fn refusal(error: Error) -> String {
    match error {
        Error::Refused { reason, .. } => reason,
        other => panic!("expected a refusal, got {other:?}"),
    }
}

#[tokio::test]
async fn a_link_to_an_address_that_is_not_public_is_refused() {
    // A registry record can hold any link. None of these may be requested.
    for url in [
        "http://127.0.0.1:9/rdap/domain/example.com",
        "http://169.254.169.254/latest/meta-data/",
        "http://[::1]:9/",
        "http://[::ffff:10.0.0.1]/",
        "http://2130706433/",
    ] {
        let reason = refusal(public_client().fetch(url, "example.com").await.unwrap_err());
        assert!(
            reason.contains("private, reserved or documentation"),
            "{url}: {reason}"
        );
    }
}

#[tokio::test]
async fn a_link_with_a_scheme_other_than_http_is_refused() {
    for url in ["file:///etc/passwd", "ftp://example.com/"] {
        let reason = refusal(public_client().fetch(url, "example.com").await.unwrap_err());
        assert!(reason.contains("is not HTTP or HTTPS"), "{url}: {reason}");
    }
}

#[tokio::test]
async fn a_name_that_resolves_only_to_loopback_is_refused() {
    // The URL names a host, so the URL check passes it. The resolver drops the
    // loopback address, and the refusal must reach the caller as a refusal.
    let reason = refusal(
        public_client()
            .fetch("http://localhost:9/rdap/domain/example.com", "example.com")
            .await
            .unwrap_err(),
    );

    // localhost resolves to 127.0.0.1, ::1, or both, in an order the system picks.
    assert!(
        reason.starts_with("localhost resolves to ") && reason.ends_with("not a public address"),
        "{reason}"
    );
}

#[tokio::test]
async fn a_server_that_redirects_without_end_is_stopped() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .respond_with(ResponseTemplate::new(302).insert_header("location", "/ip/8.8.8.8"))
        .mount(&server)
        .await;

    let reason = refusal(
        client_for(&server)
            .lookup_ip("8.8.8.8".parse().unwrap())
            .await
            .unwrap_err(),
    );

    assert_eq!(reason, "the server sent more than 5 redirects");
}

#[tokio::test]
async fn a_redirect_to_a_scheme_other_than_http_is_refused() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .respond_with(ResponseTemplate::new(302).insert_header("location", "ftp://example.com/"))
        .mount(&server)
        .await;

    let reason = refusal(
        client_for(&server)
            .lookup_ip("8.8.8.8".parse().unwrap())
            .await
            .unwrap_err(),
    );

    assert!(reason.contains("is not HTTP or HTTPS"), "{reason}");
}

#[tokio::test]
async fn a_body_that_announces_more_than_the_limit_is_refused() {
    let server = MockServer::start().await;
    Mock::given(method("GET"))
        .respond_with(ResponseTemplate::new(200).set_body_bytes(vec![b' '; MAX_RECORD_BYTES + 1]))
        .mount(&server)
        .await;

    let error = client_for(&server)
        .lookup_ip("8.8.8.8".parse().unwrap())
        .await
        .unwrap_err();

    match error {
        Error::TooLarge { limit, .. } => assert_eq!(limit, MAX_RECORD_BYTES),
        other => panic!("expected too large, got {other:?}"),
    }
}

#[tokio::test]
async fn a_chunked_body_past_the_limit_is_refused_while_it_streams() {
    // A chunked answer has no Content-Length, so only counting the chunks bounds it.
    // The server here sends chunks until the client stops reading.
    use tokio::io::{AsyncReadExt, AsyncWriteExt};

    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let address = listener.local_addr().unwrap();

    let server = tokio::spawn(async move {
        let (mut socket, _) = listener.accept().await.unwrap();
        let mut request = [0u8; 1024];
        let _ = socket.read(&mut request).await;

        let head = "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n";
        if socket.write_all(head.as_bytes()).await.is_err() {
            return;
        }
        let chunk = format!("{:x}\r\n{}\r\n", 64 * 1024, " ".repeat(64 * 1024));
        // The client closes the connection when it passes the limit, and the next
        // write fails. That ends the task.
        for _ in 0..64 {
            if socket.write_all(chunk.as_bytes()).await.is_err() {
                return;
            }
        }
    });

    let services = format!(r#"{{"services":[[["0.0.0.0/0"],["http://{address}/"]]]}}"#);
    let client = Client::with_bootstrap(
        Bootstrap {
            ipv4: Registry::from_slice(services.as_bytes()).unwrap(),
            ..Bootstrap::default()
        },
        Destinations::Any,
    )
    .unwrap();

    let error = client
        .lookup_ip("8.8.8.8".parse().unwrap())
        .await
        .unwrap_err();

    assert!(
        matches!(error, Error::TooLarge { limit, .. } if limit == MAX_RECORD_BYTES),
        "expected too large, got {error:?}"
    );
    server.abort();
}