rift-mock-core 0.25.0

Rift core engine: imposter lifecycle, matching, behaviors, scripting and flow-state — usable in-process without the CLI or admin server.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
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
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
//! Config-time resolution for `_rift.script` `file:`/`ref:` sources (issue #356).
//!
//! A `RiftScriptConfig` may specify its script via inline `code`, a `file` path, or a `ref` into
//! the imposter's `_rift.scripts` named registry. This module walks a freshly-parsed
//! `ImposterConfig`, validates that every script config carries exactly one source, and resolves
//! `file`/`ref` down to a populated `code` + `engine` — so everything downstream (validation,
//! execution, the compiled-script cache) only ever sees plain inline scripts.
//!
//! Resolution happens once, at config-load time (`--configfile`/`--datadir`, admin API create,
//! and `/admin/reload`), never per-request.

use super::types::{ImposterConfig, RiftScriptConfig, Stub, StubResponse};
use std::collections::HashMap;
use std::path::{Component, Path, PathBuf};

/// Errors from resolving `file:`/`ref:` script sources.
#[derive(Debug, thiserror::Error)]
pub enum ScriptResolveError {
    #[error("script config must specify exactly one of `code`, `file`, or `ref` (found {found})")]
    InvalidSourceCount { found: usize },
    #[error("unknown script ref '{0}': no entry named '{0}' in `_rift.scripts`")]
    UnknownRef(String),
    #[error("_rift.scripts entry '{0}' may not itself use `ref` — ref chains are not allowed")]
    RefChain(String),
    #[error("failed to read script file '{path}': {source}")]
    FileRead {
        path: String,
        #[source]
        source: std::io::Error,
    },
    #[error("script file path '{0}' escapes the scripts root")]
    PathEscape(String),
    #[error(
        "script file '{0}' cannot be resolved: no --scripts-dir is configured for the admin API"
    )]
    ScriptsDirNotConfigured(String),
}

/// Where `file:` script paths resolve relative to, and whether escaping that root is rejected.
#[derive(Debug, Clone)]
pub enum ScriptBaseDir {
    /// Explicit `--configfile` single-file load: relative to the config file's own directory. No
    /// escape restriction — a human author who can write the configfile already has filesystem
    /// access, so an absolute path or `..` is honored verbatim (matches EJS `include`).
    ConfigRelative(PathBuf),
    /// `--datadir` load (including the persisted `{port}.json` files written by the admin API):
    /// relative to the datadir root, with the SAME escape rules as [`ScriptBaseDir::ScriptsDir`].
    /// These files can be network-authored (a stub POSTed through the admin API is persisted
    /// here), so an absolute path or a `..`/symlink escape is rejected — never read (issue #356
    /// B1/B2 defense-in-depth against a datadir re-resolution reading `/etc/passwd`).
    DatadirRelative(PathBuf),
    /// Admin-API-created imposters: relative to `--scripts-dir`. A resolved path that would
    /// escape this root is rejected outright (never read).
    ScriptsDir(PathBuf),
    /// The admin API has no `--scripts-dir` configured: any `file:` reference is rejected.
    Unconfigured,
}

/// Resolve every `_rift.script` in `config` — the named registry first, then each stub response
/// — mutating `code`/`engine` in place. Returns the first error encountered; on error `config`
/// may be partially mutated and must not be used (the caller returns the error to the client /
/// aborts the load without applying anything).
pub fn resolve_scripts(
    config: &mut ImposterConfig,
    base: &ScriptBaseDir,
) -> Result<(), ScriptResolveError> {
    let default_engine = config.default_script_engine().to_owned();
    // Resolve the named registry first so response-level `ref:` lookups see fully-resolved
    // entries (populated `code` + `engine`, never another `ref`).
    if let Some(rift) = &mut config.rift {
        for (name, script) in rift.scripts.iter_mut() {
            if script.ref_name.is_some() {
                return Err(ScriptResolveError::RefChain(name.clone()));
            }
            validate_source_count(script)?;
            resolve_leaf(script, &default_engine, base)?;
        }
    }

    // Snapshot the resolved registry (small, config-time only) so per-response resolution can
    // look up `ref:` targets without holding a borrow of `config.rift` across the stub loop.
    let registry: HashMap<String, RiftScriptConfig> = config
        .rift
        .as_ref()
        .map(|r| r.scripts.clone())
        .unwrap_or_default();

    resolve_stub_scripts(&mut config.stubs, &registry, &default_engine, base)
}

/// Resolve `_rift.script` sources in a set of stubs against an already-resolved `registry`
/// (issue #356). Used by the admin-API stub sub-resource endpoints (`POST/PUT .../stubs[...]`,
/// space stubs), which add stubs to an existing imposter and must resolve — and therefore
/// escape-check — `file:`/`ref:` at WRITE time, before persisting, exactly like whole-imposter
/// create. `registry` is the target imposter's `_rift.scripts` (its entries must themselves be
/// already resolved — `code` populated, `file`/`ref` cleared — which they are once the imposter
/// was created). `default_engine` is the target imposter's
/// [`ImposterConfig::default_script_engine`]: a stub arrives without its imposter's `_rift` block,
/// so a door that passed `"rhai"` here would ignore the imposter's `defaultEngine` (issue #1159).
pub fn resolve_stub_scripts(
    stubs: &mut [Stub],
    registry: &HashMap<String, RiftScriptConfig>,
    default_engine: &str,
    base: &ScriptBaseDir,
) -> Result<(), ScriptResolveError> {
    for stub in stubs {
        for response in &mut stub.responses {
            if let Some(script) = response_script_mut(response) {
                resolve_response_script(script, registry, default_engine, base)?;
            }
        }
    }
    Ok(())
}

/// Borrow the `_rift.script` config out of a stub response, if it has one — covers both the
/// `is` response's optional `_rift` extension and the script-only `RiftScript` response.
fn response_script_mut(response: &mut StubResponse) -> Option<&mut RiftScriptConfig> {
    match response {
        StubResponse::Is {
            rift: Some(rift), ..
        } => rift.script.as_mut(),
        StubResponse::RiftScript { rift, .. } => rift.script.as_mut(),
        _ => None,
    }
}

fn validate_source_count(script: &RiftScriptConfig) -> Result<(), ScriptResolveError> {
    if script.has_valid_source() {
        Ok(())
    } else {
        Err(ScriptResolveError::InvalidSourceCount {
            found: script.source_count(),
        })
    }
}

/// Resolve a single response-level script: validate its source count, then either follow `ref:`
/// into the (already-resolved) registry or resolve it as a leaf (`code:`/`file:`).
fn resolve_response_script(
    script: &mut RiftScriptConfig,
    registry: &HashMap<String, RiftScriptConfig>,
    default_engine: &str,
    base: &ScriptBaseDir,
) -> Result<(), ScriptResolveError> {
    validate_source_count(script)?;
    if let Some(ref_name) = script.ref_name.clone() {
        let target = registry
            .get(&ref_name)
            .ok_or_else(|| ScriptResolveError::UnknownRef(ref_name.clone()))?;
        script.code = target.code.clone();
        script.component = target.component.clone();
        script.engine = target.engine.clone();
        // A wasm entry's export and knobs come with it, unless the response sets its own.
        if script.export.is_none() {
            script.export = target.export.clone();
        }
        if script.knobs.is_none() {
            script.knobs = target.knobs.clone();
        }
        if script.instance.is_none() {
            script.instance = target.instance.clone();
        }
        // `ref` fully collapses into `code`/`engine` — clearing it keeps `source_count() == 1`
        // for any post-resolution re-check (e.g. `validate_stubs`, run after resolution in the
        // admin-API handlers) and matches the resolved leaf's shape below.
        script.ref_name = None;
        return Ok(());
    }
    resolve_leaf(script, default_engine, base)
}

