web-net 0.9.0

Web layer on serve-net: URL normalization, sites and base paths, a compiled router with 404 and 405, gate checkpoints for authentication and authorization, hooks, error pages, and a render trait that reads typed views.
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
//! Static files, health paths, security headers, cookies out, and the 304
//! fast path.

use std::path::PathBuf;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

use serve_net::Scheme;
use serve_net::testing::{TestReply, TestRequest, respond};
use web_net::{App, Assets, SecurityHeaders, get};

/// A fresh folder under the system temp folder, removed on drop.
struct Dir(PathBuf);

impl Dir {
    fn new(name: &str) -> Self {
        let p = std::env::temp_dir().join(format!(
            "web-net-test-{name}-{}-{:?}",
            std::process::id(),
            std::thread::current().id()
        ));
        let _ = std::fs::remove_dir_all(&p);
        std::fs::create_dir_all(p.join("sub")).unwrap();
        std::fs::write(p.join("a.css"), b"body{color:red}").unwrap();
        std::fs::write(p.join("a.css.br"), b"BROTLI").unwrap();
        std::fs::write(p.join("a.css.gz"), b"GZIP").unwrap();
        std::fs::write(p.join("data.txt"), b"0123456789").unwrap();
        std::fs::write(p.join(".env"), b"SECRET=1").unwrap();
        std::fs::write(p.join("sub").join("index.html"), b"<h1>sub</h1>").unwrap();
        Self(p)
    }
}

impl Drop for Dir {
    fn drop(&mut self) {
        let _ = std::fs::remove_dir_all(&self.0);
    }
}

fn app(dir: &Dir) -> App {
    App::builder()
        .assets(Assets::new("/static", &dir.0))
        .assets(
            Assets::new("/docs", &dir.0)
                .index("index.html")
                .precompressed(false),
        )
        .health("/healthz")
        .route(
            get("/", |cx| {
                cx.text(200, "home");
                Ok(())
            })
            .public(),
        )
        .build()
        .unwrap()
}

fn send(app: &App, head: &str) -> TestReply {
    respond(app, format!("{head}\r\n\r\n").as_bytes())
}

fn h<'a>(r: &'a TestReply, name: &str) -> &'a str {
    r.header(name)
        .map(|v| std::str::from_utf8(v).unwrap())
        .unwrap_or("")
}

#[test]
fn test_serves_a_file_with_validators() {
    let d = Dir::new("validators");
    let app = app(&d);
    let r = send(&app, "GET /static/data.txt HTTP/1.1\r\nHost: a");
    assert_eq!((r.status, r.text()), (200, "0123456789"));
    assert_eq!(h(&r, "content-type"), "text/plain; charset=utf-8");
    assert_eq!(h(&r, "accept-ranges"), "bytes");
    assert_eq!(h(&r, "cache-control"), "no-cache");
    assert!(!h(&r, "last-modified").is_empty());
    let tag = h(&r, "etag").to_owned();
    assert!(tag.starts_with('"'));

    let r = send(
        &app,
        &format!("GET /static/data.txt HTTP/1.1\r\nHost: a\r\nIf-None-Match: {tag}"),
    );
    assert_eq!(r.status, 304);
    assert!(r.body.is_empty());
    let r = send(
        &app,
        "GET /static/data.txt HTTP/1.1\r\nHost: a\r\nIf-None-Match: \"other\"",
    );
    assert_eq!(r.status, 200);

    let lm = {
        let r = send(&app, "GET /static/data.txt HTTP/1.1\r\nHost: a");
        h(&r, "last-modified").to_owned()
    };
    let r = send(
        &app,
        &format!("GET /static/data.txt HTTP/1.1\r\nHost: a\r\nIf-Modified-Since: {lm}"),
    );
    assert_eq!(r.status, 304);
    let r = send(
        &app,
        "GET /static/data.txt HTTP/1.1\r\nHost: a\r\nIf-Modified-Since: Sun, 06 Nov 1994 08:49:37 GMT",
    );
    assert_eq!(r.status, 200);

    let r = send(&app, "HEAD /static/data.txt HTTP/1.1\r\nHost: a");
    assert_eq!((r.status, r.body.len()), (200, 0));
    assert_eq!(h(&r, "content-length"), "10");
}

