http-net 0.9.2

Sans-IO HTTP/1.1 server and client: strict RFC 9112 parsing, smuggling defences, bounded buffers, keep-alive, chunked bodies, upgrades, a blocking std adapter, no third-party dependencies.
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
//! T-35: credentials leaked through logs.
//!
//! Every `Debug` output of the crate replaces the values of
//! `authorization`, `proxy-authorization`, `cookie`, and `set-cookie` with
//! `<redacted N bytes>`, for every spelling of the field name, in both the
//! compact and the pretty form. Accessors keep returning the real values.

use http_net::{
    Asked, ClientConn, ClientEvent, FieldSpan, Limits, OwnedRequestHead, Parsed, Request, Response,
    ServerConn, ServerEvent, parse_request_head, parse_response_head,
};

/// The four credential fields, each in several spellings.
const SPELLINGS: &[&str] = &[
    "authorization",
    "Authorization",
    "AUTHORIZATION",
    "aUtHoRiZaTiOn",
    "proxy-authorization",
    "Proxy-Authorization",
    "PROXY-AUTHORIZATION",
    "cookie",
    "Cookie",
    "COOKIE",
    "cOoKiE",
    "set-cookie",
    "Set-Cookie",
    "SET-COOKIE",
    "sEt-CoOkIe",
];

/// A secret value that differs per field, so a leak names its source.
fn secret(name: &str) -> String {
    format!("s3cr3t-{}-0123456789", name.len())
}

/// Asserts `shown` (a `Debug` rendering) never contains `secret` and does
/// contain the redaction marker with the secret's length.
fn assert_redacted(what: &str, shown: &str, secret: &str) {
    assert!(!shown.contains(secret), "{what}: secret printed in {shown}");
    let marker = format!("<redacted {} bytes>", secret.len());
    assert!(shown.contains(&marker), "{what}: no {marker} in {shown}");
}

fn both(value: &impl std::fmt::Debug) -> [String; 2] {
    [format!("{value:?}"), format!("{value:#?}")]
}

fn request_with(name: &str, value: &str) -> Vec<u8> {
    format!("GET / HTTP/1.1\r\nHost: a\r\nX-Visible: shown\r\n{name}: {value}\r\n\r\n").into_bytes()
}

fn response_with(name: &str, value: &str) -> Vec<u8> {
    format!("HTTP/1.1 200 OK\r\nX-Visible: shown\r\n{name}: {value}\r\nContent-Length: 0\r\n\r\n")
        .into_bytes()
}

#[test]
fn test_request_head_headers_and_event_debug_redact_every_spelling() {
    for name in SPELLINGS {
        let value = secret(name);
        let mut conn = ServerConn::new();
        let _ = conn.feed(&request_with(name, &value));
        let event = conn.poll().unwrap();
        for shown in both(&event) {
            assert_redacted(&format!("ServerEvent::Request / {name}"), &shown, &value);
            assert!(
                shown.contains("shown"),
                "other fields stay visible: {shown}"
            );
        }
        let ServerEvent::Request(req) = event else {
            panic!("expected a request");
        };
        for shown in both(&req) {
            assert_redacted(&format!("RequestHead / {name}"), &shown, &value);
        }
        for shown in both(&req.headers()) {
            assert_redacted(&format!("Headers / {name}"), &shown, &value);
        }
        for shown in both(&req.header_all(name)) {
            assert_redacted(&format!("HeaderValues / {name}"), &shown, &value);
        }
        // Accessors still return the real bytes.
        assert_eq!(req.header(name), Some(value.as_bytes()));
        assert_eq!(req.header_all(name).next(), Some(value.as_bytes()));
        assert!(req.headers().any(|(_, v)| v == value.as_bytes()));
    }
}

