isb-daemon 1.1.1

The isb serve daemon: the tools behind its API, MCP server and web UI. Use the `isb` crate.
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
//! The preview proxy's HTTP: one request to a workspace port, and its
//! answer back, with isb's credentials kept out of it both ways.
//!
//! - **To the workspace** go the method, path, query, body and the app's own
//!   headers and cookies. Never: isb's cookies (`isb_preview`,
//!   `isb_session`), Cloudflare Access' assertion and cookie (which isb would
//!   accept as the user), an isb bearer token, hop-by-hop headers, or
//!   forwarding headers the client made up; isb sets `X-Forwarded-*` itself.
//! - **Back to the browser** goes the app's status, headers and body as
//!   they are, never isb's own headers (its UI's Content-Security-Policy
//!   does not apply to the app), with hop-by-hop headers dropped, cookies
//!   named like isb's dropped, and a cookie's `Domain` attribute removed so
//!   it stays on the preview's own host.
//! - One request per connection, as isb's server works (`Connection:
//!   close`); a websocket upgrade (dev servers' hot reload) is piped both
//!   ways once the app answers 101.

use std::io::{ErrorKind, Read, Write};
use std::net::{Shutdown as NetShutdown, SocketAddr, TcpStream};
use std::time::Duration;

use crate::server::http::{Duplex, Peer, Request, Response};

/// The cookie that carries a preview session.
pub(super) const COOKIE: &str = "isb_preview";
/// Cookies never passed to the app nor accepted from it.
const ISB_COOKIES: &[&str] = &[COOKIE, "isb_session", "CF_Authorization"];

const CONNECT: Duration = Duration::from_secs(5);
/// How long the app may take to start answering (a dev server compiling).
const FIRST_BYTE: Duration = Duration::from_secs(120);
/// How long a streamed body may go quiet.
const IDLE: Duration = Duration::from_secs(300);
const MAX_HEAD: usize = 64 * 1024;

/// Request headers never forwarded: hop-by-hop, framing (isb sets it), and
/// forwarding headers isb writes itself.
fn dropped_request_header(k: &str) -> bool {
    let k = k.to_ascii_lowercase();
    matches!(
        k.as_str(),
        "connection"
            | "keep-alive"
            | "proxy-connection"
            | "proxy-authorization"
            | "te"
            | "trailer"
            | "transfer-encoding"
            | "upgrade"
            | "expect"
            | "content-length"
            | "forwarded"
            | "x-forwarded-for"
            | "x-forwarded-proto"
            | "x-forwarded-host"
    ) || k.starts_with("cf-access-")
        || k.starts_with("x-isb-")
}

/// A `Cookie` header without isb's cookies; `None` when nothing is left.
pub(super) fn clean_cookie(v: &str) -> Option<String> {
    let kept: Vec<&str> = v
        .split(';')
        .map(str::trim)
        .filter(|p| !p.is_empty())
        .filter(|p| {
            let name = p.split('=').next().unwrap_or("").trim();
            !ISB_COOKIES.iter().any(|c| c.eq_ignore_ascii_case(name))
        })
        .collect();
    (!kept.is_empty()).then(|| kept.join("; "))
}

/// Whether the request asks for a websocket.
pub(super) fn wants_websocket(req: &Request) -> bool {
    let has = |h: &str, t: &str| {
        req.header(h)
            .is_some_and(|v| v.split(',').any(|x| x.trim().eq_ignore_ascii_case(t)))
    };
    req.method == "GET" && has("upgrade", "websocket") && has("connection", "upgrade")
}

