churust-core 0.3.2

Core engine, routing, pipeline, and extractors for the Churust web framework.
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
//! The incremental `multipart/form-data` parser.
//!
//! The interesting cases are the ones a buffered parser never has to think
//! about: a delimiter split across two network chunks, a part skipped without
//! being read, and a body that stops in the middle.

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

use bytes::Bytes;
use churust_core::{Churust, Error, MultipartStream, TestClient};
use http::StatusCode;

/// A body with two fields and one file, as a browser would send it.
fn body() -> String {
    [
        "--X\r\n",
        "Content-Disposition: form-data; name=\"title\"\r\n\r\n",
        "a report\r\n",
        "--X\r\n",
        "Content-Disposition: form-data; name=\"doc\"; filename=\"a.txt\"\r\n",
        "Content-Type: text/plain\r\n\r\n",
        "hello world\r\n",
        "--X--\r\n",
    ]
    .concat()
}

/// An app whose handler reports `name=len` for every field, in order.
fn summing_app() -> churust_core::App {
    Churust::server()
        .routing(|r| {
            r.post("/upload", |mut form: MultipartStream| async move {
                let mut out = Vec::new();
                while let Some(mut field) = form.next_field().await? {
                    let name = field.name().to_string();
                    let mut total = 0usize;
                    while let Some(chunk) = field.chunk().await? {
                        total += chunk.len();
                    }
                    out.push(format!("{name}={total}"));
                }
                Ok::<_, Error>(out.join(","))
            });
        })
        .build()
}

#[tokio::test]
async fn fields_arrive_in_order_with_their_content() {
    let res = TestClient::new(summing_app())
        .post("/upload")
        .header("content-type", "multipart/form-data; boundary=X")
        .body(body())
        .send()
        .await;

    assert_eq!(res.status(), StatusCode::OK);
    assert_eq!(res.text(), "title=8,doc=11");
}

#[tokio::test]
async fn metadata_survives_the_incremental_parse() {
    let app = Churust::server()
        .routing(|r| {
            r.post("/upload", |mut form: MultipartStream| async move {
                let mut out = Vec::new();
                while let Some(mut field) = form.next_field().await? {
                    let name = field.name().to_string();
                    let filename = field.filename().unwrap_or("-").to_string();
                    let content_type = field.content_type().unwrap_or("-").to_string();
                    let text = field.text().await?;
                    out.push(format!("{name}|{filename}|{content_type}|{text}"));
                }
                Ok::<_, Error>(out.join(" "))
            });
        })
        .build();

    let res = TestClient::new(app)
        .post("/upload")
        .header("content-type", "multipart/form-data; boundary=X")
        .body(body())
        .send()
        .await;

    assert_eq!(
        res.text(),
        "title|-|-|a report doc|a.txt|text/plain|hello world"
    );
}

#[tokio::test]
async fn a_skipped_field_does_not_derail_the_parser() {
    let app = Churust::server()
        .routing(|r| {
            r.post("/upload", |mut form: MultipartStream| async move {
                // Read nothing from the first field at all.
                let first = form.next_field().await?.map(|f| f.name().to_string());
                let second = match form.next_field().await? {
                    Some(mut f) => {
                        let name = f.name().to_string();
                        format!("{name}={}", f.text().await?)
                    }
                    None => "none".into(),
                };
                Ok::<_, Error>(format!("{}, {second}", first.unwrap_or_default()))
            });
        })
        .build();

    let res = TestClient::new(app)
        .post("/upload")
        .header("content-type", "multipart/form-data; boundary=X")
        .body(body())
        .send()
        .await;

    assert_eq!(res.text(), "title, doc=hello world");
}

#[tokio::test]
async fn content_containing_the_boundary_text_is_not_split_early() {
    // The bytes "--X" appear inside the value. Only the delimiter preceded by
    // CRLF ends a part, and a parser that searched for the bare boundary would
    // truncate here.
    let body = [
        "--X\r\n",
        "Content-Disposition: form-data; name=\"note\"\r\n\r\n",
        "a --X b\r\n",
        "--X--\r\n",
    ]
    .concat();

    let res = TestClient::new(summing_app())
        .post("/upload")
        .header("content-type", "multipart/form-data; boundary=X")
        .body(body)
        .send()
        .await;

    assert_eq!(res.text(), "note=7");
}

