mxc-sdk 1.0.1

Importable library for starting MXC sandboxes in-process, with streaming stdio.
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use std::fmt;

#[cfg(test)]
use serde::de::DeserializeOwned;
use serde::{Deserialize, Deserializer};
use serde_json::error::Category;
#[cfg(test)]
use serde_json::Value;
use unicode_general_category::{get_general_category, GeneralCategory};

/// Field-name substrings that mark a value as secret-bearing. Matched anywhere
/// within a single path segment, so `token` catches `apiToken`, `secret`
/// catches `clientSecret`, and so on.
const SECRET_PATH_MARKERS: &[&str] = &[
    "token",
    "password",
    "secret",
    "credential",
    "apikey",
    "accesskey",
    "privatekey",
    "passphrase",
    "pwd",
    "bearer",
    "saskey",
    "connectionstring",
];

/// Whole path segments that mark a value as secret-bearing. Matched as a
/// complete field name (not a substring) so they never collide with unrelated
/// fields such as `username`. `user` marks a credential bundle: a shape error
/// where the object itself is expected is pathed at `user` rather than at the
/// secret-bearing leaf inside it, so the whole subtree must be marked.
/// Over-redaction fails safe — it only replaces an error value with a location,
/// never leaks one.
const SECRET_PATH_SEGMENTS: &[&str] = &["user"];

/// Whether a single lower-cased JSON object key is secret-bearing, per
/// [`SECRET_PATH_SEGMENTS`] (whole-field match) and [`SECRET_PATH_MARKERS`]
/// (substring match). Shared by error-path redaction (this module) and raw
/// config redaction (`diagnostic::redact_raw_config_json`) so both use one
/// definition of "secret-bearing".
pub(crate) fn is_secret_path_field(field: &str) -> bool {
    SECRET_PATH_SEGMENTS.contains(&field)
        || SECRET_PATH_MARKERS
            .iter()
            .any(|marker| field.contains(marker))
}

/// A JSON deserialization failure with the path at which typed policy parsing
/// failed. Syntax errors have no meaningful policy path.
#[derive(Debug)]
pub(crate) struct ConfigDeserializeError {
    path: Option<String>,
    source: serde_json::Error,
    /// Whole-file `(line, column)` that overrides the location baked into
    /// `source` when the error was produced from a sub-slice of a larger
    /// request (e.g. a state-aware `<backendSection>.<phase>` fragment).
    /// `None` leaves `source`'s own location untouched.
    location_override: Option<(usize, usize)>,
}

impl ConfigDeserializeError {
    /// The exact source field that failed structural deserialization.
    pub(crate) fn path(&self) -> Option<&str> {
        self.path.as_deref()
    }

    fn from_path_error(error: serde_path_to_error::Error<serde_json::Error>) -> Self {
        let path = error.path().to_string();
        let path = (path != ".").then_some(path);
        Self {
            path,
            source: error.into_inner(),
            location_override: None,
        }
    }

    /// Override the source location rendered by `Display` with whole-file
    /// coordinates. Used when translating a fragment-local serde location back
    /// to its position in the complete request text.
    #[cfg(test)]
    pub(crate) fn with_source_location(mut self, line: usize, column: usize) -> Self {
        self.location_override = Some((line, column));
        self
    }

    /// The `(line, column)` serde recorded for this error, or `None` when serde
    /// could not attribute a position (it reports line 0 in that case).
    #[cfg(test)]
    pub(crate) fn source_line_column(&self) -> Option<(usize, usize)> {
        let line = self.source.line();
        (line > 0).then(|| (line, self.source.column()))
    }

    /// Prefix a path produced while deserializing a JSON subtree with its path
    /// in the complete request.
    #[cfg(test)]
    pub(crate) fn with_prefix(mut self, prefix: &str) -> Self {
        self.path = Some(match self.path.take() {
            None => prefix.to_string(),
            Some(path) if path.starts_with('[') => format!("{prefix}{path}"),
            Some(path) => format!("{prefix}.{path}"),
        });
        self
    }

