cpa-core 0.4.1

Part of cliproxy: config, credentials, formats and the execution envelope
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
//! Tagged scalars as yaml.v3 reads them, where serde_yaml_ng reads them differently.
//! yaml.v3 base64-decodes a scalar whose tag resolves to `!!binary` into any string
//! field (decode.go `scalar`); serde_yaml_ng keeps the base64 text. yaml.v3 reads a
//! scalar with an unknown local tag (`!keep x`) as its text, resolves a plain scalar
//! with the non-specific `!` tag implicitly, and treats the literal short tags
//! `!!binary`, `!!null` and `!!str` that its `shortTag` keeps (`!<!!null>`,
//! `!%21binary`) as the standard ones; serde_yaml_ng wraps all of those as tagged
//! values. The tags come from libyaml's own events, so handles, `%TAG` directives and
//! `%XX` escapes resolve as they do for yaml.v3.
//!
//! What this does not follow is refused rather than read differently: other literal
//! short tags, a non-specific tag that resolves to a number or boolean, tagged or
//! aliased mapping keys, collection keys (Go rejects them too), a repeated anchor name (serde_yaml_ng binds a redefined
//! anchor's aliases to a different node than libyaml does), a `%00` escape in a
//! tag (libyaml cuts the tag there; Go keeps the NUL), and a quoted `<<` key (yaml.v3's
//! `isMerge` reads it as a plain key; [`super::expand_merges`] cannot tell it apart).
//! A merge operand that is an alias of a sequence fails with yaml.v3's error, which
//! serde_yaml_ng's expanded aliases no longer show.

use std::collections::{HashMap, HashSet};
use std::ffi::CStr;
use std::mem::MaybeUninit;
use std::rc::Rc;

use anyhow::{Context, bail, ensure};
use serde_yaml_ng::Value;

const UNSUPPORTED_TAG: &str = "a YAML tag in the config file is not supported";
const UNSUPPORTED_KEY: &str = "a tagged or aliased mapping key in the config file is not supported";
const COLLECTION_KEY: &str = "a mapping or sequence used as a mapping key in the config file is not supported";
const QUOTED_MERGE_KEY: &str = "a quoted << mapping key in the config file is not supported";
const REPEATED_ANCHOR: &str = "a YAML anchor name is defined more than once in the config file";
const PLUGIN_PIN: &str =
    "a plugin's store.version or store.release-tag in the config file is a !!binary value, which is not supported";
/// More alias copies of rewritten scalars than a config has; alias chains past it are
/// refused. Each scalar written in the file counts once and is not limited.
const MAX_COPIES: usize = 1 << 20;
/// More entries than aliases of a config visit, kept or not: copies an alias drops (a
/// number that lands outside a string setting) still cost a step each.
const MAX_VISITS: usize = 1 << 22;

/// Go's `base64.StdEncoding` as yaml.v3 applies it: padding required, unused bits
/// ignored, CR and LF skipped. `None` for invalid data.
pub(super) fn go_base64(text: &[u8]) -> Option<Vec<u8>> {
    use base64::engine::{DecodePaddingMode, GeneralPurpose, GeneralPurposeConfig};
    use base64::{Engine as _, alphabet};
    let engine = GeneralPurpose::new(
        &alphabet::STANDARD,
        GeneralPurposeConfig::new()
            .with_decode_allow_trailing_bits(true)
            .with_decode_padding_mode(DecodePaddingMode::RequireCanonical),
    );
    let payload: Vec<u8> = text.iter().copied().filter(|b| !matches!(b, b'\r' | b'\n')).collect();
    engine.decode(payload).ok()
}

