fallow-api 3.44.0

Programmatic API contract types for fallow
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
//! The recorded arguments of the exported functions, kept in the gitignored
//! cache directory (`<cache dir>/mutation/recorded-args.json`).
//!
//! The record pass of a run gives the distinct inputs of each wrapped export. The
//! store keeps them for [`TIME_TO_LIVE_SECS`]. The values never leave the
//! cache directory: a report holds only counts and reasons. Before an input
//! is stored, the redaction removes each input that holds a value under a
//! secret key name (`token`, `secret`, `password`, `authorization`, and
//! similar) or a string that looks like a secret (high entropy), and counts
//! it as not replayable. In `shapes` mode, the store keeps the types of the
//! arguments only.

use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::mutation::RecordArgsMode;
use crate::mutation::protocol::{RecordRefusal, RecordedFunction};
use crate::mutation::replay::canonical;

/// The time that a recorded input stays usable: 7 days.
pub const TIME_TO_LIVE_SECS: u64 = 7 * 24 * 60 * 60;

/// The version of the store format.
const STORE_FORMAT: u32 = 1;

/// The tag key of the replay encoding.
const TAG: &str = "$replay";

/// Key names whose value is a secret. The match ignores case, `-` and `_`.
const SECRET_KEY_PARTS: &[&str] = &[
    "token",
    "secret",
    "password",
    "passwd",
    "pwd",
    "authorization",
    "auth",
    "bearer",
    "jwt",
    "apikey",
    "accesskey",
    "signature",
    "cookie",
    "credential",
    "privatekey",
    "session",
];

/// The reasons that the companion gives for a call that it cannot record.
/// Another reason becomes [`UNREADABLE_REASON`], so a report never holds
/// text from user data.
const KNOWN_REASONS: &[&str] = &[
    "a function",
    "a symbol",
    "a proxy",
    "a cyclic value",
    "a value that is too large",
    "an accessor property",
    "an object with a symbol key",
    "an object that this runtime cannot check for a proxy",
    UNREADABLE_REASON,
];
const UNREADABLE_REASON: &str = "a value that cannot be read";
/// The prefix of the class instance reason, which names a class of the code.
const CLASS_INSTANCE_REASON: &str = "a class instance";

/// A string at least this long, with no space, can look like a secret.
const SECRET_MIN_LEN: usize = 24;
/// The Shannon entropy in bits for each character from which such a string
/// looks like a secret. A UUID or a hex digest stays below it.
const SECRET_MIN_ENTROPY: f64 = 4.2;

const SECRET_KEY_REASON: &str = "a value under a secret key name";
const SECRET_STRING_REASON: &str = "a string that looks like a secret";

/// One stored input.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct StoredInput {
    /// The first 12 hex digits of the SHA-256 digest of the canonical
    /// arguments.
    pub input_id: String,
    /// The arguments in the replay encoding. `None` in `shapes` mode.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub args: Option<Vec<serde_json::Value>>,
    /// The types of the arguments.
    pub shape: serde_json::Value,
}

/// The stored inputs of one exported function.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct StoredFunction {
    pub recorded_at: u64,
    pub mode: RecordArgsMode,
    pub inputs: Vec<StoredInput>,
    #[serde(default)]
    pub not_replayable: Vec<StoredRefusal>,
}

/// The count of calls of one reason that the store did not keep.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct StoredRefusal {
    pub reason: String,
    pub count: u64,
}

#[derive(Debug, Default, Serialize, Deserialize)]
struct Store {
    format: u32,
    functions: std::collections::BTreeMap<String, StoredFunction>,
}

/// The path of the store below the cache directory.
pub fn store_path(cache_dir: &Path) -> PathBuf {
    cache_dir.join("mutation").join("recorded-args.json")
}