    fn path_contains_secret(&self) -> bool {
        self.path.as_deref().is_some_and(|path| {
            path.to_ascii_lowercase().split('.').any(|segment| {
                // Match on the field name only, dropping any array-index suffix
                // so `field[0]` matches on `field`.
                let field = segment.split('[').next().unwrap_or(segment);
                is_secret_path_field(field)
            })
        })
    }
}

impl fmt::Display for ConfigDeserializeError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        let source = if self.path_contains_secret() {
            redact_secret_value(&self.source)
        } else {
            self.source.to_string()
        };
        // Remap the source's baked-in `line/column` to whole-file coordinates
        // before escaping so all downstream guarantees (control-char escaping,
        // secret redaction, syntax-vs-data branch) still hold unchanged.
        let source = match self.location_override {
            Some((line, column)) => rewrite_trailing_location(&source, line, column),
            None => source,
        };
        let source = if self.source.classify() == Category::Data && self.path.is_none() {
            normalize_root_object_expectation(source)
        } else {
            source
        };
        let source = escape_control_characters(&source);
        match self.source.classify() {
            Category::Syntax | Category::Eof => {
                write!(formatter, "Invalid JSON syntax: {source}")
            }
            Category::Data => match self.path.as_deref() {
                Some(path) => write!(
                    formatter,
                    "Invalid configuration at `{}`: {source}",
                    escape_control_characters(path)
                ),
                None => write!(formatter, "Invalid configuration: {source}"),
            },
            // Current constructors cannot produce reader I/O failures. Keep a
            // defensive message in case a reader-backed constructor is added.
            Category::Io => write!(formatter, "Unable to read JSON configuration: {source}"),
        }
    }
}

fn normalize_root_object_expectation(source: String) -> String {
    const EXPECTED_STRUCT: &str = ", expected struct ";
    let Some(expectation_start) = source.rfind(EXPECTED_STRUCT) else {
        return source;
    };
    let type_start = expectation_start + EXPECTED_STRUCT.len();
    let type_end = source[type_start..]
        .find(" at line ")
        .map(|offset| type_start + offset)
        .unwrap_or(source.len());
    format!(
        "{}, expected a configuration object{}",
        &source[..expectation_start],
        &source[type_end..]
    )
}

fn redact_secret_value(source: &serde_json::Error) -> String {
    let line = source.line();
    let column = source.column();
    if line > 0 {
        return format!("invalid secret value at line {line} column {column}");
    }
    "invalid secret value".to_string()
}

/// Replace a trailing serde-style ` at line <N> column <M>` suffix in a rendered
/// error message with the supplied whole-file `line`/`column`. serde_json emits
/// this stable suffix on positioned errors; if it is absent (unpositioned
/// message), the location is appended so the caller still gets coordinates.
fn rewrite_trailing_location(rendered: &str, line: usize, column: usize) -> String {
    let replacement = format!(" at line {line} column {column}");
    if let Some(index) = rendered.rfind(" at line ") {
        if is_location_suffix(&rendered[index..]) {
            return format!("{}{}", &rendered[..index], replacement);
        }
    }
    format!("{rendered}{replacement}")
}

/// True when `suffix` is exactly ` at line <digits> column <digits>` with no
/// trailing text — serde_json's positioned-error suffix shape.
fn is_location_suffix(suffix: &str) -> bool {
    let Some(rest) = suffix.strip_prefix(" at line ") else {
        return false;
    };
    let (line_digits, rest) = split_leading_digits(rest);
    if line_digits.is_empty() {
        return false;
    }
    let Some(rest) = rest.strip_prefix(" column ") else {
        return false;
    };
    let (column_digits, rest) = split_leading_digits(rest);
    !column_digits.is_empty() && rest.is_empty()
}