#[test]
fn test_response_head_and_client_event_debug_redact_every_spelling() {
    for name in SPELLINGS {
        let value = secret(name);
        let mut conn = ClientConn::new();
        conn.send_request(&Request::get("/").header("host", "a"), &mut Vec::new())
            .unwrap();
        let _ = conn.feed(&response_with(name, &value));
        let event = conn.poll().unwrap();
        for shown in both(&event) {
            assert_redacted(&format!("ClientEvent::Response / {name}"), &shown, &value);
        }
        let ClientEvent::Response(resp) = event else {
            panic!("expected a response");
        };
        for shown in both(&resp) {
            assert_redacted(&format!("ResponseHead / {name}"), &shown, &value);
            assert!(shown.contains("shown"));
        }
        for shown in both(&resp.headers()) {
            assert_redacted(&format!("Headers / {name}"), &shown, &value);
        }
        for shown in both(&resp.header_all(name)) {
            assert_redacted(&format!("HeaderValues / {name}"), &shown, &value);
        }
        assert_eq!(resp.header(name), Some(value.as_bytes()));
    }
}

#[test]
fn test_builders_debug_redact_every_spelling() {
    for name in SPELLINGS {
        let value = secret(name);
        let request = Request::get("/")
            .header("host", "a")
            .header("x-visible", "shown")
            .header(name, &value);
        for shown in both(&request) {
            assert_redacted(&format!("Request / {name}"), &shown, &value);
            assert!(shown.contains("shown"));
        }
        assert_eq!(request.header_value(name), Some(value.as_bytes()));

        let response = Response::new(200)
            .header("x-visible", "shown")
            .header(name, &value);
        for shown in both(&response) {
            assert_redacted(&format!("Response / {name}"), &shown, &value);
        }
        assert_eq!(response.header_value(name), Some(value.as_bytes()));
    }
}

#[test]
fn test_repeated_credential_fields_are_each_redacted() {
    let mut conn = ServerConn::new();
    let _ = conn
        .feed(b"GET / HTTP/1.1\r\nHost: a\r\nCookie: first=aaaa\r\ncookie: second=bbbbbb\r\n\r\n");
    let ServerEvent::Request(req) = conn.poll().unwrap() else {
        panic!("expected a request");
    };
    for shown in both(&req)
        .into_iter()
        .chain(both(&req.header_all("cookie")))
    {
        assert!(
            !shown.contains("first=aaaa") && !shown.contains("second=bbbbbb"),
            "{shown}"
        );
        assert!(
            shown.contains("<redacted 10 bytes>") && shown.contains("<redacted 13 bytes>"),
            "{shown}"
        );
    }
}

#[test]
fn test_empty_credential_value_is_redacted_as_zero_bytes() {
    let mut conn = ServerConn::new();
    let _ = conn.feed(b"GET / HTTP/1.1\r\nHost: a\r\nAuthorization:\r\n\r\n");
    let ServerEvent::Request(req) = conn.poll().unwrap() else {
        panic!("expected a request");
    };
    assert!(format!("{req:?}").contains("<redacted 0 bytes>"));
}

/// T-35: the views that copy or rebuild a head redact like the borrowed
/// ones. An `Authorization: Bearer s3cr3t` request head through
/// `RequestHead::to_owned`, through `OwnedRequestHead::from_parts` of its
/// own `into_parts`, and through `parse_request_head` (the `Parsed` result
/// and the head), and a response with `Set-Cookie` through
/// `parse_response_head`, never print the secret in `{:?}` or `{:#?}` and
/// print `<redacted N bytes>` instead.
#[test]
fn test_owned_and_stateless_views_debug_redact_credentials() {
    let value = "Bearer s3cr3t";
    let input = request_with("Authorization", value);
    let mut conn = ServerConn::new();
    let _ = conn.feed(&input);
    let ServerEvent::Request(req) = conn.poll().unwrap() else {
        panic!("expected a request");
    };
    let owned = req.to_owned();
    for shown in both(&owned) {
        assert_redacted("OwnedRequestHead (to_owned)", &shown, value);
        assert!(shown.contains("shown"), "{shown}");
    }
    for shown in both(&owned.view()) {
        assert_redacted("OwnedRequestHead::view", &shown, value);
    }
    let (bytes, fields, facts) = owned.into_parts();
    let rebuilt = OwnedRequestHead::from_parts(bytes, fields, facts).unwrap();
    for shown in both(&rebuilt) {
        assert_redacted("OwnedRequestHead (from_parts)", &shown, value);
    }
    assert_eq!(
        rebuilt.view().header("authorization"),
        Some(value.as_bytes())
    );

    let mut slots = [FieldSpan::default(); 16];
    let parsed = parse_request_head(&input, &Limits::new(), &mut slots).unwrap();
    for shown in both(&parsed) {
        assert_redacted("Parsed<ParsedRequest>", &shown, value);
    }
    let Parsed::Complete { head, .. } = parsed else {
        panic!("expected a complete head");
    };
    for shown in both(&head).into_iter().chain(both(&head.view())) {
        assert_redacted("ParsedRequest", &shown, value);
    }

    let cookie = "sid=s3cr3t-session; HttpOnly";
    let input = response_with("Set-Cookie", cookie);
    let mut slots = [FieldSpan::default(); 16];
    let parsed = parse_response_head(&input, &Limits::new(), Asked::default(), &mut slots).unwrap();
    for shown in both(&parsed) {
        assert_redacted("Parsed<ParsedResponse>", &shown, cookie);
    }
    let Parsed::Complete { head, .. } = parsed else {
        panic!("expected a complete head");
    };
    for shown in both(&head).into_iter().chain(both(&head.view())) {
        assert_redacted("ParsedResponse", &shown, cookie);
        assert!(shown.contains("shown"), "{shown}");
    }
    assert_eq!(head.view().header("set-cookie"), Some(cookie.as_bytes()));
}

