mise 2026.9.4

Dev tools, env vars, and tasks in one CLI
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
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
//! Session-only GitHub access. Authorization is checked on the initiating machine;
//! neither the remote transport nor its callers can request a credential.
use crate::duration;
use eyre::{Result, bail};

#[derive(Clone, Debug, Default)]
pub(crate) struct Scope {
    #[cfg(unix)]
    options: Options,
    #[cfg(any(unix, test))]
    repositories: std::collections::BTreeSet<String>,
    #[cfg(any(unix, test))]
    all: bool,
}

impl Scope {
    pub(crate) fn from_flags(
        enabled: bool,
        repositories: &[String],
        all: bool,
    ) -> Result<Option<Self>> {
        if !enabled {
            if all || !repositories.is_empty() {
                bail!("repository scope requires --github-relay-read-only");
            }
            return Ok(None);
        }
        if all != repositories.is_empty() {
            bail!("choose --github-relay-repo OWNER/REPO or --github-relay-all-repos, not both");
        }
        let repositories: std::collections::BTreeSet<String> = repositories
            .iter()
            .map(|repo| repository(repo))
            .collect::<Result<_>>()?;
        #[cfg(not(any(unix, test)))]
        let _ = repositories;
        Ok(Some(Self {
            #[cfg(unix)]
            options: Options::default(),
            #[cfg(any(unix, test))]
            repositories,
            #[cfg(any(unix, test))]
            all,
        }))
    }

    #[cfg(any(unix, test))]
    fn permits(&self, repo: &str) -> bool {
        self.all || self.repositories.contains(&repo.to_ascii_lowercase())
    }
}

#[derive(Clone, Debug)]
#[cfg(unix)]
struct Options {
    log_requests: bool,
    jsonl: bool,
    max_duration: std::time::Duration,
    request_timeout: std::time::Duration,
    concurrency: usize,
}

#[cfg(unix)]
impl Default for Options {
    fn default() -> Self {
        Self {
            log_requests: false,
            jsonl: false,
            max_duration: std::time::Duration::ZERO,
            request_timeout: std::time::Duration::from_secs(300),
            concurrency: 8,
        }
    }
}

/// Resolve observability and limits only after explicit access authorization.
pub(crate) fn configure(
    scope: Option<Scope>,
    log_requests: bool,
    no_log_requests: bool,
    format: Option<&str>,
    max_duration: Option<&str>,
) -> Result<Option<Scope>> {
    let Some(scope) = scope else {
        if log_requests || no_log_requests || format.is_some() || max_duration.is_some() {
            bail!("relay options require --github-relay-read-only");
        }
        return Ok(None);
    };
    let settings = crate::config::Settings::get();
    let settings = &settings.github_relay;
    let format = format.unwrap_or(&settings.log_format);
    if !matches!(format, "text" | "jsonl") {
        bail!("relay log format must be text or jsonl");
    }
    let timeout = duration::parse_duration(&settings.request_timeout)?;
    if timeout.is_zero() {
        bail!("relay request timeout must be greater than zero");
    }
    if !(1..=32).contains(&settings.concurrency) {
        bail!("relay concurrency must be between 1 and 32");
    }
    let max_duration = duration::parse_duration(max_duration.unwrap_or(&settings.max_duration))?;
    if std::time::Instant::now().checked_add(timeout).is_none()
        || std::time::Instant::now()
            .checked_add(max_duration)
            .is_none()
    {
        bail!("relay duration is too large");
    }
    #[cfg(not(unix))]
    let _ = max_duration;
    #[cfg(unix)]
    let scope = {
        let mut scope = scope;
        scope.options = Options {
            log_requests: !no_log_requests && (log_requests || settings.log_requests),
            jsonl: format == "jsonl",
            max_duration,
            request_timeout: timeout,
            concurrency: settings.concurrency as usize,
        };
        scope
    };
    Ok(Some(scope))
}

fn repository(value: &str) -> Result<String> {
    let parts: Vec<_> = value.split('/').collect();
    if parts.len() != 2
        || parts.iter().any(|s| {
            s.is_empty()
                || *s == "."
                || *s == ".."
                || !s
                    .bytes()
                    .all(|b| b.is_ascii_alphanumeric() || b"-_.".contains(&b))
        })
    {
        bail!("expected a GitHub repository in OWNER/REPO form");
    }
    Ok(value.to_ascii_lowercase())
}

/// Expand only unambiguous shorthand, preserving paths and explicit transports.
pub(crate) fn expand_repository(value: &str) -> Result<String> {
    if value.contains(':')
        || value.starts_with(['/', '.', '~'])
        || std::path::Path::new(value).exists()
    {
        return Ok(value.to_string());
    }
    repository(value)?;
    Ok(format!(
        "https://github.com/{}.git",
        value.strip_suffix(".git").unwrap_or(value)
    ))
}

#[derive(Debug, PartialEq)]
#[cfg(any(unix, test))]
struct Target {
    url: String,
    git: bool,
    archive_repo: Option<String>,
}

#[cfg(any(unix, test))]
fn validate_path(path: &str) -> Result<()> {
    // Validate decoded segments, retaining the original spelling upstream. Reject
    // encoded separators and percent signs so a second decoder cannot change scope.
    for segment in path.split('/') {
        for (index, byte) in segment.bytes().enumerate() {
            if byte == b'%'
                && !segment
                    .as_bytes()
                    .get(index + 1..index + 3)
                    .is_some_and(|digits| digits.iter().all(u8::is_ascii_hexdigit))
            {
                bail!("invalid relay path encoding");
            }
        }
        let decoded = urlencoding::decode(segment)?;
        if decoded.is_empty()
            || matches!(decoded.as_ref(), "." | "..")
            || decoded.contains(['/', '\\', '%'])
            || decoded.chars().any(char::is_control)
        {
            bail!("invalid relay path");
        }
    }
    Ok(())
}