fn split_leading_digits(text: &str) -> (&str, &str) {
    let end = text
        .find(|character: char| !character.is_ascii_digit())
        .unwrap_or(text.len());
    text.split_at(end)
}

/// 1-based `(line, column)` (serde_json semantics) → byte offset within `text`.
///
/// Column arithmetic assumes ASCII configs (bytes == columns); the parser only
/// ever hands us JSON, which is ASCII outside string literals, and offsets are
/// only used to translate error positions. Returns `None` when the position is
/// out of range so callers can fall back gracefully.
#[cfg(test)]
fn byte_offset_of_line_col(text: &str, line: usize, column: usize) -> Option<usize> {
    if line == 0 || column == 0 {
        return None;
    }
    let bytes = text.as_bytes();
    let mut current_line = 1usize;
    let mut line_start = 0usize;
    let mut index = 0usize;
    while current_line < line && index < bytes.len() {
        if bytes[index] == b'\n' {
            current_line += 1;
            line_start = index + 1;
        }
        index += 1;
    }
    if current_line < line {
        return None;
    }
    let offset = line_start + (column - 1);
    (offset <= text.len()).then_some(offset)
}

/// Byte offset within `text` → 1-based `(line, column)` (serde_json semantics).
///
/// Line counting is byte-exact; column arithmetic assumes ASCII (see
/// [`byte_offset_of_line_col`]). Operates on bytes to avoid slicing panics on a
/// non-char-boundary offset.
#[cfg(test)]
fn line_col_of_byte_offset(text: &str, offset: usize) -> (usize, usize) {
    let bytes = text.as_bytes();
    let end = offset.min(bytes.len());
    let mut line = 1usize;
    let mut last_newline: Option<usize> = None;
    for (index, byte) in bytes.iter().enumerate().take(end) {
        if *byte == b'\n' {
            line += 1;
            last_newline = Some(index);
        }
    }
    let column = match last_newline {
        Some(index) => end - index,
        None => end + 1,
    };
    (line, column)
}

/// Translate a `ConfigDeserializeError` produced by deserializing `fragment`
/// (which begins at byte `fragment_offset` within `source_text`) so its
/// rendered location reports whole-file coordinates instead of fragment-local
/// ones. Any step that cannot be resolved returns `err` unchanged.
#[cfg(test)]
pub(crate) fn remap_error_to_source(
    err: ConfigDeserializeError,
    fragment: &str,
    fragment_offset: usize,
    source_text: &str,
) -> ConfigDeserializeError {
    let Some((line, column)) = err.source_line_column() else {
        return err;
    };
    let Some(local_offset) = byte_offset_of_line_col(fragment, line, column) else {
        return err;
    };
    let global_offset = fragment_offset + local_offset;
    let (global_line, global_column) = line_col_of_byte_offset(source_text, global_offset);
    err.with_source_location(global_line, global_column)
}

fn escape_control_characters(value: &str) -> String {
    let mut escaped = String::with_capacity(value.len());
    for character in value.chars() {
        if character.is_control() {
            escaped.extend(character.escape_default());
        } else if is_diagnostic_format_character(character) {
            escaped.extend(character.escape_unicode());
        } else {
            escaped.push(character);
        }
    }
    escaped
}

/// Escape control and invisible-format characters in free-form, user-controlled
/// text before it reaches a diagnostic sink. Shared with the manual
/// (non-serde) semantic validators so every user-derived diagnostic honors the
/// same "no raw control/format bytes in diagnostics" guarantee.
pub(crate) fn escape_diagnostic_text(value: &str) -> String {
    escape_control_characters(value)
}