/// The request as the workspace receives it.
pub(super) fn request_head(req: &Request) -> String {
    let ws = wants_websocket(req);
    let target = match &req.query {
        Some(q) => format!("{}?{q}", req.path),
        None => req.path.clone(),
    };
    let mut h = format!("{} {target} HTTP/1.1\r\n", req.method);
    for (k, v) in &req.headers {
        if dropped_request_header(k) || k.contains(['\r', '\n']) || v.contains(['\r', '\n']) {
            continue;
        }
        let v = if k.eq_ignore_ascii_case("cookie") {
            match clean_cookie(v) {
                Some(c) => c,
                None => continue,
            }
        } else if k.eq_ignore_ascii_case("authorization")
            && v.trim().to_ascii_lowercase().starts_with("bearer isb_")
        {
            continue;
        } else {
            v.clone()
        };
        h.push_str(&format!("{k}: {v}\r\n"));
    }
    let client = match &req.peer {
        Peer::Tcp(a) if a.ip().is_loopback() => req
            .header("cf-connecting-ip")
            .filter(|s| s.parse::<std::net::IpAddr>().is_ok())
            .map(String::from)
            .unwrap_or_else(|| a.ip().to_string()),
        Peer::Tcp(a) => a.ip().to_string(),
        Peer::Unix { .. } => "127.0.0.1".into(),
    };
    let proto = match req.header("x-forwarded-proto") {
        Some(p) if p.eq_ignore_ascii_case("https") => "https",
        _ => "http",
    };
    h.push_str(&format!(
        "X-Forwarded-For: {client}\r\nX-Forwarded-Proto: {proto}\r\n"
    ));
    if let Some(host) = req.header("host").filter(|v| !v.contains(['\r', '\n'])) {
        h.push_str(&format!("X-Forwarded-Host: {host}\r\n"));
    }
    if ws {
        h.push_str("Connection: Upgrade\r\nUpgrade: websocket\r\n");
    } else {
        h.push_str("Connection: close\r\n");
        if !req.body.is_empty() || matches!(req.method.as_str(), "POST" | "PUT" | "PATCH") {
            h.push_str(&format!("Content-Length: {}\r\n", req.body.len()));
        }
    }
    h.push_str("\r\n");
    h
}

/// A `Set-Cookie` as the browser gets it: `None` for a cookie named like
/// one of isb's, else without a `Domain` attribute (host-only).
pub(super) fn clean_set_cookie(v: &str) -> Option<String> {
    let mut parts = v.split(';');
    let first = parts.next()?.trim();
    let name = first.split('=').next().unwrap_or("").trim();
    if ISB_COOKIES.iter().any(|c| c.eq_ignore_ascii_case(name)) {
        return None;
    }
    let mut out = first.to_string();
    for p in parts {
        let p = p.trim();
        if p.is_empty() || p.to_ascii_lowercase().starts_with("domain") {
            continue;
        }
        out.push_str("; ");
        out.push_str(p);
    }
    Some(out)
}

/// How the app's body is delimited.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Framing {
    None,
    Length(u64),
    Chunked,
    Close,
}

/// The app's answer head: status, the headers the browser gets, framing.
#[derive(Debug)]
pub(super) struct Head {
    pub status: u16,
    pub headers: Vec<(String, String)>,
    pub framing: Framing,
    /// Bytes after the head that were already read.
    pub rest: Vec<u8>,
}

/// Parse a response head from `buf`; `Ok(None)` while it is incomplete.
pub(super) fn parse_head(buf: &[u8], head_request: bool) -> Result<Option<Head>, String> {
    let mut hs = [httparse::EMPTY_HEADER; 100];
    let mut r = httparse::Response::new(&mut hs);
    let n = match r.parse(buf) {
        Ok(httparse::Status::Complete(n)) => n,
        Ok(httparse::Status::Partial) => return Ok(None),
        Err(e) => return Err(format!("bad response from the app: {e}")),
    };
    let status = r.code.unwrap_or(502);
    let mut headers = Vec::new();
    let (mut chunked, mut length) = (false, None);
    for h in r.headers.iter() {
        let v = String::from_utf8_lossy(h.value).trim().to_string();
        let k = h.name.to_ascii_lowercase();
        match k.as_str() {
            "transfer-encoding" => chunked = v.to_ascii_lowercase().contains("chunked"),
            "content-length" => length = v.parse::<u64>().ok(),
            "connection" | "keep-alive" | "proxy-connection" | "te" | "trailer" | "upgrade" => {}
            "set-cookie" => {
                if let Some(c) = clean_set_cookie(&v) {
                    headers.push((h.name.to_string(), c));
                }
            }
            _ => headers.push((h.name.to_string(), v)),
        }
    }
    let framing = if head_request || (100..200).contains(&status) || matches!(status, 204 | 304) {
        Framing::None
    } else if chunked {
        Framing::Chunked
    } else if let Some(l) = length {
        Framing::Length(l)
    } else {
        Framing::Close
    };
    Ok(Some(Head {
        status,
        headers,
        framing,
        rest: buf[n..].to_vec(),
    }))
}