#[tokio::test]
async fn a_delimiter_followed_by_anything_but_padding_is_content() {
    // RFC 2046 §5.1.1 allows only transport padding — SP and HTAB — between a
    // delimiter and the CRLF that ends its line, so `\r\n--Xz` is content and
    // not a delimiter. A parser that accepts it frames the body differently
    // from every conforming one in front of it, which is how a field-level
    // filter in a proxy comes to pass a body whose forged field the origin then
    // acts on.
    let body = [
        "--X\r\n",
        "Content-Disposition: form-data; name=\"file\"; filename=\"a.txt\"\r\n\r\n",
        "BEGIN\r\n",
        "--Xz\r\n",
        "Content-Disposition: form-data; name=\"role\"\r\n\r\n",
        "admin\r\n",
        "--X--\r\n",
    ]
    .concat();

    let app = Churust::server()
        .routing(|r| {
            r.post("/upload", |mut form: MultipartStream| async move {
                let mut out = Vec::new();
                while let Some(mut field) = form.next_field().await? {
                    let name = field.name().to_string();
                    out.push(format!("{name}={}", field.text().await?));
                }
                Ok::<_, Error>(out.join(","))
            });
        })
        .build();

    let res = TestClient::new(app)
        .post("/upload")
        .header("content-type", "multipart/form-data; boundary=X")
        .body(body)
        .send()
        .await;

    let text = res.text();
    assert!(
        text.starts_with("file=BEGIN"),
        "the one part must own everything from BEGIN onward: {text}"
    );
    assert!(
        !text.contains("role="),
        "the padded delimiter forged a field: {text}"
    );
}

#[tokio::test]
async fn real_transport_padding_after_a_delimiter_is_still_accepted() {
    // Spaces and tabs before the CRLF are legal and ignorable, so requiring the
    // CRLF must not mean refusing what precedes it.
    let body = [
        "--X \t\r\n",
        "Content-Disposition: form-data; name=\"note\"\r\n\r\n",
        "hi\r\n",
        "--X--\r\n",
    ]
    .concat();

    let res = TestClient::new(summing_app())
        .post("/upload")
        .header("content-type", "multipart/form-data; boundary=X")
        .body(body)
        .send()
        .await;

    assert_eq!(res.status(), StatusCode::OK);
    assert_eq!(res.text(), "note=2");
}

