devsandbox 0.10.0

Manage devcontainer-style sandboxes on docker, podman, or Apple container — many throwaway instances per repo from one TOML config.
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
//! Opening a container's URL on this host (docs/architecture/helper/bridges.md,
//! "Opening URLs (`kind open`)"): the policy an `open` record passes before
//! the bridge's sink hands it to the opener, and the system opener itself.
//! The policy is pure (clocks and the limiter passed in), and so are the
//! two halves' choices: on the instance's host, the profile name an open
//! carries ([`profile_to_send`], [`local_job`]); on the opening host, which
//! may be another fleet member (docs/architecture/daemon/fleet.md, *Opening
//! URLs*), the command that name maps to in its own `profiles.toml`
//! ([`open_argv`]). Only [`prepare`] (reads `state.toml`) and [`open_here`]
//! (the display, `profiles.toml`, the spawn) touch the environment.

use std::collections::BTreeMap;
use std::ffi::OsString;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use std::sync::Arc;
use std::time::Duration;

use anyhow::{Context, Result, bail};
use serde::Deserialize;

use crate::state::{Instance, State};

/// An `open` record queued longer ago than this is dropped at delivery: the
/// outbox keeps records while no host is attached, and an OAuth URL opening
/// an hour later is worse than none. Measured from the record's `at`.
pub const OPEN_TTL: Duration = Duration::from_secs(30);

/// URLs one instance may open back to back before [`OPEN_RATE_REFILL`]
/// applies, so a looping script can't flood the desktop with tabs.
pub const OPEN_RATE_BURST: u32 = 5;

/// One more open per instance is allowed every this long.
pub const OPEN_RATE_REFILL: Duration = Duration::from_secs(10);

/// How long after its attach to an instance on another host ended a member
/// still takes that instance's opens (`serve::attaches`): a user who just
/// closed the terminal may still be finishing the login it started.
pub const OPEN_ATTACH_WINDOW: Duration = Duration::from_secs(10 * 60);

/// What happens to one `open` record.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Verdict {
    Open,
    /// A control character or whitespace somewhere in the URL: a real one
    /// percent-encodes them, and a raw one could forge a log or status line.
    BadChars,
    /// Not `http`/`https`; the scheme as given (empty when there is none).
    BadScheme(String),
    /// Queued `age` seconds ago, past [`OPEN_TTL`].
    Expired { age: u64 },
    /// The instance's rate limit is spent.
    Limited,
}

impl Verdict {
    /// Why a record isn't opened (the [`Outcome::Refused`] text); `None`
    /// for [`Verdict::Open`].
    pub fn refusal(&self) -> Option<String> {
        Some(match self {
            Verdict::Open => return None,
            Verdict::BadChars => "control characters or whitespace in the URL".to_string(),
            Verdict::BadScheme(s) if s.is_empty() => "not a URL with a scheme".to_string(),
            Verdict::BadScheme(s) => format!("scheme `{s}` (http and https only)"),
            Verdict::Expired { age } => format!("queued {age}s ago (over {}s)", OPEN_TTL.as_secs()),
            Verdict::Limited => format!("rate limit ({OPEN_RATE_BURST} per {}s)", OPEN_RATE_REFILL.as_secs()),
        })
    }

    /// The `serve.log` line for a refusal or drop, naming `instance` and
    /// `url` ([`printable`]: both may come from a container or a peer);
    /// `None` for [`Verdict::Open`].
    pub fn log_line(&self, instance: &str, url: &str) -> Option<String> {
        let why = self.refusal()?;
        let verb = if matches!(self, Verdict::Expired { .. }) { "dropped" } else { "refused" };
        Some(format!("{}: open {verb}, {why}: {}", printable(instance), printable(url)))
    }
}

/// Whether `c` may not appear raw in an opened URL ([`Verdict::BadChars`]).
fn bad_char(c: char) -> bool {
    c.is_control() || c.is_whitespace()
}

/// Whether `url` holds a control character or whitespace
/// ([`Verdict::BadChars`]).
pub fn has_bad_chars(url: &str) -> bool {
    url.chars().any(bad_char)
}

/// `s` safe for one log or status line: control characters and whitespace
/// other than a space escaped (`\n`, `\u{1b}`), the rest as is. For text a
/// container or a fleet peer chose (a refused URL, a member's error).
pub fn printable(s: &str) -> String {
    let mut out = String::with_capacity(s.len());
    for c in s.chars() {
        if bad_char(c) && c != ' ' {
            out.extend(c.escape_debug());
        } else {
            out.push(c);
        }
    }
    out
}

/// What became of one open, for the user (`opens.done`,
/// docs/reference/api.md): each open ends in exactly one, reported by the
/// host that knows: the instance's host for a refusal at intake or a failed
/// delivery, the opening host for the rest ([`open_here`]).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Outcome {
    /// The opener was started. `unforwarded`: callback ports the URL named
    /// that couldn't be forwarded (taken, say); the URL opened anyway (its
    /// page isn't on them), so this is still one `opened` line, naming them.
    Opened { unforwarded: Vec<Loopback> },
    /// The policy said no ([`Verdict::refusal`]).
    Refused(String),
    /// Nothing could open it: no display, no opener, a failed spawn, a
    /// delivery the member didn't take.
    Failed(String),
}

/// One open's [`Outcome`], with what it was about: `instance` as the line
/// names it (`machine/name` for an instance on another member), `url` the one
/// opened (rewritten to a forward's port) or, when nothing opened, the one
/// given, `profile` the name it carried.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OpenDone {
    pub instance: String,
    pub url: String,
    pub profile: Option<String>,
    pub outcome: Outcome,
}

/// The longest URL a [`OpenDone::line`] shows whole: a status line is one
/// row, and an OAuth URL easily runs to a kilobyte.
const LINE_URL: usize = 120;

/// The longest reason a [`OpenDone::line`] shows whole (a member's refusal
/// may quote the URL).
const LINE_WHY: usize = 160;

/// `s` cut to `max` chars, ending in `…` when cut.
fn shorten(s: &str, max: usize) -> String {
    match s.char_indices().nth(max.saturating_sub(1)) {
        Some((at, _)) if s.chars().count() > max => format!("{}…", &s[..at]),
        _ => s.to_string(),
    }
}

impl OpenDone {
    /// The wire's `outcome`: `opened`, `refused` or `failed`.
    pub fn kind(&self) -> &'static str {
        match self.outcome {
            Outcome::Opened { .. } => "opened",
            Outcome::Refused(_) => "refused",
            Outcome::Failed(_) => "failed",
        }
    }

    /// The wire's `why`: the refusal or failure, or for an open the ports
    /// it couldn't forward (`None` when there are none).
    pub fn why(&self) -> Option<String> {
        match &self.outcome {
            Outcome::Opened { unforwarded } if unforwarded.is_empty() => None,
            Outcome::Opened { unforwarded } => {
                let ports: Vec<String> = unforwarded
                    .iter()
                    .map(|l| format!("{}port {}", if l.callback { "callback " } else { "" }, l.port))
                    .collect();
                Some(format!("cannot forward {}", ports.join(", ")))
            }
            Outcome::Refused(why) | Outcome::Failed(why) => Some(why.clone()),
        }
    }

    /// The status line: `web: opened https://… (work)`, with `; cannot
    /// forward callback port 8085` when a port didn't forward; `web: open
    /// refused: <why>`; `web: open failed: <why>`. The URL and the reason are
    /// [`shorten`]ed, and everything is [`printable`]: a refused URL or a
    /// member's reason may carry anything.
    pub fn line(&self) -> String {
        let why = self.why().map(|w| printable(&shorten(&w, LINE_WHY)));
        let instance = printable(&self.instance);
        match (&self.outcome, why) {
            (Outcome::Opened { .. }, why) => {
                let profile = self.profile.as_ref().map_or(String::new(), |p| format!(" ({})", printable(p)));
                let note = why.map_or(String::new(), |w| format!("; {w}"));
                format!("{instance}: opened {}{profile}{note}", printable(&shorten(&self.url, LINE_URL)))
            }
            (Outcome::Refused(_), why) => format!("{instance}: open refused: {}", why.unwrap_or_default()),
            (Outcome::Failed(_), why) => format!("{instance}: open failed: {}", why.unwrap_or_default()),
        }
    }
}