/// Rewrites the tagged scalars in `value`, which serde_yaml_ng parsed from `text` with
/// merges not yet expanded, to what yaml.v3 reads, aliases of them included. Fails on
/// invalid base64 as Go does, and refuses what it does not follow (see the module
/// docs) and binary bytes that are not UTF-8, which a setting cannot hold.
/// With `literals`, a plain scalar in a [`literal_field`] is its text, as Go's typed
/// decode reads it into the config; `text` is the value at those keys of the config
/// (empty for a whole file). See [`Mode`] for what else `mode` changes.
pub(super) fn decode(text: &str, value: &mut Value, literals: Option<&[&str]>, mode: Mode) -> anyhow::Result<()> {
    // Without a tag, an anchor, a `<<` key or a collection key, serde_yaml_ng reads the
    // file as yaml.v3 does here.
    if mode != Mode::Plugins
        && !text.contains(['!', '&'])
        && !needs_walk(value)
        && !literals.is_some_and(|at| scalar_literal_field(value, &mut at.iter().map(|k| Some(*k)).collect()))
    {
        return Ok(());
    }
    let mut walk = Walk {
        events: events(text)?.into_iter(),
        defined: HashSet::new(),
        sequences: HashSet::new(),
        anchors: HashMap::new(),
        anchored: 0,
        last_field: None,
        literals: literals.map(|at| at.iter().map(|k| Some(Rc::from(*k))).collect()),
        keys: Vec::new(),
        found: Vec::new(),
        copies: 0,
        visits: 0,
        mode,
    };
    if let Some(root) = walk.events.next() {
        walk.node(root, &mut Vec::new(), false)?;
    }
    // Go reads a plugin's pin from the raw node (pluginhost `yamlScalarString`), so a
    // `!!binary` pin, aliased or merged in too, keeps its base64 text there and fails
    // the version grammar; refused rather than read decoded.
    ensure!(
        !walk.found.iter().any(|f| f.kind == Kind::Binary && plugin_pin(&f.keys)),
        PLUGIN_PIN
    );
    // yaml.v3 reads any plain scalar into a string field as its text; serde_yaml_ng
    // reads a number or boolean (`1.50`, `0x1F`, `007`, `true`) as that value, whose text
    // differs. The fields of [`literal_field`] take the text.
    let mut found: Vec<Found> = walk
        .found
        .into_iter()
        .filter(|f| f.kind != Kind::Literal(false))
        .collect();
    // Paths are entry or item positions; patch them in one ordered pass.
    found.sort_by(|a, b| a.path.cmp(&b.path));
    apply(value, 0, &found)
}

/// Whether `value` holds a `<<` key or a mapping or sequence used as a key (whose own
/// keys serde_yaml_ng's merge expansion never visits).
fn needs_walk(value: &Value) -> bool {
    match value {
        Value::Mapping(m) => m.iter().any(|(k, v)| {
            matches!(k, Value::Mapping(_) | Value::Sequence(_)) || k.as_str() == Some("<<") || needs_walk(v)
        }),
        Value::Sequence(items) => items.iter().any(needs_walk),
        Value::Tagged(tagged) => needs_walk(&tagged.value),
        _ => false,
    }
}

fn apply(node: &mut Value, depth: usize, patches: &[Found]) -> anyhow::Result<()> {
    let Some(first) = patches.first() else {
        return Ok(());
    };
    if first.path.len() == depth {
        *node = first.value.clone();
        return Ok(());
    }
    let mut node = node;
    while let Value::Tagged(tagged) = node {
        node = &mut tagged.value;
    }
    let children: Box<dyn Iterator<Item = &mut Value>> = match node {
        Value::Mapping(m) => Box::new(m.values_mut()),
        Value::Sequence(items) => Box::new(items.iter_mut()),
        _ => bail!("config YAML changed while it was read"),
    };
    let mut rest = patches;
    for (i, child) in children.enumerate() {
        if rest.is_empty() {
            break;
        }
        let n = rest.iter().take_while(|f| f.path[depth] == i).count();
        apply(child, depth + 1, &rest[..n])?;
        rest = &rest[n..];
    }
    ensure!(rest.is_empty(), "config YAML changed while it was read");
    Ok(())
}

/// Whether `value`, with no merge keys or aliases, holds a number or boolean in a
/// [`literal_field`]. `keys` leads to `value`.
fn scalar_literal_field<'a>(value: &'a Value, keys: &mut Vec<Option<&'a str>>) -> bool {
    match value {
        Value::Mapping(m) => m.iter().any(|(k, v)| {
            // A key that is not a string still names a mapping entry.
            keys.push(Some(k.as_str().unwrap_or("")));
            let found = scalar_literal_field(v, keys);
            keys.pop();
            found
        }),
        Value::Sequence(items) => items.iter().any(|v| {
            keys.push(None);
            let found = scalar_literal_field(v, keys);
            keys.pop();
            found
        }),
        // A plain timestamp is tagged ([`super::timestamp::TAG`]).
        Value::Number(_) | Value::Bool(_) | Value::Tagged(_) => literal_field(keys),
        _ => false,
    }
}