/// Invisible Unicode formatting characters (general category `Cf`) and the
/// line/paragraph separators (`Zl`/`Zp`) that `char::is_control()` does **not**
/// cover. Escaping these is a deliberate security control, not incidental
/// hardening: escaping bidirectional overrides/isolates (U+202A–U+202E,
/// U+2066–U+2069, category `Cf`) defends against "Trojan Source"
/// (CVE-2021-42574) visual-spoofing of diagnostics, escaping the line/paragraph
/// separators (U+2028/U+2029) prevents forging hard line breaks that some
/// terminals/log viewers honor, and escaping zero-width / joiner / interlinear
/// characters (also `Cf`) prevents concealing or forging log and error-envelope
/// content rendered in a terminal or editor.
fn is_diagnostic_format_character(character: char) -> bool {
    matches!(
        get_general_category(character),
        GeneralCategory::Format
            | GeneralCategory::LineSeparator
            | GeneralCategory::ParagraphSeparator
    )
}

impl std::error::Error for ConfigDeserializeError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(&self.source)
    }
}

fn deserialize_with_path<'de, T, D>(deserializer: D) -> Result<T, ConfigDeserializeError>
where
    T: Deserialize<'de>,
    D: Deserializer<'de, Error = serde_json::Error>,
{
    serde_path_to_error::deserialize(deserializer).map_err(ConfigDeserializeError::from_path_error)
}

pub(crate) fn from_str<'de, T>(json: &'de str) -> Result<T, ConfigDeserializeError>
where
    T: Deserialize<'de>,
{
    let mut deserializer = serde_json::Deserializer::from_str(json);
    let value = deserialize_with_path(&mut deserializer)?;
    deserializer
        .end()
        .map_err(|source| ConfigDeserializeError {
            path: None,
            source,
            location_override: None,
        })?;
    Ok(value)
}

#[cfg(test)]
pub(crate) fn from_value<T>(value: Value) -> Result<T, ConfigDeserializeError>
where
    T: DeserializeOwned,
{
    deserialize_with_path(value)
}