/// Gets every [`OpenDone`] this host reports. The daemon's pushes
/// `opens.done` (`serve::opens`); a seam, so this module stays free of
/// `serve` (unix-only) and tests record them.
pub type OnDone = Arc<dyn Fn(OpenDone) + Send + Sync>;

/// Decide whether `url`, queued at `at` (unix seconds), opens at `now` (unix
/// seconds). Checked in order: its characters (no control character or
/// whitespace, [`Verdict::BadChars`]), the scheme (`http`/`https`, any case,
/// followed by `://`), then [`OPEN_TTL`], then `limit`, which is only asked
/// (and only spends a token) once the others pass. A record from the future
/// (clock skew between container and host) counts as fresh.
pub fn decide(url: &str, at: u64, now: u64, limit: impl FnOnce() -> bool) -> Verdict {
    if has_bad_chars(url) {
        return Verdict::BadChars;
    }
    let scheme = match url.split_once(':') {
        Some((s, _)) if !s.is_empty() && s.chars().all(|c| c.is_ascii_alphanumeric() || "+-.".contains(c)) => s,
        _ => "",
    };
    let web = scheme.eq_ignore_ascii_case("http") || scheme.eq_ignore_ascii_case("https");
    if !web || !url[scheme.len()..].starts_with("://") {
        return Verdict::BadScheme(scheme.to_string());
    }
    let age = now.saturating_sub(at);
    if age > OPEN_TTL.as_secs() {
        return Verdict::Expired { age };
    }
    if !limit() {
        return Verdict::Limited;
    }
    Verdict::Open
}

/// The system opener's argv for `url` on `os` (`std::env::consts::OS`
/// values), `None` where there is no known one. `url` is the last entry;
/// [`decide`] already held it to `http(s)://`, so it can't read as an option.
pub fn opener_argv(os: &str, url: &str) -> Option<Vec<String>> {
    match os {
        "linux" => Some(vec!["xdg-open".into(), url.into()]),
        "macos" => Some(vec!["open".into(), url.into()]),
        _ => None,
    }
}

/// What one profile does on this host: a `[<profile>]` table of
/// `profiles.toml` (docs/architecture/instances/profiles.md, *On the opening
/// host*).
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HostProfile {
    /// The command that opens a URL in this profile (a browser and its
    /// profile flag); the URL is appended as the last argument.
    pub open: Vec<String>,
}

/// `profiles.toml`, by profile name. Commands only ever come from this
/// host's own file: only a profile's *name* comes from `state.toml` (and,
/// later, over a fleet link), so no config or machine can make this host run
/// a command.
pub type HostProfiles = BTreeMap<String, HostProfile>;

/// `profiles.toml`, next to `state.toml`.
pub fn profiles_path() -> Result<PathBuf> {
    Ok(State::path()?.with_file_name("profiles.toml"))
}

/// Parse `profiles.toml`'s contents: `[<profile>]` tables with a non-empty
/// `open` array of strings, nothing else. An empty file is no profiles.
pub fn parse_profiles(contents: &str) -> Result<HostProfiles> {
    let profiles: HostProfiles = toml::from_str(contents)?;
    if let Some(name) = profiles.iter().find(|(_, p)| p.open.is_empty()).map(|(n, _)| n) {
        bail!("[{name}]: `open` is empty (a command and its arguments)");
    }
    Ok(profiles)
}

/// The profiles in `path`; a missing file is none.
pub fn load_profiles(path: &Path) -> Result<HostProfiles> {
    match std::fs::read_to_string(path) {
        Ok(contents) => parse_profiles(&contents).with_context(|| format!("invalid {}", path.display())),
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(HostProfiles::new()),
        Err(e) => Err(e).with_context(|| format!("cannot read {}", path.display())),
    }
}

/// The argv that opens `url` in `profile` on `os`: the profile's `open`
/// command from `profiles` with `url` appended when it maps one, else the
/// system opener ([`opener_argv`]); also for no profile.
pub fn open_argv(os: &str, profile: Option<&str>, profiles: &HostProfiles, url: &str) -> Option<Vec<String>> {
    match profile.and_then(|p| profiles.get(p)) {
        Some(p) => Some(p.open.iter().cloned().chain([url.to_string()]).collect()),
        None => opener_argv(os, url),
    }
}

/// The profile an open of `url` for `instance` (its `state.toml` entry
/// `inst`, found by id; `None` when it's gone) carries to the opening host,
/// decided on the instance's host: the instance's profile, unless
/// `snapshot::profile_disallowed` says its sandbox no longer allows that one
/// (`allowed` is asked only then, it loads the config), in which case none
/// and a `serve.log` warning (config drift is flagged, never followed). No
/// instance or no profile: none. Only this name ever leaves the host
/// (`open.deliver`); the opening host maps it ([`open_argv`]).
pub fn profile_to_send(
    instance: &str,
    url: &str,
    inst: Option<&Instance>,
    allowed: impl FnOnce(&Instance) -> Option<Vec<String>>,
) -> (Option<String>, Option<String>) {
    let Some((inst, profile)) = inst.and_then(|i| Some((i, i.profile.as_deref()?))) else {
        return (None, None);
    };
    if crate::snapshot::profile_disallowed(Some(profile), allowed(inst).as_deref()) {
        let warning =
            format!("{instance}: profile `{profile}` no longer allowed by the config; opening {url} with the default opener");
        return (None, Some(warning));
    }
    (Some(profile.to_string()), None)
}

/// Where an open's loopback forwards go: an instance on this host
/// (`machine` `None`: `dir` its `config_dir`, `instance` its state key) or
/// on fleet member `machine` (an open delivered from there, `open.deliver`:
/// `dir` and `instance` as it named them).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ForwardTarget {
    pub machine: Option<String>,
    pub dir: PathBuf,
    pub instance: String,
}

impl ForwardTarget {
    /// The target of a local instance (its state key and entry), or why it
    /// has none.
    pub fn local(inst: Option<(&str, &Instance)>) -> Result<ForwardTarget, String> {
        let (key, i) = inst.ok_or_else(|| "not in state.toml".to_string())?;
        let dir = i.config_dir.clone().ok_or_else(|| "no config root in state.toml".to_string())?;
        Ok(ForwardTarget { machine: None, dir, instance: key.to_string() })
    }
}

/// One URL for the opening host to open ([`open_here`]): `instance` its
/// display name (for log lines), `profile` the name [`profile_to_send`]
/// chose, `target` where its loopback forwards go (or why there is none).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OpenJob {
    pub instance: String,
    pub url: String,
    pub profile: Option<String>,
    pub target: Result<ForwardTarget, String>,
}

/// The job for an open of `url` from local `instance`, whose `state.toml`
/// entry (key and record) is `found`: [`profile_to_send`] (its warning
/// second) and the [`ForwardTarget::local`].
pub fn local_job(
    instance: &str,
    url: &str,
    found: Option<(&str, &Instance)>,
    allowed: impl FnOnce(&Instance) -> Option<Vec<String>>,
) -> (OpenJob, Option<String>) {
    let (profile, warning) = profile_to_send(instance, url, found.map(|(_, i)| i), allowed);
    let job = OpenJob { instance: instance.to_string(), url: url.to_string(), profile, target: ForwardTarget::local(found) };
    (job, warning)
}