/// `value` as [`decode`] with literals reads it back once written: a number or boolean
/// in a [`literal_field`] is its text. For a document a JSON body edited.
pub(super) fn as_reread(value: &mut Value) {
    fn walk(value: &mut Value, keys: &mut Vec<Option<String>>) {
        match value {
            Value::Mapping(m) => {
                for (k, v) in m.iter_mut() {
                    keys.push(Some(k.as_str().unwrap_or("").to_owned()));
                    walk(v, keys);
                    keys.pop();
                }
            }
            Value::Sequence(items) => {
                for v in items {
                    keys.push(None);
                    walk(v, keys);
                    keys.pop();
                }
            }
            Value::Number(_) | Value::Bool(_) if literal_field(keys) => *value = Value::String(super::go_string(value)),
            _ => {}
        }
    }
    walk(value, &mut Vec::new());
}

/// `keys` (a mapping key, or `None` for a list item, per step) without merge keys and
/// the list items of a `<<: [...]`.
fn named<K: AsRef<str>>(keys: &[Option<K>]) -> Vec<Option<&str>> {
    let mut named = Vec::with_capacity(keys.len());
    let mut keys = keys.iter().map(|k| k.as_ref().map(AsRef::as_ref)).peekable();
    while let Some(key) = keys.next() {
        if key == Some("<<") {
            // `<<: [*a, *b]`: the list item is part of the merge too.
            keys.next_if(Option::is_none);
        } else {
            named.push(key);
        }
    }
    named
}

/// Whether the scalar at `keys` is a Go string field (in the v8 layout or the legacy one
/// it migrates from), which yaml.v3 reads any plain scalar into as its text. Untyped
/// data keeps its YAML types: plugin configs, and a payload rule's `params` and
/// `match`/`not-match` values, which reach requests with them.
fn literal_field<K: AsRef<str>>(keys: &[Option<K>]) -> bool {
    super::schema::is_string(&super::document::v8_path(&named(keys)))
}

/// Whether `keys` lead into a `plugins.configs` entry.
fn plugin_config<K: AsRef<str>>(keys: &[Option<K>]) -> bool {
    matches!(named(keys).as_slice(), [Some("plugins"), Some("configs"), Some(_), ..])
}

/// Whether `keys` lead to a plugin's `store.version` or `store.release-tag`.
fn plugin_pin<K: AsRef<str>>(keys: &[Option<K>]) -> bool {
    matches!(
        named(keys).as_slice(),
        [
            Some("plugins"),
            Some("configs"),
            Some(_),
            Some("store"),
            Some("version" | "release-tag")
        ]
    )
}

/// The document's node events, in order, without stream and document markers.
enum Event {
    Scalar {
        anchor: Option<Vec<u8>>,
        tag: Option<Vec<u8>>,
        plain: bool,
        value: Vec<u8>,
    },
    Alias(Vec<u8>),
    Start {
        anchor: Option<Vec<u8>>,
        mapping: bool,
    },
    End,
}

/// Fails on a tagged node or a document start whose source contains `%00`: libyaml
/// cuts a tag at the NUL, so its tag would not be the one Go reads.
fn events(text: &str) -> anyhow::Result<Vec<Event>> {
    events_with(text, None)
}

/// An untagged plain scalar of a text.
pub(super) struct Plain {
    /// Its byte span in the text.
    pub span: std::ops::Range<usize>,
    pub value: Vec<u8>,
    /// Whether it is a mapping key.
    pub key: bool,
    /// Where its [`super::timestamp::TAG`] starts, when it has that tag.
    pub timestamp_tag: Option<usize>,
}

/// Each plain scalar of `text` that is untagged or has [`super::timestamp::TAG`].
pub(super) fn plain_scalars(text: &str) -> anyhow::Result<Vec<Plain>> {
    let mut out = Vec::new();
    events_with(text, Some(&mut out))?;
    Ok(out)
}