#[cfg(feature = "std")]
#[test]
fn test_blocking_client_response_debug_redacts_set_cookie() {
    use http_net::blocking;

    let server = blocking::Server::bind("127.0.0.1:0").unwrap();
    let addr = server.local_addr();
    let stop = server.shutdown_handle();
    let thread = std::thread::spawn(move || {
        server.serve(|_req, _body| {
            Response::text(200, "ok")
                .header("Set-Cookie", "sid=s3cr3t-session-value; HttpOnly")
                .header("set-cookie", "csrf=s3cr3t-csrf-token")
                .header("x-visible", "shown")
        })
    });
    let resp = blocking::get(&format!("http://{addr}/")).unwrap();
    for shown in both(&resp) {
        assert!(!shown.contains("s3cr3t"), "secret printed in {shown}");
        assert!(shown.contains("<redacted 34 bytes>"), "{shown}");
        assert!(shown.contains("<redacted 22 bytes>"), "{shown}");
        assert!(shown.contains("shown"), "{shown}");
    }
    assert_eq!(
        resp.header("set-cookie"),
        Some(&b"sid=s3cr3t-session-value; HttpOnly"[..])
    );
    stop.shutdown();
    thread.join().unwrap().unwrap();
}

/// `ResponseHeadRef` (the allocation-free writer's head) redacts like every
/// other type that prints fields (fixed in 0.9.0: it derived `Debug`).
#[test]
fn test_response_head_ref_debug_redacts_every_spelling() {
    use http_net::{OutgoingBody, ResponseHeadRef};

    for &name in SPELLINGS {
        let value = secret(name);
        let fields: &[(&str, &[u8])] = &[(name, value.as_bytes()), ("x-visible", b"shown")];
        let head = ResponseHeadRef::new(200, fields, OutgoingBody::Empty);
        for shown in both(&head) {
            assert_redacted(name, &shown, &value);
            assert!(shown.contains("shown"), "{shown}");
            assert!(shown.contains("200"), "{shown}");
        }
    }
}

/// `blocking::Connection` holds the last request body and the last
/// serialized response; its `Debug` prints their lengths only (fixed in
/// 0.9.0: it derived `Debug` and printed both).
#[cfg(feature = "std")]
#[test]
fn test_blocking_connection_debug_never_prints_its_buffers() {
    use std::io::{Read, Write};

    use http_net::{Limits, blocking::Connection};

    let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
    let addr = listener.local_addr().unwrap();
    let client = std::thread::spawn(move || {
        let mut stream = std::net::TcpStream::connect(addr).unwrap();
        stream
            .write_all(
                b"POST / HTTP/1.1\r\nHost: a\r\nContent-Length: 20\r\n\r\ns3cr3t-request-body!",
            )
            .unwrap();
        let mut reply = Vec::new();
        let mut buf = [0u8; 512];
        while !reply.windows(2).any(|w| w == b"ok") {
            let n = stream.read(&mut buf).unwrap();
            assert!(n > 0);
            reply.extend_from_slice(&buf[..n]);
        }
    });
    let (stream, _) = listener.accept().unwrap();
    let mut conn = Connection::new(stream, Limits::new());
    assert!(conn.next_request().unwrap().is_some());
    conn.respond(&Response::text(200, "ok").header("set-cookie", "sid=s3cr3t-session"))
        .unwrap();
    client.join().unwrap();
    for shown in both(&conn) {
        assert!(!shown.contains("s3cr3t"), "secret printed in {shown}");
        assert!(
            !shown.contains("115, 51, 99"),
            "secret bytes printed in {shown}"
        );
        assert!(shown.contains("out_len"), "{shown}");
    }
}