/// A loopback port a URL needs reachable on this host: a container's
/// `localhost` is its own, so the opener forwards it first
/// (docs/architecture/helper/bridges.md, *Loopback forwards*).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Loopback {
    pub port: u16,
    /// An OAuth callback (`redirect_uri` and the like): the provider
    /// redirects to exactly this port, so it can't be moved.
    pub callback: bool,
}

/// The query keys whose value may be a loopback callback URL.
const CALLBACK_KEYS: [&str; 3] = ["redirect_uri", "redirect_url", "callback"];

/// A `scheme://[userinfo@]host[:port]...` URL cut into byte ranges, as a
/// browser cuts an http(s) one: the authority ends at `/`, `\`, `?` or `#`
/// (WHATWG reads `\` as `/` in an http(s) URL, so `http://a.com\@localhost/`
/// is `a.com`'s).
struct Parts<'a> {
    scheme: &'a str,
    /// Where the host starts (after any userinfo).
    host_at: usize,
    host: &'a str,
    /// The text after `:`, when there is one (may be empty or garbage).
    port: Option<&'a str>,
    /// Where the authority ends (the path, query or fragment, if any).
    rest_at: usize,
}

fn parts(url: &str) -> Option<Parts<'_>> {
    let (scheme, after) = url.split_once("://")?;
    let start = scheme.len() + 3;
    let authority = &after[..after.find(['/', '\\', '?', '#']).unwrap_or(after.len())];
    let host_at = start + authority.rfind('@').map_or(0, |i| i + 1);
    let host_port = &url[host_at..start + authority.len()];
    let (host, port) = match host_port.strip_prefix('[') {
        Some(v6) => {
            let end = v6.find(']')? + 2;
            let port = &host_port[end..];
            let port = if port.is_empty() { None } else { Some(port.strip_prefix(':')?) };
            (&host_port[..end], port)
        }
        None => match host_port.split_once(':') {
            Some((h, p)) => (h, Some(p)),
            None => (host_port, None),
        },
    };
    Some(Parts { scheme, host_at, host, port, rest_at: start + authority.len() })
}

/// A host's dotted-quad IPv4 address, a trailing dot allowed (`127.0.0.1.`).
/// Only the four-decimal form: shorthand and hex/octal forms aren't parsed
/// (`127.1`, `0x7f.0.0.1`), so they never count as loopback.
fn ipv4(host: &str) -> Option<Ipv4Addr> {
    host.strip_suffix('.').unwrap_or(host).parse().ok()
}

/// Whether `host` (as [`parts`] cuts it) is the container's own loopback:
/// `localhost` (any case), any dotted quad in `127.0.0.0/8`, `0.0.0.0`
/// (each with or without a trailing dot), or a bracketed IPv6 loopback
/// (`[::1]`, also spelled out). Zone ids and percent-encoded hosts aren't
/// parsed: not loopback.
fn is_loopback(host: &str) -> bool {
    if host.strip_suffix('.').unwrap_or(host).eq_ignore_ascii_case("localhost") {
        return true;
    }
    if let Some(ip) = ipv4(host) {
        return ip.is_loopback() || ip.is_unspecified();
    }
    let v6 = host.strip_prefix('[').and_then(|h| h.strip_suffix(']'));
    v6.and_then(|h| h.parse::<Ipv6Addr>().ok()).is_some_and(|ip| ip.is_loopback())
}

/// The host a loopback host is opened as here, when it isn't kept: the
/// forward listens on 127.0.0.1, so another `127.0.0.0/8` address or IPv6's
/// loopback would miss it, and reach whatever the host itself runs there
/// (127.0.0.1); Chrome refuses to navigate to 0.0.0.0, and a server
/// listening on it takes localhost too (`localhost`).
fn host_here(host: &str) -> Option<&'static str> {
    let v6 = host.strip_prefix('[').and_then(|h| h.strip_suffix(']'));
    if v6.and_then(|h| h.parse::<Ipv6Addr>().ok()).is_some_and(|ip| ip.is_loopback()) {
        return Some("127.0.0.1");
    }
    let ip = ipv4(host)?;
    if ip.is_unspecified() {
        Some("localhost")
    } else if ip.is_loopback() && ip != Ipv4Addr::LOCALHOST {
        Some("127.0.0.1")
    } else {
        None
    }
}

/// The port of an http(s) URL on a loopback host: given (ASCII digits
/// only), else the scheme's default. `None`: not http(s), not loopback, or
/// a port that isn't 1-65535.
fn loopback_port(url: &str) -> Option<u16> {
    let p = parts(url)?;
    if !is_loopback(p.host) {
        return None;
    }
    let default = match p.scheme.to_ascii_lowercase().as_str() {
        "http" => 80,
        "https" => 443,
        _ => return None,
    };
    match p.port {
        None | Some("") => Some(default),
        Some(port) if port.bytes().all(|b| b.is_ascii_digit()) => port.parse().ok().filter(|&p| p != 0),
        Some(_) => None,
    }
}

/// `%XX` decoded (a malformed escape stays as it is).
fn percent_decode(s: &str) -> String {
    let b = s.as_bytes();
    let mut out = Vec::with_capacity(b.len());
    let mut i = 0;
    while i < b.len() {
        let hex = || std::str::from_utf8(b.get(i + 1..i + 3)?).ok().and_then(|h| u8::from_str_radix(h, 16).ok());
        match (b[i], hex()) {
            (b'%', Some(byte)) => {
                out.push(byte);
                i += 3;
            }
            (c, _) => {
                out.push(c);
                i += 1;
            }
        }
    }
    String::from_utf8_lossy(&out).into_owned()
}

/// The loopback ports `url` needs forwarded: its own port when its host is
/// loopback ([`is_loopback`]), and the port
/// of each loopback http(s) URL in its query under [`CALLBACK_KEYS`]
/// (percent-decoded). Only a key's first value counts, so repeating a key
/// can't make the host bind many ports; nested URLs aren't followed. One
/// entry per port, the direct one first; a port that is both is a callback
/// (it needs that exact port).
pub fn loopback_ports(url: &str) -> Vec<Loopback> {
    let mut out: Vec<Loopback> = loopback_port(url).map(|port| Loopback { port, callback: false }).into_iter().collect();
    let query = parts(url).map_or("", |p| {
        let rest = &url[p.rest_at..];
        let rest = &rest[..rest.find('#').unwrap_or(rest.len())];
        rest.split_once('?').map_or("", |(_, q)| q)
    });
    let mut seen = Vec::new();
    for (key, value) in query.split('&').filter_map(|kv| kv.split_once('=')) {
        let key = percent_decode(key);
        if !CALLBACK_KEYS.contains(&key.as_str()) || seen.contains(&key) {
            continue;
        }
        seen.push(key);
        let Some(port) = loopback_port(&percent_decode(value)) else { continue };
        match out.iter_mut().find(|l| l.port == port) {
            Some(l) => l.callback = true,
            None => out.push(Loopback { port, callback: true }),
        }
    }
    out
}

/// `url` with its loopback host and port made right for this host: the port
/// set to `port` (added, or replaced; userinfo, path, query and fragment
/// kept), and the host [`host_here`] says, also with `port` `None`. A URL
/// that doesn't parse is returned as is.
pub fn rewrite_direct(url: &str, port: Option<u16>) -> String {
    let Some(p) = parts(url) else { return url.to_string() };
    let host = host_here(p.host).unwrap_or(p.host);
    let port = match (port, p.port) {
        (Some(n), _) => format!(":{n}"),
        (None, Some(text)) => format!(":{text}"),
        (None, None) => String::new(),
    };
    format!("{}{host}{port}{}", &url[..p.host_at], &url[p.rest_at..])
}