/// The input id of an argument list.
pub fn input_id(args: &[serde_json::Value]) -> String {
    let text = canonical(&serde_json::Value::Array(args.to_vec()));
    let digest = Sha256::digest(text.as_bytes());
    digest.iter().take(6).fold(String::new(), |mut hex, byte| {
        use std::fmt::Write as _;
        let _ = write!(hex, "{byte:02x}");
        hex
    })
}

fn normalized_key(key: &str) -> String {
    key.chars()
        .filter(|char| *char != '-' && *char != '_')
        .flat_map(char::to_lowercase)
        .collect()
}

fn is_secret_key(key: &str) -> bool {
    let key = normalized_key(key);
    SECRET_KEY_PARTS.iter().any(|part| key.contains(part))
}

/// The Shannon entropy of `text`, in bits for each character.
fn entropy(text: &str) -> f64 {
    let mut counts: rustc_hash::FxHashMap<char, u32> = rustc_hash::FxHashMap::default();
    let mut total = 0_u32;
    for char in text.chars() {
        *counts.entry(char).or_default() += 1;
        total += 1;
    }
    let total = f64::from(total);
    counts
        .values()
        .map(|&count| {
            let share = f64::from(count) / total;
            -share * share.log2()
        })
        .sum()
}

/// The characters that separate the parts of a path, a URL or a query. A
/// key is one part, so a long path with many names does not count as one
/// key.
const PART_SEPARATORS: &[char] = &['/', '\\', ':', '?', '&', '=', '#', ',', ';'];

/// A byte array of this size range can be a key.
const KEY_BYTES: std::ops::RangeInclusive<usize> = 16..=512;
/// The byte entropy in bits from which a key-sized byte array looks random.
const KEY_BYTES_MIN_ENTROPY: f64 = 3.5;

/// True for the `values` of a key-sized byte array with random-looking
/// bytes.
fn looks_like_key_bytes(values: &serde_json::Value) -> bool {
    let Some(values) = values.as_array() else {
        return false;
    };
    if !KEY_BYTES.contains(&values.len()) {
        return false;
    }
    let text: String = values
        .iter()
        .filter_map(serde_json::Value::as_u64)
        .filter_map(|byte| char::from_u32(u32::try_from(byte).ok()?))
        .collect();
    entropy(&text) >= KEY_BYTES_MIN_ENTROPY
}

/// A hex text at least this long looks like a key. Its entropy cannot pass
/// [`SECRET_MIN_ENTROPY`], so it has its own rule.
const SECRET_MIN_HEX_LEN: usize = 32;

/// True for one word with a part that looks like a key: long, and with
/// high entropy, or a long hex text.
fn is_secret_word(word: &str) -> bool {
    // A base64 key can hold `/`, so a word of base64 characters only also
    // counts as a whole.
    let base64 = word.bytes().all(|byte| {
        byte.is_ascii_alphanumeric() || matches!(byte, b'+' | b'/' | b'=' | b'-' | b'_')
    });
    if base64 && word.chars().count() >= SECRET_MIN_LEN && entropy(word) >= SECRET_MIN_ENTROPY {
        return true;
    }
    word.split(PART_SEPARATORS).any(|part| {
        let len = part.chars().count();
        (len >= SECRET_MIN_LEN && entropy(part) >= SECRET_MIN_ENTROPY)
            || (len >= SECRET_MIN_HEX_LEN && part.bytes().all(|byte| byte.is_ascii_hexdigit()))
    })
}

/// True for a three-part base64url text, the shape of a JSON Web Token.
fn is_jwt(word: &str) -> bool {
    let parts: Vec<&str> = word.split('.').collect();
    parts.len() == 3
        && parts.iter().all(|part| {
            part.len() >= 8
                && part
                    .bytes()
                    .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-' || byte == b'_')
        })
}

/// True for a URL with a password in its user part, such as
/// `https://user:pass@host`.
fn has_url_password(text: &str) -> bool {
    text.split("://").skip(1).any(|rest| {
        let authority = rest.split(['/', '?', '#']).next().unwrap_or_default();
        authority
            .rsplit_once('@')
            .is_some_and(|(user, _)| user.contains(':'))
    })
}