#[cfg(any(unix, test))]
fn authorize(scope: &Scope, method: &str, path: &str, query: Option<&str>) -> Result<Target> {
    validate_path(path)?;
    let p: Vec<_> = path.split('/').collect();
    let (owner, repo) = match p.as_slice() {
        ["api", "repos", owner, repo, ..] => (*owner, *repo),
        ["git" | "web", owner, repo, ..] => (*owner, repo.strip_suffix(".git").unwrap_or(repo)),
        _ => bail!("unsupported GitHub operation"),
    };
    let name = repository(&format!("{owner}/{repo}"))?;
    if !scope.permits(&name) {
        bail!("repository is outside the approved relay scope");
    }
    let git = p[0] == "git";
    let allowed = match p.as_slice() {
        ["git", _, _, "info", "refs"] => {
            method == "GET" && query == Some("service=git-upload-pack")
        }
        ["git", _, _, "git-upload-pack"] => method == "POST" && query.is_none(),
        ["api", "repos", _, _] => method == "GET" || method == "HEAD",
        ["api", "repos", _, _, "git", kind, ..] => {
            matches!(*kind, "refs" | "matching-refs") && matches!(method, "GET" | "HEAD")
        }
        ["api", "repos", _, _, kind, ..] => {
            matches!(
                *kind,
                "contents" | "releases" | "tags" | "branches" | "tarball" | "zipball"
            ) && matches!(method, "GET" | "HEAD")
        }
        ["web", _, _, "releases", "download", _, ..] => matches!(method, "GET" | "HEAD"),
        ["web", _, _, "archive", _, ..] => matches!(method, "GET" | "HEAD"),
        _ => false,
    };
    if !allowed {
        bail!("GitHub relay permits read-only repository operations only");
    }
    if !git && let Some(query) = query {
        for (key, _) in url::form_urlencoded::parse(query.as_bytes()) {
            if !matches!(key.as_ref(), "ref" | "page" | "per_page") {
                bail!("unsupported query parameter");
            }
        }
    }
    let host = if p[0] == "api" {
        "api.github.com"
    } else {
        "github.com"
    };
    let suffix = path.split_once('/').expect("validated path").1;
    let mut url = format!("https://{host}/{suffix}");
    let archive_repo = match p.as_slice() {
        ["api", "repos", _, _, "tarball" | "zipball", ..] => Some(name.clone()),
        ["web", _, _, "archive", rest @ ..] => {
            let reference = rest.join("/");
            let (kind, reference) = if let Some(reference) = reference.strip_suffix(".tar.gz") {
                ("tarball", reference)
            } else if let Some(reference) = reference.strip_suffix(".zip") {
                ("zipball", reference)
            } else {
                bail!("unsupported archive format");
            };
            if reference.is_empty() {
                bail!("missing archive reference");
            }
            // The API supplies short-lived private archive links; credentials
            // remain at the API origin and are never attached to that redirect.
            url = format!("https://api.github.com/repos/{name}/{kind}/{reference}");
            Some(name)
        }
        _ => None,
    };
    if let Some(query) = query {
        url.push('?');
        url.push_str(query);
    }
    Ok(Target {
        url,
        git,
        archive_repo,
    })
}

#[cfg(unix)]
pub(crate) mod unix {
    use super::*;
    use axum::{
        Router,
        body::{Body, to_bytes},
        extract::{Request, State},
        response::Response,
    };
    use reqwest::{Client, Method, Url};
    use std::{
        path::{Path, PathBuf},
        sync::Arc,
        time::Duration,
    };
    use tokio::{net::UnixListener, sync::Semaphore, task::JoinHandle};
    use tokio_util::sync::CancellationToken;