/// [`events`], recording each untagged plain scalar in `plain`.
fn events_with(text: &str, mut plain: Option<&mut Vec<Plain>>) -> anyhow::Result<Vec<Event>> {
    let nul_escape = text.contains("%00");
    // libyaml with its encoding set reads a leading BOM as a character, indenting the
    // first line by one, so it is fed without it and its marks shifted past it.
    let bom = if text.starts_with('\u{feff}') { 3 } else { 0 };
    struct Parser(Box<MaybeUninit<unsafe_libyaml::yaml_parser_t>>);
    impl Drop for Parser {
        fn drop(&mut self) {
            // SAFETY: only built after a successful yaml_parser_initialize.
            unsafe { unsafe_libyaml::yaml_parser_delete(self.0.as_mut_ptr()) }
        }
    }
    fn bytes(ptr: *const u8) -> Option<Vec<u8>> {
        // SAFETY: libyaml's anchors and tags are NUL-terminated or null.
        (!ptr.is_null()).then(|| unsafe { CStr::from_ptr(ptr.cast()) }.to_bytes().to_vec())
    }
    let mut out = Vec::new();
    // Each open collection: whether it is a mapping, and its nodes so far.
    let mut open: Vec<(bool, usize)> = Vec::new();
    // SAFETY: the parser is boxed, so it does not move while libyaml holds pointers
    // into it; `text` outlives it; each event is deleted once its data is copied.
    unsafe {
        let mut raw = Box::new(MaybeUninit::<unsafe_libyaml::yaml_parser_t>::uninit());
        ensure!(
            !unsafe_libyaml::yaml_parser_initialize(raw.as_mut_ptr()).fail,
            "failed to start the YAML parser"
        );
        let mut parser = Parser(raw);
        let p = parser.0.as_mut_ptr();
        unsafe_libyaml::yaml_parser_set_encoding(p, unsafe_libyaml::YAML_UTF8_ENCODING);
        unsafe_libyaml::yaml_parser_set_input_string(p, text[bom..].as_ptr(), (text.len() - bom) as _);
        loop {
            let mut event = MaybeUninit::<unsafe_libyaml::yaml_event_t>::uninit();
            // serde_yaml_ng parsed the same text, so this does not fail.
            ensure!(
                !unsafe_libyaml::yaml_parser_parse(p, event.as_mut_ptr()).fail,
                "failed to parse config file"
            );
            let event = event.assume_init_mut();
            let kind = event.type_;
            if nul_escape {
                let tagged = match kind {
                    unsafe_libyaml::YAML_SCALAR_EVENT => !event.data.scalar.tag.is_null(),
                    unsafe_libyaml::YAML_SEQUENCE_START_EVENT => !event.data.sequence_start.tag.is_null(),
                    unsafe_libyaml::YAML_MAPPING_START_EVENT => !event.data.mapping_start.tag.is_null(),
                    // Its span holds the document's directives.
                    unsafe_libyaml::YAML_DOCUMENT_START_EVENT => true,
                    _ => false,
                };
                let (start, end) = (
                    event.start_mark.index as usize + bom,
                    event.end_mark.index as usize + bom,
                );
                let span = text.get(start..end);
                if tagged && span.is_none_or(|s| s.contains("%00")) {
                    unsafe_libyaml::yaml_event_delete(event);
                    bail!(UNSUPPORTED_TAG);
                }
            }
            let key = match kind {
                unsafe_libyaml::YAML_SCALAR_EVENT
                | unsafe_libyaml::YAML_ALIAS_EVENT
                | unsafe_libyaml::YAML_SEQUENCE_START_EVENT
                | unsafe_libyaml::YAML_MAPPING_START_EVENT => open.last_mut().is_some_and(|(mapping, nodes)| {
                    *nodes += 1;
                    *mapping && *nodes % 2 == 1
                }),
                _ => false,
            };
            match kind {
                unsafe_libyaml::YAML_SEQUENCE_START_EVENT => open.push((false, 0)),
                unsafe_libyaml::YAML_MAPPING_START_EVENT => open.push((true, 0)),
                unsafe_libyaml::YAML_SEQUENCE_END_EVENT | unsafe_libyaml::YAML_MAPPING_END_EVENT => {
                    open.pop();
                }
                _ => {}
            }
            let next = match kind {
                unsafe_libyaml::YAML_SCALAR_EVENT => {
                    let s = &event.data.scalar;
                    let value = std::slice::from_raw_parts(s.value, s.length as usize);
                    // The event's span starts at an anchor or tag, so the scalar is the
                    // source bytes that equal its value. One folded over lines
                    // (`2001-12-14\n  21:59:43`) is not its source: an untagged one starts
                    // at the event, or after its `&anchor` and the blanks, line breaks
                    // (NEL, LS and PS too, as libyaml reads them) and comments after it.
                    let end = event.end_mark.index as usize + bom;
                    let timestamp = bytes(s.tag).is_some_and(|t| t == super::timestamp::TAG.as_bytes());
                    let folded_start = || {
                        let anchor = bytes(s.anchor).map_or(0, |a| 1 + a.len());
                        let after = event.start_mark.index as usize + bom + anchor;
                        let rest = text.get(after..end)?;
                        let mut comment = false;
                        let skipped = rest
                            .char_indices()
                            .find(|&(_, c)| {
                                let line_break = matches!(c, '\r' | '\n' | '\u{85}' | '\u{2028}' | '\u{2029}');
                                if line_break {
                                    comment = false;
                                } else if c == '#' {
                                    comment = true;
                                }
                                !(comment || line_break || matches!(c, ' ' | '\t'))
                            })
                            .map(|(i, _)| i)?;
                        let first = after + skipped;
                        (text.as_bytes().get(first) == value.first()).then_some(first)
                    };
                    let span = end
                        .checked_sub(value.len())
                        .filter(|&start| text.as_bytes().get(start..end) == Some(value))
                        .or_else(|| s.tag.is_null().then(folded_start).flatten())
                        .map(|start| start..end);
                    if let Some(plain) = plain.as_deref_mut()
                        && s.style == unsafe_libyaml::YAML_PLAIN_SCALAR_STYLE
                        && (s.tag.is_null() || timestamp)
                        && let Some(span) = span
                    {
                        plain.push(Plain {
                            span,
                            value: value.to_vec(),
                            key,
                            timestamp_tag: timestamp.then_some(event.start_mark.index as usize + bom),
                        });
                    }
                    Some(Event::Scalar {
                        anchor: bytes(s.anchor),
                        tag: bytes(s.tag),
                        plain: s.style == unsafe_libyaml::YAML_PLAIN_SCALAR_STYLE,
                        value: std::slice::from_raw_parts(s.value, s.length as usize).to_vec(),
                    })
                }
                unsafe_libyaml::YAML_ALIAS_EVENT => bytes(event.data.alias.anchor).map(Event::Alias),
                unsafe_libyaml::YAML_SEQUENCE_START_EVENT => Some(Event::Start {
                    anchor: bytes(event.data.sequence_start.anchor),
                    mapping: false,
                }),
                unsafe_libyaml::YAML_MAPPING_START_EVENT => Some(Event::Start {
                    anchor: bytes(event.data.mapping_start.anchor),
                    mapping: true,
                }),
                unsafe_libyaml::YAML_SEQUENCE_END_EVENT | unsafe_libyaml::YAML_MAPPING_END_EVENT => Some(Event::End),
                _ => None,
            };
            unsafe_libyaml::yaml_event_delete(event);
            out.extend(next);
            if kind == unsafe_libyaml::YAML_STREAM_END_EVENT {
                return Ok(out);
            }
        }
    }
}

