tapid-runner 0.0.5

Policy-aware package execution foundations for Tapid
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
//! Validated execution requests, runtime identity, and platform argument/environment limits.

use super::*;

#[cfg(windows)]
fn windows_file_identity(path: &Path) -> Result<(u32, u64), ExecutionError> {
    use std::mem::MaybeUninit;
    use std::os::windows::io::AsRawHandle;
    use windows_sys::Win32::Storage::FileSystem::{
        BY_HANDLE_FILE_INFORMATION, GetFileInformationByHandle,
    };

    let file =
        fs::File::open(path).map_err(|error| path_error("trusted Node runtime", path, error))?;
    let mut information = MaybeUninit::<BY_HANDLE_FILE_INFORMATION>::zeroed();
    // SAFETY: `file` keeps a valid owned handle alive for the call and `information` points to
    // writable storage of the exact structure required by `GetFileInformationByHandle`.
    let succeeded = unsafe {
        GetFileInformationByHandle(file.as_raw_handle() as isize, information.as_mut_ptr())
    };
    if succeeded == 0 {
        return Err(path_error(
            "trusted Node runtime identity",
            path,
            std::io::Error::last_os_error(),
        ));
    }
    // SAFETY: a successful API call initialized the complete output structure.
    let information = unsafe { information.assume_init() };
    let file_index =
        (u64::from(information.nFileIndexHigh) << 32) | u64::from(information.nFileIndexLow);
    Ok((information.dwVolumeSerialNumber, file_index))
}

/// Canonical trusted Node executable with its native file identity captured at construction.
#[derive(Clone, Debug, Eq, PartialEq)]
pub(super) struct TrustedNodeRuntime {
    pub(super) path: PathBuf,
    #[cfg(unix)]
    pub(super) identity: NativeIdentity,
    #[cfg(windows)]
    pub(super) volume_serial_number: u32,
    #[cfg(windows)]
    pub(super) file_index: u64,
}

impl TrustedNodeRuntime {
    pub(super) fn checked(path: &Path) -> Result<Self, ExecutionError> {
        let canonical = canonical_path(path, "trusted Node runtime")?;
        if canonical != path || !is_node_executable_name(&canonical) {
            return Err(invalid_request(
                "trusted Node runtime must be canonical and named node or node.exe",
            ));
        }
        let metadata = fs::metadata(&canonical)
            .map_err(|error| path_error("trusted Node runtime", &canonical, error))?;
        if !metadata.is_file() {
            return Err(invalid_request(
                "trusted Node runtime must be a regular file",
            ));
        }
        #[cfg(unix)]
        {
            use std::os::unix::fs::{MetadataExt, PermissionsExt};
            if metadata.permissions().mode() & 0o111 == 0 {
                return Err(invalid_request("trusted Node runtime must be executable"));
            }
            Ok(Self {
                path: canonical,
                identity: NativeIdentity {
                    device: metadata.dev(),
                    inode: metadata.ino(),
                },
            })
        }
        #[cfg(windows)]
        {
            let (volume_serial_number, file_index) = windows_file_identity(&canonical)?;
            Ok(Self {
                path: canonical,
                volume_serial_number,
                file_index,
            })
        }
        #[cfg(not(any(unix, windows)))]
        Ok(Self { path: canonical })
    }

    pub(super) fn validate(&self, search_paths: &[PathBuf]) -> Result<(), ExecutionError> {
        #[cfg(not(target_os = "macos"))]
        if search_paths
            .first()
            .and_then(|directory| fs::canonicalize(directory.join(self.path.file_name()?)).ok())
            .as_deref()
            != Some(self.path.as_path())
        {
            return Err(invalid_request(
                "first executable search directory must resolve node to the verified runtime",
            ));
        }
        #[cfg(target_os = "macos")]
        if !search_paths
            .iter()
            .any(|p| Some(p.as_path()) == self.path.parent())
        {
            return Err(invalid_request(
                "search paths must contain the trusted runtime parent",
            ));
        }
        let current = Self::checked(&self.path).map_err(|_| {
            ExecutionError::new(
                ExecutionErrorCategory::PolicyViolation,
                "trusted Node runtime identity changed after request construction",
            )
        })?;
        if &current != self {
            return Err(ExecutionError::new(
                ExecutionErrorCategory::PolicyViolation,
                "trusted Node runtime identity changed after request construction",
            ));
        }
        Ok(())
    }
}