fn read_head(up: &mut TcpStream, head_request: bool) -> Result<Head, String> {
    let mut buf = Vec::with_capacity(8192);
    let mut chunk = [0u8; 8192];
    loop {
        if let Some(h) = parse_head(&buf, head_request)? {
            // An informational answer (103 Early Hints) precedes the real one.
            if (102..200).contains(&h.status) {
                buf = h.rest;
                continue;
            }
            return Ok(h);
        }
        if buf.len() > MAX_HEAD {
            return Err("the app's response headers are too large".into());
        }
        match up.read(&mut chunk) {
            Ok(0) => return Err("the app closed the connection without answering".into()),
            Ok(n) => buf.extend_from_slice(&chunk[..n]),
            Err(e) if e.kind() == ErrorKind::Interrupted => {}
            Err(e) => return Err(format!("the app did not answer: {e}")),
        }
    }
}

/// A reader over the already-read bytes, then the stream.
struct Prefixed<R> {
    pre: std::io::Cursor<Vec<u8>>,
    inner: R,
}

impl<R: Read> Read for Prefixed<R> {
    fn read(&mut self, b: &mut [u8]) -> std::io::Result<usize> {
        let n = self.pre.read(b)?;
        if n > 0 || b.is_empty() {
            return Ok(n);
        }
        self.inner.read(b)
    }
}

fn read_line<R: Read>(r: &mut R) -> std::io::Result<String> {
    let mut line = Vec::new();
    let mut b = [0u8; 1];
    loop {
        if r.read(&mut b)? == 0 {
            return Err(ErrorKind::UnexpectedEof.into());
        }
        if b[0] == b'\n' {
            break;
        }
        line.push(b[0]);
        if line.len() > 4096 {
            return Err(std::io::Error::new(
                ErrorKind::InvalidData,
                "chunk line too long",
            ));
        }
    }
    Ok(String::from_utf8_lossy(&line).trim().to_string())
}

/// Copy a body as `framing` delimits it, decoding chunks (the browser's
/// copy is delimited by the connection closing).
pub(super) fn copy_body<R: Read>(
    r: &mut R,
    w: &mut dyn Write,
    framing: Framing,
) -> std::io::Result<()> {
    let mut buf = [0u8; 16384];
    match framing {
        Framing::None => {}
        Framing::Length(n) => {
            std::io::copy(&mut r.by_ref().take(n), w)?;
        }
        Framing::Close => loop {
            let n = r.read(&mut buf)?;
            if n == 0 {
                break;
            }
            w.write_all(&buf[..n])?;
            w.flush()?;
        },
        Framing::Chunked => loop {
            let line = read_line(r)?;
            let size = u64::from_str_radix(line.split(';').next().unwrap_or("").trim(), 16)
                .map_err(|_| std::io::Error::new(ErrorKind::InvalidData, "bad chunk size"))?;
            if size == 0 {
                break;
            }
            std::io::copy(&mut r.by_ref().take(size), w)?;
            w.flush()?;
            read_line(r)?;
        },
    }
    w.flush()
}

/// Pipe a websocket both ways until either side closes.
fn pipe(client: &mut dyn Duplex, mut up: TcpStream, early: Vec<u8>) {
    if !early.is_empty()
        && client
            .write_all(&early)
            .and_then(|_| client.flush())
            .is_err()
    {
        return;
    }
    let tick = Some(Duration::from_millis(20));
    let _ = client.set_read_timeout(tick);
    let _ = up.set_read_timeout(tick);
    let quiet =
        |e: &std::io::Error| matches!(e.kind(), ErrorKind::WouldBlock | ErrorKind::TimedOut);
    let mut buf = [0u8; 16384];
    loop {
        match client.read(&mut buf) {
            Ok(0) => break,
            Ok(n) if up.write_all(&buf[..n]).is_err() => break,
            Ok(_) => {}
            Err(e) if quiet(&e) || e.kind() == ErrorKind::Interrupted => {}
            Err(_) => break,
        }
        match up.read(&mut buf) {
            Ok(0) => break,
            Ok(n)
                if client
                    .write_all(&buf[..n])
                    .and_then(|_| client.flush())
                    .is_err() =>
            {
                break;
            }
            Ok(_) => {}
            Err(e) if quiet(&e) || e.kind() == ErrorKind::Interrupted => {}
            Err(_) => break,
        }
    }
    let _ = up.shutdown(NetShutdown::Both);
}