/// yaml.v3's null spellings.
fn is_null(value: &[u8]) -> bool {
    matches!(value, b"" | b"~" | b"null" | b"Null" | b"NULL")
}

/// A plain scalar that yaml.v3 resolves to a string, judged without resolving it: text
/// that starts with a letter and is not a boolean word. Numbers, timestamps, `.inf`,
/// `<<` and the like start with something else.
fn resolves_to_text(value: &[u8]) -> bool {
    const WORDS: &[&[u8]] = &[b"true", b"false", b"yes", b"no", b"on", b"off", b"y", b"n"];
    value.first().is_some_and(u8::is_ascii_alphabetic) && !WORDS.iter().any(|w| value.eq_ignore_ascii_case(w))
}

/// What yaml.v3 reads for a tagged scalar, where serde_yaml_ng reads something else;
/// `None` where they agree.
fn rewrite(tag: &[u8], plain: bool, value: Vec<u8>) -> anyhow::Result<Option<Value>> {
    let text = || {
        String::from_utf8(value.clone())
            .ok()
            .context("config YAML is not UTF-8")
    };
    Ok(Some(match tag {
        b"tag:yaml.org,2002:binary" | b"!!binary" => {
            let bytes = go_base64(&value).context("yaml: !!binary value contains invalid base64 data")?;
            let text = String::from_utf8(bytes)
                .ok()
                .context("a !!binary value in the config file is not UTF-8 text")?;
            Value::String(text)
        }
        // Non-specific: a plain scalar resolves as if untagged, a quoted one is text.
        b"!" if plain && is_null(&value) => Value::Null,
        b"!" if plain && !resolves_to_text(&value) => bail!(UNSUPPORTED_TAG),
        b"!" | b"!!str" => Value::String(text()?),
        b"!!null" if is_null(&value) => Value::Null,
        t if t.starts_with(b"!!") => bail!(UNSUPPORTED_TAG),
        // Any other local tag is unknown to yaml.v3: the scalar is its text.
        t if t.starts_with(b"!") => Value::String(text()?),
        _ => return Ok(None),
    }))
}