/// Body bytes print as their length, never as bytes: a body can carry
/// credentials no field redaction sees (fixed in 0.9.1 for the body events,
/// `blocking::Incoming`, and the leftover bytes of `blocking::Upgraded`).
#[test]
fn test_body_events_debug_print_lengths_only() {
    let mut server = ServerConn::new();
    let _ =
        server.feed(b"POST / HTTP/1.1\r\nHost: a\r\nContent-Length: 16\r\n\r\npassword=s3cr3t!");
    assert!(matches!(server.poll(), Ok(ServerEvent::Request(_))));
    let event = server.poll().unwrap();
    assert!(matches!(event, ServerEvent::Body(_)));
    for shown in both(&event) {
        assert!(
            !shown.contains("s3cr3t") && !shown.contains("112, 97"),
            "{shown}"
        );
        assert!(shown.contains("16 bytes"), "{shown}");
    }

    let mut client = ClientConn::new();
    client
        .send_request(&Request::get("/").header("host", "a"), &mut Vec::new())
        .unwrap();
    let _ = client.feed(b"HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\ntoken=s3cr3t!");
    assert!(matches!(client.poll(), Ok(ClientEvent::Response(_))));
    let event = client.poll().unwrap();
    assert!(matches!(event, ClientEvent::Body(_)));
    for shown in both(&event) {
        assert!(
            !shown.contains("s3cr3t") && !shown.contains("116, 111"),
            "{shown}"
        );
        assert!(shown.contains("13 bytes"), "{shown}");
    }
}

#[cfg(feature = "std")]
#[test]
fn test_blocking_incoming_and_upgraded_debug_print_lengths_only() {
    use std::io::Write;

    use http_net::{Limits, blocking::Connection};

    fn pair() -> (std::net::TcpStream, std::net::TcpStream) {
        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let client = std::net::TcpStream::connect(listener.local_addr().unwrap()).unwrap();
        let (server, _) = listener.accept().unwrap();
        (client, server)
    }

    let (mut client, server) = pair();
    client
        .write_all(b"POST /login HTTP/1.1\r\nHost: a\r\nContent-Length: 16\r\n\r\npassword=s3cr3t!")
        .unwrap();
    let mut conn = Connection::new(server, Limits::new());
    let incoming = conn.next_request().unwrap().unwrap();
    for shown in both(&incoming) {
        assert!(
            !shown.contains("s3cr3t") && !shown.contains("112, 97"),
            "{shown}"
        );
        assert!(shown.contains("16 bytes"), "{shown}");
    }

    // A plain-HTTP tunnel: the tunnelled request, credentials and all, is
    // already buffered when the 200 is sent.
    let (mut client, server) = pair();
    client
        .write_all(
            b"CONNECT origin:80 HTTP/1.1\r\nHost: origin:80\r\n\r\n\
              GET / HTTP/1.1\r\nHost: origin\r\nAuthorization: Basic s3cr3t\r\n\r\n",
        )
        .unwrap();
    // Give the tunnelled bytes time to arrive with the CONNECT, so they are
    // the leftover.
    std::thread::sleep(std::time::Duration::from_millis(100));
    let mut conn = Connection::new(server, Limits::new());
    assert!(conn.next_request().unwrap().is_some());
    conn.respond(&Response::new(200)).unwrap();
    let upgraded = conn.into_upgraded().unwrap();
    for shown in both(&upgraded) {
        assert!(
            !shown.contains("s3cr3t") && !shown.contains("71, 69, 84"),
            "{shown}"
        );
        assert!(shown.contains("bytes"), "{shown}");
    }
}