/// Resolve a leaf script config (`code:` or `file:`, never `ref:`): load `file:` content into
/// `code` if needed, then normalize `engine` to the effective value: explicit, else inferred from
/// the file extension, else the imposter's `defaultEngine` (issue #1159). Writing it back keeps a
/// persisted or replayed imposter unambiguous.
///
/// `file` is cleared once its content is loaded into `code` — resolution is meant to collapse
/// every source down to a plain `code`/`engine` pair, so everything downstream (validation,
/// execution, the compiled-script cache) only ever sees an inline script, and a repeated
/// `source_count()` check (post-resolution) still sees exactly one source.
fn resolve_leaf(
    script: &mut RiftScriptConfig,
    default_engine: &str,
    base: &ScriptBaseDir,
) -> Result<(), ScriptResolveError> {
    if script.engine.is_none() {
        // A component reference is a wasm component whatever the imposter's default (issue #1757).
        script.engine = Some(if script.component.is_some() {
            "wasm".to_owned()
        } else {
            infer_engine(script.file.as_deref(), default_engine)
        });
    }
    if let Some(file) = script.file.take() {
        // A component is binary: its bytes travel in `code` as base64 (issue #1751), so a persisted
        // or saved imposter still carries it.
        let wasm = script
            .engine
            .as_deref()
            .is_some_and(crate::scripting::ScriptEngineKind::is_wasm);
        let bytes = read_script_file(&file, base)?;
        script.code = Some(if wasm {
            use base64::Engine as _;
            base64::engine::general_purpose::STANDARD.encode(&bytes)
        } else {
            String::from_utf8(bytes).map_err(|e| ScriptResolveError::FileRead {
                path: file.clone(),
                source: std::io::Error::new(std::io::ErrorKind::InvalidData, e),
            })?
        });
    }
    Ok(())
}

/// Infer the engine from a `file:` path's extension; `.rhai`/`.js`/`.wasm` map to their engines,
/// `.lua` to `lua` (refused later, with the removed-engine message), and anything else (or no
/// `file`, i.e. inline `code:`) takes `default_engine`. The extension outranks the default on
/// purpose: a `.rhai` file under `defaultEngine: "javascript"` is still Rhai.
fn infer_engine(file: Option<&str>, default_engine: &str) -> String {
    use crate::scripting::{EngineNameError, ScriptEngineKind};
    let ext = file
        .and_then(|f| Path::new(f).extension())
        .and_then(|e| e.to_str());
    match ext.and_then(ScriptEngineKind::from_extension) {
        Some(Ok(kind)) => kind.as_str(),
        Some(Err(EngineNameError::Removed)) => "lua",
        Some(Err(EngineNameError::Unknown(_))) | None => default_engine,
    }
    .to_string()
}

/// Read a `file:` script's content per `base`. For the escape-checked roots (`ScriptsDir` /
/// `DatadirRelative`) the path is resolved AND canonicalized-under-root BEFORE any read (issue
/// #356 B5), so a symlink inside the root that points outside is rejected without its target ever
/// being read, and a canonicalize failure fails closed (reject) rather than open.
fn read_script_file(file: &str, base: &ScriptBaseDir) -> Result<Vec<u8>, ScriptResolveError> {
    match base {
        ScriptBaseDir::Unconfigured => Err(ScriptResolveError::ScriptsDirNotConfigured(
            file.to_string(),
        )),
        // Explicit --configfile: human-authored, any path allowed (no escape check).
        ScriptBaseDir::ConfigRelative(dir) => {
            let path = dir.join(file);
            std::fs::read(&path).map_err(|source| ScriptResolveError::FileRead {
                path: file.to_string(),
                source,
            })
        }
        // Escape-checked roots: resolve strictly under `root`, then read the vetted path.
        ScriptBaseDir::ScriptsDir(root) | ScriptBaseDir::DatadirRelative(root) => {
            let vetted = resolve_within_root(root, file)?;
            std::fs::read(&vetted).map_err(|source| ScriptResolveError::FileRead {
                path: file.to_string(),
                source,
            })
        }
    }
}

/// Resolve `file` strictly under `root`, returning the vetted on-disk path to read, or an error —
/// WITHOUT reading the file (issue #356 B5, fail-closed). In order:
///  1. lexically reject an absolute path or a `..` that climbs above `root` ([`safe_join`]);
///  2. canonicalize the candidate and require it to stay under the canonical `root` — this
///     follows symlinks, so a link inside the root pointing outside resolves outside and is
///     rejected before its target is read;
///  3. if canonicalization fails, distinguish a genuinely-missing file (its parent still resolves
///     under the root → surface a `FileRead` so the message names the missing file) from anything
///     else (a broken/escaping symlink, an unreadable parent) → reject as `PathEscape`. Either way
///     the target is never read here.
fn resolve_within_root(root: &Path, file: &str) -> Result<PathBuf, ScriptResolveError> {
    let candidate = safe_join(root, file)?;
    let canon_root = root
        .canonicalize()
        .map_err(|source| ScriptResolveError::FileRead {
            path: file.to_string(),
            source,
        })?;
    match candidate.canonicalize() {
        Ok(canon_candidate) => {
            if canon_candidate.starts_with(&canon_root) {
                Ok(canon_candidate)
            } else {
                Err(ScriptResolveError::PathEscape(file.to_string()))
            }
        }
        Err(source) => {
            // The candidate itself doesn't resolve. If its PARENT canonicalizes under the root
            // it's simply a missing file (safe to report as such); otherwise fail closed.
            let parent_ok = candidate
                .parent()
                .and_then(|p| p.canonicalize().ok())
                .is_some_and(|cp| cp.starts_with(&canon_root));
            if parent_ok {
                Err(ScriptResolveError::FileRead {
                    path: file.to_string(),
                    source,
                })
            } else {
                Err(ScriptResolveError::PathEscape(file.to_string()))
            }
        }
    }
}

/// Lexically join `root` with `rel`, rejecting any absolute path or `..` component that would
/// climb above `root` — computed purely from path components (no filesystem access), so an
/// escaping reference is rejected before the file is ever opened.
fn safe_join(root: &Path, rel: &str) -> Result<PathBuf, ScriptResolveError> {
    let mut joined = root.to_path_buf();
    let mut depth: i32 = 0;
    for component in Path::new(rel).components() {
        match component {
            Component::Normal(part) => {
                joined.push(part);
                depth += 1;
            }
            Component::CurDir => {}
            Component::ParentDir => {
                depth -= 1;
                if depth < 0 {
                    return Err(ScriptResolveError::PathEscape(rel.to_string()));
                }
                joined.pop();
            }
            Component::RootDir | Component::Prefix(_) => {
                return Err(ScriptResolveError::PathEscape(rel.to_string()));
            }
        }
    }
    Ok(joined)
}

/// What a script running in an imposter may read from disk at request time (issue #1611): the
/// modules a Mountebank-style hook loads with `require()`.
///
/// This is the runtime counterpart of [`ScriptBaseDir`], which vets a `file:` path once, at
/// admission. A `require` path is computed by code on every request, so it is vetted here, each
/// time it is used. The mode belongs to the running imposter and is chosen by the door that wrote
/// it; it is never part of [`ImposterConfig`], so it is never serialized, never a `stub_key` input
/// and never in the bytes an embedder replicates.
///
/// None of this is a sandbox: `--allowInjection` also admits `shellTransform`, which runs host
/// commands. Confinement keeps a network-authored script from reading the host's files by the
/// route that looks harmless.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub enum ScriptFileAccess {
    /// A network-authored door with `--scripts-dir`: only modules under this root load.
    Confined(PathBuf),
    /// A network-authored door without `--scripts-dir`, and any embedder that has not chosen:
    /// every `require` is refused.
    #[default]
    Refused,
    /// A local, human-authored door (`--configfile`, the in-process FFI APIs): any path, a relative
    /// one joined to `base`. Its author already has the filesystem.
    Trusted { base: PathBuf },
}

impl ScriptFileAccess {
    /// The access a network-authored door grants: confined to `scripts_dir`, or refused without
    /// one.
    #[must_use]
    pub fn network(scripts_dir: Option<&Path>) -> Self {
        scripts_dir.map_or(Self::Refused, |dir| Self::Confined(dir.to_path_buf()))
    }