/// A rewritten scalar.
#[derive(Clone)]
struct Found {
    /// Entry or item positions.
    path: Vec<usize>,
    /// The mapping key, or `None` for a list item, at each position.
    keys: Vec<Key>,
    /// What yaml.v3 reads.
    value: Value,
    kind: Kind,
}

type Key = Option<Rc<str>>;

#[derive(Clone, Copy, PartialEq)]
enum Kind {
    Tag,
    Binary,
    /// The text of a plain scalar serde_yaml_ng may read as a number or boolean, or of
    /// one tagged `!!int`, `!!float` or `!!bool`; applied only where it is a
    /// [`literal_field`] (`true`), and kept elsewhere only for an anchor's aliases.
    Literal(bool),
}

struct Walk {
    events: std::vec::IntoIter<Event>,
    defined: HashSet<Vec<u8>>,
    /// Anchors of sequences.
    sequences: HashSet<Vec<u8>>,
    /// Each anchor's rewritten scalars, by their path below it.
    anchors: HashMap<Vec<u8>, Vec<Found>>,
    /// How many anchored collections are around the current node.
    anchored: usize,
    /// The keys [`Self::literal`] last judged, and whether they are a [`literal_field`].
    last_field: Option<(Vec<Key>, bool)>,
    /// Whether literals are read at all, and the config keys the text sits at.
    literals: Option<Vec<Key>>,
    /// The key of each position in the current path.
    keys: Vec<Key>,
    /// Rewritten scalars, by their path from the root.
    found: Vec<Found>,
    /// How many of them are alias copies.
    copies: usize,
    /// How many anchor entries aliases visited, [`MAX_VISITS`] at most.
    visits: usize,
    mode: Mode,
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub(super) enum Mode {
    /// A config file: literals are its string settings.
    File,
    /// A JSON management body, whose `<<` keys are merges: Go saves its keys plain.
    Body,
    /// Literals are the scalars of plugin configs, as Go hands a plugin its raw node,
    /// tagged [`PLAIN`]: `007` is a string to serde_yaml_ng too, but quoted it is another.
    Plugins,
}

/// The tag of a plain scalar's text in [`Mode::Plugins`].
pub const PLAIN: &str = "!cpa-plain";

/// `text` as a YAML double-quoted scalar that reads back as `text`: line breaks, C0 and
/// C1 controls, DEL, U+2028, U+2029, U+FEFF, U+FFFE and U+FFFF escaped.
pub fn double_quoted(text: &str) -> String {
    let mut out = String::with_capacity(text.len() + 2);
    out.push('"');
    for c in text.chars() {
        match c {
            '"' => out.push_str("\\\""),
            '\\' => out.push_str("\\\\"),
            '\n' => out.push_str("\\n"),
            '\t' => out.push_str("\\t"),
            '\r' => out.push_str("\\r"),
            '\u{2028}' => out.push_str("\\L"),
            '\u{2029}' => out.push_str("\\P"),
            '\u{feff}' => out.push_str("\\uFEFF"),
            '\u{fffe}' => out.push_str("\\uFFFE"),
            '\u{ffff}' => out.push_str("\\uFFFF"),
            c if c < ' ' || ('\u{7f}'..='\u{9f}').contains(&c) => out.push_str(&format!("\\x{:02X}", c as u32)),
            c => out.push(c),
        }
    }
    out.push('"');
    out
}

impl Walk {
    fn next(&mut self) -> anyhow::Result<Event> {
        self.events.next().context("config YAML ended early")
    }