#[test]
fn test_ranges() {
    let d = Dir::new("ranges");
    let app = app(&d);
    let r = send(
        &app,
        "GET /static/data.txt HTTP/1.1\r\nHost: a\r\nRange: bytes=2-4",
    );
    assert_eq!((r.status, r.text()), (206, "234"));
    assert_eq!(h(&r, "content-range"), "bytes 2-4/10");
    let r = send(
        &app,
        "GET /static/data.txt HTTP/1.1\r\nHost: a\r\nRange: bytes=-3",
    );
    assert_eq!(r.text(), "789");
    let r = send(
        &app,
        "GET /static/data.txt HTTP/1.1\r\nHost: a\r\nRange: bytes=50-60",
    );
    assert_eq!((r.status, h(&r, "content-range")), (416, "bytes */10"));
    let r = send(
        &app,
        "GET /static/data.txt HTTP/1.1\r\nHost: a\r\nRange: bytes=0-0, 5-5",
    );
    assert_eq!((r.status, r.text()), (200, "0123456789"));
    let r = send(
        &app,
        "GET /static/data.txt HTTP/1.1\r\nHost: a\r\nRange: bytes=2-4\r\nIf-Range: \"stale\"",
    );
    assert_eq!(r.status, 200);
    let tag = h(
        &send(&app, "GET /static/data.txt HTTP/1.1\r\nHost: a"),
        "etag",
    )
    .to_owned();
    let r = send(
        &app,
        &format!("GET /static/data.txt HTTP/1.1\r\nHost: a\r\nRange: bytes=2-4\r\nIf-Range: {tag}"),
    );
    assert_eq!(r.status, 206);
}

#[test]
fn test_precompressed_variants() {
    let d = Dir::new("precompressed");
    let app = app(&d);
    let r = send(
        &app,
        "GET /static/a.css HTTP/1.1\r\nHost: a\r\nAccept-Encoding: gzip, br",
    );
    assert_eq!((r.text(), h(&r, "content-encoding")), ("BROTLI", "br"));
    assert_eq!(h(&r, "vary"), "accept-encoding");
    assert_eq!(h(&r, "content-type"), "text/css; charset=utf-8");
    let br_tag = h(&r, "etag").to_owned();
    let r = send(
        &app,
        "GET /static/a.css HTTP/1.1\r\nHost: a\r\nAccept-Encoding: gzip",
    );
    assert_eq!((r.text(), h(&r, "content-encoding")), ("GZIP", "gzip"));
    assert_ne!(h(&r, "etag"), br_tag);
    let r = send(&app, "GET /static/a.css HTTP/1.1\r\nHost: a");
    assert_eq!(
        (r.text(), h(&r, "content-encoding")),
        ("body{color:red}", "")
    );
    let r = send(
        &app,
        "GET /static/a.css HTTP/1.1\r\nHost: a\r\nAccept-Encoding: br;q=0, gzip;q=0",
    );
    assert_eq!(r.text(), "body{color:red}");
    // Off for this mount.
    let r = send(
        &app,
        "GET /docs/a.css HTTP/1.1\r\nHost: a\r\nAccept-Encoding: br",
    );
    assert_eq!((r.text(), h(&r, "vary")), ("body{color:red}", ""));
}

#[test]
fn test_refused_names_and_folders() {
    let d = Dir::new("refused");
    let app = app(&d);
    for p in [
        "/static/.env",
        "/static/a.css::$DATA",
        "/static/data.txt.",
        "/static/data.txt%20",
        "/static/CON",
        "/static/nul.txt",
        "/static/sub",
        "/static/missing.txt",
        "/static",
    ] {
        let r = send(&app, &format!("GET {p} HTTP/1.1\r\nHost: a"));
        assert_eq!(r.status, 404, "{p}");
    }
    // Paths that climb are canonicalized before the mount is even looked at.
    let r = send(&app, "GET /static/../static/data.txt HTTP/1.1\r\nHost: a");
    assert_eq!((r.status, h(&r, "location")), (301, "/static/data.txt"));
    let r = send(&app, "GET /static/..%2f..%2fetc HTTP/1.1\r\nHost: a");
    assert_eq!(r.status, 400);
    // An index file for a folder where the mount has one.
    assert_eq!(
        send(&app, "GET /docs/sub HTTP/1.1\r\nHost: a").text(),
        "<h1>sub</h1>"
    );
    assert_eq!(send(&app, "GET /docs HTTP/1.1\r\nHost: a").status, 404);
    // Files take GET and HEAD only.
    let r = send(
        &app,
        "POST /static/data.txt HTTP/1.1\r\nHost: a\r\nContent-Length: 0",
    );
    assert_eq!((r.status, h(&r, "allow")), (405, "GET, HEAD, OPTIONS"));
}