    /// The access a local, human-authored door grants: unconfined, with a relative path joined to
    /// the process's working directory, as `require` has always resolved one.
    #[must_use]
    pub fn trusted_cwd() -> Self {
        // Domain-equivalent fallback, not a swallow: `.` is the working directory, so a relative
        // path still resolves against it when the absolute form cannot be read.
        let base = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
        Self::Trusted { base }
    }

    /// The access that allows no more than either (issue #1645): what a running imposter's scripts
    /// keep when a door writes some of its stubs and leaves the rest. Two different roots allow
    /// nothing in common, so they meet at [`Self::Refused`]; two trusted doors keep `other`'s base.
    #[must_use]
    pub fn meet(&self, other: &Self) -> Self {
        match (self, other) {
            (Self::Trusted { .. }, other) => other.clone(),
            (narrower, Self::Trusted { .. }) => narrower.clone(),
            (Self::Confined(a), Self::Confined(b)) if a == b => self.clone(),
            _ => Self::Refused,
        }
    }

    /// Whether `other` allows no more than `self`, so an imposter's access can become `other` in
    /// place without any of its stubs gaining access (issue #1645).
    #[must_use]
    pub fn admits(&self, other: &Self) -> bool {
        self.meet(other) == *other
    }
}

/// Why [`resolve_confined`] refused a path.
#[derive(Debug, thiserror::Error)]
pub(crate) enum ConfinedPathError {
    /// The path is outside the root, or reaches outside through a link. One answer for every such
    /// path, whether or not anything exists there.
    #[error("outside the root")]
    Outside,
    /// The path is inside the root and nothing is there.
    #[error("no such file under the root")]
    NotFound,
    /// The root itself cannot be resolved.
    #[error("the root cannot be resolved: {0}")]
    Root(std::io::Error),
    /// The vetted file could not be opened or read (see [`read_confined`]).
    #[error("{0}")]
    Read(std::io::Error),
}

/// Resolve `request` to a file strictly under `root`, without reading it (issue #1611).
///
/// A relative `request` joins `base` (the root itself, or the directory of the module asking); an
/// absolute one is taken as written. Either way the path is first normalised lexically, and one
/// outside the root is refused before the filesystem is touched. A path inside it is then
/// canonicalised and must still be under the canonical root, so a link inside the root that
/// points out is refused as well. Every refusal of a path outside the root is the same
/// [`ConfinedPathError::Outside`], so the answer never depends on what exists outside it.
///
/// The returned path is canonical; read that one.
pub(crate) fn resolve_confined(
    root: &Path,
    base: &Path,
    request: &str,
) -> Result<PathBuf, ConfinedPathError> {
    let lexical_root =
        normalize_lexically(&std::path::absolute(root).map_err(ConfinedPathError::Root)?);
    let canonical_root = lexical_root
        .canonicalize()
        .map_err(ConfinedPathError::Root)?;
    let requested = Path::new(request);
    let joined = if requested.is_absolute() {
        requested.to_path_buf()
    } else {
        std::path::absolute(base)
            .map_err(ConfinedPathError::Root)?
            .join(requested)
    };
    let candidate = normalize_lexically(&joined);
    // Either spelling of the root: a module's own directory is canonical, a path written into a
    // script usually is not.
    if !candidate.starts_with(&lexical_root) && !candidate.starts_with(&canonical_root) {
        return Err(ConfinedPathError::Outside);
    }
    match candidate.canonicalize() {
        Ok(canonical) if canonical.starts_with(&canonical_root) => Ok(canonical),
        Ok(_) => Err(ConfinedPathError::Outside),
        Err(e) => {
            // A link whose target is missing is refused like any link out, never reported as a
            // missing file: that would say whether its target exists.
            let is_link = candidate
                .symlink_metadata()
                .is_ok_and(|meta| meta.file_type().is_symlink());
            let parent_inside = candidate
                .parent()
                .and_then(|parent| parent.canonicalize().ok())
                .is_some_and(|parent| parent.starts_with(&canonical_root));
            if !is_link && parent_inside && e.kind() == std::io::ErrorKind::NotFound {
                Err(ConfinedPathError::NotFound)
            } else {
                Err(ConfinedPathError::Outside)
            }
        }
    }
}

/// Read the file [`resolve_confined`] returned as `canonical` for `request`, failing closed if what
/// is opened is not that file (issues #1611, #1617).
///
/// A link swapped into the path between resolving it and opening it would let the open reach a
/// file outside the root. So the file is opened first, `request` is resolved again, and the read
/// goes ahead only when the second resolution is the same path and names the very file that is
/// open (same device and inode on Unix). Off Unix that identity check is path-only: the second
/// resolution must land on the same canonical path. Anything else is
/// [`ConfinedPathError::Outside`], the same answer as any other path out of the root.
///
/// Only a regular file is read. The open does not block (`O_NONBLOCK` on Unix), so a FIFO or a
/// device, under the root or swapped in during the race, cannot hold a worker at `open`, and
/// anything that is not a regular file once open, a directory included, is refused as
/// [`ConfinedPathError::Outside`] before it is resolved again or read. Links inside the root are
/// still followed.
// Only the JavaScript engine's `require()` reads a confined file whole; a `lookup` CSV opens it with
// [`open_confined`] and reads it under the body limits (issue #1654).
#[cfg_attr(not(feature = "javascript"), allow(dead_code))]
pub(crate) fn read_confined(
    root: &Path,
    base: &Path,
    request: &str,
    canonical: &Path,
) -> Result<String, ConfinedPathError> {
    use std::io::Read;
    let mut file = open_confined(root, base, request, canonical)?;
    let mut text = String::new();
    file.read_to_string(&mut text)
        .map_err(ConfinedPathError::Read)?;
    Ok(text)
}

/// [`read_confined`]'s checks, returning the vetted file open and unread, for a caller that reads
/// it under limits of its own (a `lookup` CSV, issue #1654).
pub(crate) fn open_confined(
    root: &Path,
    base: &Path,
    request: &str,
    canonical: &Path,
) -> Result<std::fs::File, ConfinedPathError> {
    let file = open_without_blocking(canonical).map_err(ConfinedPathError::Read)?;
    let opened = file.metadata().map_err(ConfinedPathError::Read)?;
    if !opened.is_file() {
        return Err(ConfinedPathError::Outside);
    }
    let again = resolve_confined(root, base, request)?;
    // Fails closed: a path that no longer resolves to a readable file was changed under the read.
    let current = std::fs::metadata(&again).map_err(|_| ConfinedPathError::Outside)?;
    if again != canonical || !same_file(&opened, &current) {
        return Err(ConfinedPathError::Outside);
    }
    Ok(file)
}

/// Open `path` for reading without waiting on it: a FIFO with no writer, or a device, would
/// otherwise block `open` itself. `O_NONBLOCK` changes nothing for a regular file's reads.
#[cfg(unix)]
fn open_without_blocking(path: &Path) -> std::io::Result<std::fs::File> {
    use std::os::unix::fs::OpenOptionsExt;
    std::fs::OpenOptions::new()
        .read(true)
        .custom_flags(libc::O_NONBLOCK)
        .open(path)
}

#[cfg(not(unix))]
fn open_without_blocking(path: &Path) -> std::io::Result<std::fs::File> {
    std::fs::File::open(path)
}

/// Whether two metadata reads describe the same file.
#[cfg(unix)]
fn same_file(a: &std::fs::Metadata, b: &std::fs::Metadata) -> bool {
    use std::os::unix::fs::MetadataExt;
    a.dev() == b.dev() && a.ino() == b.ino()
}

/// Off Unix there is no stable file identity in `std`; the second resolution above still has to
/// land on the same canonical path.
#[cfg(not(unix))]
fn same_file(_: &std::fs::Metadata, _: &std::fs::Metadata) -> bool {
    true
}