    fn define(&mut self, anchor: &Option<Vec<u8>>) -> anyhow::Result<()> {
        if let Some(name) = anchor {
            ensure!(self.defined.insert(name.clone()), REPEATED_ANCHOR);
        }
        Ok(())
    }

    /// A literal at `keys`, if it is kept: it is a [`literal_field`], or it is anchored,
    /// so an alias may make it one. Decided here, not over the tree afterwards, so the
    /// cost stays linear in the file and the aliases' copies.
    /// A literal's value: its text, or in [`Mode::Plugins`] the YAML that writes it as
    /// Go writes the raw node, tagged [`PLAIN`]: a standard tag stays (`!!float 1.50`),
    /// and a quoted scalar it tags stays quoted.
    fn text_value(&self, text: String, tag: Option<&[u8]>, plain: bool) -> Value {
        if self.mode != Mode::Plugins {
            return Value::String(text);
        }
        let text = match tag.and_then(|t| t.strip_prefix(b"tag:yaml.org,2002:")) {
            Some(name) => {
                let name = String::from_utf8_lossy(name);
                let text = if plain { text } else { double_quoted(&text) };
                format!("!!{name} {text}")
            }
            None => text,
        };
        Value::Tagged(Box::new(serde_yaml_ng::value::TaggedValue {
            tag: serde_yaml_ng::value::Tag::new(PLAIN),
            value: Value::String(text),
        }))
    }

    fn literal(&mut self, keys: &[Key], anchored: bool) -> Option<Kind> {
        let at = self.literals.as_ref()?;
        // The items of one list share their keys.
        let field = match &self.last_field {
            Some((last, field)) if last.as_slice() == keys => *field,
            _ => {
                let field = if self.mode == Mode::Plugins {
                    plugin_config(keys)
                } else if at.is_empty() {
                    literal_field(keys)
                } else {
                    literal_field(&[at.as_slice(), keys].concat())
                };
                self.last_field = Some((keys.to_vec(), field));
                field
            }
        };
        (field || anchored).then_some(Kind::Literal(field))
    }