fn is_node_executable_name(path: &Path) -> bool {
    path.file_name().is_some_and(|name| {
        #[cfg(windows)]
        return name.to_string_lossy().eq_ignore_ascii_case("node.exe");
        #[cfg(not(windows))]
        return name == OsStr::new("node");
    })
}

/// Platform-neutral, validated request passed to a private execution backend.
///
/// Search paths remain private adapter input: external callers can add them only through the
/// checked builder and cannot replace validated paths after construction.
///
/// ```compile_fail
/// use tapid_runner::ExecutionRequest;
/// let request = ExecutionRequest::builder("node")
///     .executable_search_path("/runtime/bin")
///     .build()
///     .unwrap();
/// let _ = request.executable_search_paths();
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ExecutionRequest {
    #[cfg(target_os = "macos")]
    pub(super) reserved_node: Option<platform_backend::ReservedNodeBinding>,
    pub(super) launcher: Option<crate::PrivateLauncher>,
    pub(super) program: OsString,
    pub(super) arguments: Vec<OsString>,
    pub(super) executable_search_paths: Vec<PathBuf>,
    pub(super) trusted_node_runtime: Option<TrustedNodeRuntime>,
    pub(super) windows_verbatim_arguments: bool,
    pub(super) allow_process_memory_stats: bool,
    pub(super) project_root: PathBuf,
    pub(super) policy: SandboxPolicy,
    pub(super) environment: BTreeMap<OsString, OsString>,
}

impl ExecutionRequest {
    pub fn builder(program: impl Into<OsString>) -> ExecutionRequestBuilder {
        ExecutionRequestBuilder {
            program: program.into(),
            arguments: Vec::new(),
            executable_search_paths: Vec::new(),
            trusted_node_runtime: None,
            windows_verbatim_arguments: false,
            allow_process_memory_stats: false,
            project_root: PathBuf::from("."),
            policy: SandboxPolicy::default(),
            environment: BTreeMap::new(),
        }
    }

    pub fn program(&self) -> &OsStr {
        &self.program
    }
    pub fn arguments(&self) -> &[OsString] {
        &self.arguments
    }
    /// Whether a Windows adapter must pass arguments without MSVCRT re-quoting.
    pub fn uses_windows_verbatim_arguments(&self) -> bool {
        self.windows_verbatim_arguments
    }
    /// Ordered executable search directories for private platform adapters.
    pub(super) fn executable_search_paths(&self) -> &[PathBuf] {
        &self.executable_search_paths
    }
    #[cfg(all(test, unix))]
    pub(super) fn trusted_node_runtime(&self) -> &Path {
        self.trusted_node_runtime
            .as_ref()
            .expect("trusted Node runtime was requested")
            .path
            .as_path()
    }
    pub(super) fn validate_trusted_node_runtime(&self) -> Result<(), ExecutionError> {
        #[cfg(target_os = "macos")]
        if let Some(binding) = &self.reserved_node {
            return binding.validate();
        }
        if let Some(runtime) = &self.trusted_node_runtime {
            runtime.validate(&self.executable_search_paths)?;
        }
        Ok(())
    }
    pub fn project_root(&self) -> &Path {
        &self.project_root
    }
    pub fn policy(&self) -> &SandboxPolicy {
        &self.policy
    }

    /// Whether the caller explicitly requested process-memory statistics through isolated procfs.
    pub fn allow_process_memory_stats(&self) -> bool {
        self.allow_process_memory_stats
    }

    #[cfg(target_os = "linux")]
    pub(crate) fn private_launcher_executable(&self) -> Option<&Path> {
        self.launcher
            .as_ref()
            .and_then(|launcher| launcher.executable.as_deref())
    }

    /// Explicit caller-provided environment.
    ///
    /// This does not expose the mandatory adapter-owned `PATH`. Private adapters receive the
    /// explicit entries plus `PATH` serialized from the ordered executable-search directories;
    /// an empty directory list produces an explicitly empty `PATH`.
    pub fn environment(&self) -> &BTreeMap<OsString, OsString> {
        &self.environment
    }