/// Why a `lookup` behavior may not read the CSV file its `fromDataSource.csv.path` names (issue
/// #1617). The path is the one the config wrote; nothing in a message depends on what exists
/// outside `--scripts-dir`.
#[derive(Debug, thiserror::Error)]
pub enum LookupFileError {
    /// The path is absolute outside the root, climbs out of it with `..`, or reaches out through a
    /// link. One answer for all of them, whether or not anything exists there.
    #[error("lookup CSV path '{0}' is outside --scripts-dir")]
    Outside(String),
    /// The imposter was written by a network door and the server has no `--scripts-dir`.
    #[error("lookup CSV path '{0}' cannot be read: no --scripts-dir is configured")]
    NoRoot(String),
    /// `--scripts-dir` itself cannot be resolved.
    #[error("lookup CSV path '{path}' cannot be read: --scripts-dir cannot be resolved: {source}")]
    Root {
        path: String,
        #[source]
        source: std::io::Error,
    },
    /// The path is allowed and nothing readable is there (yet).
    #[error("lookup CSV path '{path}' cannot be read: {source}")]
    Unreadable {
        path: String,
        #[source]
        source: std::io::Error,
    },
}

impl LookupFileError {
    /// Whether a config door refuses a lookup that fails this way. A missing file is admitted, as
    /// in Mountebank: the file may be written after the imposter. Every other failure is a path
    /// the imposter could never read.
    #[must_use]
    pub fn refuses_admission(&self) -> bool {
        !matches!(self, Self::Unreadable { .. })
    }
}

/// The file a `lookup` behavior's CSV `path` names, under the imposter's [`ScriptFileAccess`]
/// (issue #1617). Mountebank reads any path; Rift reads, for an imposter written by a network door
/// (the admin API, `--datadir`, an `https:` source), only files under `--scripts-dir`, a relative
/// path from there, and none without it. A local door's imposter ([`ScriptFileAccess::Trusted`])
/// reads any path, a relative one from its base, as before.
///
/// The result is canonical, so it is also the cache key. Read it with [`read_lookup_file`].
///
/// # Errors
/// See [`LookupFileError`].
pub fn resolve_lookup_path(
    path: &str,
    access: &ScriptFileAccess,
) -> Result<PathBuf, LookupFileError> {
    match access {
        ScriptFileAccess::Refused => Err(LookupFileError::NoRoot(path.to_string())),
        ScriptFileAccess::Confined(root) => {
            resolve_confined(root, root, path).map_err(|e| lookup_error(path, e))
        }
        ScriptFileAccess::Trusted { base } => {
            base.join(path)
                .canonicalize()
                .map_err(|source| LookupFileError::Unreadable {
                    path: path.to_string(),
                    source,
                })
        }
    }
}

/// Read the file [`resolve_lookup_path`] resolved `path` to, re-checking it under a confined
/// access at the moment of the read ([`read_confined`]), since files and links can change after
/// the imposter was admitted.
///
/// # Errors
/// See [`LookupFileError`].
pub fn read_lookup_file(
    path: &str,
    resolved: &Path,
    access: &ScriptFileAccess,
) -> Result<String, LookupFileError> {
    use std::io::Read;
    let mut text = String::new();
    open_lookup_file(path, resolved, access)?
        .read_to_string(&mut text)
        .map_err(|source| LookupFileError::Unreadable {
            path: path.to_string(),
            source,
        })?;
    Ok(text)
}

/// [`read_lookup_file`]'s checks, returning the file open and unread, so the caller can read it
/// under the body limits (issue #1654).
///
/// # Errors
/// See [`LookupFileError`].
pub fn open_lookup_file(
    path: &str,
    resolved: &Path,
    access: &ScriptFileAccess,
) -> Result<std::fs::File, LookupFileError> {
    match access {
        ScriptFileAccess::Refused => Err(LookupFileError::NoRoot(path.to_string())),
        ScriptFileAccess::Confined(root) => {
            open_confined(root, root, path, resolved).map_err(|e| lookup_error(path, e))
        }
        // Opened without blocking and refused unless a regular file, as a confined read is: a
        // FIFO or a device would otherwise hold the reading thread (issue #1654).
        ScriptFileAccess::Trusted { .. } => {
            let unreadable = |source| LookupFileError::Unreadable {
                path: path.to_string(),
                source,
            };
            let file = open_without_blocking(resolved).map_err(unreadable)?;
            let regular = file.metadata().map_err(unreadable)?.is_file();
            if !regular {
                return Err(unreadable(std::io::Error::new(
                    std::io::ErrorKind::InvalidInput,
                    "not a regular file",
                )));
            }
            Ok(file)
        }
    }
}

fn lookup_error(path: &str, e: ConfinedPathError) -> LookupFileError {
    let path = path.to_string();
    match e {
        ConfinedPathError::Outside => LookupFileError::Outside(path),
        ConfinedPathError::NotFound => LookupFileError::Unreadable {
            path,
            source: std::io::Error::new(
                std::io::ErrorKind::NotFound,
                "no such file under --scripts-dir",
            ),
        },
        ConfinedPathError::Root(source) => LookupFileError::Root { path, source },
        ConfinedPathError::Read(source) => LookupFileError::Unreadable { path, source },
    }
}

/// Refuse a stub whose `lookup` behaviors name a CSV file its imposter could never read under
/// `access` (issue #1617): a path outside `--scripts-dir`, or any path when a network door has no
/// `--scripts-dir`. Run by the admin API's write doors with the access they give the imposter.
///
/// Deliberately not part of [`crate::imposter::admission_check`] or any `Deserialize`: what is
/// readable depends on the door and the server's flags, not on the config, so a stored or replayed
/// config is never refused for it. Such an imposter loads and its lookup is refused when it runs,
/// by [`resolve_lookup_path`].
///
/// # Errors
/// The first refused lookup's message.
pub fn check_lookup_paths(stubs: &[Stub], access: &ScriptFileAccess) -> Result<(), String> {
    for path in stubs.iter().flat_map(executed_lookup_paths) {
        if let Err(e) = resolve_lookup_path(&path, access)
            && e.refuses_admission()
        {
            return Err(e.to_string());
        }
    }
    Ok(())
}

/// The CSV paths of the `lookup` behaviors a stub runs. Only the response types that run their
/// behaviors count; a `fault` or `_rift.script` response's behaviors are ignored, so never read.
fn executed_lookup_paths(stub: &Stub) -> Vec<String> {
    stub.responses
        .iter()
        .filter(|response| {
            matches!(
                response,
                StubResponse::Is { .. } | StubResponse::Inject { .. } | StubResponse::Proxy { .. }
            )
        })
        .filter_map(StubResponse::behaviors_block)
        .filter_map(serde_json::Value::as_array)
        // Domain-optional: a program that does not parse is refused by the doors' behaviors check
        // and never runs, so it reads no file.
        .filter_map(|elements| crate::behaviors::BehaviorProgram::from_elements(elements).ok())
        .flat_map(|program| program.steps)
        .filter_map(|step| match step {
            crate::behaviors::BehaviorStep::Lookup(lookup) => {
                Some(lookup.from_data_source.csv.path)
            }
            _ => None,
        })
        .collect()
}