fn looks_like_secret(text: &str) -> bool {
    let lower = text.trim_start().to_ascii_lowercase();
    lower.starts_with("bearer ")
        || lower.starts_with("basic ")
        || text.contains("-----BEGIN")
        || has_url_password(text)
        || text
            .split_whitespace()
            .any(|word| is_secret_word(word) || is_jwt(word))
}

/// The redaction reason of `value`, or `None` when it holds no secret.
fn secret_reason(value: &serde_json::Value) -> Option<&'static str> {
    match value {
        serde_json::Value::String(text) => looks_like_secret(text).then_some(SECRET_STRING_REASON),
        serde_json::Value::Array(items) => items.iter().find_map(secret_reason),
        serde_json::Value::Object(map) => match map.get(TAG).and_then(serde_json::Value::as_str) {
            Some("bytes") if map.get("values").is_some_and(looks_like_key_bytes) => {
                Some(SECRET_STRING_REASON)
            }
            // A map and an escaped object hold `[key, value]` entries. A
            // string key counts as a key name.
            Some("map" | "object") => map.get("entries").and_then(secret_in_entries),
            Some(_) => map.values().find_map(secret_reason),
            None => map.iter().find_map(|(key, item)| {
                if is_secret_key(key) || looks_like_secret(key) {
                    return Some(SECRET_KEY_REASON);
                }
                secret_reason(item)
            }),
        },
        _ => None,
    }
}

/// The reason of the first secret in the `[key, value]` entries of a map or
/// an escaped object.
fn secret_in_entries(entries: &serde_json::Value) -> Option<&'static str> {
    entries.as_array()?.iter().find_map(|entry| {
        let pair = entry.as_array()?;
        let (key, value) = (pair.first()?, pair.get(1)?);
        if key
            .as_str()
            .is_some_and(|key| is_secret_key(key) || looks_like_secret(key))
        {
            return Some(SECRET_KEY_REASON);
        }
        secret_reason(key).or_else(|| secret_reason(value))
    })
}

/// A reason of the companion as the report may show it.
fn known_reason(reason: &str) -> &'static str {
    if reason.starts_with(CLASS_INSTANCE_REASON) {
        return CLASS_INSTANCE_REASON;
    }
    KNOWN_REASONS
        .iter()
        .find(|known| **known == reason)
        .copied()
        .unwrap_or(UNREADABLE_REASON)
}

/// The types of a value in the replay encoding.
fn shape(value: &serde_json::Value) -> serde_json::Value {
    use serde_json::Value;
    match value {
        Value::Null => Value::from("null"),
        Value::Bool(_) => Value::from("boolean"),
        Value::Number(_) => Value::from("number"),
        Value::String(_) => Value::from("string"),
        Value::Array(items) => Value::Array(items.iter().map(shape).collect()),
        Value::Object(map) => match map.get(TAG).and_then(Value::as_str) {
            Some(tag) => Value::from(tag),
            None => Value::Object(
                map.iter()
                    .map(|(key, item)| (key.clone(), shape(item)))
                    .collect(),
            ),
        },
    }
}

fn read_store(path: &Path) -> Store {
    std::fs::read(path)
        .ok()
        .and_then(|bytes| serde_json::from_slice::<Store>(&bytes).ok())
        .filter(|store| store.format == STORE_FORMAT)
        .unwrap_or_default()
}

/// An entry is fresh for its time to live. An entry from the future (a clock
/// that moved back) is not.
const fn is_fresh(entry: &StoredFunction, now: u64) -> bool {
    entry.recorded_at <= now && now - entry.recorded_at <= TIME_TO_LIVE_SECS
}