    /// Complete adapter-owned child environment, including a controlled `PATH` entry.
    pub(super) fn child_environment(&self) -> BTreeMap<OsString, OsString> {
        let mut environment = self.environment.clone();
        let path = join_executable_search_paths(&self.executable_search_paths)
            .expect("validated executable search paths must remain joinable");
        environment.insert(OsString::from("PATH"), path);
        environment
    }

    pub(super) fn validate_windows_verbatim_boundary(&self) -> Result<(), ExecutionError> {
        if !self.windows_verbatim_arguments {
            return Ok(());
        }
        let is_cmd = Path::new(&self.program)
            .file_name()
            .and_then(OsStr::to_str)
            .is_some_and(|name| {
                name.eq_ignore_ascii_case("cmd") || name.eq_ignore_ascii_case("cmd.exe")
            });
        if !is_cmd
            || self.arguments.len() != 4
            || self.arguments[0] != OsStr::new("/D")
            || self.arguments[1] != OsStr::new("/S")
            || self.arguments[2] != OsStr::new("/C")
        {
            return Err(invalid_request(
                "Windows verbatim arguments require cmd.exe /D /S /C plus one command payload",
            ));
        }
        let payload = self.arguments[3].to_str().ok_or_else(|| {
            invalid_request("Windows verbatim command payload must be valid Unicode")
        })?;
        if payload.contains(['\r', '\n']) {
            return Err(invalid_request(
                "Windows verbatim command payload cannot contain CR or LF",
            ));
        }
        Ok(())
    }

    pub(super) fn validate(&self) -> Result<(), ExecutionError> {
        self.validate_windows_verbatim_boundary()?;
        #[cfg(not(target_os = "linux"))]
        if self.allow_process_memory_stats {
            return Err(invalid_request(
                "process memory statistics opt-in is supported only by the Linux Restricted backend",
            ));
        }
        #[cfg(target_os = "linux")]
        if self.allow_process_memory_stats
            && (self.policy.mode() != SandboxMode::Required
                || self.policy.assurance() != AssuranceLevel::Restricted)
        {
            return Err(invalid_request(
                "process memory statistics opt-in requires required sandbox mode and Linux Restricted assurance",
            ));
        }
        #[cfg(target_os = "linux")]
        if self.allow_process_memory_stats && self.private_launcher_executable().is_none() {
            return Err(invalid_request(
                "process memory statistics opt-in requires early private-launcher initialization",
            ));
        }
        let program_units =
            validate_os_value("execution program", &self.program, MAX_PROGRAM_UNITS)?;
        if self.program.is_empty() {
            return Err(invalid_request("execution program must not be empty"));
        }
        if self.arguments.len() > MAX_ARGUMENT_COUNT {
            return Err(invalid_request(format!(
                "execution request exceeds {MAX_ARGUMENT_COUNT} arguments"
            )));
        }
        #[cfg(windows)]
        let mut argument_units = Vec::with_capacity(self.arguments.len());
        #[cfg(not(windows))]
        let mut argv_units = program_units.saturating_add(1);
        for argument in &self.arguments {
            let units = validate_os_value("execution argument", argument, MAX_ARGUMENT_UNITS)?;
            #[cfg(windows)]
            argument_units.push(units);
            #[cfg(not(windows))]
            {
                argv_units = argv_units.saturating_add(units).saturating_add(1);
            }
        }
        #[cfg(windows)]
        validate_windows_command_line_units(program_units, argument_units.iter().copied())?;
        #[cfg(not(windows))]
        if argv_units > MAX_ARGV_UNITS {
            return Err(invalid_request(format!(
                "execution argv exceeds {MAX_ARGV_UNITS} bytes/code units"
            )));
        }
        validate_os_value(
            "project root",
            self.project_root.as_os_str(),
            MAX_PROJECT_ROOT_UNITS,
        )?;
        if self.project_root.as_os_str().is_empty() {
            return Err(invalid_request("project root must not be empty"));
        }
        let executable_search_path =
            validate_executable_search_paths(&self.executable_search_paths)?;
        self.validate_trusted_node_runtime()?;
        // The complete block always contains PATH=<joined paths>\0 followed by the block's final
        // terminator. An empty path list therefore still contributes `PATH=\0\0`.
        let mut environment_units = 1usize
            .checked_add(os_units(OsStr::new("PATH")))
            .and_then(|units| units.checked_add(os_units(&executable_search_path)))
            .and_then(|units| units.checked_add(2))
            .ok_or_else(environment_block_too_large)?;
        if environment_units > MAX_ENVIRONMENT_BLOCK_UNITS {
            return Err(environment_block_too_large());
        }
        for (name, value) in &self.environment {
            let Some(name) = name.to_str() else {
                return Err(invalid_request(
                    "environment variable names must be valid UTF-8",
                ));
            };
            if name.eq_ignore_ascii_case("PATH") {
                return Err(invalid_request(
                    "caller-controlled PATH is forbidden; containment backends own executable search paths",
                ));
            }
            if validate_environment_name(name).is_err() {
                return Err(invalid_request(format!(
                    "invalid environment variable name: {name:?}"
                )));
            }
            let name_units = os_units(name.as_ref());
            let value_units = validate_os_value(
                "environment variable value",
                value,
                MAX_ENVIRONMENT_VALUE_UNITS,
            )?;
            environment_units = environment_units
                .checked_add(name_units)
                .and_then(|units| units.checked_add(value_units))
                .and_then(|units| units.checked_add(2))
                .ok_or_else(environment_block_too_large)?;
            if environment_units > MAX_ENVIRONMENT_BLOCK_UNITS {
                return Err(environment_block_too_large());
            }
            if !self
                .policy
                .environment()
                .iter()
                .any(|allowed| allowed == name)
            {
                return Err(invalid_request(format!(
                    "environment variable is not allowlisted by policy: {name}"
                )));
            }
        }
        Ok(())
    }
}