/// Forward one [`Loopback`] port of the instance at a [`ForwardTarget`] →
/// the bound host port. `Err(Some(why))` for the opener's log line,
/// `Err(None)` when the forwarder already logged why (an OAuth callback
/// port it couldn't bind). The daemon's is the forwards registry's open
/// forwards (`serve::opens`); a seam, so this module stays free of `serve`
/// (unix-only) and tests fake it.
pub type Forwarder = Arc<dyn Fn(&ForwardTarget, Loopback) -> Result<u16, Option<String>> + Send + Sync>;

/// The forwarding step before an open: each of `url`'s [`loopback_ports`]
/// forwarded to `target` through `forward`, and the URL to open: rewritten
/// to the direct port's bound host port when that differs, and to the host
/// [`host_here`] says ([`rewrite_direct`]).
///
/// The URL's own loopback port (the direct one, first) must forward: when it
/// can't (no target, a forward refused, a port that is also the callback
/// and couldn't be bound), the URL isn't opened (`Err`, `cannot forward port
/// N: <why>`) and nothing else is forwarded, since the browser would reach
/// whatever this host runs on that port, the URL (maybe a token) with it. A
/// callback port that fails is only a log line naming `instance` and `url`
/// (unless the forwarder logged it, `Err(None)`): the page itself is
/// remote, so the URL still opens. Third: every callback port that didn't
/// forward, for the [`Outcome::Opened`] line.
pub fn forward_loopbacks(
    instance: &str,
    url: &str,
    target: Result<&ForwardTarget, &str>,
    forward: impl Fn(&ForwardTarget, Loopback) -> Result<u16, Option<String>>,
) -> (Result<String, String>, Vec<String>, Vec<Loopback>) {
    let wanted = loopback_ports(url);
    let direct = loopback_port(url);
    let mut lines = Vec::new();
    let mut unforwarded = Vec::new();
    let mut bound = None;
    if !wanted.is_empty() {
        match target {
            Err(why) => {
                if let Some(port) = direct {
                    return (Err(format!("cannot forward port {port}: {why}")), lines, unforwarded);
                }
                let ports: Vec<String> = wanted.iter().map(|l| l.port.to_string()).collect();
                let s = if ports.len() > 1 { "s" } else { "" };
                lines.push(format!("{instance}: cannot forward callback port{s} {} for {url} ({why}); opening it as is", ports.join(", ")));
                unforwarded = wanted;
            }
            Ok(target) => {
                for lb in wanted {
                    let own = Some(lb.port) == direct;
                    match forward(target, lb) {
                        Ok(host_port) if own => bound = Some(host_port),
                        Ok(_) => {}
                        Err(why) if own => {
                            let why = why.unwrap_or_else(|| "it is also the callback port, which couldn't be bound".into());
                            return (Err(format!("cannot forward port {}: {why}", lb.port)), lines, unforwarded);
                        }
                        Err(None) => unforwarded.push(lb),
                        Err(Some(why)) => {
                            lines.push(format!("{instance}: cannot forward callback port {} for {url}: {why}", lb.port));
                            unforwarded.push(lb);
                        }
                    }
                }
            }
        }
    }
    let rewrite = bound.filter(|&b| Some(b) != direct);
    let opened = if rewrite.is_some() || parts(url).is_some_and(|p| host_here(p.host).is_some()) && direct.is_some() {
        rewrite_direct(url, rewrite)
    } else {
        url.to_string()
    };
    (Ok(opened), lines, unforwarded)
}

/// The session variables a browser on this host needs: the display
/// (X11 or Wayland) and X11's auth cookie (GNOME's Xwayland puts it under
/// `$XDG_RUNTIME_DIR`, so `~/.Xauthority` isn't it).
const DISPLAY_ENV: [&str; 3] = ["DISPLAY", "WAYLAND_DISPLAY", "XAUTHORITY"];

/// Whether this host can show a browser: on Linux, `DISPLAY` or
/// `WAYLAND_DISPLAY` set (non-empty), read through `env`. Other OSes always
/// say yes (macOS's `open` needs no env).
pub fn has_display(os: &str, env: impl Fn(&str) -> Option<OsString>) -> bool {
    os != "linux" || DISPLAY_ENV[..2].iter().any(|k| env(k).is_some_and(|v| !v.is_empty()))
}

/// [`DISPLAY_ENV`] out of `systemctl --user show-environment`'s output
/// (`KEY=value` lines). A shell-quoted value (systemd prints one with special
/// characters as `$'...'`) is skipped: display names and the auth path never
/// need quoting.
pub fn session_display_env(show_environment: &str) -> Vec<(String, String)> {
    show_environment
        .lines()
        .filter_map(|l| l.split_once('='))
        .filter(|(k, v)| DISPLAY_ENV.contains(k) && !v.is_empty() && !v.starts_with("$'"))
        .map(|(k, v)| (k.to_string(), v.to_string()))
        .collect()
}

/// The display env for the opener on `os`: `Some(empty)` when the daemon's
/// own env has a display (the child inherits it); on Linux otherwise the
/// user manager's current env, which the desktop session imports into at
/// login (GNOME and Plasma do; `systemctl --user import-environment` in
/// other sessions). A `serve install`ed daemon starts with `default.target`,
/// before the session does that, so its own env never has one; reading the
/// manager's at open time also follows a session that came (or went) since.
/// `None`: no display anywhere.
fn display_env(os: &str) -> Option<Vec<(String, String)>> {
    if has_display(os, |k| std::env::var_os(k)) {
        return Some(Vec::new());
    }
    if os != "linux" {
        return None;
    }
    let out = Command::new("systemctl")
        .args(["--user", "show-environment"])
        .stdin(Stdio::null())
        .stderr(Stdio::null())
        .output()
        .ok()
        .filter(|o| o.status.success())?;
    let env = session_display_env(&String::from_utf8_lossy(&out.stdout));
    let lookup = |k: &str| env.iter().find(|(n, _)| n == k).map(|(_, v)| OsString::from(v));
    has_display(os, lookup).then_some(env)
}

/// The instance's host's half of an open from `instance` (`instance_id`, so
/// a rename doesn't lose it): its `state.toml` entry, read on every open,
/// made into an [`OpenJob`] ([`local_job`]; the drift warning logged). An
/// unreadable `state.toml` is logged and the job goes on with no profile
/// and no forward target.
pub fn prepare(instance: &str, instance_id: &str, url: &str, log: fn(&str)) -> OpenJob {
    let state = State::load().unwrap_or_else(|e| {
        log(&format!("{instance}: {e:#}; opening {url} with the default opener"));
        State::default()
    });
    let found = state.instances.iter().find(|(_, i)| i.instance_id == instance_id).map(|(k, i)| (k.as_str(), i));
    let allowed = |i: &Instance| crate::snapshot::allowed_now(&mut BTreeMap::new(), i);
    let (job, warning) = local_job(instance, url, found, allowed);
    if let Some(warning) = &warning {
        log(warning);
    }
    job
}