fn add_refusal(refusals: &mut Vec<StoredRefusal>, reason: &str, count: u64) {
    match refusals.iter_mut().find(|refusal| refusal.reason == reason) {
        Some(refusal) => refusal.count += count,
        None => refusals.push(StoredRefusal {
            reason: reason.to_owned(),
            count,
        }),
    }
}

/// The stored form of one recorded function: redacted, and in `shapes`
/// mode without values.
fn stored_function(function: &RecordedFunction, mode: RecordArgsMode, now: u64) -> StoredFunction {
    let mut not_replayable = Vec::new();
    for RecordRefusal { reason, count } in &function.not_replayable {
        add_refusal(&mut not_replayable, known_reason(reason), *count);
    }
    let mut inputs = Vec::new();
    for args in &function.inputs {
        let as_array = serde_json::Value::Array(args.clone());
        if let Some(reason) = secret_reason(&as_array) {
            add_refusal(&mut not_replayable, reason, 1);
            continue;
        }
        inputs.push(StoredInput {
            input_id: input_id(args),
            args: (mode == RecordArgsMode::Values).then(|| args.clone()),
            shape: shape(&as_array),
        });
    }
    not_replayable.sort_by(|left, right| left.reason.cmp(&right.reason));
    StoredFunction {
        recorded_at: now,
        mode,
        inputs,
        not_replayable,
    }
}

/// The recorded functions with one entry for each key: the inputs and the
/// reasons of a key that the pass gives twice join.
fn merged(functions: &[RecordedFunction]) -> Vec<RecordedFunction> {
    let mut by_key: std::collections::BTreeMap<&str, RecordedFunction> =
        std::collections::BTreeMap::new();
    for function in functions {
        let entry = by_key
            .entry(function.key.as_str())
            .or_insert_with(|| RecordedFunction {
                key: function.key.clone(),
                inputs: Vec::new(),
                not_replayable: Vec::new(),
            });
        for input in &function.inputs {
            if !entry.inputs.contains(input) {
                entry.inputs.push(input.clone());
            }
        }
        entry
            .not_replayable
            .extend(function.not_replayable.iter().cloned());
    }
    by_key.into_values().collect()
}

/// Writes `bytes` to `path` through a private staging file, so a reader
/// never sees a partial file and other users cannot read the values.
pub(super) fn write_private(path: &Path, bytes: &[u8]) -> std::io::Result<()> {
    use std::io::Write as _;
    static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
    let unique = NEXT.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
    let staging = path.with_extension(format!("{}.{unique}.tmp", std::process::id()));
    let mut options = std::fs::OpenOptions::new();
    options.write(true).create_new(true);
    #[cfg(unix)]
    {
        use std::os::unix::fs::OpenOptionsExt as _;
        options.mode(0o600);
    }
    let written = options
        .open(&staging)
        .and_then(|mut file| file.write_all(bytes).and_then(|()| file.sync_all()))
        .and_then(|()| std::fs::rename(&staging, path));
    if written.is_err() {
        let _ = std::fs::remove_file(&staging);
    }
    written
}

/// Removes the expired entries of the store, so a value does not stay on
/// disk past its time to live. A run calls it also without recording.
pub fn prune(cache_dir: &Path, now: u64) {
    let path = store_path(cache_dir);
    if !path.is_file() {
        return;
    }
    let mut store = read_store(&path);
    let before = store.functions.len();
    store.functions.retain(|_, entry| is_fresh(entry, now));
    if store.functions.len() == before {
        return;
    }
    if store.functions.is_empty() {
        let _ = std::fs::remove_file(&path);
    } else if let Ok(bytes) = serde_json::to_vec_pretty(&store) {
        let _ = write_private(&path, &bytes);
    }
}

