Skip to main content

mutations/
mutations.rs

1//! Emits valid and mutated payloads together with this crate's verdict on each, as JSON on
2//! stdout. CI pipes this into `scripts/verify_mutations.php`, which asks the real Laravel
3//! encrypter about the same payloads: both must accept and reject exactly the same ones, and
4//! return the same plaintext for the accepted ones.
5
6use std::io::Write as _;
7
8use base64::engine::general_purpose::STANDARD;
9use base64::Engine as _;
10use laravel_rust_encryption::{Cipher, Encrypter};
11use serde_json::{json, Value};
12
13/// Field values that exercise PHP's loose typing: null, empty, "0", numbers, arrays, short
14/// or invalid base64.
15fn odd_values() -> Vec<Value> {
16    vec![
17        json!(null),
18        json!(""),
19        json!("0"),
20        json!(5),
21        json!(true),
22        json!([]),
23        json!("AAAA"),
24        json!("***"),
25        json!("AAAAAAAAAAAAAAAAAAAAAA=="),
26    ]
27}
28
29fn main() {
30    let mut state = 0x1234_5678_9abc_def1_u64;
31    let mut next = move || {
32        state ^= state << 13;
33        state ^= state >> 7;
34        state ^= state << 17;
35        state
36    };
37    let encode = |j: &Value| STANDARD.encode(serde_json::to_vec(j).expect("serialize"));
38
39    let mut cases = Vec::new();
40    for cipher in [
41        Cipher::Aes128Cbc,
42        Cipher::Aes256Cbc,
43        Cipher::Aes128Gcm,
44        Cipher::Aes256Gcm,
45    ] {
46        let enc = Encrypter::new(&vec![0x2b; cipher.key_size()], cipher).expect("valid key");
47
48        for text in ["", "hello", "0123456789abcdef"] {
49            let valid = enc.encrypt_string(text).expect("encrypt");
50            let base: Value =
51                serde_json::from_slice(&STANDARD.decode(&valid).expect("base64")).expect("json");
52
53            let mut payloads = vec![
54                valid.clone(),
55                format!(" {valid}\n"),
56                valid.trim_end_matches('=').to_owned(),
57            ];
58
59            // Field-level edits: odd values, removal, upper case, an extra character.
60            for field in ["iv", "value", "mac", "tag"] {
61                for odd in odd_values() {
62                    let mut j = base.clone();
63                    j[field] = odd;
64                    payloads.push(encode(&j));
65                }
66                let mut j = base.clone();
67                j.as_object_mut().expect("object").remove(field);
68                payloads.push(encode(&j));
69
70                let text = base[field].as_str().expect("string").to_owned();
71                let mut j = base.clone();
72                j[field] = json!(text.to_uppercase());
73                payloads.push(encode(&j));
74                let mut j = base.clone();
75                j[field] = json!(format!("{text}A"));
76                payloads.push(encode(&j));
77            }
78
79            // Byte-level edits of the outer base64 text.
80            for _ in 0..600 {
81                let mut b = valid.clone().into_bytes();
82                let at = next() as usize % b.len();
83                match next() % 5 {
84                    0 => b[at] = b"ABCxyz09+/=!\n "[next() as usize % 14],
85                    1 => {
86                        b.remove(at);
87                    }
88                    2 => b.truncate(at),
89                    3 => b.insert(at, b" \n=*"[next() as usize % 4]),
90                    _ => {
91                        let other = next() as usize % b.len();
92                        b.swap(at, other);
93                    }
94                }
95                payloads.push(String::from_utf8_lossy(&b).into_owned());
96            }
97
98            for payload in payloads {
99                let plain = enc.decrypt_string(&payload).ok();
100                cases.push(json!({ "cipher": cipher.name(), "payload": payload, "plain": plain }));
101            }
102        }
103    }
104
105    // A closed pipe (PHP exited early) is not an error worth a panic.
106    let _ = writeln!(
107        std::io::stdout(),
108        "{}",
109        json!({ "key_b64": STANDARD.encode([0x2b; 32]), "cases": cases })
110    );
111}