    // Bound accepted connections too, including clients that never send headers.
    struct BoundedListener<L> {
        inner: L,
        permits: Arc<Semaphore>,
    }
    impl<L> BoundedListener<L> {
        fn new(inner: L) -> Self {
            Self {
                inner,
                permits: Arc::new(Semaphore::new(32)),
            }
        }
    }
    struct Connection<T> {
        inner: T,
        _permit: tokio::sync::OwnedSemaphorePermit,
    }
    impl<T: tokio::io::AsyncRead + Unpin> tokio::io::AsyncRead for Connection<T> {
        fn poll_read(
            mut self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
            buf: &mut tokio::io::ReadBuf<'_>,
        ) -> std::task::Poll<std::io::Result<()>> {
            std::pin::Pin::new(&mut self.inner).poll_read(cx, buf)
        }
    }
    impl<T: tokio::io::AsyncWrite + Unpin> tokio::io::AsyncWrite for Connection<T> {
        fn poll_write(
            mut self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
            buf: &[u8],
        ) -> std::task::Poll<std::io::Result<usize>> {
            std::pin::Pin::new(&mut self.inner).poll_write(cx, buf)
        }
        fn poll_flush(
            mut self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<std::io::Result<()>> {
            std::pin::Pin::new(&mut self.inner).poll_flush(cx)
        }
        fn poll_shutdown(
            mut self: std::pin::Pin<&mut Self>,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<std::io::Result<()>> {
            std::pin::Pin::new(&mut self.inner).poll_shutdown(cx)
        }
    }
    impl<L: axum::serve::Listener> axum::serve::Listener for BoundedListener<L> {
        type Io = Connection<L::Io>;
        type Addr = L::Addr;
        async fn accept(&mut self) -> (Self::Io, Self::Addr) {
            let permit = self
                .permits
                .clone()
                .acquire_owned()
                .await
                .expect("listener semaphore stays open");
            let (inner, addr) = self.inner.accept().await;
            (
                Connection {
                    inner,
                    _permit: permit,
                },
                addr,
            )
        }
        fn local_addr(&self) -> std::io::Result<Self::Addr> {
            self.inner.local_addr()
        }
    }

    /// Session-local audit data; never stores credentials, URLs or response bodies.
    struct Audit {
        destination: String,
        options: Options,
        requests: std::sync::atomic::AtomicU64,
        denied: std::sync::atomic::AtomicU64,
        bytes: std::sync::atomic::AtomicU64,
        repositories: std::sync::Mutex<std::collections::BTreeSet<String>>,
    }

    impl Audit {
        fn new(destination: &str, options: Options) -> Self {
            Self {
                destination: destination.to_string(),
                options,
                requests: Default::default(),
                denied: Default::default(),
                bytes: Default::default(),
                repositories: Default::default(),
            }
        }

        fn operation(&self, scope: &Scope, request: &Request) -> String {
            let method = match request.method().as_str() {
                "GET" => "GET",
                "HEAD" => "HEAD",
                "POST" => "POST",
                _ => "OTHER",
            };
            let Ok(target) = authorize(
                scope,
                request.method().as_str(),
                request.uri().path().strip_prefix('/').unwrap_or_default(),
                request.uri().query(),
            ) else {
                return format!("{method} unapproved operation");
            };
            let url = Url::parse(&target.url).expect("authorized URL");
            let segments: Vec<_> = url.path().trim_start_matches('/').split('/').collect();
            let offset = usize::from(url.host_str() == Some("api.github.com"));
            let repository = format!(
                "{}/{}",
                segments[offset],
                segments[offset + 1].trim_end_matches(".git")
            );
            let mut repositories = self.repositories.lock().unwrap();
            if repositories.len() < 128 {
                repositories.insert(repository.clone());
            }
            // Only fixed route names are shown. Refs, filenames, and URL query
            // values can contain private data unrelated to debugging transport.
            let route = segments.get(offset + 2).copied().unwrap_or("metadata");
            format!("{method} {repository}/{route}")
        }

        fn line(&self, mut event: serde_json::Value) -> String {
            event["destination"] = self.destination.clone().into();
            event["timestamp_ms"] = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_millis()
                .to_string()
                .into();
            if self.options.jsonl {
                event.to_string()
            } else {
                let label = serde_json::to_string(&self.destination).unwrap();
                match event["event"].as_str() {
                    Some("request") => format!(
                        "github-relay [{label}] {} → {} (headers: {}ms)",
                        event["operation"].as_str().unwrap_or_default(),
                        event["status"],
                        event["headers_ms"]
                    ),
                    Some("expired") => format!("github-relay [{label}] borrowed access expired"),
                    Some("summary") => format!(
                        "github-relay [{label}] {} requests, {} denied/unavailable, {} bytes, repositories: {}",
                        event["requests"],
                        event["denied_or_unavailable"],
                        event["bytes"],
                        event["repositories"]
                    ),
                    _ => format!("github-relay [{label}] {event}"),
                }
            }
        }

        fn emit(&self, event: serde_json::Value) {
            use std::io::Write;
            let line = self.line(event);
            // A closed stderr must never panic or affect the remote command.
            let _ = writeln!(std::io::stderr().lock(), "{line}");
        }

        fn summary(&self) {
            use std::sync::atomic::Ordering::Relaxed;
            self.emit(serde_json::json!({"event": "summary", "requests": self.requests.load(Relaxed), "denied_or_unavailable": self.denied.load(Relaxed), "bytes": self.bytes.load(Relaxed), "repositories": *self.repositories.lock().unwrap()}));
        }
    }

    #[derive(Clone)]
    struct Broker {
        audit: Arc<Audit>,
        scope: Scope,
        client: Client,
        permits: Arc<Semaphore>,
        cancel: CancellationToken,
        token: Arc<String>,
        #[cfg(test)]
        test_upstream: Option<String>,
    }

    pub(crate) struct Relay {
        audit: Arc<Audit>,
        expiry: Option<AbortTask>,
        task: JoinHandle<()>,
        directory: tempfile::TempDir,
        cancel: CancellationToken,
    }
    impl Relay {
        pub(crate) fn socket(&self) -> PathBuf {
            self.directory.path().join("relay.sock")
        }
        pub(crate) async fn start(scope: Scope, destination: &str) -> Result<Self> {
            let (token, _) = crate::github::resolve_token("github.com").ok_or_else(|| {
                eyre::eyre!(
                    "no local GitHub credential found; sign in locally with `mise token github`"
                )
            })?;
            // Unix socket names have a small platform limit; TMPDIR can itself
            // exceed it (notably macOS and isolated test environments).
            let directory = tempfile::Builder::new()
                .prefix("mise-relay-")
                .tempdir_in("/tmp")?;
            let listener = UnixListener::bind(directory.path().join("relay.sock"))?;
            let cancel = CancellationToken::new();
            let audit = Arc::new(Audit::new(destination, scope.options.clone()));
            let expiry = expiry_task(cancel.clone(), audit.clone());
            let broker = Broker {
                audit: audit.clone(),
                client: Client::builder()
                    .no_proxy()
                    .redirect(reqwest::redirect::Policy::none())
                    .timeout(scope.options.request_timeout)
                    .connect_timeout(Duration::from_secs(15))
                    .user_agent("mise-github-relay")
                    .build()?,
                permits: Arc::new(Semaphore::new(scope.options.concurrency)),
                scope,
                cancel: cancel.clone(),
                token: Arc::new(token),
                #[cfg(test)]
                test_upstream: None,
            };
            let task = tokio::spawn(async move {
                let _ = axum::serve(
                    BoundedListener::new(listener),
                    Router::new().fallback(handle).with_state(broker),
                )
                .await;
            });
            Ok(Self {
                audit,
                expiry,
                task,
                directory,
                cancel,
            })
        }
    }
    impl Drop for Relay {
        fn drop(&mut self) {
            self.cancel.cancel();
            self.task.abort();
            self.expiry.take();
            self.audit.summary();
        }
    }

    fn expiry_task(cancel: CancellationToken, audit: Arc<Audit>) -> Option<AbortTask> {
        if audit.options.max_duration.is_zero() {
            return None;
        }
        Some(AbortTask(tokio::spawn(async move {
            tokio::time::sleep(audit.options.max_duration).await;
            cancel.cancel();
            audit.emit(serde_json::json!({"event": "expired"}));
        })))
    }

    async fn handle(State(broker): State<Broker>, request: Request) -> Response {
        if request.method() == Method::GET && request.uri().path() == "/_session" {
            return Response::builder()
                .status(if broker.cancel.is_cancelled() {
                    403
                } else {
                    204
                })
                .header(
                    "x-mise-relay-timeout",
                    serde_json::to_string(&broker.audit.options.request_timeout)
                        .expect("duration is serializable"),
                )
                .body(Body::empty())
                .expect("valid response");
        }
        let started = std::time::Instant::now();
        let audit = broker.audit.clone();
        let operation = audit.operation(&broker.scope, &request);
        audit
            .requests
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        let cancel = broker.cancel.clone();
        let result = tokio::select! {
            biased;
            _ = cancel.cancelled() => Err(eyre::eyre!("session ended")),
            result = tokio::time::timeout(audit.options.request_timeout, forward(broker, request)) => result.unwrap_or_else(|_| Err(eyre::eyre!("request timeout"))),
        };
        let status = result.as_ref().map(|r| r.status().as_u16()).unwrap_or(403);
        if status >= 400 {
            audit
                .denied
                .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        }
        if audit.options.log_requests && result.is_err() {
            audit.emit(serde_json::json!({"event": "request", "operation": operation, "status": status, "headers_ms": started.elapsed().as_millis()}));
        }
        match result {
            Ok(response) => response,
            // Deliberately never serialize upstream errors, URLs or credentials.
            Err(_) => Response::builder()
                .status(403)
                .body(Body::from("GitHub relay request denied or unavailable"))
                .expect("valid response"),
        }
    }

    async fn forward(broker: Broker, request: Request) -> Result<Response> {
        let deadline = tokio::time::Instant::now() + broker.audit.options.request_timeout;
        let operation = broker.audit.operation(&broker.scope, &request);
        let permit = broker.permits.clone().try_acquire_owned()?;
        let target = authorize(
            &broker.scope,
            request.method().as_str(),
            request.uri().path().strip_prefix('/').unwrap_or_default(),
            request.uri().query(),
        )?;
        let (parts, body) = request.into_parts();
        let body = to_bytes(body, 8 * 1024 * 1024).await?;
        if parts.method != Method::POST && !body.is_empty() {
            bail!("unexpected request body");
        }
        let upstream = target.url.clone();
        #[cfg(test)]
        let upstream = if let Some(base) = &broker.test_upstream {
            let url = Url::parse(&upstream)?;
            format!(
                "{base}{}{}",
                url.path(),
                url.query().map(|q| format!("?{q}")).unwrap_or_default()
            )
        } else {
            upstream
        };
        let mut req = broker.client.request(parts.method.clone(), upstream);
        if target.git {
            req = req.basic_auth("x-access-token", Some(broker.token.as_str()));
        } else {
            req = req.bearer_auth(broker.token.as_str());
        }
        for name in [
            "accept",
            "range",
            "if-range",
            "git-protocol",
            "content-encoding",
        ] {
            if let Some(value) = parts.headers.get(name) {
                req = req.header(name, value);
            }
        }
        if parts.method == Method::POST {
            req = req.header("content-type", "application/x-git-upload-pack-request");
        }
        let sent_at = std::time::Instant::now();
        let mut response = req.body(body).send().await?;
        if broker.audit.options.log_requests {
            broker.audit.emit(serde_json::json!({"event": "request", "operation": operation, "status": response.status().as_u16(), "headers_ms": sent_at.elapsed().as_millis()}));
        }
        // Only asset redirects are followed. Preserve resume headers, never credentials.
        for _ in 0..3 {
            if !response.status().is_redirection() {
                break;
            }
            let location = response
                .headers()
                .get("location")
                .ok_or_else(|| eyre::eyre!("missing redirect"))?
                .to_str()?;
            let url = Url::parse(location)?;
            if target.git
                || !(asset_redirect(&url) || archive_redirect(&url, target.archive_repo.as_deref()))
            {
                bail!("unsupported redirect");
            }
            let redirect_host = url.host_str().unwrap_or_default().to_string();
            let redirected_at = std::time::Instant::now();
            let mut redirected = broker.client.request(parts.method.clone(), url);
            for name in ["range", "if-range"] {
                if let Some(value) = parts.headers.get(name) {
                    redirected = redirected.header(name, value);
                }
            }
            response = redirected.send().await?;
            if broker.audit.options.log_requests {
                broker.audit.emit(serde_json::json!({"event": "request", "operation": format!("{} {redirect_host}/<download>", parts.method), "status": response.status().as_u16(), "headers_ms": redirected_at.elapsed().as_millis()}));
            }
        }
        if response.status().is_redirection() {
            bail!("too many redirects");
        }
        let mut builder = Response::builder().status(response.status());
        for name in [
            "content-type",
            "content-length",
            "content-range",
            "etag",
            "last-modified",
            "link",
        ] {
            if let Some(value) = response.headers().get(name) {
                builder = builder.header(name, value);
            }
        }
        let stream = futures_util::stream::try_unfold(
            (response, permit, broker.cancel, broker.audit, deadline),
            |(mut response, permit, cancel, audit, deadline)| async move {
                let chunk = tokio::select! {
                    biased;
                    _ = cancel.cancelled() => return Err(std::io::Error::other("session ended")),
                    result = tokio::time::timeout_at(deadline, response.chunk()) => result.map_err(|_| std::io::Error::other("relay transfer timeout"))?.map_err(|_| std::io::Error::other("relay transfer failed"))?,
                };
                if let Some(chunk) = &chunk {
                    audit
                        .bytes
                        .fetch_add(chunk.len() as u64, std::sync::atomic::Ordering::Relaxed);
                }
                Ok(chunk.map(|chunk| (chunk, (response, permit, cancel, audit, deadline))))
            },
        );
        Ok(builder.body(Body::from_stream(stream))?)
    }

    fn archive_redirect(url: &Url, repository: Option<&str>) -> bool {
        let Some(repository) = repository else {
            return false;
        };
        let parts: Vec<_> = url.path().trim_start_matches('/').split('/').collect();
        safe_redirect_origin(url)
            && url.host_str() == Some("codeload.github.com")
            && parts.len() >= 4
            && format!("{}/{}", parts[0], parts[1]).eq_ignore_ascii_case(repository)
            && matches!(parts[2], "tar.gz" | "zip" | "legacy.tar.gz" | "legacy.zip")
            && validate_path(url.path().trim_start_matches('/')).is_ok()
    }

    fn safe_redirect_origin(url: &Url) -> bool {
        url.scheme() == "https"
            && url.username().is_empty()
            && url.password().is_none()
            && url.port().is_none()
            && url.fragment().is_none()
    }

    fn asset_redirect(url: &Url) -> bool {
        safe_redirect_origin(url)
            && matches!(
                url.host_str(),
                Some("release-assets.githubusercontent.com" | "objects.githubusercontent.com")
            )
    }

    /// Git only speaks TCP HTTP. A loopback bridge with a per-session capability
    /// adapts it to the private socket; the broker remains the authority.
    pub(crate) async fn session(socket: &Path, command: Vec<String>) -> Result<()> {
        let (client, request_timeout) = adapter_client(socket).await?;
        let listener = tokio::net::TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, 0)).await?;
        let address = listener.local_addr()?;
        let capability = rand::random::<[u8; 32]>()
            .iter()
            .map(|b| format!("{b:02x}"))
            .collect::<String>();
        let prefix = format!("/{capability}/");
        let permits = Arc::new(Semaphore::new(8));
        let service = Router::new().fallback(move |request: Request| {
            let client = client.clone();
            let prefix = prefix.clone();
            let permits = permits.clone();
            async move {
                let result: Result<Response> = async {
                    let permit = permits.try_acquire_owned()?;
                    let path = request
                        .uri()
                        .path_and_query()
                        .ok_or_else(|| eyre::eyre!("missing path"))?
                        .as_str();
                    let path = path
                        .strip_prefix(&prefix)
                        .filter(|p| p.starts_with("git/"))
                        .ok_or_else(|| eyre::eyre!("invalid capability"))?;
                    let url = format!("http://localhost/{path}");
                    let (parts, body) = request.into_parts();
                    let body =
                        tokio::time::timeout(request_timeout, to_bytes(body, 8 * 1024 * 1024))
                            .await??;
                    let mut req = client.request(parts.method, url);
                    for name in ["accept", "content-type", "git-protocol", "content-encoding"] {
                        if let Some(value) = parts.headers.get(name) {
                            req = req.header(name, value);
                        }
                    }
                    let response = send_adapter_request(req.body(body), request_timeout).await?;
                    let mut builder = Response::builder().status(response.status());
                    for name in ["content-type", "content-length"] {
                        if let Some(value) = response.headers().get(name) {
                            builder = builder.header(name, value);
                        }
                    }
                    let stream = futures_util::stream::try_unfold(
                        (response, permit),
                        |(mut response, permit)| async move {
                            let chunk = response
                                .chunk()
                                .await
                                .map_err(|_| std::io::Error::other("relay disconnected"))?;
                            Ok::<_, std::io::Error>(chunk.map(|chunk| (chunk, (response, permit))))
                        },
                    );
                    Ok(builder.body(Body::from_stream(stream))?)
                }
                .await;
                result.unwrap_or_else(|_| {
                    Response::builder()
                        .status(403)
                        .body(Body::from("relay unavailable"))
                        .expect("valid response")
                })
            }
        });
        let task = tokio::spawn(async move {
            let _ = axum::serve(BoundedListener::new(listener), service).await;
        });
        // Aborting this task is sufficient here: exiting the adapter also closes
        // all accepted loopback connections. Local broker cancellation is separate.
        let guard = AbortTask(task);
        let mut child = if command.is_empty() {
            let shell = std::env::var_os("SHELL").unwrap_or_else(|| "/bin/sh".into());
            let mut child = tokio::process::Command::new(shell);
            child.arg("-l");
            child
        } else {
            let mut child = tokio::process::Command::new(&command[0]);
            child.args(&command[1..]);
            child
        };
        let executable = std::env::current_exe()?;
        let mut paths = vec![
            executable
                .parent()
                .ok_or_else(|| eyre::eyre!("missing mise executable directory"))?
                .to_path_buf(),
        ];
        paths.extend(std::env::split_paths(
            &std::env::var_os("PATH").unwrap_or_default(),
        ));
        child.env("PATH", std::env::join_paths(paths)?);
        let count: usize = std::env::var("GIT_CONFIG_COUNT")
            .unwrap_or_else(|_| "0".into())
            .parse()?;
        if count > 1000 {
            bail!("too many inherited Git configuration entries");
        }
        let base = format!("http://{address}/{capability}/git/");
        for (index, source) in [
            "https://github.com/",
            "git@github.com:",
            "ssh://git@github.com/",
        ]
        .iter()
        .enumerate()
        {
            child.env(
                format!("GIT_CONFIG_KEY_{}", count + index),
                format!("url.{base}.insteadOf"),
            );
            child.env(format!("GIT_CONFIG_VALUE_{}", count + index), source);
        }
        child
            .env("GIT_CONFIG_COUNT", (count + 3).to_string())
            .env("MISE_GITHUB_RELAY_SOCKET", socket)
            .kill_on_drop(true);
        let status = wait_command(&mut child, Some(socket)).await?;
        drop(guard);
        Err(crate::request_exit(status.code().unwrap_or(255)))
    }