    fn node(&mut self, event: Event, path: &mut Vec<usize>, key: bool) -> anyhow::Result<()> {
        let start = self.found.len();
        let anchor = match event {
            Event::Scalar {
                anchor,
                tag,
                plain,
                value,
            } => {
                self.define(&anchor)?;
                let anchored = anchor.is_some() || self.anchored > 0;
                let literal = match &tag {
                    // yaml.v3 reads a scalar tagged as a number or boolean into a string
                    // field as its text, as it does an untagged one. A plugin also gets
                    // a timestamp with its tag, which `any` reads as a time.
                    Some(tag) => {
                        matches!(
                            tag.as_slice(),
                            b"tag:yaml.org,2002:int" | b"tag:yaml.org,2002:float" | b"tag:yaml.org,2002:bool"
                        ) || (self.mode == Mode::Plugins && tag.as_slice() == b"tag:yaml.org,2002:timestamp")
                    }
                    None => plain && !is_null(&value) && !resolves_to_text(&value),
                };
                // yaml.v3's layout code matches keys by their node, tag included
                // (`!!str <<` is no merge key).
                ensure!(tag.is_none() || !key, UNSUPPORTED_KEY);
                if literal {
                    if key {
                        // An anchored key: its aliases are values, which take its text.
                        if let Some(name) = &anchor {
                            let text = String::from_utf8(value).ok().context("config YAML is not UTF-8")?;
                            let found = Found {
                                path: Vec::new(),
                                keys: Vec::new(),
                                value: self.text_value(text, None, true),
                                kind: Kind::Literal(false),
                            };
                            self.anchors.insert(name.clone(), vec![found]);
                            return Ok(());
                        }
                    } else if let Some(kind) = {
                        let keys = std::mem::take(&mut self.keys);
                        let kind = self.literal(&keys, anchored);
                        self.keys = keys;
                        kind
                    } {
                        let text = String::from_utf8(value).ok().context("config YAML is not UTF-8")?;
                        let value = self.text_value(text, tag.as_deref(), plain);
                        self.found.push(Found {
                            path: path.clone(),
                            keys: self.keys.clone(),
                            value,
                            kind,
                        });
                    }
                } else if let Some(tag) = tag {
                    let kind = match tag.as_slice() {
                        b"tag:yaml.org,2002:binary" | b"!!binary" => Kind::Binary,
                        _ => Kind::Tag,
                    };
                    if let Some(v) = rewrite(&tag, plain, value)? {
                        self.found.push(Found {
                            path: path.clone(),
                            keys: self.keys.clone(),
                            value: v,
                            kind,
                        });
                    }
                }
                anchor
            }
            Event::Alias(name) => {
                ensure!(!key, UNSUPPORTED_KEY);
                self.visits += self.anchors.get(&name).map_or(0, Vec::len);
                ensure!(self.visits <= MAX_VISITS, "config YAML aliases expand too far");
                let copies = self.anchors.get(&name).cloned().unwrap_or_default();
                for mut below in copies {
                    let keys = [self.keys.as_slice(), &below.keys].concat();
                    if let Kind::Literal(_) = below.kind {
                        match self.literal(&keys, self.anchored > 0) {
                            Some(kind) => below.kind = kind,
                            None => continue,
                        }
                    }
                    self.copies += 1;
                    ensure!(self.copies <= MAX_COPIES, "config YAML aliases expand too far");
                    self.found.push(Found {
                        path: [path.as_slice(), &below.path].concat(),
                        keys,
                        ..below
                    });
                }
                None
            }
            Event::Start { anchor, mapping } => {
                // Paths address mapping values only; Go rejects collection keys.
                ensure!(!key, COLLECTION_KEY);
                self.define(&anchor)?;
                if let Some(name) = anchor.as_ref().filter(|_| !mapping) {
                    self.sequences.insert(name.clone());
                }
                if anchor.is_some() {
                    self.anchored += 1;
                }
                let mut i = 0;
                loop {
                    let event = self.next()?;
                    if matches!(event, Event::End) {
                        break;
                    }
                    if mapping {
                        // yaml.v3 `isMerge` takes an untagged `<<` key only when plain; a
                        // quoted one is refused here, a tagged one below.
                        let merge = matches!(&event, Event::Scalar { tag: None, value, .. } if value == b"<<");
                        ensure!(
                            !merge || self.mode == Mode::Body || matches!(event, Event::Scalar { plain: true, .. }),
                            QUOTED_MERGE_KEY
                        );
                        let name: Rc<str> = match &event {
                            Event::Scalar { value, .. } => String::from_utf8_lossy(value).into(),
                            _ => "".into(),
                        };
                        self.node(event, path, true)?;
                        let value = self.next()?;
                        // yaml.v3 `merge`: an alias operand must name a mapping.
                        if merge && let Event::Alias(name) = &value {
                            ensure!(!self.sequences.contains(name), super::MERGE_NOT_MAPS);
                        }
                        path.push(i);
                        self.keys.push(Some(name));
                        self.node(value, path, false)?;
                    } else {
                        path.push(i);
                        self.keys.push(None);
                        self.node(event, path, false)?;
                    }
                    path.pop();
                    self.keys.pop();
                    i += 1;
                }
                if anchor.is_some() {
                    self.anchored -= 1;
                }
                anchor
            }
            Event::End => bail!("config YAML is malformed"),
        };
        if let Some(name) = anchor {
            let below = self.found[start..]
                .iter()
                .map(|f| Found {
                    path: f.path[path.len()..].to_vec(),
                    keys: f.keys[path.len()..].to_vec(),
                    ..f.clone()
                })
                .collect();
            self.anchors.insert(name, below);
        }
        Ok(())
    }
}