#[cfg(test)]
pub(crate) fn from_value_ref<'de, T>(value: &'de Value) -> Result<T, ConfigDeserializeError>
where
    T: Deserialize<'de>,
{
    deserialize_with_path(value)
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;

    use super::*;

    #[derive(Debug, Deserialize)]
    #[serde(deny_unknown_fields)]
    #[allow(dead_code)]
    struct Outer {
        inner: Inner,
    }

    #[derive(Debug, Deserialize)]
    #[serde(deny_unknown_fields)]
    #[allow(dead_code)]
    struct Inner {
        count: u16,
    }

    #[derive(Debug, Deserialize)]
    #[serde(rename_all = "camelCase")]
    #[allow(dead_code)]
    struct Secret {
        api_token: String,
    }

    #[derive(Debug, Deserialize)]
    #[serde(rename_all = "camelCase")]
    #[allow(dead_code)]
    struct NumericSecret {
        api_token: u32,
    }

    #[derive(Debug, Deserialize)]
    #[allow(dead_code)]
    struct MapOuter {
        items: HashMap<String, Inner>,
    }

    #[derive(Debug, Deserialize)]
    #[serde(rename_all = "camelCase")]
    #[allow(dead_code)]
    struct UserHolder {
        user: Secret,
    }

    #[test]
    fn redacts_scalar_supplied_where_credential_subtree_is_expected() {
        // A token supplied where the `user` object is expected pathes the error
        // at `user`, not at the secret leaf inside it; the whole subtree must still
        // redact so the scalar is never echoed into diagnostics or the envelope.
        let error = from_str::<UserHolder>(r#"{"user": "super-secret-bearer-token"}"#).unwrap_err();
        let message = error.to_string();

        assert!(
            message.contains("Invalid configuration at `user`"),
            "got: {message}"
        );
        assert!(message.contains("invalid secret value"), "got: {message}");
        assert!(
            !message.contains("super-secret-bearer-token"),
            "got: {message}"
        );
    }

    #[test]
    fn data_errors_include_the_nested_path_and_source_location() {
        let error = from_str::<Outer>(
            r#"{
                "inner": {
                    "count": 70000
                }
            }"#,
        )
        .unwrap_err();

        assert_eq!(error.path.as_deref(), Some("inner.count"));
        let message = error.to_string();
        assert!(message.contains("Invalid configuration at `inner.count`"));
        assert!(message.contains("expected u16"));
        assert!(message.contains("line 3"));
    }

    #[test]
    fn syntax_errors_are_distinguished_from_typed_config_errors() {
        let error = from_str::<Outer>(r#"{"inner": {"count": 1}"#).unwrap_err();
        let message = error.to_string();
        assert!(message.starts_with("Invalid JSON syntax:"));
        assert!(message.contains("line 1"));
    }

    #[test]
    fn trailing_json_data_is_rejected() {
        let error = from_str::<Outer>(r#"{"inner": {"count": 1}} {"second": true}"#).unwrap_err();
        assert!(error.to_string().starts_with("Invalid JSON syntax:"));
    }

    #[test]
    fn subtree_paths_can_be_prefixed_with_their_request_location() {
        let value = serde_json::json!({"count": "many"});
        let error = from_value_ref::<Inner>(&value)
            .unwrap_err()
            .with_prefix("experimental.example.start");

        assert_eq!(
            error.path.as_deref(),
            Some("experimental.example.start.count")
        );
        assert!(error
            .to_string()
            .contains("experimental.example.start.count"));
    }

    #[test]
    fn root_level_errors_have_no_policy_path_or_rust_type_name() {
        fn assert_exact_root<T>()
        where
            T: serde::de::DeserializeOwned + std::fmt::Debug,
        {
            let error = from_str::<T>(r#""not an object""#).unwrap_err();
            assert_eq!(error.path.as_deref(), None);
            let message = error.to_string();
            assert!(message.contains("expected a configuration object"));
            let rust_type_name = std::any::type_name::<T>()
                .rsplit("::")
                .next()
                .unwrap_or_default();
            assert!(!message.contains(rust_type_name), "{message}");
        }

        assert_exact_root::<crate::mxc_contract::published::v0_9_0_alpha::OneShotRequest>();
        assert_exact_root::<
            crate::mxc_contract::published::v0_9_0_alpha::IsolationSessionProvisionRequest,
        >();
        assert_exact_root::<crate::mxc_contract::published::v0_9_0_alpha::WslcProvisionRequest>();
        assert_exact_root::<crate::mxc_contract::published::v0_9_0_alpha::StartRequest>();
        assert_exact_root::<crate::mxc_contract::published::v0_9_0_alpha::ExecRequest>();
        assert_exact_root::<crate::mxc_contract::published::v0_9_0_alpha::StopRequest>();
        assert_exact_root::<crate::mxc_contract::published::v0_9_0_alpha::DeprovisionRequest>();
        assert_exact_root::<crate::mxc_contract::published::v1_0_0::OneShotRequest>();
        assert_exact_root::<crate::mxc_contract::published::v1_0_0::IsolationSessionProvisionRequest>(
        );
        assert_exact_root::<crate::mxc_contract::published::v1_0_0::WslcProvisionRequest>();
        assert_exact_root::<crate::mxc_contract::published::v1_0_0::StartRequest>();
        assert_exact_root::<crate::mxc_contract::published::v1_0_0::ExecRequest>();
        assert_exact_root::<crate::mxc_contract::published::v1_0_0::StopRequest>();
        assert_exact_root::<crate::mxc_contract::published::v1_0_0::DeprovisionRequest>();
        assert_exact_root::<crate::mxc_contract::dev::OneShotRequest>();
        assert_exact_root::<crate::mxc_contract::dev::WindowsSandboxProvisionRequest>();
        assert_exact_root::<crate::mxc_contract::dev::IsolationSessionProvisionRequest>();
        assert_exact_root::<crate::mxc_contract::dev::WslcProvisionRequest>();
        assert_exact_root::<crate::mxc_contract::dev::StartRequest>();
        assert_exact_root::<crate::mxc_contract::dev::ExecRequest>();
        assert_exact_root::<crate::mxc_contract::dev::StopRequest>();
        assert_exact_root::<crate::mxc_contract::dev::DeprovisionRequest>();
    }

    #[test]
    fn root_expectation_rewrite_uses_the_final_serde_marker() {
        let error = from_str::<crate::mxc_contract::dev::OneShotRequest>(
            r#""marker, expected struct Decoy at line 7 column 9""#,
        )
        .unwrap_err();
        let message = error.to_string();

        assert!(message.contains("marker, expected struct Decoy at line 7 column 9"));
        assert!(message.contains("expected a configuration object"));
        assert!(
            !message.contains(", expected struct Request at line"),
            "{message}"
        );
    }

    #[test]
    fn array_subtree_paths_are_prefixed_without_an_extra_dot() {
        let value = serde_json::json!([{"count": "many"}]);
        let error = from_value_ref::<Vec<Inner>>(&value)
            .unwrap_err()
            .with_prefix("experimental.example.start");

        assert_eq!(
            error.path.as_deref(),
            Some("experimental.example.start[0].count")
        );
    }

    #[test]
    fn from_value_reports_the_typed_error_path() {
        let value = serde_json::json!({"inner": {"count": "many"}});
        let error = from_value::<Outer>(value).unwrap_err();

        assert_eq!(error.path.as_deref(), Some("inner.count"));
        assert!(error.to_string().contains("expected u16"));
    }

    #[test]
    fn display_escapes_control_characters_from_paths_and_sources() {
        let json = "{\"items\":{\"forged\\n\\u001b[31mline\":{\"count\":\"value\\n\\u001b[32m\"}}}";
        let error = from_str::<MapOuter>(json).unwrap_err();
        let message = error.to_string();

        assert!(!message.contains('\n'));
        assert!(!message.contains('\u{1b}'));
        assert!(message.contains("\\n"), "got: {message}");
        assert!(message.contains("\\u{1b}"), "got: {message}");
    }

    #[test]
    fn display_escapes_unicode_format_characters_from_paths_and_sources() {
        let json = "{\"items\":{\"forged\u{202e}line\":{\"count\":\"value\u{200b}hidden\"}}}";
        let error = from_str::<MapOuter>(json).unwrap_err();
        let message = error.to_string();

        assert!(!message.contains('\u{202e}'));
        assert!(!message.contains('\u{200b}'));
        assert!(message.contains("\\u{202e}"), "got: {message}");
        assert!(message.contains("\\u{200b}"), "got: {message}");
    }

    #[test]
    fn display_redacts_values_at_secret_bearing_paths() {
        let error = from_str::<Secret>(r#"{"apiToken": 123456789}"#).unwrap_err();
        let message = error.to_string();

        assert!(message.contains("Invalid configuration at `apiToken`"));
        assert!(message.contains("invalid secret value"));
        assert!(message.contains("line 1"));
        assert!(!message.contains("123456789"));
    }

    #[test]
    fn secret_redaction_never_parses_attacker_controlled_error_text() {
        let error = from_str::<NumericSecret>(
            r#"{"apiToken": "expected leak-of-secret-data", "other": true}"#,
        )
        .unwrap_err();
        let message = error.to_string();

        assert!(message.contains("Invalid configuration at `apiToken`"));
        assert!(message.contains("invalid secret value at line 1 column"));
        assert!(!message.contains("expected leak-of-secret-data"));
        assert!(!message.contains("leak-of-secret-data"));
    }

    #[test]
    fn secret_markers_are_detected_case_insensitively() {
        for path in [
            "apiToken",
            "adminPassword",
            "clientSecret",
            "serviceCredential",
            "apiKey",
            "accessKey",
            "privateKey",
            "passphrase",
            "pwd",
            "bearer",
            "sasKey",
            "connectionString",
        ] {
            let error = ConfigDeserializeError {
                path: Some(path.to_string()),
                source: serde_json::from_str::<Secret>(r#"{"apiToken": 123456789}"#).unwrap_err(),
                location_override: None,
            };

            assert!(
                error.to_string().contains("invalid secret value"),
                "path {path} was not treated as sensitive"
            );
        }
    }

    #[test]
    fn ordinary_key_suffixes_are_not_treated_as_secrets() {
        let error = ConfigDeserializeError {
            path: Some("monkey".to_string()),
            source: serde_json::from_str::<Secret>(r#"{"apiToken": 123456789}"#).unwrap_err(),
            location_override: None,
        };

        assert!(!error.to_string().contains("invalid secret value"));
    }

    #[test]
    fn user_segment_is_secret_but_similar_field_names_are_not() {
        // `user` marks the credential bundle when matched as a whole path
        // segment. It must redact at the segment itself, nested under a parent,
        // with an array index, and at any descendant — but must NOT collide with
        // unrelated fields that merely contain the substring `user`.
        let redacted = [
            "user",
            "experimental.someBackend.user",
            "experimental.someBackend.user.name",
            "users[0].user",
        ];
        for path in redacted {
            let error = ConfigDeserializeError {
                path: Some(path.to_string()),
                source: serde_json::from_str::<Secret>(r#"{"apiToken": 123456789}"#).unwrap_err(),
                location_override: None,
            };
            assert!(
                error.to_string().contains("invalid secret value"),
                "path {path} should be treated as sensitive"
            );
        }

        let not_redacted = ["username", "userProfile", "userName", "superuser"];
        for path in not_redacted {
            let error = ConfigDeserializeError {
                path: Some(path.to_string()),
                source: serde_json::from_str::<Secret>(r#"{"apiToken": 123456789}"#).unwrap_err(),
                location_override: None,
            };
            assert!(
                !error.to_string().contains("invalid secret value"),
                "path {path} should NOT be treated as sensitive"
            );
        }
    }

    #[test]
    fn escapes_line_and_paragraph_separators() {
        // U+2028 (LINE SEPARATOR) and U+2029 (PARAGRAPH SEPARATOR) render as
        // hard line breaks in some terminals/log viewers, so they must be
        // escaped to prevent forged multi-line diagnostics.
        let escaped = escape_diagnostic_text("a\u{2028}b\u{2029}c");

        assert!(!escaped.contains('\u{2028}'));
        assert!(!escaped.contains('\u{2029}'));
        assert!(escaped.contains("\\u{2028}"), "got: {escaped}");
        assert!(escaped.contains("\\u{2029}"), "got: {escaped}");
    }

    #[test]
    fn leaves_plain_text_unchanged() {
        let plain = "plain diagnostic text 123";
        assert_eq!(escape_diagnostic_text(plain), plain);
    }

    #[test]
    fn byte_offset_and_line_col_round_trip() {
        let text = "line one\nline two\nline three\n";
        // Walk every byte offset and confirm the offset -> (line,col) -> offset
        // round-trip is stable.
        for offset in 0..=text.len() {
            let (line, column) = line_col_of_byte_offset(text, offset);
            assert_eq!(
                byte_offset_of_line_col(text, line, column),
                Some(offset),
                "round trip failed at offset {offset} -> ({line},{column})"
            );
        }
    }

    #[test]
    fn line_col_of_byte_offset_hand_computed_cases() {
        let text = "abc\ndefgh\nij";
        // Offset 0 is line 1 column 1.
        assert_eq!(line_col_of_byte_offset(text, 0), (1, 1));
        // Offset 2 ('c') is line 1 column 3.
        assert_eq!(line_col_of_byte_offset(text, 2), (1, 3));
        // Offset 4 (start of "defgh") is line 2 column 1.
        assert_eq!(line_col_of_byte_offset(text, 4), (2, 1));
        // Offset 7 ('g') is line 2 column 4.
        assert_eq!(line_col_of_byte_offset(text, 7), (2, 4));
        // Offset 10 (start of "ij") is line 3 column 1.
        assert_eq!(line_col_of_byte_offset(text, 10), (3, 1));
    }

    #[test]
    fn byte_offset_of_line_col_hand_computed_and_out_of_range() {
        let text = "abc\ndefgh\nij";
        // Line 2 column 1 is the byte after the first newline.
        assert_eq!(byte_offset_of_line_col(text, 2, 1), Some(4));
        // Line 3 column 2 -> 'j'.
        assert_eq!(byte_offset_of_line_col(text, 3, 2), Some(11));
        // A line beyond the text has no offset.
        assert_eq!(byte_offset_of_line_col(text, 9, 1), None);
        // serde reports 0 for unknown positions; reject those.
        assert_eq!(byte_offset_of_line_col(text, 0, 1), None);
        assert_eq!(byte_offset_of_line_col(text, 1, 0), None);
    }

    #[test]
    fn rewrite_trailing_location_replaces_existing_suffix() {
        let rendered = "missing field `configuration_id` at line 2 column 5";
        let rewritten = rewrite_trailing_location(rendered, 7, 11);
        assert_eq!(
            rewritten,
            "missing field `configuration_id` at line 7 column 11"
        );
    }

    #[test]
    fn rewrite_trailing_location_appends_when_absent() {
        let rendered = "some message without a position";
        let rewritten = rewrite_trailing_location(rendered, 3, 4);
        assert_eq!(
            rewritten,
            "some message without a position at line 3 column 4"
        );
    }

    #[test]
    fn serde_json_positioned_error_display_contract_is_pinned() {
        // `rewrite_trailing_location` recognizes and replaces serde_json's
        // rendered ` at line <N> column <M>` suffix. Pin that upstream Display
        // contract so a format change is caught here rather than silently
        // producing duplicated coordinates in diagnostics.
        let err = serde_json::from_str::<i32>("\n  \"x\"").unwrap_err();
        assert!(err.line() > 0, "expected a positioned error");
        let rendered = err.to_string();
        let suffix = format!(" at line {} column {}", err.line(), err.column());
        assert!(
            rendered.ends_with(&suffix),
            "serde_json positioned-error suffix drifted: {rendered:?}"
        );
        assert!(
            is_location_suffix(&suffix),
            "positioned-error suffix no longer matches the recognized shape: {suffix:?}"
        );

        // And the rewrite must replace, not append, so coordinates never double.
        let rewritten = rewrite_trailing_location(&rendered, 9, 3);
        assert!(rewritten.ends_with(" at line 9 column 3"));
        assert_eq!(
            rewritten.matches(" at line ").count(),
            1,
            "coordinates were duplicated instead of replaced: {rewritten:?}"
        );
    }

    #[test]
    fn remap_error_translates_fragment_local_location_to_whole_file() {
        // A fragment that starts several lines into the whole file. The typed
        // error inside it must be reported at its whole-file line/column.
        let source_text = "line1\nline2\nline3\n{\n  \"count\": \"many\"\n}\n";
        let fragment = "{\n  \"count\": \"many\"\n}";
        let fragment_offset = source_text.find(fragment).unwrap();

        let err = from_str::<Inner>(fragment).unwrap_err();
        // Fragment-local location: line 2 of the fragment.
        assert_eq!(err.source_line_column().map(|(l, _)| l), Some(2));

        let remapped = remap_error_to_source(err, fragment, fragment_offset, source_text);
        let message = remapped.to_string();
        // The offending field sits on whole-file line 5.
        assert!(
            message.contains("line 5"),
            "expected whole-file line 5, got: {message}"
        );
        assert!(
            !message.contains("line 2"),
            "still fragment-local: {message}"
        );
    }
}