/// Builder for an [`ExecutionRequest`].
#[derive(Clone, Debug)]
pub struct ExecutionRequestBuilder {
    program: OsString,
    arguments: Vec<OsString>,
    executable_search_paths: Vec<PathBuf>,
    trusted_node_runtime: Option<PathBuf>,
    windows_verbatim_arguments: bool,
    allow_process_memory_stats: bool,
    project_root: PathBuf,
    policy: SandboxPolicy,
    environment: BTreeMap<OsString, OsString>,
}

impl ExecutionRequestBuilder {
    pub fn arg(mut self, argument: impl Into<OsString>) -> Self {
        self.arguments.push(argument.into());
        self
    }

    pub fn args<I, S>(mut self, arguments: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<OsString>,
    {
        self.arguments.extend(arguments.into_iter().map(Into::into));
        self
    }

    /// Adds one executable search directory after existing entries.
    ///
    /// [`Self::build`] rejects entries that the target platform's [`std::env::join_paths`] cannot
    /// serialize, exact duplicates, and requests whose complete child environment (including the
    /// resulting mandatory `PATH`) exceeds [`MAX_ENVIRONMENT_BLOCK_UNITS`].
    pub fn executable_search_path(mut self, path: impl Into<PathBuf>) -> Self {
        self.executable_search_paths.push(path.into());
        self
    }

    /// Binds the exact canonical Node executable to the first child PATH directory.
    pub fn trusted_node_runtime(mut self, path: impl Into<PathBuf>) -> Self {
        self.trusted_node_runtime = Some(path.into());
        self
    }

    /// Requires the Windows adapter to use `CommandExt::raw_arg` or an equivalent verbatim seam.
    pub fn windows_verbatim_arguments(mut self, enabled: bool) -> Self {
        self.windows_verbatim_arguments = enabled;
        self
    }

    /// Enables read-only procfs access inside a private PID/mount namespace for this process and its descendants.
    pub fn allow_process_memory_stats(mut self, enabled: bool) -> Self {
        self.allow_process_memory_stats = enabled;
        self
    }

    /// Adds ordered executable search directories without consulting ambient `PATH`.
    ///
    /// [`Self::build`] validates every entry's representation and size before duplicate checks,
    /// then applies the target platform's exact [`std::env::join_paths`] serialization rules.
    pub fn executable_search_paths<I, P>(mut self, paths: I) -> Self
    where
        I: IntoIterator<Item = P>,
        P: Into<PathBuf>,
    {
        self.executable_search_paths
            .extend(paths.into_iter().map(Into::into));
        self
    }

    pub fn project_root(mut self, project_root: impl Into<PathBuf>) -> Self {
        self.project_root = project_root.into();
        self
    }

    pub fn policy(mut self, policy: SandboxPolicy) -> Self {
        self.policy = policy;
        self
    }

    /// Adds one explicit child environment value.
    pub fn env(mut self, name: impl Into<OsString>, value: impl Into<OsString>) -> Self {
        self.environment.insert(name.into(), value.into());
        self
    }

    /// Adds explicit child environment values without reading the ambient environment.
    pub fn envs<I, K, V>(mut self, environment: I) -> Self
    where
        I: IntoIterator<Item = (K, V)>,
        K: Into<OsString>,
        V: Into<OsString>,
    {
        self.environment.extend(
            environment
                .into_iter()
                .map(|(name, value)| (name.into(), value.into())),
        );
        self
    }

    pub fn build(self) -> Result<ExecutionRequest, ExecutionError> {
        let trusted_node_runtime = self
            .trusted_node_runtime
            .as_deref()
            .map(TrustedNodeRuntime::checked)
            .transpose()?;
        let request = ExecutionRequest {
            #[cfg(target_os = "macos")]
            reserved_node: None,
            launcher: crate::LAUNCHER.get().cloned(),
            program: self.program,
            arguments: self.arguments,
            executable_search_paths: self.executable_search_paths,
            trusted_node_runtime,
            windows_verbatim_arguments: self.windows_verbatim_arguments,
            allow_process_memory_stats: self.allow_process_memory_stats,
            project_root: self.project_root,
            policy: self.policy,
            environment: self.environment,
        };
        request.validate()?;
        Ok(request)
    }
}

pub(super) fn validate_executable_search_paths(
    paths: &[PathBuf],
) -> Result<OsString, ExecutionError> {
    if paths.len() > MAX_EXECUTABLE_SEARCH_PATH_COUNT {
        return Err(invalid_request(format!(
            "execution request exceeds {MAX_EXECUTABLE_SEARCH_PATH_COUNT} executable search directories"
        )));
    }

    // Bound and validate every value before any duplicate comparison. This keeps comparisons over
    // attacker-controlled native strings bounded even when an earlier value is repeated.
    for path in paths {
        validate_os_value(
            "executable search directory",
            path.as_os_str(),
            MAX_EXECUTABLE_SEARCH_PATH_UNITS,
        )?;
    }

    let joined = join_executable_search_paths(paths)?;
    let total_units = os_units(&joined)
        .checked_add(1)
        .ok_or_else(executable_search_path_too_large)?;
    if total_units > MAX_EXECUTABLE_SEARCH_PATHS_UNITS {
        return Err(executable_search_path_too_large());
    }

    let mut seen = BTreeSet::new();
    for path in paths {
        if !seen.insert(path.as_os_str()) {
            return Err(invalid_request(format!(
                "duplicate executable search directory: {}",
                path.display()
            )));
        }
    }
    Ok(joined)
}

fn join_executable_search_paths(paths: &[PathBuf]) -> Result<OsString, ExecutionError> {
    std::env::join_paths(paths).map_err(|error| {
        invalid_request(format!(
            "executable search directory cannot be joined into PATH: {error}"
        ))
    })
}

fn executable_search_path_too_large() -> ExecutionError {
    invalid_request(format!(
        "executable search path exceeds {MAX_EXECUTABLE_SEARCH_PATHS_UNITS} bytes/code units"
    ))
}

fn environment_block_too_large() -> ExecutionError {
    invalid_request(format!(
        "execution environment exceeds {MAX_ENVIRONMENT_BLOCK_UNITS} bytes/code units"
    ))
}

fn invalid_request(message: impl Into<String>) -> ExecutionError {
    ExecutionError::new(ExecutionErrorCategory::InvalidRequest, message)
}

/// Returns a conservative upper bound rather than the exact Windows command-line serialization.
/// The program and every argument are assumed to need surrounding quotes, and every input code
/// unit is budgeted to double under worst-case quote/backslash escaping. Separators and the
/// mandatory terminating NUL are counted separately.
#[cfg(any(windows, test))]
pub(super) fn windows_command_line_units_upper_bound(
    program_units: usize,
    argument_units: impl IntoIterator<Item = usize>,
) -> Option<usize> {
    let mut units = program_units
        .checked_mul(2)?
        .checked_add(2)?
        .checked_add(1)?;
    for argument_units in argument_units {
        units = units
            .checked_add(1)?
            .checked_add(argument_units.checked_mul(2)?.checked_add(2)?)?;
    }
    Some(units)
}

#[cfg(any(windows, test))]
pub(super) fn validate_windows_command_line_units(
    program_units: usize,
    argument_units: impl IntoIterator<Item = usize>,
) -> Result<(), ExecutionError> {
    if windows_command_line_units_upper_bound(program_units, argument_units)
        .is_none_or(|units| units > MAX_ARGV_UNITS)
    {
        return Err(invalid_request(format!(
            "execution argv may exceed {MAX_ARGV_UNITS} UTF-16 code units after Windows command-line serialization"
        )));
    }
    Ok(())
}

/// Mirrors `std::env::join_paths` on Windows so its separator quoting remains host-testable.
#[cfg(test)]
pub(super) fn join_windows_path_units<'a>(
    paths: impl IntoIterator<Item = &'a [u16]>,
) -> Result<Vec<u16>, ()> {
    const SEPARATOR: u16 = b';' as u16;
    const QUOTE: u16 = b'"' as u16;
    let mut joined = Vec::new();
    for (index, path) in paths.into_iter().enumerate() {
        if index != 0 {
            joined.push(SEPARATOR);
        }
        if path.contains(&QUOTE) {
            return Err(());
        }
        if path.contains(&SEPARATOR) {
            joined.push(QUOTE);
            joined.extend_from_slice(path);
            joined.push(QUOTE);
        } else {
            joined.extend_from_slice(path);
        }
    }
    Ok(joined)
}