/// `path` with every `.` dropped and every `..` applied to what precedes it, computed from its
/// components alone. A `..` at the top stays at the top, as the filesystem treats it.
fn normalize_lexically(path: &Path) -> PathBuf {
    let mut normalized = PathBuf::new();
    for component in path.components() {
        match component {
            Component::CurDir => {}
            Component::ParentDir => {
                normalized.pop();
            }
            other => normalized.push(other),
        }
    }
    normalized
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::imposter::types::{RiftConfig, RiftResponseExtension, Stub};

    fn script(code: Option<&str>, file: Option<&str>, ref_name: Option<&str>) -> RiftScriptConfig {
        RiftScriptConfig {
            engine: None,
            code: code.map(str::to_string),
            file: file.map(str::to_string),
            ref_name: ref_name.map(str::to_string),
            ..Default::default()
        }
    }

    fn stub_with_script(script_config: RiftScriptConfig) -> Stub {
        Stub {
            name: None,
            id: None,
            route_pattern: None,
            predicates: vec![],
            responses: vec![StubResponse::RiftScript {
                rift: Box::new(RiftResponseExtension {
                    dataset: None,
                    conditional: None,
                    fault: None,
                    script: Some(script_config),
                    templated: false,
                    state_ops: Vec::new(),
                }),
                ignored_behaviors: None,
            }],
            scenario_name: None,
            required_scenario_state: None,
            new_scenario_state: None,
            space: None,
            recorded_from: None,
            recorded_at: None,
            verify: None,
        }
    }

    fn extract_script(config: &ImposterConfig) -> &RiftScriptConfig {
        match &config.stubs[0].responses[0] {
            StubResponse::RiftScript { rift, .. } => rift.script.as_ref().unwrap(),
            other => panic!("expected RiftScript, got {other:?}"),
        }
    }

    #[test]
    fn inline_code_resolves_with_default_engine() {
        let mut config = ImposterConfig {
            stubs: vec![stub_with_script(script(
                Some("fn respond() {}"),
                None,
                None,
            ))],
            ..Default::default()
        };
        resolve_scripts(&mut config, &ScriptBaseDir::Unconfigured).unwrap();
        let resolved = extract_script(&config);
        assert_eq!(resolved.code.as_deref(), Some("fn respond() {}"));
        assert_eq!(resolved.engine.as_deref(), Some("rhai"));
    }

    fn with_default_engine(config: &mut ImposterConfig, engine: &str) {
        config.rift = Some(
            serde_json::from_value(serde_json::json!({
                "scriptEngine": { "defaultEngine": engine }
            }))
            .expect("rift config"),
        );
    }

    /// Issue #1159: the imposter's `defaultEngine` was parsed and never read; inline code always
    /// resolved to Rhai.
    #[test]
    fn inline_code_takes_the_imposters_default_engine() {
        let mut config = ImposterConfig {
            stubs: vec![stub_with_script(script(
                Some("function respond() {}"),
                None,
                None,
            ))],
            ..Default::default()
        };
        with_default_engine(&mut config, "javascript");
        resolve_scripts(&mut config, &ScriptBaseDir::Unconfigured).unwrap();
        assert_eq!(
            extract_script(&config).engine.as_deref(),
            Some("javascript")
        );
    }

    #[test]
    fn an_explicit_engine_outranks_the_default() {
        let mut explicit = script(Some("fn respond() {}"), None, None);
        explicit.engine = Some("rhai".to_owned());
        let mut config = ImposterConfig {
            stubs: vec![stub_with_script(explicit)],
            ..Default::default()
        };
        with_default_engine(&mut config, "javascript");
        resolve_scripts(&mut config, &ScriptBaseDir::Unconfigured).unwrap();
        assert_eq!(extract_script(&config).engine.as_deref(), Some("rhai"));
    }

    #[test]
    fn a_file_extension_outranks_the_default() {
        let root = tempfile::tempdir().expect("tempdir");
        std::fs::write(root.path().join("s.rhai"), "fn respond() {}").expect("write");
        let mut config = ImposterConfig {
            stubs: vec![stub_with_script(script(None, Some("s.rhai"), None))],
            ..Default::default()
        };
        with_default_engine(&mut config, "javascript");
        resolve_scripts(
            &mut config,
            &ScriptBaseDir::ScriptsDir(root.path().to_path_buf()),
        )
        .unwrap();
        assert_eq!(extract_script(&config).engine.as_deref(), Some("rhai"));
    }

    #[test]
    fn a_file_without_a_known_extension_takes_the_default() {
        let root = tempfile::tempdir().expect("tempdir");
        std::fs::write(root.path().join("s.txt"), "function respond() {}").expect("write");
        let mut config = ImposterConfig {
            stubs: vec![stub_with_script(script(None, Some("s.txt"), None))],
            ..Default::default()
        };
        with_default_engine(&mut config, "javascript");
        resolve_scripts(
            &mut config,
            &ScriptBaseDir::ScriptsDir(root.path().to_path_buf()),
        )
        .unwrap();
        assert_eq!(
            extract_script(&config).engine.as_deref(),
            Some("javascript")
        );
    }

    /// The stub doors pass the live imposter's default; this is the seam they call.
    #[test]
    fn stub_resolution_uses_the_default_it_is_given() {
        let mut stubs = vec![stub_with_script(script(Some("x"), None, None))];
        resolve_stub_scripts(
            &mut stubs,
            &HashMap::new(),
            "javascript",
            &ScriptBaseDir::Unconfigured,
        )
        .unwrap();
        let config = ImposterConfig {
            stubs,
            ..Default::default()
        };
        assert_eq!(
            extract_script(&config).engine.as_deref(),
            Some("javascript")
        );
    }

    /// A stale `"lua"` default with no engine-less script is never used, so it is not refused at
    /// resolution — such configs load today and must keep loading.
    #[test]
    fn an_unused_unknown_default_is_not_refused() {
        let mut explicit = script(Some("fn respond() {}"), None, None);
        explicit.engine = Some("rhai".to_owned());
        let mut config = ImposterConfig {
            stubs: vec![stub_with_script(explicit)],
            ..Default::default()
        };
        with_default_engine(&mut config, "lua");
        resolve_scripts(&mut config, &ScriptBaseDir::Unconfigured).unwrap();
    }

    #[test]
    fn zero_sources_is_rejected() {
        let mut config = ImposterConfig {
            stubs: vec![stub_with_script(script(None, None, None))],
            ..Default::default()
        };
        let err = resolve_scripts(&mut config, &ScriptBaseDir::Unconfigured).unwrap_err();
        assert!(matches!(
            err,
            ScriptResolveError::InvalidSourceCount { found: 0 }
        ));
    }

    #[test]
    fn multiple_sources_is_rejected() {
        let mut config = ImposterConfig {
            stubs: vec![stub_with_script(script(Some("x"), Some("y.rhai"), None))],
            ..Default::default()
        };
        let err = resolve_scripts(&mut config, &ScriptBaseDir::Unconfigured).unwrap_err();
        assert!(matches!(
            err,
            ScriptResolveError::InvalidSourceCount { found: 2 }
        ));
    }

    #[test]
    fn file_resolves_relative_to_config_dir_and_infers_engine() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("script.lua"), "return {inject=false}").unwrap();
        let mut config = ImposterConfig {
            stubs: vec![stub_with_script(script(None, Some("script.lua"), None))],
            ..Default::default()
        };
        resolve_scripts(
            &mut config,
            &ScriptBaseDir::ConfigRelative(dir.path().to_path_buf()),
        )
        .unwrap();
        let resolved = extract_script(&config);
        assert_eq!(resolved.code.as_deref(), Some("return {inject=false}"));
        assert_eq!(resolved.engine.as_deref(), Some("lua"));
    }

    #[test]
    fn missing_file_is_an_error() {
        let dir = tempfile::tempdir().unwrap();
        let mut config = ImposterConfig {
            stubs: vec![stub_with_script(script(None, Some("nope.rhai"), None))],
            ..Default::default()
        };
        let err = resolve_scripts(
            &mut config,
            &ScriptBaseDir::ConfigRelative(dir.path().to_path_buf()),
        )
        .unwrap_err();
        assert!(matches!(err, ScriptResolveError::FileRead { .. }));
    }

    #[test]
    fn scripts_dir_unconfigured_rejects_file_reference() {
        let mut config = ImposterConfig {
            stubs: vec![stub_with_script(script(None, Some("a.rhai"), None))],
            ..Default::default()
        };
        let err = resolve_scripts(&mut config, &ScriptBaseDir::Unconfigured).unwrap_err();
        assert!(matches!(
            err,
            ScriptResolveError::ScriptsDirNotConfigured(_)
        ));
    }

    #[test]
    fn scripts_dir_escape_is_rejected_without_reading() {
        let dir = tempfile::tempdir().unwrap();
        // A real file that a naive join would successfully read if the escape check were
        // skipped — its presence proves the rejection is the `..` check, not a missing file.
        std::fs::write(dir.path().join("secret.rhai"), "SECRET").unwrap();
        let root = dir.path().join("scripts");
        std::fs::create_dir(&root).unwrap();
        let mut config = ImposterConfig {
            stubs: vec![stub_with_script(script(None, Some("../secret.rhai"), None))],
            ..Default::default()
        };
        let err = resolve_scripts(&mut config, &ScriptBaseDir::ScriptsDir(root)).unwrap_err();
        assert!(matches!(err, ScriptResolveError::PathEscape(_)));
    }

    #[test]
    fn scripts_dir_within_root_is_read() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("ok.rhai"), "fn respond() {}").unwrap();
        let mut config = ImposterConfig {
            stubs: vec![stub_with_script(script(None, Some("ok.rhai"), None))],
            ..Default::default()
        };
        resolve_scripts(
            &mut config,
            &ScriptBaseDir::ScriptsDir(dir.path().to_path_buf()),
        )
        .unwrap();
        let resolved = extract_script(&config);
        assert_eq!(resolved.code.as_deref(), Some("fn respond() {}"));
    }

    #[test]
    fn ref_resolves_from_registry() {
        let mut scripts = HashMap::new();
        scripts.insert(
            "failTwice".to_string(),
            script(Some("fn respond() {}"), None, None),
        );
        let mut config = ImposterConfig {
            rift: Some(RiftConfig {
                scripts,
                ..Default::default()
            }),
            stubs: vec![stub_with_script(script(None, None, Some("failTwice")))],
            ..Default::default()
        };
        resolve_scripts(&mut config, &ScriptBaseDir::Unconfigured).unwrap();
        let resolved = extract_script(&config);
        assert_eq!(resolved.code.as_deref(), Some("fn respond() {}"));
        assert_eq!(resolved.engine.as_deref(), Some("rhai"));
    }

    #[test]
    fn ref_resolves_a_file_backed_registry_entry() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("fail-twice.rhai"), "fn respond() {}").unwrap();
        let mut scripts = HashMap::new();
        scripts.insert(
            "failTwice".to_string(),
            script(None, Some("scripts/fail-twice.rhai"), None),
        );
        // Registry file paths resolve the same way as response-level file paths.
        std::fs::create_dir(dir.path().join("scripts")).unwrap();
        std::fs::write(
            dir.path().join("scripts").join("fail-twice.rhai"),
            "fn respond() {}",
        )
        .unwrap();
        let mut config = ImposterConfig {
            rift: Some(RiftConfig {
                scripts,
                ..Default::default()
            }),
            stubs: vec![stub_with_script(script(None, None, Some("failTwice")))],
            ..Default::default()
        };
        resolve_scripts(
            &mut config,
            &ScriptBaseDir::ConfigRelative(dir.path().to_path_buf()),
        )
        .unwrap();
        let resolved = extract_script(&config);
        assert_eq!(resolved.code.as_deref(), Some("fn respond() {}"));
        assert_eq!(resolved.engine.as_deref(), Some("rhai"));
    }

    /// Issue #1209: `response_script_mut` has two arms, and every other test in this module goes
    /// through `stub_with_script`, which only ever builds the `RiftScript` variant. This covers the
    /// other one — a `_rift.script` written beside an `is` response — so the `Is` arm's resolution
    /// is pinned rather than assumed to behave like its sibling.
    #[test]
    fn a_ref_resolves_on_a_script_written_beside_an_is_response() {
        let mut scripts = HashMap::new();
        scripts.insert(
            "failTwice".to_string(),
            script(Some("fn respond() {}"), None, None),
        );
        let is_with_script = Stub {
            name: None,
            id: None,
            route_pattern: None,
            predicates: vec![],
            responses: vec![StubResponse::new_is(
                crate::imposter::types::IsResponse::new(
                    200,
                    Default::default(),
                    None,
                    Default::default(),
                ),
                None,
                Some(Box::new(RiftResponseExtension {
                    dataset: None,
                    conditional: None,
                    fault: None,
                    script: Some(script(None, None, Some("failTwice"))),
                    templated: false,
                    state_ops: Vec::new(),
                })),
            )],
            scenario_name: None,
            required_scenario_state: None,
            new_scenario_state: None,
            space: None,
            recorded_from: None,
            recorded_at: None,
            verify: None,
        };
        let mut config = ImposterConfig {
            rift: Some(RiftConfig {
                scripts,
                ..Default::default()
            }),
            stubs: vec![is_with_script],
            ..Default::default()
        };

        resolve_scripts(&mut config, &ScriptBaseDir::Unconfigured).unwrap();

        let StubResponse::Is {
            rift: Some(rift), ..
        } = &config.stubs[0].responses[0]
        else {
            panic!("a `_rift` block beside an `is` response must stay on the Is variant");
        };
        let resolved = rift.script.as_ref().expect("script present");
        assert_eq!(resolved.code.as_deref(), Some("fn respond() {}"));
        assert_eq!(resolved.engine.as_deref(), Some("rhai"));
        assert_eq!(resolved.ref_name, None, "the ref must be consumed");
    }

    #[test]
    fn unknown_ref_is_an_error() {
        let mut config = ImposterConfig {
            stubs: vec![stub_with_script(script(None, None, Some("nope")))],
            ..Default::default()
        };
        let err = resolve_scripts(&mut config, &ScriptBaseDir::Unconfigured).unwrap_err();
        assert!(matches!(err, ScriptResolveError::UnknownRef(name) if name == "nope"));
    }

    #[test]
    fn ref_to_ref_is_rejected() {
        let mut scripts = HashMap::new();
        scripts.insert("a".to_string(), script(None, None, Some("b")));
        scripts.insert("b".to_string(), script(Some("code"), None, None));
        let mut config = ImposterConfig {
            rift: Some(RiftConfig {
                scripts,
                ..Default::default()
            }),
            stubs: vec![stub_with_script(script(None, None, Some("a")))],
            ..Default::default()
        };
        let err = resolve_scripts(&mut config, &ScriptBaseDir::Unconfigured).unwrap_err();
        assert!(matches!(err, ScriptResolveError::RefChain(name) if name == "a"));
    }

    // Issue #356 B5: an ABSOLUTE `file:` path under a `--scripts-dir` root is rejected lexically
    // (the RootDir component in `safe_join`) before any read — `/etc/passwd` never opened.
    #[test]
    fn scripts_dir_absolute_path_is_rejected_without_reading() {
        let dir = tempfile::tempdir().unwrap();
        // A real, readable absolute target outside the root — proves the rejection is the
        // absolute-path check, not a missing file.
        let secret = dir.path().join("secret.rhai");
        std::fs::write(&secret, "SECRET").unwrap();
        let root = dir.path().join("scripts");
        std::fs::create_dir(&root).unwrap();
        let mut config = ImposterConfig {
            stubs: vec![stub_with_script(script(
                None,
                Some(secret.to_str().unwrap()),
                None,
            ))],
            ..Default::default()
        };
        let err = resolve_scripts(&mut config, &ScriptBaseDir::ScriptsDir(root)).unwrap_err();
        assert!(
            matches!(err, ScriptResolveError::PathEscape(_)),
            "absolute path must be PathEscape, got {err:?}"
        );
        // Nothing was loaded into `code`.
        assert_eq!(extract_script(&config).code, None);
    }

    // Issue #356 B5: a symlink INSIDE the root pointing OUTSIDE it is rejected (fail-closed) and
    // its target's content is never returned — the canonicalize-before-read guard.
    #[cfg(unix)]
    #[test]
    fn scripts_dir_symlink_escape_is_rejected_without_reading() {
        let dir = tempfile::tempdir().unwrap();
        let secret = dir.path().join("secret.rhai");
        std::fs::write(&secret, "SUPER-SECRET-CONTENTS").unwrap();
        let root = dir.path().join("scripts");
        std::fs::create_dir(&root).unwrap();
        // scripts/leak.rhai -> ../secret.rhai (target is real and outside the root).
        std::os::unix::fs::symlink(&secret, root.join("leak.rhai")).unwrap();

        let mut config = ImposterConfig {
            stubs: vec![stub_with_script(script(None, Some("leak.rhai"), None))],
            ..Default::default()
        };
        let err = resolve_scripts(&mut config, &ScriptBaseDir::ScriptsDir(root)).unwrap_err();
        assert!(
            matches!(err, ScriptResolveError::PathEscape(_)),
            "symlink escape must be PathEscape, got {err:?}"
        );
        // The secret's content must NOT have been read into `code`.
        let resolved = extract_script(&config);
        assert_eq!(resolved.code, None);
    }

    // Issue #356 B2: the datadir base applies the SAME escape rules as `--scripts-dir` — a
    // network-authored `{port}.json` cannot read an absolute or `..`-escaping path.
    #[test]
    fn datadir_relative_rejects_absolute_and_parent_escape() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("secret.rhai"), "SECRET").unwrap();
        let root = dir.path().join("data");
        std::fs::create_dir(&root).unwrap();

        for bad in ["/etc/passwd", "../secret.rhai"] {
            let mut config = ImposterConfig {
                stubs: vec![stub_with_script(script(None, Some(bad), None))],
                ..Default::default()
            };
            let err = resolve_scripts(&mut config, &ScriptBaseDir::DatadirRelative(root.clone()))
                .unwrap_err();
            assert!(
                matches!(err, ScriptResolveError::PathEscape(_)),
                "datadir `{bad}` must be PathEscape, got {err:?}"
            );
            assert_eq!(extract_script(&config).code, None);
        }
    }

    #[test]
    fn datadir_relative_within_root_is_read() {
        let root = tempfile::tempdir().unwrap();
        std::fs::write(root.path().join("ok.rhai"), "fn respond() {}").unwrap();
        let mut config = ImposterConfig {
            stubs: vec![stub_with_script(script(None, Some("ok.rhai"), None))],
            ..Default::default()
        };
        resolve_scripts(
            &mut config,
            &ScriptBaseDir::DatadirRelative(root.path().to_path_buf()),
        )
        .unwrap();
        assert_eq!(
            extract_script(&config).code.as_deref(),
            Some("fn respond() {}")
        );
    }

    // Issue #356: a `.js` file infers the "javascript" engine.
    #[test]
    fn js_file_extension_infers_javascript_engine() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("script.js"),
            "function respond() { return pass(); }",
        )
        .unwrap();
        let mut config = ImposterConfig {
            stubs: vec![stub_with_script(script(None, Some("script.js"), None))],
            ..Default::default()
        };
        resolve_scripts(
            &mut config,
            &ScriptBaseDir::ConfigRelative(dir.path().to_path_buf()),
        )
        .unwrap();
        assert_eq!(
            extract_script(&config).engine.as_deref(),
            Some("javascript")
        );
    }

    // ---- Issue #1611: the runtime resolver behind `require()` ----

    /// A canary tree: `root/` is the confinement root, `outside/` sits beside it.
    struct Confinement {
        _dir: tempfile::TempDir,
        root: PathBuf,
        outside: PathBuf,
    }

    fn confinement() -> Confinement {
        let dir = tempfile::tempdir().expect("tempdir");
        let base = dir.path().canonicalize().expect("canonical tempdir");
        let root = base.join("root");
        let outside = base.join("outside");
        std::fs::create_dir_all(root.join("lib")).unwrap();
        std::fs::create_dir_all(&outside).unwrap();
        std::fs::write(root.join("mod.js"), "module.exports = 1;").unwrap();
        std::fs::write(root.join("lib/helper.js"), "module.exports = 2;").unwrap();
        std::fs::write(outside.join("secret.js"), "CANARY_1611").unwrap();
        Confinement {
            _dir: dir,
            root,
            outside,
        }
    }

    fn outside_err(result: Result<PathBuf, ConfinedPathError>) -> bool {
        matches!(result, Err(ConfinedPathError::Outside))
    }

    #[test]
    fn confined_resolves_a_module_in_the_root_by_relative_and_absolute_path() {
        let c = confinement();
        let canonical = c.root.join("mod.js");
        assert_eq!(
            resolve_confined(&c.root, &c.root, "mod.js").unwrap(),
            canonical
        );
        assert_eq!(
            resolve_confined(&c.root, &c.root, "./lib/../mod.js").unwrap(),
            canonical
        );
        assert_eq!(
            resolve_confined(&c.root, &c.root, canonical.to_str().unwrap()).unwrap(),
            canonical
        );
    }

    #[test]
    fn confined_resolves_a_relative_path_from_the_requiring_module_directory() {
        let c = confinement();
        assert_eq!(
            resolve_confined(&c.root, &c.root.join("lib"), "../mod.js").unwrap(),
            c.root.join("mod.js")
        );
        assert_eq!(
            resolve_confined(&c.root, &c.root.join("lib"), "helper.js").unwrap(),
            c.root.join("lib/helper.js")
        );
    }

    #[test]
    fn confined_refuses_absolute_dotdot_and_symlink_escapes() {
        let c = confinement();
        assert!(outside_err(resolve_confined(
            &c.root,
            &c.root,
            "/etc/hosts"
        )));
        assert!(outside_err(resolve_confined(
            &c.root,
            &c.root,
            "../outside/secret.js"
        )));
        assert!(outside_err(resolve_confined(
            &c.root,
            &c.root.join("lib"),
            "../../outside/secret.js"
        )));
        assert!(outside_err(resolve_confined(
            &c.root,
            &c.root,
            c.outside.join("secret.js").to_str().unwrap()
        )));
        #[cfg(unix)]
        {
            std::os::unix::fs::symlink(c.outside.join("secret.js"), c.root.join("link.js"))
                .unwrap();
            std::os::unix::fs::symlink(&c.outside, c.root.join("linkdir")).unwrap();
            std::os::unix::fs::symlink(c.outside.join("gone.js"), c.root.join("dangling.js"))
                .unwrap();
            assert!(outside_err(resolve_confined(&c.root, &c.root, "link.js")));
            assert!(outside_err(resolve_confined(
                &c.root,
                &c.root,
                "linkdir/secret.js"
            )));
            assert!(outside_err(resolve_confined(
                &c.root,
                &c.root,
                "linkdir/gone.js"
            )));
            // A dangling link is a link out, not a missing file: that would say its target is gone.
            assert!(outside_err(resolve_confined(
                &c.root,
                &c.root,
                "dangling.js"
            )));
        }
    }

    #[test]
    fn confined_refuses_a_missing_and_an_existing_outside_path_alike() {
        let c = confinement();
        let present = c.outside.join("secret.js");
        let missing = c.outside.join("no-such-file.js");
        for path in [&present, &missing] {
            let err = resolve_confined(&c.root, &c.root, path.to_str().unwrap()).unwrap_err();
            assert_eq!(err.to_string(), "outside the root", "{}", path.display());
        }
    }

    #[test]
    fn confined_reports_a_missing_file_inside_the_root() {
        let c = confinement();
        assert!(matches!(
            resolve_confined(&c.root, &c.root, "lib/missing.js"),
            Err(ConfinedPathError::NotFound)
        ));
    }

    #[test]
    fn confined_accepts_either_spelling_of_a_linked_root() {
        let c = confinement();
        let parent = c.root.parent().unwrap().to_path_buf();
        #[cfg(unix)]
        {
            let alias = parent.join("alias");
            std::os::unix::fs::symlink(&c.root, &alias).unwrap();
            // Written through the alias, read from the canonical root, and the other way round.
            let through_alias = alias.join("mod.js");
            assert_eq!(
                resolve_confined(&alias, &alias, through_alias.to_str().unwrap()).unwrap(),
                c.root.join("mod.js")
            );
            assert_eq!(
                resolve_confined(&alias, &c.root.join("lib"), "helper.js").unwrap(),
                c.root.join("lib/helper.js")
            );
        }
    }

    #[test]
    fn script_file_access_for_a_network_door_is_confined_or_refused() {
        assert_eq!(
            ScriptFileAccess::network(Some(Path::new("/srv/scripts"))),
            ScriptFileAccess::Confined(PathBuf::from("/srv/scripts"))
        );
        assert_eq!(ScriptFileAccess::network(None), ScriptFileAccess::Refused);
        assert_eq!(ScriptFileAccess::default(), ScriptFileAccess::Refused);
    }

    /// A `root/` holding `data.csv`, beside an `outside/` holding `secret.csv` (issue #1617).
    fn lookup_tree() -> (tempfile::TempDir, PathBuf) {
        let dir = tempfile::tempdir().expect("tempdir");
        let base = dir.path().canonicalize().expect("canonical tempdir");
        std::fs::create_dir_all(base.join("root/sub")).expect("mkdir root");
        std::fs::create_dir_all(base.join("outside")).expect("mkdir outside");
        std::fs::write(base.join("root/data.csv"), "id\n1\n").expect("write data");
        std::fs::write(base.join("outside/secret.csv"), "id\n1\n").expect("write secret");
        (dir, base)
    }

    #[test]
    fn a_confined_lookup_resolves_only_under_the_root() {
        let (_dir, base) = lookup_tree();
        let access = ScriptFileAccess::Confined(base.join("root"));
        let inside = base.join("root/data.csv");
        for path in [
            "data.csv",
            "./sub/../data.csv",
            inside.to_str().expect("utf-8"),
        ] {
            assert_eq!(
                resolve_lookup_path(path, &access).expect("inside"),
                inside,
                "{path}"
            );
        }
        let secret = base.join("outside/secret.csv");
        for path in [
            "../outside/secret.csv",
            "sub/../../outside/secret.csv",
            secret.to_str().expect("utf-8"),
            "/etc/shells",
        ] {
            let err = resolve_lookup_path(path, &access).expect_err(path);
            assert!(matches!(err, LookupFileError::Outside(_)), "{path}: {err}");
            assert!(err.refuses_admission());
            assert_eq!(
                err.to_string(),
                format!("lookup CSV path '{path}' is outside --scripts-dir")
            );
        }
        // Missing inside the root: admitted, then unreadable.
        let missing = resolve_lookup_path("later.csv", &access).expect_err("missing");
        assert!(!missing.refuses_admission(), "{missing}");
    }

    #[cfg(unix)]
    #[test]
    fn a_confined_lookup_refuses_a_link_out_of_the_root() {
        let (_dir, base) = lookup_tree();
        let access = ScriptFileAccess::Confined(base.join("root"));
        std::os::unix::fs::symlink(base.join("outside/secret.csv"), base.join("root/link.csv"))
            .expect("file link");
        std::os::unix::fs::symlink(base.join("outside"), base.join("root/dir")).expect("dir link");
        std::os::unix::fs::symlink(base.join("outside/nothing"), base.join("root/dangling.csv"))
            .expect("dangling link");
        for path in ["link.csv", "dir/secret.csv", "dangling.csv"] {
            let err = resolve_lookup_path(path, &access).expect_err(path);
            assert!(matches!(err, LookupFileError::Outside(_)), "{path}: {err}");
        }
    }

    /// The read re-resolves once the file is open, so a path that moved out of the root between
    /// resolving it and reading it is refused rather than read.
    #[cfg(unix)]
    #[test]
    fn a_confined_read_refuses_a_path_that_moved_after_it_was_resolved() {
        let (_dir, base) = lookup_tree();
        let root = base.join("root");
        let access = ScriptFileAccess::Confined(root.clone());
        std::os::unix::fs::symlink(root.join("data.csv"), root.join("swap.csv")).expect("link");
        let resolved = resolve_lookup_path("swap.csv", &access).expect("inside");
        assert_eq!(
            read_lookup_file("swap.csv", &resolved, &access).expect("reads"),
            "id\n1\n"
        );

        std::fs::remove_file(root.join("swap.csv")).expect("unlink");
        std::os::unix::fs::symlink(base.join("outside/secret.csv"), root.join("swap.csv"))
            .expect("relink");
        let err = read_lookup_file("swap.csv", &resolved, &access).expect_err("moved");
        assert!(matches!(err, LookupFileError::Outside(_)), "{err}");

        // The resolved file itself replaced by another file: not the file that was vetted.
        std::fs::remove_file(root.join("swap.csv")).expect("unlink");
        std::fs::rename(root.join("data.csv"), root.join("old.csv")).expect("rename");
        std::fs::write(root.join("data.csv"), "id\n2\n").expect("rewrite");
        std::os::unix::fs::symlink(root.join("old.csv"), root.join("swap.csv")).expect("relink");
        let err = read_lookup_file("swap.csv", &resolved, &access).expect_err("replaced");
        assert!(matches!(err, LookupFileError::Outside(_)), "{err}");
    }

    /// A FIFO under the root would block `open` until a writer came, holding a worker; a directory
    /// would reach the read. Both are refused as fast as any path out of the root.
    #[cfg(unix)]
    #[test]
    fn a_confined_read_refuses_a_fifo_and_a_directory_without_blocking() {
        let (_dir, base) = lookup_tree();
        let root = base.join("root");
        let status = std::process::Command::new("mkfifo")
            .arg(root.join("pipe.csv"))
            .status()
            .expect("run mkfifo");
        assert!(status.success(), "mkfifo");
        let access = ScriptFileAccess::Confined(root.clone());
        for path in ["pipe.csv", "sub"] {
            let resolved = resolve_lookup_path(path, &access).expect("inside the root");
            let (tx, rx) = std::sync::mpsc::channel();
            let worker_access = access.clone();
            std::thread::spawn(move || {
                // The receiver is gone only once the test has already failed on the timeout.
                let _ = tx.send(read_lookup_file(path, &resolved, &worker_access));
            });
            let read = rx
                .recv_timeout(std::time::Duration::from_secs(5))
                .unwrap_or_else(|_| panic!("{path}: the read blocked"));
            let err = read.expect_err(path);
            assert!(matches!(err, LookupFileError::Outside(_)), "{path}: {err}");
        }
    }

    #[test]
    fn a_refused_access_reads_no_lookup_and_a_trusted_one_any() {
        let (_dir, base) = lookup_tree();
        let secret = base.join("outside/secret.csv");
        let err = resolve_lookup_path("data.csv", &ScriptFileAccess::Refused).expect_err("refused");
        assert_eq!(
            err.to_string(),
            "lookup CSV path 'data.csv' cannot be read: no --scripts-dir is configured"
        );
        assert!(err.refuses_admission());

        let trusted = ScriptFileAccess::Trusted {
            base: base.join("root"),
        };
        assert_eq!(
            resolve_lookup_path(secret.to_str().expect("utf-8"), &trusted).expect("any path"),
            secret
        );
        assert_eq!(
            resolve_lookup_path("../outside/secret.csv", &trusted).expect("relative to base"),
            secret
        );
        let missing = resolve_lookup_path("nope.csv", &trusted).expect_err("missing");
        assert!(!missing.refuses_admission(), "{missing}");
    }

    fn lookup_stub(path: &str, response: &str) -> Stub {
        serde_json::from_value(serde_json::json!({
            "responses": [{
                response: if response == "fault" {
                    serde_json::json!("CONNECTION_RESET_BY_PEER")
                } else {
                    serde_json::json!({})
                },
                "_behaviors": { "lookup": {
                    "key": { "from": "path", "using": { "method": "regex", "selector": ".*" } },
                    "fromDataSource": { "csv": { "path": path, "keyColumn": "id" } },
                    "into": "${row}"
                } }
            }]
        }))
        .expect("stub")
    }

    #[test]
    fn check_lookup_paths_refuses_what_the_access_could_never_read() {
        let (_dir, base) = lookup_tree();
        let access = ScriptFileAccess::Confined(base.join("root"));
        assert_eq!(
            check_lookup_paths(&[lookup_stub("data.csv", "is")], &access),
            Ok(())
        );
        assert_eq!(
            check_lookup_paths(&[lookup_stub("later.csv", "is")], &access),
            Ok(()),
            "a file that may be written later is admitted"
        );
        assert_eq!(
            check_lookup_paths(&[lookup_stub("../outside/secret.csv", "is")], &access),
            Err("lookup CSV path '../outside/secret.csv' is outside --scripts-dir".to_string())
        );
        assert!(
            check_lookup_paths(&[lookup_stub("data.csv", "is")], &ScriptFileAccess::Refused)
                .is_err()
        );
        // A fault response's behaviors never run, so they read nothing.
        assert_eq!(
            check_lookup_paths(&[lookup_stub("/etc/shells", "fault")], &access),
            Ok(())
        );
        assert_eq!(
            check_lookup_paths(
                &[lookup_stub("/etc/shells", "is")],
                &ScriptFileAccess::trusted_cwd()
            ),
            Ok(())
        );
    }
}