khive-storage 0.21.0

Storage capability contracts and backend-neutral request context.
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
use super::{check, scan};
use serde_json::json;

fn aws() -> String {
    ["AK", "IA", "QWERTYUIOPASDFGH"].concat()
}

fn github() -> String {
    ["gh", "p_", "aB3dE5gH7jK9mN1pQ3sT5vW7yZ9bC1dF3hJ5"].concat()
}

fn url() -> String {
    ["https://user:", "Zx9", "Qw7Er5Ty3Ui1", "@example.com/x"].concat()
}

/// (name, plain credential, the same credential with one JSON escape)
fn cases() -> Vec<(&'static str, String, String)> {
    let aws = aws();
    let github = github();
    let url = url();
    vec![
        ("aws", aws.clone(), format!("\\u0041{}", &aws[1..])),
        ("github", github.clone(), format!("\\u0067{}", &github[1..])),
        ("url", url.clone(), url.replace('/', "\\/")),
    ]
}

fn json_text(inner: &str) -> String {
    format!("{{\"v\": \"{inner}\"}}")
}

#[test]
fn escaped_credentials_in_json_text_were_invisible_to_the_raw_scan() {
    for (name, _, escaped) in cases() {
        assert!(
            scan(&json_text(&escaped)).is_none(),
            "{name}: the raw-text scan is the control and must read the escaped form clean"
        );
    }
}

#[test]
fn json_text_with_an_escaped_credential_is_refused() {
    for (name, _, escaped) in cases() {
        let content = json_text(&escaped);
        assert!(check(&content).is_err(), "{name}: {content}");
    }
}

#[test]
fn json_text_with_a_plain_credential_is_still_refused() {
    for (name, plain, _) in cases() {
        assert!(check(&json_text(&plain)).is_err(), "{name}");
        assert!(check(&plain).is_err(), "{name}");
    }
}

#[test]
fn an_escaped_credential_in_a_key_or_nested_array_is_refused() {
    let (_, _, escaped) = cases().remove(0);
    for content in [
        format!("{{\"{escaped}\": 1}}"),
        format!("[1, {{\"a\": [\"x\", \"{escaped}\"]}}]"),
        format!("\"{escaped}\""),
    ] {
        assert!(check(&content).is_err(), "{content}");
    }
}

#[test]
fn json_text_nested_inside_a_json_string_is_decoded_again() {
    let (_, _, escaped) = cases().remove(1);
    let inner = json_text(&escaped);
    let outer = json!({ "body": inner }).to_string();
    assert!(scan(&outer).is_none(), "control: raw scan reads it clean");
    assert!(check(&outer).is_err(), "{outer}");
}

#[test]
fn escaped_text_that_is_not_a_json_document_keeps_its_verdict() {
    for content in [
        "see C:\\\\Users\\\\docs and \"quoted \\\" text\"",
        "{not json \\u0041 at all",
        "plain prose with a \\n newline escape",
    ] {
        assert_eq!(check(content).is_ok(), scan(content).is_none(), "{content}");
    }
}

#[test]
fn benign_json_with_escapes_still_passes() {
    for content in [
        r#"{"path": "docs\/guide.md", "text": "line one\nline two \u00e9"}"#,
        r#"["tab\there", {"quote": "she said \"hi\""}]"#,
        r#"{"url": "https:\/\/example.com\/docs"}"#,
    ] {
        assert!(check(content).is_ok(), "{content}");
    }
}

fn wrap(levels: usize, inner: String) -> String {
    (0..levels).fold(inner, |text, _| json!({ "body": text }).to_string())
}

#[test]
fn credentials_nested_two_and_three_documents_deep_are_refused() {
    for (name, plain, escaped) in cases() {
        for levels in [2, 3] {
            let escaped_content = wrap(levels, json_text(&escaped));
            assert!(scan(&escaped_content).is_none(), "{name}/{levels}: control");
            for content in [wrap(levels, json_text(&plain)), escaped_content] {
                assert!(check(&content).is_err(), "{name}/{levels}: {content}");
            }
        }
    }
}