/// Writes the recorded functions of a run into the store. Each function of
/// the run replaces its earlier entry, and each expired entry goes.
///
/// # Errors
///
/// Returns the IO error of the write.
pub fn store(
    cache_dir: &Path,
    mode: RecordArgsMode,
    functions: &[RecordedFunction],
    now: u64,
) -> std::io::Result<Vec<(String, StoredFunction)>> {
    crate::mutation::last_report::ignore_cache_dir(cache_dir)?;
    let path = store_path(cache_dir);
    let mut store = read_store(&path);
    store.format = STORE_FORMAT;
    store.functions.retain(|_, entry| is_fresh(entry, now));
    let mut written = Vec::new();
    for function in &merged(functions) {
        let entry = stored_function(function, mode, now);
        store.functions.insert(function.key.clone(), entry.clone());
        written.push((function.key.clone(), entry));
    }
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }
    let text = serde_json::to_vec_pretty(&store).map_err(std::io::Error::other)?;
    write_private(&path, &text)?;
    Ok(written)
}

/// The fresh stored inputs of `key` (`<file>#<export>`), or `None`.
pub fn load(cache_dir: &Path, key: &str, now: u64) -> Option<StoredFunction> {
    read_store(&store_path(cache_dir))
        .functions
        .remove(key)
        .filter(|entry| is_fresh(entry, now))
}

/// The result of the record pass of a run.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RecordPass {
    pub functions: Vec<RecordedFunction>,
    /// Why the pass gave no records, for example a runner that records
    /// nothing. `None` when the pass ran.
    pub error: Option<String>,
}

/// Stores the recorded functions of a run and gives the report summary:
/// counts and reasons, never values. `None` without recording. `pass` is
/// `None` when no test ran.
pub fn save_recording(
    cache_dir: &Path,
    mode: RecordArgsMode,
    pass: Option<&RecordPass>,
) -> Option<fallow_output::MutationRecordingOutput> {
    let output_mode = match mode {
        RecordArgsMode::Off => return None,
        RecordArgsMode::Shapes => fallow_output::MutationRecordMode::Shapes,
        RecordArgsMode::Values => fallow_output::MutationRecordMode::Values,
    };
    let summary = |saved, error: Option<String>, entries: Vec<(String, StoredFunction)>| {
        fallow_output::MutationRecordingOutput {
            mode: output_mode,
            saved,
            error,
            functions: summary_functions(entries),
        }
    };
    let Some(pass) = pass else {
        return Some(summary(
            false,
            Some("no test ran, so no argument was recorded".to_owned()),
            Vec::new(),
        ));
    };
    if let Some(error) = &pass.error {
        return Some(summary(false, Some(error.clone()), Vec::new()));
    }
    Some(match store(cache_dir, mode, &pass.functions, now_secs()) {
        Ok(entries) => summary(true, None, entries),
        Err(error) => summary(
            false,
            Some(format!(
                "the cache file cannot be written: {}",
                error.kind()
            )),
            merged(&pass.functions)
                .iter()
                .map(|function| (function.key.clone(), stored_function(function, mode, 0)))
                .collect(),
        ),
    })
}

fn summary_functions(
    entries: Vec<(String, StoredFunction)>,
) -> Vec<fallow_output::MutationRecordedFunction> {
    let mut functions: Vec<fallow_output::MutationRecordedFunction> = entries
        .into_iter()
        .filter_map(|(key, entry)| {
            let (file, export) = key.rsplit_once('#')?;
            Some(fallow_output::MutationRecordedFunction {
                file: file.to_owned(),
                export: export.to_owned(),
                input_count: entry.inputs.len(),
                not_replayable: entry
                    .not_replayable
                    .into_iter()
                    .map(|refusal| fallow_output::MutationNotReplayable {
                        reason: refusal.reason,
                        count: refusal.count,
                    })
                    .collect(),
            })
        })
        .collect();
    functions.sort_by(|left, right| {
        (left.file.as_str(), left.export.as_str())
            .cmp(&(right.file.as_str(), right.export.as_str()))
    });
    functions
}

/// The current time in seconds since the Unix epoch.
pub fn now_secs() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |elapsed| elapsed.as_secs())
}

pub mod witnesses;

#[cfg(test)]
mod tests;