/// Windows answers a file by its 8.3 short name too (`ENVSEC~1` for
/// `.envsecret`); the resolved name is what is checked. Skipped on volumes
/// that make no short names.
#[cfg(windows)]
#[test]
fn test_short_names_do_not_reach_hidden_files() {
    let d = Dir::new("shortnames");
    std::fs::write(d.0.join(".envsecret"), b"SECRET").unwrap();
    let app = app(&d);
    for short in ["ENVSEC~1", "envsec~1"] {
        if std::fs::canonicalize(d.0.join(short)).is_err() {
            continue;
        }
        let r = send(&app, &format!("GET /static/{short} HTTP/1.1\r\nHost: a"));
        assert_eq!(r.status, 404, "{short}");
    }
}

#[test]
fn test_more_device_names() {
    let d = Dir::new("devices");
    let app = app(&d);
    for p in [
        "/static/COM%C2%B9",
        "/static/lpt%C2%B3.log",
        "/static/CON%20.txt",
    ] {
        assert_eq!(
            send(&app, &format!("GET {p} HTTP/1.1\r\nHost: a")).status,
            404,
            "{p}"
        );
    }
}

#[cfg(unix)]
#[test]
fn test_unreadable_file_is_a_clean_500() {
    use std::os::unix::fs::PermissionsExt;
    let d = Dir::new("unreadable");
    let f = d.0.join("locked.txt");
    std::fs::write(&f, b"x").unwrap();
    std::fs::set_permissions(&f, std::fs::Permissions::from_mode(0o000)).unwrap();
    if std::fs::read(&f).is_ok() {
        return; // running as root: the file is readable anyway
    }
    let app = app(&d);
    let r = send(&app, "GET /static/locked.txt HTTP/1.1\r\nHost: a");
    assert_eq!(r.status, 500);
    assert_eq!(h(&r, "etag"), "");
    assert_eq!(h(&r, "cache-control"), "no-store");
    assert_eq!(
        r.headers
            .iter()
            .filter(|(n, _)| n == "content-type")
            .count(),
        1
    );
    std::fs::set_permissions(&f, std::fs::Permissions::from_mode(0o644)).unwrap();
}

#[cfg(unix)]
#[test]
fn test_symlink_out_of_the_root_is_refused() {
    let d = Dir::new("symlink");
    let outside = Dir::new("symlink-outside");
    std::os::unix::fs::symlink(outside.0.join("data.txt"), d.0.join("leak.txt")).unwrap();
    std::os::unix::fs::symlink(d.0.join("data.txt"), d.0.join("inside.txt")).unwrap();
    let app = app(&d);
    assert_eq!(
        send(&app, "GET /static/leak.txt HTTP/1.1\r\nHost: a").status,
        404
    );
    assert_eq!(
        send(&app, "GET /static/inside.txt HTTP/1.1\r\nHost: a").status,
        200
    );
}

/// Sends one request on a test connection and returns the status and the
/// body with chunked framing removed (`TestReply` reads only length-framed
/// bodies).
fn raw(app: App, head: &str) -> (u16, Vec<u8>) {
    let mut conn = serve_net::testing::TestConnection::new(app);
    let mut out = Vec::new();
    conn.send_into(format!("{head}\r\n\r\n").as_bytes(), &mut out);
    let end = out
        .windows(4)
        .position(|w| w == b"\r\n\r\n")
        .expect("a head");
    let head_text = std::str::from_utf8(&out[..end])
        .unwrap()
        .to_ascii_lowercase();
    let status: u16 = head_text[9..12].parse().unwrap();
    assert!(
        head_text.contains("transfer-encoding: chunked"),
        "{head_text}"
    );
    let mut body = Vec::new();
    let mut rest = &out[end + 4..];
    loop {
        let line_end = rest
            .windows(2)
            .position(|w| w == b"\r\n")
            .expect("a chunk size line");
        let size =
            usize::from_str_radix(std::str::from_utf8(&rest[..line_end]).unwrap(), 16).unwrap();
        rest = &rest[line_end + 2..];
        if size == 0 {
            break;
        }
        body.extend_from_slice(&rest[..size]);
        rest = &rest[size + 2..];
    }
    (status, body)
}