#[test]
fn nesting_past_the_decode_depth_is_refused_not_passed() {
    let benign = json_text("nothing secret here");
    let within = wrap(super::MAX_ESCAPED_JSON_DEPTH, benign.clone());
    assert!(check(&within).is_ok(), "benign nesting at the cap passes");
    let past = wrap(super::MAX_ESCAPED_JSON_DEPTH + 1, benign);
    let Err(matched) = check(&past) else {
        panic!("nesting past the cap must refuse");
    };
    assert_eq!(matched.detector, "nested-escaped-json");
}

#[test]
fn an_escaped_credential_in_a_fragment_inside_prose_is_refused() {
    for (name, _, escaped) in cases() {
        for content in [
            format!("config was {}", json_text(&escaped)),
            format!("{} was the config", json_text(&escaped)),
            format!("we saw \"{escaped}\" in the log"),
        ] {
            assert!(scan(&content).is_none(), "{name}: control: {content}");
            assert!(check(&content).is_err(), "{name}: {content}");
        }
    }
}

#[test]
fn escaped_credentials_after_stray_prose_backslashes_are_refused() {
    for (name, plain, escaped) in cases() {
        let Err(expected) = check(&plain) else {
            panic!("{name}: plain credential control must refuse");
        };
        for content in [
            format!(r#"we saw \"{escaped}" in the log"#),
            format!(r#"we saw \\"{escaped}" in the log"#),
            format!(r#"we saw \\\"{escaped}" in the log"#),
            format!(r#"she said "hello" then \"{escaped}" in the log"#),
            format!(r#"an earlier \"C:\Users\docs then \"{escaped}" in the log"#),
            format!(r#"an earlier \"\uD800 then \"{escaped}" in the log"#),
            format!(r#"we saw \"{escaped} and \"quoted\" text" in the log"#),
            format!(r#"we saw \"quoted\" text and {escaped}" in the log"#),
        ] {
            assert!(scan(&content).is_none(), "{name}: raw control: {content}");
            let Err(actual) = check(&content) else {
                panic!("{name}: escaped credential must refuse: {content}");
            };
            assert_eq!(actual.detector, expected.detector, "{name}: {content}");
        }
    }
}

#[test]
fn unclosed_escaped_credentials_are_refused_at_end_of_field() {
    for (name, plain, escaped) in cases() {
        let Err(expected) = check(&plain) else {
            panic!("{name}: plain credential control must refuse");
        };
        for content in [
            format!(r#"she said "hi then \"{escaped} in the log"#),
            format!(r#"we saw \"{escaped} in the log"#),
            format!(r#""{escaped}"#),
        ] {
            assert!(scan(&content).is_none(), "{name}: raw control: {content}");
            let Err(actual) = check(&content) else {
                panic!("{name}: unclosed escaped credential must refuse: {content}");
            };
            assert_eq!(actual.detector, expected.detector, "{name}: {content}");
        }
    }
}

#[test]
fn benign_unclosed_text_and_invalid_escape_tails_keep_their_verdict() {
    for content in [
        r#"she said "hello"#,
        r#"she said "hello then \"goodbye"#,
        r#"we saw \"docs\/guide.md"#,
        r#""line\none and \u00e9"#,
        r#""a final escaped backslash \\"#,
    ] {
        assert!(scan(content).is_none(), "raw control: {content}");
        assert!(check(content).is_ok(), "{content}");
    }
    for content in [
        r#""C:\Users\docs"#,
        r#""invalid \q"#,
        r#""short unicode \u004"#,
        r#""unpaired surrogate \uD800"#,
        "\"dangling backslash \\",
        "\"literal newline\nthen \\n",
    ] {
        if content == "\"literal newline\nthen \\n" {
            assert_eq!(
                super::escaped_json::escaped_strings(content),
                vec!["then \n"],
                "{content}"
            );
        } else {
            assert!(
                super::escaped_json::escaped_strings(content).is_empty(),
                "{content}"
            );
        }
        assert!(check(content).is_ok(), "{content}");
    }
}

#[test]
fn a_stray_prose_backslash_does_not_make_invalid_json_escapes_decodable() {
    for (name, plain, escaped) in cases() {
        let Err(expected) = check(&plain) else {
            panic!("{name}: plain credential control must refuse");
        };
        for suffix in [r"\q", r"\uD800", r"\uDC00", "\n"] {
            let content = format!(r#"we saw \"{escaped}{suffix}" in the log"#);
            assert!(scan(&content).is_none(), "{name}: raw control: {content}");
            assert_eq!(
                super::escaped_json::escaped_strings(&content),
                vec![plain.clone()],
                "{name}: {content}"
            );
            let Err(actual) = check(&content) else {
                panic!("{name}: valid credential prefix must refuse: {content}");
            };
            assert_eq!(actual.detector, expected.detector, "{name}: {content}");
        }
    }
}

#[test]
fn prose_quotes_and_backslash_paths_keep_their_verdict() {
    for content in [
        r#"she said "hello" and "goodbye" then left"#,
        r#"open C:\Users\me\docs and "C:\Program Files\app""#,
        r#"a 5" screen and a "quoted \" phrase" here"#,
        r#"see "docs\/guide.md" and "line\none""#,
        r#"a lone \" quote and \\ backslashes"#,
        r#"we saw \"docs\/guide.md" in the log"#,
        r#"an earlier \"bad\q then \"line\none" in the log"#,
        r#"we saw \"music \uD834\uDD1E and \"quoted\" text" in the log"#,
    ] {
        assert!(check(content).is_ok(), "{content}");
    }
}

#[test]
fn each_quoted_run_is_decoded_once() {
    let content = "\"a\\nb\" ".repeat(50_000) + "\"x\\\" ";
    let decoded = super::escaped_json::escaped_strings(&content);
    // The final valid unfinished run now contributes its own decoded value.
    assert_eq!(decoded.len(), 50_001);
    assert_eq!(decoded.last().map(String::as_str), Some("x\" "));
    assert!(check(&content).is_ok());
}

#[test]
fn many_quoted_runs_do_not_hide_an_unclosed_escaped_credential() {
    let prefix = "\"a\\nb\" ".repeat(50_000) + &r#"\"bad\q "#.repeat(50_000);
    for (name, plain, escaped) in cases() {
        let Err(expected) = check(&plain) else {
            panic!("{name}: plain credential control must refuse");
        };
        for (valid_runs, malformed_runs, mut expected_views) in [
            (1, 1, vec!["a\nb", " \"bad", " \""]),
            (1, 2, vec!["a\nb", " \"bad", " \"", " \""]),
            (2, 1, vec!["a\nb", "a\nb", " \"bad", " \""]),
            (2, 2, vec!["a\nb", "a\nb", " \"bad", " \"", " \""]),
        ] {
            let small = "\"a\\nb\" ".repeat(valid_runs)
                + &r#"\"bad\q "#.repeat(malformed_runs)
                + &format!("\\\"{escaped}");
            expected_views.push(&plain);
            assert_eq!(
                super::escaped_json::escaped_strings(&small),
                expected_views,
                "{name}: {valid_runs} valid / {malformed_runs} malformed runs"
            );
        }
        let content = format!("{prefix}\\\"{escaped}");
        assert!(scan(&content).is_none(), "{name}: raw control");
        let decoded = super::escaped_json::escaped_strings(&content);
        // N valid runs + M recovered quote spans + the first boundary span
        // and the final credential, for N >= 1 and M >= 1.
        assert_eq!(decoded.len(), 50_000 + 50_000 + 2, "{name}: N + M + 2");
        assert_eq!(decoded.last(), Some(&plain));
        let Err(actual) = check(&content) else {
            panic!("{name}: final escaped credential must refuse");
        };
        assert_eq!(actual.detector, expected.detector);
    }
}

#[test]
fn many_escaped_quotes_in_one_unclosed_run_are_decoded_once() {
    let prefix = "\"intro ".to_owned() + &r#"\"quoted\" "#.repeat(50_000);
    for (name, plain, escaped) in cases() {
        let Err(expected) = check(&plain) else {
            panic!("{name}: plain credential control must refuse");
        };
        let content = format!("{prefix}{escaped}");
        assert!(scan(&content).is_none(), "{name}: raw control");
        let decoded = super::escaped_json::escaped_strings(&content);
        assert_eq!(
            decoded.len(),
            1,
            "{name}: one candidate across escaped quotes"
        );
        assert!(decoded[0].ends_with(&plain));
        let Err(actual) = check(&content) else {
            panic!("{name}: unclosed escaped credential must refuse");
        };
        assert_eq!(actual.detector, expected.detector);
    }
}

#[test]
fn many_malformed_prose_quotes_do_not_hide_a_later_escaped_credential() {
    let (_, plain, escaped) = cases().remove(0);
    let Err(expected) = check(&plain) else {
        panic!("plain credential control must refuse");
    };
    let prefix = r#"\"bad\q "#.repeat(50_000);
    let content = format!("{prefix}\\\"{escaped}\"");
    assert!(scan(&content).is_none(), "raw control");
    let Err(actual) = check(&content) else {
        panic!("escaped credential must refuse");
    };
    assert_eq!(actual.detector, expected.detector);
}

#[test]
fn invalid_escapes_preserve_credential_spans_before_and_after() {
    for (name, plain, escaped) in cases() {
        let Err(expected) = check(&plain) else {
            panic!("{name}: plain credential control must refuse");
        };
        for (invalid, remainder) in [
            (r"\q", ""),
            (r"\u00", ""),
            (r"\u00Z", "Z"),
            (r"\uD800", ""),
            (r"\uDC00", ""),
            (r"\é", ""),
            ("\n", ""),
            ("\r", ""),
            ("\t", ""),
            ("\0", ""),
        ] {
            for (content, decoded) in [
                (format!("\"{escaped} {invalid}\""), format!("{plain} ")),
                (
                    format!("\"{invalid} {escaped}\""),
                    format!("{remainder} {plain}"),
                ),
                (format!("\"{escaped} {invalid}"), format!("{plain} ")),
                (
                    format!("\"{invalid} {escaped}"),
                    format!("{remainder} {plain}"),
                ),
            ] {
                assert!(scan(&content).is_none(), "{name}: raw control: {content:?}");
                let Err(actual) = check(&content) else {
                    panic!("{name}: recovered credential must refuse: {content:?}");
                };
                assert_eq!(actual.detector, expected.detector, "{name}: {content:?}");
                assert_eq!(
                    super::escaped_json::escaped_strings(&content),
                    vec![decoded],
                    "{name}: maximal valid span: {content:?}"
                );
            }
        }
        let content = format!("\"{escaped}\\");
        assert!(
            scan(&content).is_none(),
            "{name}: dangling escape raw control"
        );
        let Err(actual) = check(&content) else {
            panic!("{name}: credential before dangling escape must refuse");
        };
        assert_eq!(actual.detector, expected.detector);
        assert_eq!(
            super::escaped_json::escaped_strings(&content),
            vec![plain.clone()]
        );
        for levels in [1, 2] {
            let content = wrap(levels, format!("\"{escaped}\\q\""));
            assert!(scan(&content).is_none(), "{name}: nested raw control");
            let Err(actual) = check(&content) else {
                panic!("{name}: nested malformed candidate must refuse");
            };
            assert_eq!(actual.detector, expected.detector);
        }
    }
}

#[test]
fn invalid_escape_boundaries_do_not_join_decoded_spans() {
    for (name, plain, _) in cases() {
        assert!(
            check(&plain).is_err(),
            "{name}: complete credential control"
        );
        let (left, right) = plain.split_at(plain.len() / 2);
        let encode = |part: &str| {
            part.chars()
                .map(|c| format!("\\u{:04x}", u32::from(c)))
                .collect::<String>()
        };
        for invalid in [r"\q", r"\uD800", r"\uDC00", "\n"] {
            let content = format!("\"{}{invalid}{}\"", encode(left), encode(right));
            assert!(scan(&content).is_none(), "{name}: raw control");
            assert_eq!(
                super::escaped_json::escaped_strings(&content),
                vec![left, right],
                "{name}: {invalid:?} must separate the spans"
            );
            assert!(check(&content).is_ok(), "{name}: spans must not join");
        }
    }
    for (content, expected) in [
        (r#""é\n\q雪\t""#, vec!["é\n", "雪\t"]),
        (r#""a\uD834\uDD1E\q\u00e9""#, vec!["a𝄞", "é"]),
        (r#""\uD800\u0041""#, vec!["A"]),
        (r#""\uD800\q\uDC00""#, vec![]),
        (r#""\u00é\n""#, vec!["é\n"]),
        (r#""\é雪\n""#, vec!["雪\n"]),
        (r#""\uD800\uD834\uDD1E""#, vec!["𝄞"]),
    ] {
        assert_eq!(super::escaped_json::escaped_strings(content), expected);
        assert!(check(content).is_ok(), "benign strict span: {content}");
    }
}

#[test]
fn many_invalid_spans_are_processed_once() {
    let repetitions = 50_000;
    let content = format!("\"{}\"", r"\q\n".repeat(repetitions));
    let (decoded, work) = super::escaped_json::escaped_strings_with_work(&content, 2 * repetitions);
    assert_eq!(work.passes, 1, "one candidate requires one recovery pass");
    assert_eq!(work.tokens, 100_000, "each token is classified once");
    assert_eq!(decoded.len(), repetitions);
    assert!(decoded.iter().all(|span| span == "\n"));
    assert!(
        check(&content).is_ok(),
        "separate newline spans stay benign"
    );
    for (repetitions, expected) in [(1, vec!["\n"]), (2, vec!["\n", "\n"])] {
        let content = format!("\"{}\"", r"\q\n".repeat(repetitions));
        let (decoded, work) =
            super::escaped_json::escaped_strings_with_work(&content, 2 * repetitions);
        assert_eq!(decoded, expected);
        assert_eq!(work.passes, 1);
        assert_eq!(work.tokens, 2 * repetitions);
    }
}

#[test]
fn escaped_quote_pairs_preserve_credential_refusal() {
    for (name, plain, escaped) in cases() {
        let Err(expected) = check(&plain) else {
            panic!("{name}: plain credential control must refuse");
        };
        for (content, decoded) in [
            (
                format!(r#"we saw \"{escaped}\" in the log"#),
                format!("{plain}\" in the log"),
            ),
            (
                format!(r#"payload {{\"v\": \"{escaped}\"}} sent"#),
                format!("v\": \"{plain}\"}} sent"),
            ),
        ] {
            assert!(scan(&content).is_none(), "{name}: raw encoded control");
            let Err(actual) = check(&content) else {
                panic!("{name}: credential between escaped quotes must refuse: {content}");
            };
            assert_eq!(actual.detector, expected.detector, "{name}: {content}");
            let (views, work) = super::escaped_json::escaped_strings_with_work(&content, 0);
            assert_eq!(views, vec![decoded]);
            assert_eq!(work.passes, 0, "valid suffix uses the EOF decoder");
            assert_eq!(work.tokens, 0, "no malformed-span recovery is needed");
        }
    }
    for (content, decoded) in [
        (
            r#"we saw \"docs\/guide.md\" in the log"#,
            "docs/guide.md\" in the log",
        ),
        (
            r#"payload {\"v\": \"line\none and \u00e9\"} sent"#,
            "v\": \"line\none and é\"} sent",
        ),
    ] {
        assert!(scan(content).is_none(), "benign raw control");
        assert!(check(content).is_ok(), "benign escaped-pair content");
        let (views, work) = super::escaped_json::escaped_strings_with_work(content, 0);
        assert_eq!(views, vec![decoded]);
        assert_eq!(work.passes, 0);
        assert_eq!(work.tokens, 0);
    }
}