/// Forward `req` to `upstream` and answer with what the app says.
pub(super) fn forward(req: &Request, upstream: SocketAddr) -> Result<Response, String> {
    let mut up = TcpStream::connect_timeout(&upstream, CONNECT)
        .map_err(|e| format!("nothing answers on {upstream}: {e}"))?;
    let _ = up.set_nodelay(true);
    let _ = up.set_write_timeout(Some(Duration::from_secs(30)));
    let _ = up.set_read_timeout(Some(FIRST_BYTE));
    up.write_all(request_head(req).as_bytes())
        .and_then(|_| up.write_all(&req.body))
        .map_err(|e| format!("sending to {upstream}: {e}"))?;
    let head = read_head(&mut up, req.method == "HEAD")?;
    let _ = up.set_read_timeout(Some(IDLE));
    if head.status == 101 && wants_websocket(req) {
        let mut r = Response::upgrade(
            "websocket",
            Box::new(move |client: &mut dyn Duplex| pipe(client, up, head.rest)),
        );
        for (k, v) in head.headers {
            if k.to_ascii_lowercase().starts_with("sec-websocket-") {
                r = r.header(k, v);
            }
        }
        return Ok(r);
    }
    let (framing, rest) = (head.framing, head.rest);
    let mut r = Response::stream(
        head.status,
        "",
        Box::new(move |w: &mut dyn Write| {
            let mut body = Prefixed {
                pre: std::io::Cursor::new(rest),
                inner: up,
            };
            copy_body(&mut body, w, framing)
        }),
    );
    r.headers = head.headers;
    Ok(r)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn req(headers: &[(&str, &str)]) -> Request {
        Request {
            method: "GET".into(),
            path: "/app".into(),
            query: Some("x=1".into()),
            headers: headers
                .iter()
                .map(|(k, v)| (k.to_string(), v.to_string()))
                .collect(),
            body: vec![],
            peer: Peer::Tcp("127.0.0.1:5000".parse().unwrap()),
        }
    }

    #[test]
    fn isb_credentials_never_reach_the_app() {
        let r = req(&[
            ("Host", "3000-workspace-acme.localhost:8192"),
            (
                "Cookie",
                "isb_preview=s3cret; theme=dark; isb_session=sess; CF_Authorization=jwt",
            ),
            ("Cf-Access-Jwt-Assertion", "eyJ.jwt"),
            ("Authorization", "Bearer isb_tok_abc"),
            ("X-Isb-Csrf", "1"),
            ("X-Forwarded-For", "6.6.6.6"),
            ("Connection", "keep-alive"),
            ("Accept", "text/html"),
        ]);
        let h = request_head(&r);
        assert!(h.starts_with("GET /app?x=1 HTTP/1.1\r\n"), "{h}");
        assert!(h.contains("Cookie: theme=dark\r\n"), "{h}");
        for leak in [
            "s3cret",
            "sess",
            "jwt",
            "isb_tok",
            "X-Isb",
            "6.6.6.6",
            "keep-alive",
        ] {
            assert!(!h.contains(leak), "{leak} leaked: {h}");
        }
        assert!(h.contains("Accept: text/html\r\n"));
        assert!(h.contains("X-Forwarded-For: 127.0.0.1\r\n"));
        assert!(h.contains("X-Forwarded-Host: 3000-workspace-acme.localhost:8192\r\n"));
        assert!(h.contains("Connection: close\r\n"));
        // The app's own credentials pass.
        let own = request_head(&req(&[("Authorization", "Basic dTpw")]));
        assert!(own.contains("Authorization: Basic dTpw\r\n"));
        // Only isb's cookies: no Cookie header at all.
        let only = request_head(&req(&[("Cookie", "isb_preview=a")]));
        assert!(!only.to_ascii_lowercase().contains("cookie"));
    }

    #[test]
    fn websocket_upgrades_are_forwarded_as_upgrades() {
        let r = req(&[
            ("Upgrade", "websocket"),
            ("Connection", "keep-alive, Upgrade"),
            ("Sec-WebSocket-Key", "k"),
            ("Sec-WebSocket-Protocol", "vite-hmr"),
        ]);
        assert!(wants_websocket(&r));
        let h = request_head(&r);
        assert!(
            h.contains("Connection: Upgrade\r\nUpgrade: websocket\r\n"),
            "{h}"
        );
        assert!(h.contains("Sec-WebSocket-Protocol: vite-hmr\r\n"));
        assert!(!wants_websocket(&req(&[("Upgrade", "websocket")])));
    }

    #[test]
    fn the_app_cannot_set_isb_cookies_or_cookies_for_other_hosts() {
        assert_eq!(clean_set_cookie("isb_preview=x; Path=/"), None);
        assert_eq!(clean_set_cookie("ISB_SESSION=x"), None);
        assert_eq!(
            clean_set_cookie("sid=1; Domain=localhost; Path=/; HttpOnly").as_deref(),
            Some("sid=1; Path=/; HttpOnly")
        );
        let head = b"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nSet-Cookie: isb_session=evil\r\nSet-Cookie: a=b; domain=.example.com\r\nConnection: keep-alive\r\nContent-Security-Policy: default-src *\r\nContent-Length: 2\r\n\r\nhi";
        let h = parse_head(head, false).unwrap().unwrap();
        assert_eq!(h.status, 200);
        assert_eq!(h.framing, Framing::Length(2));
        assert_eq!(h.rest, b"hi");
        let names: Vec<String> = h.headers.iter().map(|(k, v)| format!("{k}: {v}")).collect();
        assert_eq!(
            names,
            vec![
                "Content-Type: text/html",
                "Set-Cookie: a=b",
                // The app's own policy, not isb's UI's.
                "Content-Security-Policy: default-src *",
            ]
        );
        assert!(parse_head(b"HTTP/1.1 200 OK\r\n", false).unwrap().is_none());
        let h = parse_head(b"HTTP/1.1 304 Not Modified\r\n\r\n", false)
            .unwrap()
            .unwrap();
        assert_eq!(h.framing, Framing::None);
        let h = parse_head(b"HTTP/1.1 200 OK\r\nContent-Length: 9\r\n\r\n", true)
            .unwrap()
            .unwrap();
        assert_eq!(h.framing, Framing::None);
    }

    #[test]
    fn chunked_bodies_are_decoded() {
        let mut r: &[u8] = b"5\r\nhello\r\n6;ext=1\r\n world\r\n0\r\n\r\n";
        let mut out = Vec::new();
        copy_body(&mut r, &mut out, Framing::Chunked).unwrap();
        assert_eq!(out, b"hello world");
        let mut r: &[u8] = b"abcdef";
        let mut out = Vec::new();
        copy_body(&mut r, &mut out, Framing::Length(3)).unwrap();
        assert_eq!(out, b"abc");
        let mut bad: &[u8] = b"zz\r\n";
        assert!(copy_body(&mut bad, &mut Vec::new(), Framing::Chunked).is_err());
    }

    #[test]
    fn a_request_goes_through_and_its_answer_comes_back() {
        let l = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = l.local_addr().unwrap();
        let app = std::thread::spawn(move || {
            let (mut s, _) = l.accept().unwrap();
            let mut got = Vec::new();
            let mut b = [0u8; 4096];
            while !got.windows(4).any(|w| w == b"\r\n\r\n") {
                let n = s.read(&mut b).unwrap();
                got.extend_from_slice(&b[..n]);
            }
            s.write_all(b"HTTP/1.1 201 Created\r\nTransfer-Encoding: chunked\r\nX-App: 1\r\n\r\n3\r\nabc\r\n0\r\n\r\n")
                .unwrap();
            String::from_utf8(got).unwrap()
        });
        let r = forward(&req(&[("Cookie", "isb_preview=x; a=1")]), addr).unwrap();
        assert_eq!(r.status, 201);
        assert_eq!(r.get_header("x-app"), Some("1"));
        assert!(r.get_header("transfer-encoding").is_none());
        let f = r.stream.unwrap().lock().unwrap().take().unwrap();
        let mut body = Vec::new();
        f(&mut body).unwrap();
        assert_eq!(body, b"abc");
        let seen = app.join().unwrap();
        assert!(
            seen.contains("Cookie: a=1\r\n") && !seen.contains("isb_preview"),
            "{seen}"
        );
        // Nothing listening: said so, not hung.
        let free = std::net::TcpListener::bind("127.0.0.1:0")
            .unwrap()
            .local_addr()
            .unwrap();
        assert!(forward(&req(&[]), free).is_err());
    }
}