#[test]
fn test_large_file_in_parts() {
    let d = Dir::new("large");
    let big: Vec<u8> = (0..(3 << 20) + 17).map(|i| (i % 251) as u8).collect();
    std::fs::write(d.0.join("big.bin"), &big).unwrap();
    let (status, body) = raw(app(&d), "GET /static/big.bin HTTP/1.1\r\nHost: a");
    assert_eq!((status, body.len()), (200, big.len()));
    assert!(body == big);
    let (status, body) = raw(
        app(&d),
        "GET /static/big.bin HTTP/1.1\r\nHost: a\r\nRange: bytes=1048570-3145740",
    );
    assert_eq!(status, 206);
    assert!(body == big[1_048_570..=3_145_740]);
}

#[test]
fn test_health_and_ready() {
    let up = Arc::new(AtomicBool::new(false));
    let flag = Arc::clone(&up);
    let app = App::builder()
        .health("/healthz")
        .ready("/readyz", move || flag.load(Ordering::Relaxed))
        .build()
        .unwrap();
    let r = send(&app, "GET /healthz HTTP/1.1\r\nHost: a");
    assert_eq!(
        (r.status, r.text(), h(&r, "cache-control")),
        (200, "ok", "no-store")
    );
    assert_eq!(send(&app, "GET /readyz HTTP/1.1\r\nHost: a").status, 503);
    up.store(true, Ordering::Relaxed);
    assert_eq!(
        send(&app, "GET /readyz HTTP/1.1\r\nHost: a").text(),
        "ready"
    );
}

#[test]
fn test_security_headers() {
    let app = App::builder()
        .host("example.com")
        .route(
            get("/", |cx| {
                cx.text(200, "x");
                Ok(())
            })
            .public(),
        )
        .route(
            get("/framed", |cx| {
                cx.set_header("x-frame-options", "SAMEORIGIN");
                cx.text(200, "x");
                Ok(())
            })
            .public(),
        )
        .build()
        .unwrap();
    let r = send(&app, "GET / HTTP/1.1\r\nHost: example.com");
    assert_eq!(h(&r, "x-frame-options"), "DENY");
    assert_eq!(h(&r, "content-security-policy"), web_net::DEFAULT_CSP);
    assert_eq!(h(&r, "referrer-policy"), "strict-origin-when-cross-origin");
    assert_eq!(h(&r, "x-content-type-options"), "nosniff");
    assert_eq!(
        h(&r, "strict-transport-security"),
        "",
        "no HSTS over plain HTTP"
    );
    let r = TestRequest::new(b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n")
        .scheme(Scheme::Https)
        .send(&app);
    assert_eq!(h(&r, "strict-transport-security"), "max-age=31536000");
    let r = send(&app, "GET /framed HTTP/1.1\r\nHost: example.com");
    assert_eq!(h(&r, "x-frame-options"), "SAMEORIGIN");
    // Error pages carry them too.
    let r = send(&app, "GET /nope HTTP/1.1\r\nHost: example.com");
    assert_eq!(h(&r, "x-frame-options"), "DENY");

    let bare = App::builder()
        .security_headers(SecurityHeaders::none())
        .route(
            get("/", |cx| {
                cx.text(200, "x");
                Ok(())
            })
            .public(),
        )
        .build()
        .unwrap();
    let r = send(&bare, "GET / HTTP/1.1\r\nHost: a");
    assert_eq!(
        (h(&r, "x-frame-options"), h(&r, "x-content-type-options")),
        ("", "nosniff")
    );
}

#[test]
fn test_no_hsts_for_development_hosts() {
    let app = App::builder()
        .host("example.com")
        .development()
        .route(
            get("/", |cx| {
                cx.text(200, "x");
                Ok(())
            })
            .public(),
        )
        .build()
        .unwrap();
    let r = TestRequest::new(b"GET / HTTP/1.1\r\nHost: localhost:8443\r\n\r\n")
        .scheme(Scheme::Https)
        .send(&app);
    assert_eq!(h(&r, "strict-transport-security"), "");
}

#[test]
fn test_build_checks_for_assets() {
    let e = App::builder()
        .assets(Assets::new("/static", "/definitely/not/here/web-net"))
        .assets(Assets::new("static", "."))
        .route(
            get("/static/x", |cx| {
                cx.text(200, "x");
                Ok(())
            })
            .public(),
        )
        .health("health")
        .build()
        .unwrap_err();
    assert_eq!(e.problems().len(), 4, "{e}");
}