    struct AbortTask(JoinHandle<()>);
    impl Drop for AbortTask {
        fn drop(&mut self) {
            self.0.abort();
        }
    }

    /// Handle TERM/HUP as well as the CLI's existing Ctrl-C cancellation. A
    /// heartbeat also closes remote commands when a non-TTY SSH connection dies.
    pub(crate) async fn wait_command(
        command: &mut tokio::process::Command,
        socket: Option<&Path>,
    ) -> Result<std::process::ExitStatus> {
        let mut terminate =
            tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())?;
        let mut hangup = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::hangup())?;
        let heartbeat = if let Some(socket) = socket {
            let client = Client::builder()
                .unix_socket(socket)
                .no_proxy()
                .timeout(Duration::from_secs(3))
                .build()?;
            if !client
                .get("http://localhost/_session")
                .send()
                .await
                .is_ok_and(|r| r.status() == 204)
            {
                bail!("GitHub relay is not connected");
            }
            Some(client)
        } else {
            None
        };
        let disconnected = async {
            let Some(client) = heartbeat else {
                std::future::pending::<()>().await;
                return;
            };
            let mut failures = 0;
            loop {
                tokio::time::sleep(Duration::from_secs(2)).await;
                if client
                    .get("http://localhost/_session")
                    .send()
                    .await
                    .is_ok_and(|r| r.status() == 204)
                {
                    failures = 0;
                } else {
                    failures += 1;
                    if failures >= 3 {
                        return;
                    }
                }
            }
        };
        let mut child = command.kill_on_drop(true).spawn()?;
        let code = tokio::select! {
            status = child.wait() => {
                let status = status?;
                use std::os::unix::process::ExitStatusExt;
                if let Some(signal) = status.signal() {
                    return Err(crate::request_exit(128 + signal));
                }
                return Ok(status);
            },
            _ = terminate.recv() => 143,
            _ = hangup.recv() => 129,
            _ = disconnected => 255,
        };
        child.kill().await?;
        Err(crate::request_exit(code))
    }

    pub(crate) async fn lifecycle<T>(
        operation: impl std::future::Future<Output = Result<T>>,
    ) -> Result<T> {
        let mut interrupt =
            tokio::signal::unix::signal(tokio::signal::unix::SignalKind::interrupt())?;
        let mut terminate =
            tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())?;
        let mut hangup = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::hangup())?;
        if crate::ui::ctrlc::is_cancelled() {
            return Err(crate::request_exit(130));
        }
        tokio::select! {
            biased;
            _ = interrupt.recv() => Err(crate::request_exit(130)),
            _ = terminate.recv() => Err(crate::request_exit(143)),
            _ = hangup.recv() => Err(crate::request_exit(129)),
            result = operation => result,
        }
    }

    /// API adapter: an HTTP request over the forwarded private socket. No upstream
    /// authentication headers are sent to the target or supplied by the target.
    pub(crate) async fn request(
        socket: &Path,
        method: Method,
        url: &Url,
        headers: &http::HeaderMap,
    ) -> Result<reqwest::Response> {
        if url.scheme() != "https"
            || !url.username().is_empty()
            || url.password().is_some()
            || url.port().is_some()
        {
            bail!("unsupported relay destination");
        }
        let prefix = match url.host_str() {
            Some("api.github.com") => "api",
            Some("github.com") => "web",
            _ => bail!("unsupported relay host"),
        };
        let mut relay_url = Url::parse(&format!("http://localhost/{prefix}{}", url.path()))?;
        relay_url.set_query(url.query());
        let (client, request_timeout) = adapter_client(socket).await?;
        let mut req = client.request(method, relay_url);
        for name in ["accept", "range", "if-range"] {
            if let Some(value) = headers.get(name) {
                req = req.header(name, value);
            }
        }
        send_adapter_request(req, request_timeout).await
    }

    // Obtain the initiating machine's policy, not the target's saved settings.
    // Discovery itself has a short fixed bound and bypasses broker request slots.
    async fn adapter_client(socket: &Path) -> Result<(Client, Duration)> {
        let builder = || {
            Client::builder()
                .unix_socket(socket)
                .no_proxy()
                .redirect(reqwest::redirect::Policy::none())
        };
        let response = builder()
            .build()?
            .get("http://localhost/_session")
            .timeout(Duration::from_secs(3))
            .send()
            .await
            .map_err(|error| {
                eyre::Report::new(error.without_url()).wrap_err("GitHub relay policy unavailable")
            })?;
        if response.status() != 204 {
            bail!("GitHub relay is not connected");
        }
        let value = response
            .headers()
            .get("x-mise-relay-timeout")
            .ok_or_else(|| eyre::eyre!("GitHub relay timeout policy missing"))?;
        let timeout: Duration = serde_json::from_slice(value.as_bytes())?;
        if timeout.is_zero() || std::time::Instant::now().checked_add(timeout).is_none() {
            bail!("invalid GitHub relay timeout policy");
        }
        Ok((builder().read_timeout(timeout).build()?, timeout))
    }

    // Bound connecting and writing as well as waiting for response headers.
    // The client's read timeout separately protects the streamed response.
    async fn send_adapter_request(
        req: reqwest::RequestBuilder,
        timeout: Duration,
    ) -> Result<reqwest::Response> {
        tokio::time::timeout(timeout, req.send())
            .await
            .map_err(|_| eyre::eyre!("GitHub relay request setup timed out"))?
            .map_err(|error| {
                eyre::Report::new(error.without_url())
                    .wrap_err("GitHub relay disconnected or unavailable")
            })
    }
    #[cfg(test)]
    mod tests {
        use super::*;

        #[tokio::test]
        async fn adapters_use_the_brokers_timeout_policy() {
            for timeout in [Duration::from_secs(600), Duration::from_millis(20)] {
                let directory = tempfile::Builder::new()
                    .prefix("relay-policy-")
                    .tempdir_in("/tmp")
                    .unwrap();
                let socket = directory.path().join("relay.sock");
                let listener = UnixListener::bind(&socket).unwrap();
                let service = Router::new().fallback(move |request: Request| async move {
                    if request.uri().path() == "/_session" {
                        Response::builder()
                            .status(204)
                            .header(
                                "x-mise-relay-timeout",
                                serde_json::to_string(&timeout).unwrap(),
                            )
                            .body(Body::empty())
                            .unwrap()
                    } else {
                        tokio::time::sleep(Duration::from_millis(100)).await;
                        Response::new(Body::empty())
                    }
                });
                let _server = AbortTask(tokio::spawn(async move {
                    axum::serve(listener, service).await.unwrap();
                }));
                // This same policy/client pair is used by Git's adapter.
                let (client, actual) = adapter_client(&socket).await.unwrap();
                assert_eq!(actual, timeout);
                let git = send_adapter_request(
                    client.post("http://localhost/git/owner/repo/git-upload-pack"),
                    actual,
                )
                .await;
                let api = request(
                    &socket,
                    Method::GET,
                    &Url::parse("https://api.github.com/repos/owner/repo/releases").unwrap(),
                    &http::HeaderMap::new(),
                )
                .await;
                assert_eq!(git.is_ok(), timeout.as_secs() == 600);
                assert_eq!(api.is_ok(), timeout.as_secs() == 600);
            }
        }

        #[tokio::test]
        async fn adapter_setup_timeout_bounds_stalled_socket_writes() {
            let directory = tempfile::Builder::new()
                .prefix("relay-timeout-")
                .tempdir_in("/tmp")
                .unwrap();
            let socket = directory.path().join("relay.sock");
            let listener = tokio::net::UnixListener::bind(&socket).unwrap();
            let client = Client::builder()
                .unix_socket(socket.as_path())
                .no_proxy()
                .build()
                .unwrap();
            let request = client
                .post("http://localhost/git/owner/repo/git-upload-pack")
                .body(vec![0_u8; 8 * 1024 * 1024]);
            let operation = async {
                let (stream, _) = listener.accept().await.unwrap();
                // Keep the socket open without reading the oversized request.
                std::future::pending::<()>().await;
                drop(stream);
            };
            let result = tokio::select! {
                result = send_adapter_request(request, Duration::from_millis(50)) => result,
                _ = operation => panic!("stalled peer unexpectedly completed"),
            };
            assert!(result.unwrap_err().to_string().contains("setup timed out"));
        }
        #[test]
        fn audit_redacts_dynamic_paths_queries_and_denied_requests() {
            let audit = Audit::new(
                "devbox",
                Options {
                    jsonl: true,
                    ..Default::default()
                },
            );
            let scope = Scope::from_flags(true, &["jdx/mise".into()], false)
                .unwrap()
                .unwrap();
            for uri in [
                "/api/repos/jdx/mise/contents/private-secret?ref=query-secret",
                "/api/repos/other/private-secret?token=query-secret",
            ] {
                let request = Request::builder().uri(uri).body(Body::empty()).unwrap();
                let operation = audit.operation(&scope, &request);
                let line = audit.line(serde_json::json!({"event":"request", "operation":operation, "status":403, "headers_ms":1}));
                assert!(!line.contains("private-secret"));
                assert!(!line.contains("query-secret"));
                let event: serde_json::Value = serde_json::from_str(&line).unwrap();
                assert_eq!(event["destination"], "devbox");
            }
        }

        #[tokio::test]
        async fn access_duration_expires_and_timer_is_owned() {
            let audit = Arc::new(Audit::new(
                "test",
                Options {
                    max_duration: Duration::from_millis(10),
                    ..Default::default()
                },
            ));
            let cancel = CancellationToken::new();
            let _timer = expiry_task(cancel.clone(), audit.clone());
            tokio::time::timeout(Duration::from_secs(1), cancel.cancelled())
                .await
                .unwrap();
            let cancel = CancellationToken::new();
            drop(expiry_task(cancel.clone(), audit));
            tokio::time::sleep(Duration::from_millis(20)).await;
            assert!(!cancel.is_cancelled());
        }
        #[test]
        fn archives_only_redirect_to_the_authorized_repository() {
            for (url, expected) in [
                (
                    "https://codeload.github.com/jdx/mise/legacy.tar.gz/main?token=ephemeral",
                    true,
                ),
                ("https://codeload.github.com/other/private/zip/main", false),
                ("https://codeload.github.com/jdx/mise/other/main", false),
                ("http://codeload.github.com/jdx/mise/zip/main", false),
                ("https://127.0.0.1/jdx/mise/zip/main", false),
                (
                    "https://codeload.github.com.attacker.invalid/jdx/mise/zip/main",
                    false,
                ),
            ] {
                assert_eq!(
                    archive_redirect(&Url::parse(url).unwrap(), Some("jdx/mise")),
                    expected
                );
                assert!(!archive_redirect(&Url::parse(url).unwrap(), None));
            }
        }
        #[tokio::test]
        async fn broker_authenticates_locally_and_redacts_failures() {
            let mut upstream = mockito::Server::new_async().await;
            let api = upstream
                .mock("GET", "/repos/jdx/mise/releases/latest")
                .match_header("authorization", "Bearer fake-local-token")
                .match_header("range", "bytes=10-")
                .match_header("if-range", "etag-1")
                .with_header("authorization", "must-not-reach-target")
                .with_body("release")
                .create_async()
                .await;
            let git = upstream
                .mock("POST", "/jdx/mise.git/git-upload-pack")
                .match_header("content-type", "application/x-git-upload-pack-request")
                .with_body("pack")
                .create_async()
                .await;
            let redirect = upstream
                .mock("GET", "/repos/jdx/mise/releases/assets/1")
                .with_status(302)
                .with_header("location", "http://127.0.0.1/secret?token=fake-local-token")
                .create_async()
                .await;
            let broker = Broker {
                audit: Arc::new(Audit::new("test", Options::default())),
                scope: Scope::from_flags(true, &["jdx/mise".into()], false)
                    .unwrap()
                    .unwrap(),
                client: Client::builder()
                    .no_proxy()
                    .redirect(reqwest::redirect::Policy::none())
                    .build()
                    .unwrap(),
                permits: Arc::new(Semaphore::new(8)),
                cancel: CancellationToken::new(),
                token: Arc::new("fake-local-token".into()),
                test_upstream: Some(upstream.url()),
            };
            for (method, path, expected) in [
                ("GET", "/api/repos/jdx/mise/releases/latest", 200),
                ("POST", "/git/jdx/mise.git/git-upload-pack", 200),
                ("GET", "/api/repos/jdx/mise/releases/assets/1", 403),
                ("POST", "/api/repos/jdx/mise/releases", 403),
                ("GET", "/api/repos/other/private", 403),
            ] {
                let request = Request::builder()
                    .method(method)
                    .uri(path)
                    .header("authorization", "remote-cannot-select-credentials")
                    .header("range", "bytes=10-")
                    .header("if-range", "etag-1")
                    .body(Body::empty())
                    .unwrap();
                let response = handle(State(broker.clone()), request).await;
                assert_eq!(response.status(), expected);
                assert!(response.headers().get("authorization").is_none());
                let body = to_bytes(response.into_body(), 1024).await.unwrap();
                assert!(!String::from_utf8_lossy(&body).contains("fake-local-token"));
            }
            api.assert_async().await;
            git.assert_async().await;
            redirect.assert_async().await;
            use std::sync::atomic::Ordering::Relaxed;
            assert_eq!(broker.audit.requests.load(Relaxed), 5);
            assert_eq!(broker.audit.denied.load(Relaxed), 3);
            assert_eq!(broker.audit.bytes.load(Relaxed), 11);
            let permits = broker.permits.clone().acquire_many_owned(8).await.unwrap();
            let response = handle(
                State(broker.clone()),
                Request::builder()
                    .uri("/api/repos/jdx/mise/releases/latest")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await;
            assert_eq!(response.status(), 403);
            drop(permits);
            broker.cancel.cancel();
            let requests = broker.audit.requests.load(Relaxed);
            let heartbeat = handle(
                State(broker.clone()),
                Request::builder()
                    .uri("/_session")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await;
            assert_eq!(heartbeat.status(), 403);
            assert_eq!(broker.audit.requests.load(Relaxed), requests);
            let response = handle(
                State(broker),
                Request::builder()
                    .uri("/api/repos/jdx/mise")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await;
            assert_eq!(response.status(), 403);
        }
        #[test]
        fn redirects_are_exact_https_asset_hosts() {
            for url in [
                "https://release-assets.githubusercontent.com/asset?signature=x",
                "https://objects.githubusercontent.com/asset",
            ] {
                assert!(asset_redirect(&Url::parse(url).unwrap()));
            }
            for url in [
                "https://github.com/asset",
                "https://127.0.0.1/",
                "http://objects.githubusercontent.com/asset",
                "https://objects.githubusercontent.com.evil.invalid/a",
                "https://user@objects.githubusercontent.com/a",
                "https://objects.githubusercontent.com:8443/a",
            ] {
                assert!(!asset_redirect(&Url::parse(url).unwrap()));
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    fn scope() -> Scope {
        Scope::from_flags(true, &["jdx/mise".into()], false)
            .unwrap()
            .unwrap()
    }
    #[test]
    fn scope_requires_explicit_choice() {
        assert!(Scope::from_flags(true, &[], false).is_err());
        assert!(Scope::from_flags(false, &[], true).is_err());
        assert!(Scope::from_flags(true, &["jdx/mise".into()], true).is_err());
        assert!(
            Scope::from_flags(true, &[], true)
                .unwrap()
                .unwrap()
                .permits("other/private")
        );
    }
    #[test]
    fn reads_only() {
        for path in [
            "api/repos/jdx/mise",
            "api/repos/jdx/mise/releases/latest",
            "api/repos/jdx/mise/contents/Cargo.toml",
            "api/repos/jdx/mise/releases/tags/v%C3%A9",
        ] {
            assert!(authorize(&scope(), "GET", path, None).is_ok());
            assert!(authorize(&scope(), "POST", path, None).is_err());
        }
        assert!(authorize(&scope(), "POST", "git/jdx/mise.git/git-upload-pack", None).is_ok());
        assert!(
            authorize(
                &scope(),
                "GET",
                "git/jdx/mise.git/info/refs",
                Some("service=git-upload-pack")
            )
            .is_ok()
        );
        for path in [
            "api/repos/other/private",
            "api/user",
            "api/graphql",
            "api/repos/jdx/mise/../../user",
            "api/repos/jdx/mise/contents/%2e%2e",
            "api/repos/jdx/mise/contents/%252e%252e",
            "api/repos/jdx/mise/contents/a%2Fb",
            "api/repos/jdx/mise/contents/%5c",
            "api/repos/jdx/mise/contents/%00",
            "api/repos/jdx/mise/contents/%zz",
            "git/jdx/mise.git/git-receive-pack",
        ] {
            assert!(authorize(&scope(), "GET", path, None).is_err(), "{path}");
        }
    }
    #[test]
    fn shorthand() {
        assert_eq!(
            expand_repository("jdx/mise").unwrap(),
            "https://github.com/jdx/mise.git"
        );
        for value in [
            "./my/repo",
            "git@github.com:jdx/mise.git",
            "https://example.com/r.git",
            "/tmp/repo",
        ] {
            assert_eq!(expand_repository(value).unwrap(), value);
        }
        assert!(expand_repository("not/a/repository").is_err());
    }

    #[test]
    fn observability_options_never_enable_access() {
        assert!(configure(None, false, false, None, None).unwrap().is_none());
        assert!(configure(None, true, false, None, None).is_err());
        assert!(configure(None, false, false, Some("jsonl"), None).is_err());
        assert!(configure(None, false, false, None, Some("1h")).is_err());
        assert!(configure(Some(scope()), false, false, Some("invalid"), None).is_err());
        assert!(configure(Some(scope()), false, false, None, Some("-1h")).is_err());
    }

    #[cfg(unix)]
    #[test]
    fn cli_observability_overrides() {
        let scope = configure(Some(scope()), true, false, Some("jsonl"), Some("1h"))
            .unwrap()
            .unwrap();
        assert!(scope.options.log_requests);
        assert!(scope.options.jsonl);
        assert_eq!(scope.options.max_duration.as_secs(), 3600);
        let scope = configure(Some(scope), true, true, Some("text"), Some("0s"))
            .unwrap()
            .unwrap();
        assert!(!scope.options.log_requests);
        assert!(!scope.options.jsonl);
        assert!(scope.options.max_duration.is_zero());
    }

    #[test]
    fn web_archives_use_scoped_api_downloads() {
        let target = authorize(
            &scope(),
            "GET",
            "web/jdx/mise/archive/refs/tags/v1.tar.gz",
            None,
        )
        .unwrap();
        assert_eq!(
            target.url,
            "https://api.github.com/repos/jdx/mise/tarball/refs/tags/v1"
        );
        assert_eq!(target.archive_repo.as_deref(), Some("jdx/mise"));
        assert!(authorize(&scope(), "GET", "api/repos/jdx/mise/zipball/main", None).is_ok());
        assert!(authorize(&scope(), "POST", "api/repos/jdx/mise/tarball/main", None).is_err());
        assert!(authorize(&scope(), "GET", "web/other/private/archive/main.zip", None).is_err());
    }
}