#[tokio::test]
async fn a_body_that_stops_inside_a_part_is_a_400() {
    let truncated = [
        "--X\r\n",
        "Content-Disposition: form-data; name=\"doc\"\r\n\r\n",
        "half a fi",
    ]
    .concat();

    let res = TestClient::new(summing_app())
        .post("/upload")
        .header("content-type", "multipart/form-data; boundary=X")
        .body(truncated)
        .send()
        .await;

    assert_eq!(res.status(), StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn a_body_with_no_delimiter_at_all_is_a_400() {
    let res = TestClient::new(summing_app())
        .post("/upload")
        .header("content-type", "multipart/form-data; boundary=X")
        .body("not multipart at all")
        .send()
        .await;

    assert_eq!(res.status(), StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn a_part_without_a_name_is_refused() {
    let body = [
        "--X\r\n",
        "Content-Type: text/plain\r\n\r\n",
        "x\r\n",
        "--X--\r\n",
    ]
    .concat();

    let res = TestClient::new(summing_app())
        .post("/upload")
        .header("content-type", "multipart/form-data; boundary=X")
        .body(body)
        .send()
        .await;

    assert_eq!(res.status(), StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn a_non_multipart_content_type_is_a_415() {
    let res = TestClient::new(summing_app())
        .post("/upload")
        .header("content-type", "application/json")
        .body("{}")
        .send()
        .await;

    assert_eq!(res.status(), StatusCode::UNSUPPORTED_MEDIA_TYPE);
}

#[tokio::test]
async fn an_empty_part_reads_as_zero_bytes() {
    let body = [
        "--X\r\n",
        "Content-Disposition: form-data; name=\"empty\"\r\n\r\n",
        "\r\n",
        "--X--\r\n",
    ]
    .concat();

    let res = TestClient::new(summing_app())
        .post("/upload")
        .header("content-type", "multipart/form-data; boundary=X")
        .body(body)
        .send()
        .await;

    assert_eq!(res.text(), "empty=0");
}

#[tokio::test]
async fn a_preamble_before_the_first_delimiter_is_ignored() {
    // RFC 2046 §5.1.1 allows text before the first delimiter, for readers that
    // do not understand multipart at all.
    let body = [
        "this is a preamble for non-multipart readers\r\n",
        "--X\r\n",
        "Content-Disposition: form-data; name=\"note\"\r\n\r\n",
        "hi\r\n",
        "--X--\r\n",
    ]
    .concat();

    let res = TestClient::new(summing_app())
        .post("/upload")
        .header("content-type", "multipart/form-data; boundary=X")
        .body(body)
        .send()
        .await;

    assert_eq!(res.text(), "note=2");
}

#[tokio::test]
async fn a_quoted_boundary_is_accepted() {
    let res = TestClient::new(summing_app())
        .post("/upload")
        .header("content-type", "multipart/form-data; boundary=\"X\"")
        .body(body())
        .send()
        .await;

    assert_eq!(res.text(), "title=8,doc=11");
}

#[tokio::test]
async fn a_field_larger_than_the_collect_limit_is_refused() {
    let app = Churust::server()
        .routing(|r| {
            r.post("/upload", |form: MultipartStream| async move {
                let mut form = form.max_field_bytes(8);
                let mut field = form.next_field().await?.expect("one field");
                let text = field.text().await?;
                Ok::<_, Error>(text)
            });
        })
        .build();

    let body = [
        "--X\r\n",
        "Content-Disposition: form-data; name=\"big\"\r\n\r\n",
        "0123456789abcdef\r\n",
        "--X--\r\n",
    ]
    .concat();

    let res = TestClient::new(app)
        .post("/upload")
        .header("content-type", "multipart/form-data; boundary=X")
        .body(body)
        .send()
        .await;

    assert_eq!(res.status(), StatusCode::PAYLOAD_TOO_LARGE);
    assert!(res.text().contains("field too large"));
}

#[tokio::test]
async fn chunking_the_body_does_not_change_what_is_parsed() {
    // The parser is fed from a real socket here, so a delimiter that lands
    // across two TCP segments exercises the held-back-tail path rather than
    // being handed over in one buffer.
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let port = listener.local_addr().unwrap().port();

    let app = Churust::server()
        .host("127.0.0.1")
        .port(port)
        .routing(|r| {
            r.post("/upload", |mut form: MultipartStream| async move {
                let mut out = Vec::new();
                while let Some(mut field) = form.next_field().await? {
                    let name = field.name().to_string();
                    let content = field.bytes().await?;
                    out.push(format!("{name}={}", content.len()));
                }
                Ok::<_, Error>(out.join(","))
            });
        })
        .build();

    tokio::spawn(async move {
        let _ = app.start_on(listener, std::future::pending()).await;
    });
    for _ in 0..50 {
        if tokio::net::TcpStream::connect(("127.0.0.1", port))
            .await
            .is_ok()
        {
            break;
        }
        tokio::time::sleep(std::time::Duration::from_millis(20)).await;
    }

    // 64 KiB of content, so the body is split across many segments by the
    // kernel regardless of how it is written.
    let payload = "z".repeat(64 * 1024);
    let body = format!(
        "--X\r\nContent-Disposition: form-data; name=\"big\"; filename=\"b.bin\"\r\n\r\n{payload}\r\n--X--\r\n"
    );

    let response = {
        use tokio::io::{AsyncReadExt, AsyncWriteExt};
        let mut socket = tokio::net::TcpStream::connect(("127.0.0.1", port))
            .await
            .unwrap();
        let request = format!(
            "POST /upload HTTP/1.1\r\nHost: localhost\r\nContent-Type: multipart/form-data; boundary=X\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
            body.len()
        );
        socket.write_all(request.as_bytes()).await.unwrap();
        // Deliberately dribbled out, so the delimiter at the end lands in its
        // own segment rather than alongside the content.
        for piece in body.as_bytes().chunks(4096) {
            socket.write_all(piece).await.unwrap();
            socket.flush().await.unwrap();
        }
        let mut buf = String::new();
        socket.read_to_string(&mut buf).await.unwrap();
        buf
    };

    assert!(
        response.contains("big=65536"),
        "the streamed parse should see every byte exactly once: {response}"
    );
}

#[tokio::test]
async fn the_server_wide_body_cap_still_applies() {
    // Streaming removes the memory cost, not the ceiling: the engine wraps the
    // stream in `max_body_bytes`, so an oversized upload is still refused.
    //
    // This has to go over a socket. `TestClient` drives the pipeline directly
    // and the server-wide cap is applied by the engine in front of it, so the
    // in-process client would accept a body the real server refuses.
    let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
    let port = listener.local_addr().unwrap().port();

    let app = Churust::server()
        .host("127.0.0.1")
        .port(port)
        .max_body_bytes(64)
        .routing(|r| {
            r.post("/upload", |mut form: MultipartStream| async move {
                let mut total = 0usize;
                while let Some(mut field) = form.next_field().await? {
                    while let Some(chunk) = field.chunk().await? {
                        total += chunk.len();
                    }
                }
                Ok::<_, Error>(format!("{total}"))
            });
        })
        .build();

    tokio::spawn(async move {
        let _ = app.start_on(listener, std::future::pending()).await;
    });
    for _ in 0..50 {
        if tokio::net::TcpStream::connect(("127.0.0.1", port))
            .await
            .is_ok()
        {
            break;
        }
        tokio::time::sleep(std::time::Duration::from_millis(20)).await;
    }

    let payload = "y".repeat(4096);
    let body = format!(
        "--X\r\nContent-Disposition: form-data; name=\"big\"\r\n\r\n{payload}\r\n--X--\r\n"
    );

    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    let mut socket = tokio::net::TcpStream::connect(("127.0.0.1", port))
        .await
        .unwrap();
    let request = format!(
        "POST /upload HTTP/1.1\r\nHost: localhost\r\nContent-Type: multipart/form-data; boundary=X\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
        body.len()
    );
    socket.write_all(request.as_bytes()).await.unwrap();
    let mut response = String::new();
    let _ = socket.read_to_string(&mut response).await;

    assert!(
        response.starts_with("HTTP/1.1 413"),
        "a 4 KiB upload must not pass a 64 byte cap: {}",
        response.lines().next().unwrap_or("")
    );
}

#[tokio::test]
async fn a_chunk_is_never_larger_than_what_arrived() {
    // Guards the invariant the whole design rests on: content is handed out as
    // it arrives, so no single chunk is the size of the body.
    let app = Churust::server()
        .max_body_bytes(1024 * 1024)
        .routing(|r| {
            r.post("/upload", |mut form: MultipartStream| async move {
                let mut field = form.next_field().await?.expect("one field");
                let mut largest = 0usize;
                let mut total = 0usize;
                while let Some(chunk) = field.chunk().await? {
                    largest = largest.max(chunk.len());
                    total += chunk.len();
                }
                Ok::<_, Error>(format!("{total}/{largest}"))
            });
        })
        .build();

    let payload = Bytes::from("q".repeat(200 * 1024));
    let body = format!(
        "--X\r\nContent-Disposition: form-data; name=\"big\"\r\n\r\n{}\r\n--X--\r\n",
        String::from_utf8(payload.to_vec()).unwrap()
    );

    let res = TestClient::new(app)
        .post("/upload")
        .header("content-type", "multipart/form-data; boundary=X")
        .body(body)
        .send()
        .await;

    let text = res.text();
    let (total, _largest) = text.split_once('/').expect("total/largest");
    assert_eq!(total, (200 * 1024).to_string());
}