/// The opening host's half: open `job`'s URL here with the command
/// [`open_argv`] maps its profile to in this host's `profiles.toml` (read on
/// every open, so an edit applies without a daemon restart; unreadable, it
/// is logged and the system opener used) and the [`display_env`], all stdio
/// null, after [`forward_loopbacks`] through `forward` (the URL the command
/// gets is the rewritten one). Blocks until the child exits (it reaps it):
/// callers run it on a thread of its own, so a bridge's handler or an API
/// request never waits on it (nor on the forwards), like
/// `desktop::notify_desktop`. No display, a direct loopback port that can't
/// forward, no known opener or a failed spawn is one `log` line naming the
/// instance and the URL, and nothing opens. Every open ends in
/// one `done` ([`Outcome::Opened`] once the child is spawned, else
/// [`Outcome::Failed`]), naming a delivered open's instance `machine/name`.
pub fn open_here(job: OpenJob, forward: &Forwarder, log: fn(&str), done: &OnDone) {
    let OpenJob { instance, url, profile, target } = job;
    let who = match &target {
        Ok(ForwardTarget { machine: Some(m), .. }) => format!("{m}/{instance}"),
        _ => instance.clone(),
    };
    let finish = |url: &str, outcome: Outcome| done(OpenDone { instance: who.clone(), url: url.to_string(), profile: profile.clone(), outcome });
    let os = std::env::consts::OS;
    let Some(env) = display_env(os) else {
        log(&format!("{instance}: no display to open {url} on"));
        return finish(&url, Outcome::Failed("no display on this host".into()));
    };
    let profiles = profiles_path().and_then(|p| load_profiles(&p)).unwrap_or_else(|e| {
        log(&format!("{instance}: {e:#}; opening {url} with the default opener"));
        HostProfiles::new()
    });
    let (opened, lines, unforwarded) = forward_loopbacks(&instance, &url, target.as_ref().map_err(String::as_str), &**forward);
    for line in &lines {
        log(line);
    }
    let url = match opened {
        Ok(url) => url,
        Err(why) => {
            log(&format!("{instance}: not opening {url}: {why}"));
            return finish(&url, Outcome::Failed(why));
        }
    };
    let Some(argv) = open_argv(os, profile.as_deref(), &profiles, &url) else {
        log(&format!("{instance}: no opener on {os} for {url}"));
        return finish(&url, Outcome::Failed(format!("no opener on {os}")));
    };
    let spawned = Command::new(&argv[0])
        .args(&argv[1..])
        .envs(env)
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn();
    match spawned {
        Ok(mut child) => {
            // Told before the wait: `xdg-open` may not exit until the
            // browser does.
            finish(&url, Outcome::Opened { unforwarded });
            let _ = child.wait();
        }
        Err(e) => {
            log(&format!("{instance}: cannot run `{}` to open {url}: {e}", argv[0]));
            finish(&url, Outcome::Failed(format!("cannot run `{}`: {e}", argv[0])));
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::devsbd::desktop::RateLimit;
    use std::time::Instant;

    const AT: u64 = 1_790_000_000;

    fn open_now(url: &str) -> Verdict {
        decide(url, AT, AT, || true)
    }

    #[test]
    fn decide_allows_http_and_https_only() {
        for url in ["https://example.com/a?b=c", "http://localhost:8080/", "HTTPS://EXAMPLE.COM"] {
            assert_eq!(open_now(url), Verdict::Open, "{url}");
        }
        let bad = |url: &str, scheme: &str| assert_eq!(open_now(url), Verdict::BadScheme(scheme.into()), "{url}");
        bad("ssh://host", "ssh");
        bad("file:///etc/passwd", "file");
        bad("javascript:alert(1)", "javascript");
        bad("mailto:a@b.c", "mailto");
        bad("vscode://file/x", "vscode");
        bad("https:example.com", "https");
        bad("/etc/passwd", "");
        bad("report.pdf", "");
        bad("", "");
        bad("-https://x", "-https");
    }

    #[test]
    fn decide_refuses_control_characters_and_whitespace_first() {
        for url in [
            "https://x/a b",
            "https://x/\n",
            "https://x/\r\nweb: opened https://evil",
            "https://x/\u{1b}[2J",
            "https://x/\t",
            "https://x/\u{7f}",
            "https://x/\u{2028}",
            "https://x/\u{a0}",
            "ssh://x y",
        ] {
            assert_eq!(open_now(url), Verdict::BadChars, "{url:?}");
        }
        assert_eq!(decide("https://x/\n", AT, AT, || panic!("not asked")), Verdict::BadChars);
        assert_eq!(open_now("https://x/a%20b%0A"), Verdict::Open, "percent-encoded is fine");
        assert_eq!(open_now("https://x/é"), Verdict::Open);
    }

    #[test]
    fn printable_escapes_controls_and_keeps_the_rest() {
        assert_eq!(printable("web: a b é `x` hasn't"), "web: a b é `x` hasn't");
        assert_eq!(printable("a\nb\r\t\u{1b}[2J\u{2028}"), "a\\nb\\r\\t\\u{1b}[2J\\u{2028}");
    }

    #[test]
    fn decide_drops_records_past_the_ttl() {
        let ttl = OPEN_TTL.as_secs();
        assert_eq!(decide("https://x", AT, AT + ttl, || true), Verdict::Open, "exactly the TTL still opens");
        assert_eq!(decide("https://x", AT, AT + ttl + 1, || true), Verdict::Expired { age: ttl + 1 });
        assert_eq!(decide("https://x", AT + 5, AT, || true), Verdict::Open, "skewed into the future");
    }

    #[test]
    fn decide_spends_the_limit_only_on_otherwise_good_opens() {
        let mut rl = RateLimit::new(OPEN_RATE_BURST, OPEN_RATE_REFILL);
        let t0 = Instant::now();
        let mut try_open = |url: &str, at: u64, t: Instant| decide(url, at, AT, || rl.allow("web-1", None, t));
        // Refused and expired records don't spend tokens.
        for _ in 0..10 {
            assert_eq!(try_open("ssh://x", AT, t0), Verdict::BadScheme("ssh".into()));
            assert!(matches!(try_open("https://x", AT - 60, t0), Verdict::Expired { .. }));
        }
        for _ in 0..OPEN_RATE_BURST {
            assert_eq!(try_open("https://x", AT, t0), Verdict::Open);
        }
        assert_eq!(try_open("https://x", AT, t0), Verdict::Limited);
        assert_eq!(try_open("https://x", AT, t0 + OPEN_RATE_REFILL), Verdict::Open, "one refilled");
        // Another instance has its own bucket.
        assert!(rl.allow("web-2", None, t0));
    }

    #[test]
    fn refusals_log_the_instance_and_url() {
        assert_eq!(Verdict::Open.log_line("web-1", "https://x"), None);
        let line = |v: Verdict| v.log_line("web-1", "ssh://x").unwrap();
        assert_eq!(line(Verdict::BadScheme("ssh".into())), "web-1: open refused, scheme `ssh` (http and https only): ssh://x");
        assert_eq!(line(Verdict::BadScheme(String::new())), "web-1: open refused, not a URL with a scheme: ssh://x");
        assert_eq!(line(Verdict::Expired { age: 45 }), "web-1: open dropped, queued 45s ago (over 30s): ssh://x");
        assert_eq!(line(Verdict::Limited), "web-1: open refused, rate limit (5 per 10s): ssh://x");
        assert_eq!(
            Verdict::BadChars.log_line("web\n-1", "https://x/\nweb: opened").unwrap(),
            "web\\n-1: open refused, control characters or whitespace in the URL: https://x/\\nweb: opened",
            "one line, whatever the URL holds"
        );
    }

    #[test]
    fn opener_argv_per_os() {
        assert_eq!(opener_argv("linux", "https://x/?a=b c"), Some(vec!["xdg-open".into(), "https://x/?a=b c".into()]));
        assert_eq!(opener_argv("macos", "https://x"), Some(vec!["open".into(), "https://x".into()]));
        assert_eq!(opener_argv("windows", "https://x"), None);
    }

    #[test]
    fn profiles_toml_parses_open_commands() {
        let profiles = parse_profiles(
            "[work]\nopen = [\"google-chrome\", \"--profile-directory=Profile 2\"]\n\n[personal]\nopen = [\"firefox\"]\n",
        )
        .unwrap();
        assert_eq!(profiles.len(), 2);
        assert_eq!(profiles["work"].open, vec!["google-chrome", "--profile-directory=Profile 2"]);
        assert_eq!(profiles["personal"].open, vec!["firefox"]);
        assert!(parse_profiles("").unwrap().is_empty(), "an empty file is no profiles");
        assert!(parse_profiles("# nothing yet\n").unwrap().is_empty());
    }

    #[test]
    fn profiles_toml_refuses_bad_entries() {
        let err = |s: &str| format!("{:#}", parse_profiles(s).unwrap_err());
        assert!(err("[work]\nopen = []\n").contains("[work]: `open` is empty"));
        err("[work]\nopen = [\"chrome\", 2]\n");
        err("[work]\nopen = \"chrome\"\n");
        err("[work]\n");
        assert!(err("[work]\nopen = [\"chrome\"]\nicon = \"x\"\n").contains("icon"), "unknown keys refused");
        err("open = [\"chrome\"]\n");
    }

    #[test]
    fn a_missing_profiles_toml_is_none() {
        let dir = std::env::temp_dir().join(format!("devsandbox-open-test-{}", std::process::id()));
        assert!(load_profiles(&dir.join("profiles.toml")).unwrap().is_empty());
    }

    fn profiles() -> HostProfiles {
        parse_profiles("[work]\nopen = [\"chrome\", \"--profile-directory=Work\"]\n").unwrap()
    }

    #[test]
    fn open_argv_uses_a_mapped_profile_else_the_system_opener() {
        let url = "https://x/?a=b";
        let argv = |v: &[&str]| Some(v.iter().map(|s| s.to_string()).collect::<Vec<_>>());
        assert_eq!(open_argv("linux", Some("work"), &profiles(), url), argv(&["chrome", "--profile-directory=Work", url]));
        assert_eq!(open_argv("macos", Some("work"), &profiles(), url), argv(&["chrome", "--profile-directory=Work", url]));
        assert_eq!(open_argv("linux", Some("personal"), &profiles(), url), argv(&["xdg-open", url]), "unmapped");
        assert_eq!(open_argv("macos", None, &profiles(), url), argv(&["open", url]), "no profile");
        assert_eq!(open_argv("linux", None, &HostProfiles::new(), url), argv(&["xdg-open", url]));
    }

    /// One state entry, as `state.toml` holds it, with `profile`.
    fn instance(profile: Option<&str>) -> Instance {
        let profile = profile.map_or(String::new(), |p| format!("profile = \"{p}\"\n"));
        let state: State = toml::from_str(&format!(
            "[instance.web-1]\nsandbox = \"web\"\ninstance_id = \"web-1\"\ncontainer = \"devsandbox-web-1\"\n\
             folder = \"/src/web\"\nworkspace = \"/workspaces/web\"\ncreated_unix = 1\n{profile}"
        ))
        .unwrap();
        state.instances.into_values().next().unwrap()
    }

    #[test]
    fn the_instances_profile_is_sent_unless_the_config_dropped_it() {
        let url = "https://x";
        let allowed = |set: &'static [&'static str]| {
            move |_: &Instance| Some(set.iter().map(|s| s.to_string()).collect::<Vec<_>>())
        };
        let send = |inst: Option<&Instance>, set: &'static [&'static str]| profile_to_send("web-1", url, inst, allowed(set));
        let named = |p: &str| (Some(p.to_string()), None);

        let work = instance(Some("work"));
        assert_eq!(send(Some(&work), &["work", "personal"]), named("work"));
        assert_eq!(send(Some(&instance(Some("personal"))), &["work", "personal"]), named("personal"), "mapped or not: the opener's call");
        assert_eq!(send(Some(&instance(None)), &["work"]), (None, None), "no profile");
        assert_eq!(send(None, &["work"]), (None, None), "no instance");
        assert_eq!(profile_to_send("web-1", url, Some(&work), |_| None), named("work"), "an allowed set that can't be told doesn't flag");
        assert_eq!(
            send(Some(&work), &["personal"]),
            (
                None,
                Some("web-1: profile `work` no longer allowed by the config; opening https://x with the default opener".into())
            ),
            "disallowed"
        );
        let unasked = |_: &Instance| -> Option<Vec<String>> { panic!("no profile: the config isn't loaded") };
        profile_to_send("web-1", url, Some(&instance(None)), unasked);
    }

    #[test]
    fn a_local_job_carries_the_profile_and_the_instances_target() {
        let inst = Instance { config_dir: Some("/cfg".into()), ..instance(Some("work")) };
        let (job, warning) = local_job("web", "https://x", Some(("web-1", &inst)), |_| None);
        assert_eq!(warning, None);
        assert_eq!(
            job,
            OpenJob {
                instance: "web".into(),
                url: "https://x".into(),
                profile: Some("work".into()),
                target: Ok(ForwardTarget { machine: None, dir: "/cfg".into(), instance: "web-1".into() }),
            }
        );
        let bare = instance(None);
        assert_eq!(local_job("web", "https://x", Some(("web-1", &bare)), |_| None).0.target, Err("no config root in state.toml".into()));
        assert_eq!(local_job("web", "https://x", None, |_| None).0.target, Err("not in state.toml".into()));
    }

    fn direct(port: u16) -> Loopback {
        Loopback { port, callback: false }
    }

    fn callback(port: u16) -> Loopback {
        Loopback { port, callback: true }
    }

    #[test]
    fn loopback_ports_of_direct_urls() {
        let cases: &[(&str, &[Loopback])] = &[
            ("http://localhost:3000/app?x=1#top", &[direct(3000)]),
            ("http://127.0.0.1:8080", &[direct(8080)]),
            ("http://[::1]:5173/", &[direct(5173)]),
            ("http://[0:0:0:0:0:0:0:1]:5173/", &[direct(5173)]),
            ("http://0.0.0.0:8000/", &[direct(8000)]),
            ("HTTP://LOCALHOST:3000/", &[direct(3000)]),
            ("http://[::1]/", &[direct(80)]),
            ("http://localhost/", &[direct(80)]),
            ("https://localhost", &[direct(443)]),
            ("http://localhost:/", &[direct(80)]),
            ("http://user:pw@localhost:9000/", &[direct(9000)]),
            // A trailing dot, and the whole of 127.0.0.0/8.
            ("http://localhost.:3000/", &[direct(3000)]),
            ("http://127.0.0.1.:3000/", &[direct(3000)]),
            ("http://0.0.0.0.:3000/", &[direct(3000)]),
            ("http://127.0.0.2:3000/", &[direct(3000)]),
            ("http://127.255.1.9/", &[direct(80)]),
            // A browser reads `\` as `/`: the host is evil.com.
            ("http://evil.com\\@localhost:3000/", &[]),
            ("http://localhost\\@evil.com:3000/", &[direct(80)]),
            ("http://localhost@example.com:3000/", &[]),
            ("https://example.com/", &[]),
            ("http://localhost.evil.com:3000/", &[]),
            ("http://localhost..:3000/", &[]),
            ("http://128.0.0.1:3000/", &[]),
            ("http://[::2]:3000/", &[]),
            ("http://localhost:0/", &[]),
            ("http://localhost:65536/", &[]),
            ("http://localhost:3x/", &[]),
            ("http://localhost:+80/", &[]),
            ("http://localhost:-1/", &[]),
            ("http://localhost: 80/", &[]),
            ("http://[::1]x/", &[]),
            // Not parsed, so not loopback (they open unforwarded).
            ("http://127.1:3000/", &[]),
            ("http://0x7f.0.0.1:3000/", &[]),
            ("http://2130706433:3000/", &[]),
            ("http://[fe80::1%25lo]:3000/", &[]),
            ("http://%6cocalhost:3000/", &[]),
            ("not a url", &[]),
        ];
        for (url, want) in cases {
            assert_eq!(loopback_ports(url), *want, "{url}");
        }
    }

    #[test]
    fn loopback_ports_of_callbacks() {
        let cases: &[(&str, &[Loopback])] = &[
            (
                "https://github.com/login/oauth/authorize?client_id=x&redirect_uri=http%3A%2F%2Flocalhost%3A8085%2Fcb&state=y",
                &[callback(8085)],
            ),
            ("https://idp.example/auth?redirect_uri=http://127.0.0.1:9004/cb", &[callback(9004)]),
            ("https://idp.example/auth?redirect_url=http%3a%2f%2f%5b%3a%3a1%5d%3a7000", &[callback(7000)]),
            ("https://idp.example/auth?callback=http://localhost/cb", &[callback(80)]),
            (
                "https://idp.example/auth?redirect_uri=http://localhost:1&redirect_url=http://localhost:2&callback=http://localhost:3",
                &[callback(1), callback(2), callback(3)],
            ),
            // Only a key's first value, even when it isn't a loopback one.
            ("https://idp.example/a?redirect_uri=http://localhost:1&redirect_uri=http://localhost:2", &[callback(1)]),
            ("https://idp.example/a?redirect_uri=https://example.com&redirect_uri=http://localhost:2", &[]),
            ("https://idp.example/a?callback=ssh://localhost:22", &[]),
            ("https://idp.example/a?callback=localhost:22", &[]),
            ("https://idp.example/a?redirect_uri=http://localhost.evil.com:1", &[]),
            ("https://idp.example/a?return_to=http://localhost:1", &[]),
            ("https://idp.example/a#redirect_uri=http://localhost:1", &[]),
            ("https://idp.example/a?x=1#redirect_uri=http://localhost:1", &[]),
            // Nested URLs aren't followed.
            ("https://idp.example/a?next=https%3A%2F%2Fb%2F%3Fredirect_uri%3Dhttp%3A%2F%2Flocalhost%3A1", &[]),
            // Direct first; the same port both ways is a callback.
            ("http://localhost:3000/login?redirect_uri=http://localhost:4000/cb", &[direct(3000), callback(4000)]),
            ("http://localhost:3000/login?redirect_uri=http://localhost:3000/cb", &[callback(3000)]),
        ];
        for (url, want) in cases {
            assert_eq!(loopback_ports(url), *want, "{url}");
        }
    }

    #[test]
    fn rewrite_direct_sets_the_port_and_leaves_the_rest() {
        let cases: &[(&str, Option<u16>, &str)] = &[
            ("http://localhost:3000/a?b=c#d", Some(3001), "http://localhost:3001/a?b=c#d"),
            ("http://localhost/a", Some(3001), "http://localhost:3001/a"),
            ("https://localhost", Some(8443), "https://localhost:8443"),
            ("http://localhost?q=http://x:1", Some(5), "http://localhost:5?q=http://x:1"),
            ("http://[::1]:3000/", Some(3001), "http://127.0.0.1:3001/"),
            ("http://[::1]/", Some(3001), "http://127.0.0.1:3001/"),
            ("http://[::1]:3000/", None, "http://127.0.0.1:3000/"),
            ("http://u:p@127.0.0.1:3000/", Some(3001), "http://u:p@127.0.0.1:3001/"),
            ("http://0.0.0.0:8000/x", None, "http://localhost:8000/x"),
            ("http://0.0.0.0/x", None, "http://localhost/x"),
            ("http://0.0.0.0.:8000/x", None, "http://localhost:8000/x"),
            ("http://0.0.0.0:8000/x", Some(8001), "http://localhost:8001/x"),
            ("http://127.0.0.2:8000/x", None, "http://127.0.0.1:8000/x"),
            ("http://127.9.9.9:8000/x", Some(8001), "http://127.0.0.1:8001/x"),
            ("http://127.0.0.1.:8000/x", None, "http://127.0.0.1.:8000/x"),
            ("http://localhost.:3000/", Some(3001), "http://localhost.:3001/"),
            ("http://localhost:3000/", None, "http://localhost:3000/"),
            ("http://evil.com\\@localhost:3000/", None, "http://evil.com\\@localhost:3000/"),
            ("not a url", Some(1), "not a url"),
        ];
        for (url, port, want) in cases {
            assert_eq!(rewrite_direct(url, *port), *want, "{url} {port:?}");
        }
    }

    /// The local target of state key `web-1` with config root `/cfg`.
    fn local() -> ForwardTarget {
        ForwardTarget::local(Some(("web-1", &Instance { config_dir: Some("/cfg".into()), ..instance(None) }))).unwrap()
    }

    type Call = (ForwardTarget, Loopback);

    /// A fake forwarder: bind `bound(port)`, recording each call.
    fn forwarding(
        calls: &std::cell::RefCell<Vec<Call>>,
        bound: impl Fn(Loopback) -> Result<u16, Option<String>>,
    ) -> impl Fn(&ForwardTarget, Loopback) -> Result<u16, Option<String>> {
        move |t, lb| {
            calls.borrow_mut().push((t.clone(), lb));
            bound(lb)
        }
    }

    fn opens(url: &str) -> Result<String, String> {
        Ok(url.to_string())
    }

    #[test]
    fn forward_loopbacks_rewrites_a_moved_direct_port() {
        let calls = Default::default();
        let t = local();
        let got = forward_loopbacks("web", "http://localhost:3000/x", Ok(&t), forwarding(&calls, |_| Ok(3001)));
        assert_eq!(got, (opens("http://localhost:3001/x"), vec![], vec![]));
        assert_eq!(calls.into_inner(), vec![(t.clone(), direct(3000))]);

        let got = forward_loopbacks("web", "http://localhost:3000/x", Ok(&t), |_: &ForwardTarget, lb: Loopback| Ok(lb.port));
        assert_eq!(got, (opens("http://localhost:3000/x"), vec![], vec![]), "bound on the same port");

        let got = forward_loopbacks("web", "http://0.0.0.0:8000/", Ok(&t), |_: &ForwardTarget, lb: Loopback| Ok(lb.port));
        assert_eq!(got.0, opens("http://localhost:8000/"), "0.0.0.0 made localhost");
        let got = forward_loopbacks("web", "http://0.0.0.0:8000/", Ok(&t), |_: &ForwardTarget, _| Ok(8001));
        assert_eq!(got.0, opens("http://localhost:8001/"));
        let got = forward_loopbacks("web", "http://127.0.0.5:8000/", Ok(&t), |_: &ForwardTarget, lb: Loopback| Ok(lb.port));
        assert_eq!(got.0, opens("http://127.0.0.1:8000/"), "onto the forward's 127.0.0.1");
    }

    #[test]
    fn forward_loopbacks_hands_a_machines_target_through() {
        let calls = Default::default();
        let t = ForwardTarget { machine: Some("box".into()), dir: "/home/u/cfg".into(), instance: "web-1".into() };
        let url = "https://idp.example/a?redirect_uri=http://localhost:3000/";
        let got = forward_loopbacks("web-1", url, Ok(&t), forwarding(&calls, |_| Err(Some("no machine named `box`".into()))));
        assert_eq!(got.1, vec![format!("web-1: cannot forward callback port 3000 for {url}: no machine named `box`")]);
        assert_eq!(got.2, vec![callback(3000)]);
        assert_eq!(calls.into_inner(), vec![(t, callback(3000))]);
    }

    #[test]
    fn forward_loopbacks_leaves_a_callback_url_as_is() {
        let calls = Default::default();
        let t = local();
        let url = "https://github.com/login?redirect_uri=http%3A%2F%2Flocalhost%3A8085%2Fcb";
        let got = forward_loopbacks("web", url, Ok(&t), forwarding(&calls, |lb| Ok(lb.port)));
        assert_eq!(got, (opens(url), vec![], vec![]));
        assert_eq!(calls.into_inner(), vec![(t.clone(), callback(8085))]);

        // A callback the forwarder already reported: no second log line,
        // still unforwarded, and the URL (its page remote) still opens.
        let got = forward_loopbacks("web", url, Ok(&t), |_: &ForwardTarget, _| Err(None));
        assert_eq!(got, (opens(url), vec![], vec![callback(8085)]));
        let got = forward_loopbacks("web", url, Ok(&t), |_: &ForwardTarget, _| Err(Some("registry stopped".into())));
        assert_eq!(got.1, vec![format!("web: cannot forward callback port 8085 for {url}: registry stopped")]);
        assert_eq!((got.0, got.2), (opens(url), vec![callback(8085)]));

        // No target: the same, one line for them all.
        let never = |_: &ForwardTarget, _| -> Result<u16, Option<String>> { panic!("nothing to forward to") };
        let url = "https://idp.example/a?redirect_uri=http://localhost:1&callback=http://localhost:2";
        let got = forward_loopbacks("web", url, Err("not in state.toml"), never);
        assert_eq!(
            got,
            (
                opens(url),
                vec![format!("web: cannot forward callback ports 1, 2 for {url} (not in state.toml); opening it as is")],
                vec![callback(1), callback(2)]
            )
        );
    }

    /// The URL's own port not forwarded: it isn't opened (the browser would
    /// reach whatever this host runs there), and nothing else is forwarded.
    #[test]
    fn forward_loopbacks_never_opens_an_unforwarded_direct_port() {
        let calls = Default::default();
        let t = local();
        let url = "http://localhost:3000/?redirect_uri=http://localhost:4000/cb";
        let got = forward_loopbacks("web", url, Ok(&t), forwarding(&calls, |_| Err(Some("port 3000: nope".into()))));
        assert_eq!(got, (Err("cannot forward port 3000: port 3000: nope".into()), vec![], vec![]));
        assert_eq!(calls.into_inner(), vec![(t.clone(), direct(3000))], "the callback isn't bound");

        // Direct and callback at once, its `Fixed` bind failed (the
        // forwarder's status line said why).
        let url = "http://localhost:3000/login?redirect_uri=http://localhost:3000/cb";
        let got = forward_loopbacks("web", url, Ok(&t), |_: &ForwardTarget, _| Err(None));
        assert_eq!(got.0, Err("cannot forward port 3000: it is also the callback port, which couldn't be bound".into()));

        // A later callback failing doesn't matter once the direct one bound.
        let url = "http://localhost:3000/?redirect_uri=http://localhost:4000/cb";
        let got = forward_loopbacks("web", url, Ok(&t), |_: &ForwardTarget, lb: Loopback| if lb.callback { Err(None) } else { Ok(3001) });
        assert_eq!(got, (opens("http://localhost:3001/?redirect_uri=http://localhost:4000/cb"), vec![], vec![callback(4000)]));

        let never = |_: &ForwardTarget, _| -> Result<u16, Option<String>> { panic!("nothing to forward to") };
        let got = forward_loopbacks("web", "http://0.0.0.0:3000/?callback=http://localhost:9", Err("not in state.toml"), never);
        assert_eq!(got, (Err("cannot forward port 3000: not in state.toml".into()), vec![], vec![]));
        assert_eq!(forward_loopbacks("web", "https://example.com/", Err("x"), never), (opens("https://example.com/"), vec![], vec![]));
        let url = "http://evil.com\\@localhost:3000/";
        assert_eq!(forward_loopbacks("web", url, Err("x"), never), (opens(url), vec![], vec![]), "not loopback, untouched");
    }

    fn done(profile: Option<&str>, outcome: Outcome) -> OpenDone {
        OpenDone { instance: "web".into(), url: "https://x.example/a".into(), profile: profile.map(Into::into), outcome }
    }

    #[test]
    fn each_outcome_is_one_status_line() {
        let opened = Outcome::Opened { unforwarded: vec![] };
        assert_eq!(done(Some("work"), opened.clone()).line(), "web: opened https://x.example/a (work)");
        assert_eq!(done(None, opened.clone()).line(), "web: opened https://x.example/a");
        assert_eq!((done(None, opened).kind(), done(None, Outcome::Opened { unforwarded: vec![] }).why()), ("opened", None));
        let blocked = done(Some("work"), Outcome::Opened { unforwarded: vec![callback(8085), direct(3000)] });
        assert_eq!(blocked.line(), "web: opened https://x.example/a (work); cannot forward callback port 8085, port 3000");
        assert_eq!(blocked.why().as_deref(), Some("cannot forward callback port 8085, port 3000"));

        let why = Verdict::BadScheme("ssh".into()).refusal().unwrap();
        let refused = done(None, Outcome::Refused(why));
        assert_eq!((refused.kind(), refused.line().as_str()), ("refused", "web: open refused: scheme `ssh` (http and https only)"));
        let failed = done(Some("work"), Outcome::Failed("no display on this host".into()));
        assert_eq!((failed.kind(), failed.line().as_str()), ("failed", "web: open failed: no display on this host"));
        assert_eq!(failed.why().as_deref(), Some("no display on this host"));

        // Whatever a refused URL or a member's reason holds, one line.
        let d = OpenDone { url: "https://x/\u{1b}[2J".into(), ..done(None, Outcome::Failed("nope\nweb: opened".into())) };
        assert_eq!(d.line(), "web: open failed: nope\\nweb: opened");
        let d = OpenDone { url: "https://x/\r\n".into(), ..done(None, Outcome::Opened { unforwarded: vec![] }) };
        assert_eq!(d.line(), "web: opened https://x/\\r\\n");
    }

    #[test]
    fn long_urls_and_reasons_are_shortened_in_the_line_only() {
        let url = format!("https://idp.example/auth?{}", "q".repeat(500));
        let d = OpenDone { url: url.clone(), ..done(Some("work"), Outcome::Opened { unforwarded: vec![] }) };
        let line = d.line();
        let shown = line.strip_prefix("web: opened ").unwrap().strip_suffix(" (work)").unwrap();
        assert_eq!(shown.chars().count(), LINE_URL);
        assert!(shown.ends_with('…') && url.starts_with(shown.trim_end_matches('…')));
        assert_eq!(d.url, url, "the wire keeps it whole");
        let d = done(None, Outcome::Failed("é".repeat(400)));
        assert_eq!(d.line().strip_prefix("web: open failed: ").unwrap().chars().count(), LINE_WHY);
        assert_eq!(shorten("abc", 3), "abc");
        assert_eq!(shorten("abcd", 3), "ab…");
    }

    #[test]
    fn display_comes_from_the_env_on_linux() {
        let env = |set: &'static [(&'static str, &'static str)]| {
            move |k: &str| set.iter().find(|(n, _)| *n == k).map(|(_, v)| OsString::from(v))
        };
        assert!(!has_display("linux", env(&[])));
        assert!(!has_display("linux", env(&[("DISPLAY", "")])));
        assert!(has_display("linux", env(&[("DISPLAY", ":0")])));
        assert!(has_display("linux", env(&[("WAYLAND_DISPLAY", "wayland-0")])));
        assert!(!has_display("linux", env(&[("XAUTHORITY", "/run/user/1000/a")])), "auth alone isn't a display");
        assert!(has_display("macos", env(&[])));
    }

    #[test]
    fn session_display_env_picks_the_display_vars() {
        let out = "DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus\n\
                   DISPLAY=:0\n\
                   HOME=/home/u\n\
                   WAYLAND_DISPLAY=wayland-0\n\
                   XAUTHORITY=/run/user/1000/.mutter-Xwaylandauth.AB12\n\
                   XDG_CURRENT_DESKTOP=GNOME\n";
        let pair = |k: &str, v: &str| (k.to_string(), v.to_string());
        assert_eq!(
            session_display_env(out),
            vec![
                pair("DISPLAY", ":0"),
                pair("WAYLAND_DISPLAY", "wayland-0"),
                pair("XAUTHORITY", "/run/user/1000/.mutter-Xwaylandauth.AB12"),
            ]
        );
        // A headless manager: nothing; quoted and empty values are skipped.
        assert!(session_display_env("HOME=/home/u\nPATH=/usr/bin\n").is_empty());
        assert!(session_display_env("DISPLAY=\nXAUTHORITY=$'/tmp/a b'\n").is_empty());
    }
}