#[cfg(any(windows, test))]
pub(super) fn windows_path_units_semantically_equal(left: &[u16], right: &[u16]) -> bool {
    fn fold_ascii_case(unit: u16) -> u16 {
        if unit >= u16::from(b'A') && unit <= u16::from(b'Z') {
            unit + u16::from(b'a' - b'A')
        } else {
            unit
        }
    }

    left.len() == right.len()
        && left
            .iter()
            .zip(right)
            .all(|(&left, &right)| fold_ascii_case(left) == fold_ascii_case(right))
}

fn validate_os_value(kind: &str, value: &OsStr, maximum: usize) -> Result<usize, ExecutionError> {
    validate_platform_representation(kind, value)?;
    let units = os_units(value);
    if os_contains_nul(value) {
        return Err(invalid_request(format!("{kind} contains an embedded NUL")));
    }
    if units > maximum {
        return Err(invalid_request(format!(
            "{kind} exceeds {maximum} bytes/code units"
        )));
    }
    Ok(units)
}

#[cfg(any(unix, windows))]
fn validate_platform_representation(_kind: &str, _value: &OsStr) -> Result<(), ExecutionError> {
    Ok(())
}

#[cfg(not(any(unix, windows)))]
fn validate_platform_representation(kind: &str, value: &OsStr) -> Result<(), ExecutionError> {
    if value.to_str().is_none() {
        return Err(invalid_request(format!(
            "{kind} cannot be represented on this platform"
        )));
    }
    Ok(())
}

#[cfg(unix)]
pub(super) fn os_units(value: &OsStr) -> usize {
    use std::os::unix::ffi::OsStrExt;
    value.as_bytes().len()
}

#[cfg(unix)]
fn os_contains_nul(value: &OsStr) -> bool {
    use std::os::unix::ffi::OsStrExt;
    value.as_bytes().contains(&0)
}

#[cfg(windows)]
pub(super) fn os_units(value: &OsStr) -> usize {
    use std::os::windows::ffi::OsStrExt;
    value.encode_wide().count()
}

#[cfg(windows)]
fn os_contains_nul(value: &OsStr) -> bool {
    use std::os::windows::ffi::OsStrExt;
    value.encode_wide().any(|unit| unit == 0)
}

#[cfg(not(any(unix, windows)))]
pub(super) fn os_units(value: &OsStr) -> usize {
    value.to_string_lossy().len()
}

#[cfg(not(any(unix, windows)))]
fn os_contains_nul(value: &OsStr) -> bool {
    value.to_string_lossy().contains('\0')
}