rullst-orm 13.0.0-alpha.2

Fast, un-opinionated, database-agnostic ORM and query builder for Rust
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
#![allow(clippy::unwrap_used, clippy::expect_used)]

use super::*;
use std::collections::BTreeMap;

/// Serializes the tests that change the process-wide key variables.
static ENVIRONMENT_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

fn environment_lock() -> std::sync::MutexGuard<'static, ()> {
    ENVIRONMENT_LOCK
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner)
}

struct EnvironmentGuard {
    values: BTreeMap<&'static str, Option<String>>,
}

impl EnvironmentGuard {
    fn new() -> Self {
        Self {
            values: BTreeMap::new(),
        }
    }

    fn remember(&mut self, key: &'static str) {
        self.values
            .entry(key)
            .or_insert_with(|| std::env::var(key).ok());
    }

    fn set(&mut self, key: &'static str, value: &str) {
        self.remember(key);
        unsafe { std::env::set_var(key, value) };
    }

    fn clear(&mut self, key: &'static str) {
        self.remember(key);
        unsafe { std::env::remove_var(key) };
    }
}

impl Drop for EnvironmentGuard {
    fn drop(&mut self) {
        for (key, value) in &self.values {
            unsafe {
                match value {
                    Some(value) => std::env::set_var(key, value),
                    None => std::env::remove_var(key),
                }
            }
        }
    }
}

fn generate_test_key_32() -> String {
    let mut bytes = [0u8; 16];
    rand::fill(&mut bytes);
    bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}

fn generate_short_key() -> String {
    let mut bytes = [0u8; 4];
    rand::fill(&mut bytes);
    bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}

#[test]
fn secret_string_encryption_round_trips() {
    let key = generate_test_key_32();
    let encrypted = encrypt_aes_gcm("Sensitive Data 123", &key).expect("encryption should work");
    assert_ne!(encrypted, "Sensitive Data 123");
    assert_eq!(
        decrypt_aes_gcm(&encrypted, &key).expect("decryption should work"),
        "Sensitive Data 123"
    );
}

#[test]
fn secret_string_redacts_debug_and_reveals_explicitly() {
    let secret = SecretString::new("my-cpf-123");
    assert_eq!(format!("{secret:?}"), "[ENCRYPTED_SECRET]");
    assert_eq!(secret.reveal_audited(), "my-cpf-123");
}

#[test]
fn legacy_payload_requires_nonce_and_authentication_tag() {
    let key = generate_test_key_32();
    for length in [11, 12, NONCE_LENGTH + TAG_LENGTH - 1] {
        let payload = STANDARD.encode(vec![0u8; length]);
        assert_eq!(
            decrypt_aes_gcm(&payload, &key).expect_err("short payload must fail"),
            PrivacyError::PayloadTooShort
        );
    }
}

#[test]
fn versioned_envelope_authenticates_context_and_key_id() {
    let key = generate_test_key_32();
    let context = model_context("accounts", "api_token");
    let encrypted = encrypt_with_context("sensitive", key.as_bytes(), "key-2026", &context)
        .expect("versioned encryption should succeed");

    assert!(encrypted.starts_with("RULLST:v2:key-2026:"));
    let envelope = parse_envelope(&encrypted).expect("envelope should parse");
    assert_eq!(envelope.key_id, "key-2026");
    assert_eq!(
        decrypt_envelope(&envelope, key.as_bytes(), &context)
            .expect("matching context should decrypt"),
        "sensitive"
    );
    assert!(matches!(
        decrypt_envelope(
            &envelope,
            key.as_bytes(),
            &model_context("accounts", "different_column")
        ),
        Err(PrivacyError::DecryptionFailed(_))
    ));
}

#[test]
fn versioned_envelope_rejects_invalid_and_future_formats() {
    assert_eq!(
        parse_envelope("RULLST:v2:bad:key:id:extra")
            .err()
            .expect("extra envelope fields must fail"),
        PrivacyError::InvalidEnvelope
    );
    assert_eq!(
        parse_envelope("RULLST:v3:default:AA:AA")
            .err()
            .expect("future envelope versions must fail"),
        PrivacyError::UnsupportedEnvelopeVersion("v3".to_string())
    );
    assert_eq!(
        parse_envelope("ENC:v2:default:AA:AA")
            .err()
            .expect("foreign envelope prefixes must fail"),
        PrivacyError::InvalidEnvelope
    );

    for malformed in [
        "RULLST",
        "RULLST:v2",
        "RULLST:v2:default",
        "RULLST:v2:default:AA",
        "RULLST:v2::AA:AA",
        "RULLST:v2:bad/key:AA:AA",
    ] {
        assert!(parse_envelope(malformed).is_err(), "{malformed}");
    }

    let tag = URL_SAFE_NO_PAD.encode([0_u8; TAG_LENGTH]);
    assert!(matches!(
        parse_envelope(&format!("RULLST:v2:default:not-base64!:{tag}")),
        Err(PrivacyError::Base64Error(_))
    ));
    assert_eq!(
        parse_envelope(&format!("RULLST:v2:default:AA:{tag}"))
            .err()
            .unwrap(),
        PrivacyError::PayloadTooShort
    );
    let nonce = URL_SAFE_NO_PAD.encode([0_u8; NONCE_LENGTH]);
    assert_eq!(
        parse_envelope(&format!("RULLST:v2:default:{nonce}:AA"))
            .err()
            .unwrap(),
        PrivacyError::PayloadTooShort
    );
}

#[test]
fn configured_keys_accept_raw_base64_and_hex_material() {
    let key = b"0123456789abcdef0123456789abcdef";
    assert_eq!(
        decode_configured_key(std::str::from_utf8(key).expect("ASCII test key"))
            .expect("raw key should decode"),
        key
    );
    assert_eq!(
        decode_configured_key(&format!("base64:{}", STANDARD.encode(key)))
            .expect("base64 key should decode"),
        key
    );
    assert_eq!(
        decode_configured_key(
            "hex:3031323334353637383961626364656630313233343536373839616263646566"
        )
        .expect("hex key should decode"),
        key
    );
    assert_eq!(
        decode_configured_key("hex:not-hex").expect_err("invalid hex must fail"),
        PrivacyError::InvalidKeyEncoding("hex")
    );
    assert_eq!(
        decode_configured_key("base64:!").unwrap_err(),
        PrivacyError::InvalidKeyEncoding("base64")
    );
    assert_eq!(
        decode_configured_key("hex:0").unwrap_err(),
        PrivacyError::InvalidKeyEncoding("hex")
    );
    assert_eq!(
        decode_configured_key("base64:c2hvcnQ=").unwrap_err(),
        PrivacyError::InvalidKeyLength
    );
}

#[test]
fn privacy_errors_and_key_validation_are_typed() {
    let short_key = generate_short_key();
    assert_eq!(
        encrypt_aes_gcm("data", &short_key).expect_err("short key must fail"),
        PrivacyError::InvalidKeyLength
    );
    assert_eq!(
        decrypt_aes_gcm("data", &short_key).expect_err("short key must fail"),
        PrivacyError::InvalidKeyLength
    );

    let key = generate_test_key_32();
    assert!(matches!(
        decrypt_aes_gcm("invalid base64!@#", &key).expect_err("invalid base64 must fail"),
        PrivacyError::Base64Error(_)
    ));
    assert_eq!(
        PrivacyError::InvalidKeyLength.to_string(),
        "RULLST_ENCRYPTION_KEY must be exactly 32 bytes long"
    );
    assert_eq!(
        PrivacyError::PayloadTooShort.to_string(),
        "Invalid encrypted payload (too short)"
    );
    assert!(
        PrivacyError::EncryptionFailed("err".to_string())
            .to_string()
            .contains("Encryption failed")
    );
    assert!(
        PrivacyError::DecryptionFailed("err".to_string())
            .to_string()
            .contains("Decryption failed")
    );
    assert!(
        PrivacyError::Utf8Error("err".to_string())
            .to_string()
            .contains("UTF-8")
    );
    assert!(
        PrivacyError::EnvError("err".to_string())
            .to_string()
            .contains("Environment variable")
    );
}

#[test]
fn legacy_ciphertext_decrypts_and_invalid_utf8_is_typed() {
    let key = generate_test_key_32();
    let cipher = Aes256Gcm::new_from_slice(key.as_bytes()).unwrap();
    let nonce_bytes = [7_u8; NONCE_LENGTH];
    let nonce = Nonce::<Aes256Gcm>::try_from(nonce_bytes.as_slice()).unwrap();

    let ciphertext = cipher.encrypt(&nonce, b"legacy secret".as_slice()).unwrap();
    let mut payload = nonce_bytes.to_vec();
    payload.extend_from_slice(&ciphertext);
    assert_eq!(
        decrypt_aes_gcm(&STANDARD.encode(payload), &key).unwrap(),
        "legacy secret"
    );

    let invalid_utf8 = cipher.encrypt(&nonce, [0xff].as_slice()).unwrap();
    let mut invalid_payload = nonce_bytes.to_vec();
    invalid_payload.extend_from_slice(&invalid_utf8);
    assert!(matches!(
        decrypt_aes_gcm(&STANDARD.encode(invalid_payload), &key),
        Err(PrivacyError::Utf8Error(_))
    ));
}

#[test]
fn environment_key_selection_rotation_and_context_fail_closed() {
    let _serial = environment_lock();
    let mut environment = EnvironmentGuard::new();
    for key in [KEY_ENV, KEY_ID_ENV, KEYRING_ENV] {
        environment.clear(key);
    }

    assert_eq!(current_key_id().unwrap(), DEFAULT_KEY_ID);
    assert!(matches!(current_key(), Err(PrivacyError::EnvError(_))));
    environment.set(KEY_ID_ENV, "bad/id");
    assert_eq!(current_key_id().unwrap_err(), PrivacyError::InvalidKeyId);

    let primary = "0123456789abcdef0123456789abcdef";
    let rotated = "abcdef0123456789abcdef0123456789";
    environment.set(KEY_ID_ENV, "primary-2026");
    environment.set(KEY_ENV, primary);
    assert_eq!(current_key().unwrap(), primary.as_bytes());

    let encrypted = encrypt_model_field("secret", "accounts", "token").unwrap();
    assert_eq!(
        decrypt_model_field(&encrypted, "accounts", "token").unwrap(),
        "secret"
    );
    assert!(decrypt_model_field(&encrypted, "accounts", "other").is_err());

    environment.set(KEY_ID_ENV, "rotated-2027");
    environment.set(KEY_ENV, rotated);
    environment.clear(KEYRING_ENV);
    assert!(matches!(
        decrypt_model_field(&encrypted, "accounts", "token"),
        Err(PrivacyError::EnvError(_))
    ));
    environment.set(KEYRING_ENV, "not-json");
    assert!(matches!(
        decrypt_model_field(&encrypted, "accounts", "token"),
        Err(PrivacyError::EnvError(_))
    ));
    environment.set(KEYRING_ENV, "{}");
    assert_eq!(
        decrypt_model_field(&encrypted, "accounts", "token").unwrap_err(),
        PrivacyError::KeyNotFound("primary-2026".to_string())
    );
    environment.set(KEYRING_ENV, &format!(r#"{{"primary-2026":"{primary}"}}"#));
    assert_eq!(
        decrypt_model_field(&encrypted, "accounts", "token").unwrap(),
        "secret"
    );

    let configured = encrypt_configured_secret("configured").unwrap();
    assert_eq!(
        decrypt_configured_secret(&configured).unwrap(),
        "configured"
    );

    // SecretString serde emits a keyring-aware envelope, never plaintext.
    let secret = SecretString::new("123.456.789-00");
    let serialized = serde_json::to_string(&secret).unwrap();
    assert!(serialized.starts_with("\"RULLST:v2:rotated-2027:"));
    assert!(!serialized.contains("123.456.789-00"));
    let restored: SecretString = serde_json::from_str(&serialized).unwrap();
    assert_eq!(restored.reveal_audited(), "123.456.789-00");
    let sql_column_envelope = serde_json::to_string(&configured).unwrap();
    assert!(
        serde_json::from_str::<SecretString>(&sql_column_envelope).is_err(),
        "a SQL-column envelope must not decrypt through the serde context"
    );
    environment.set(KEY_ID_ENV, "primary-2026");
    environment.set(KEY_ENV, primary);
    environment.clear(KEYRING_ENV);
    assert!(
        serde_json::from_str::<SecretString>(&serialized).is_err(),
        "an envelope for a key missing from the keyring must fail closed"
    );
    environment.clear(KEY_ENV);
    assert!(
        serde_json::to_string(&secret).is_err(),
        "serializing without a key must fail instead of emitting plaintext"
    );
}

#[test]
fn secret_string_serde_redacts_projections_and_accepts_plaintext_input() {
    let secret = SecretString::new("cpf-123");
    let redacted = with_redacted_secrets(|| {
        with_redacted_secrets(|| serde_json::to_string(&vec![secret.clone()]))
    })
    .unwrap();
    assert_eq!(redacted, r#"["***"]"#);

    let parsed: SecretString = serde_json::from_str("\"plain input\"").unwrap();
    assert_eq!(parsed.reveal_audited(), "plain input");
    for malformed in [
        "\"RULLST:v2:k:!!:!!\"",
        "\"RULLST:v9:k:a:b\"",
        "\"RULLST:\"",
    ] {
        assert!(serde_json::from_str::<SecretString>(malformed).is_err());
    }
}

fn legacy_ciphertext(key: &str, plaintext: &str) -> String {
    let cipher = Aes256Gcm::new_from_slice(key.as_bytes()).unwrap();
    let nonce_bytes = [9_u8; NONCE_LENGTH];
    let nonce = Nonce::<Aes256Gcm>::try_from(nonce_bytes.as_slice()).unwrap();
    let mut payload = nonce_bytes.to_vec();
    payload.extend_from_slice(&cipher.encrypt(&nonce, plaintext.as_bytes()).unwrap());
    STANDARD.encode(payload)
}

/// Pre-v12 values name no key, so after the documented rotation (old key
/// moved into the keyring) they decrypt through the keyring instead of
/// failing with the new current key.
#[test]
fn legacy_ciphertext_stays_readable_through_the_keyring_after_rotation() {
    let _serial = environment_lock();
    let mut environment = EnvironmentGuard::new();
    let retired = "0123456789abcdef0123456789abcdef";
    let unrelated = "fedcba9876543210fedcba9876543210";
    let current = "abcdef0123456789abcdef0123456789";
    let legacy = legacy_ciphertext(retired, "pre-v12 secret");

    environment.set(KEY_ENV, current);
    environment.set(KEY_ID_ENV, "rotated-2027");
    environment.clear(KEYRING_ENV);
    assert!(matches!(
        decrypt_configured_secret(&legacy),
        Err(PrivacyError::DecryptionFailed(_))
    ));
    environment.set(KEYRING_ENV, &format!(r#"{{"unrelated":"{unrelated}"}}"#));
    assert!(matches!(
        decrypt_configured_secret(&legacy),
        Err(PrivacyError::DecryptionFailed(_))
    ));

    for keyring in [
        format!(r#"{{"unrelated":"{unrelated}","default":"{retired}"}}"#),
        format!(r#"{{"primary-2026":"{retired}"}}"#),
    ] {
        environment.set(KEYRING_ENV, &keyring);
        assert_eq!(
            decrypt_configured_secret(&legacy).unwrap(),
            "pre-v12 secret"
        );
    }

    environment.set(KEY_ENV, retired);
    environment.set(KEYRING_ENV, "not-json");
    assert_eq!(
        decrypt_configured_secret(&legacy).unwrap(),
        "pre-v12 secret",
        "the current key is tried before the keyring"
    );
    environment.set(KEY_ENV, current);
    assert!(matches!(
        decrypt_configured_secret(&legacy),
        Err(PrivacyError::EnvError(_))
    ));
    environment.set(KEYRING_ENV, r#"{"default":"short"}"#);
    assert_eq!(
        decrypt_configured_secret(&legacy).unwrap_err(),
        PrivacyError::InvalidKeyLength
    );
}

#[derive(serde::Deserialize)]
struct ClientSettings {
    #[serde(deserialize_with = "deserialize_plaintext_secret")]
    webhook_secret: SecretString,
    #[serde(default, deserialize_with = "deserialize_optional_plaintext_secret")]
    backup_secret: Option<SecretString>,
}

/// An envelope exposed for one record must not become another record's
/// secret through a client request: the plaintext-only deserializers reject
/// it without decrypting, while `SecretString`'s own `Deserialize` keeps its
/// documented round trip for trusted stores.
#[test]
fn client_input_deserializers_reject_envelopes() {
    let _serial = environment_lock();
    let mut environment = EnvironmentGuard::new();
    environment.set(KEY_ENV, "0123456789abcdef0123456789abcdef");
    environment.set(KEY_ID_ENV, "client-input-2026");
    environment.clear(KEYRING_ENV);

    let exposed = serde_json::to_string(&SecretString::new("victim secret")).unwrap();
    let restored: SecretString = serde_json::from_str(&exposed).unwrap();
    assert_eq!(restored.reveal_audited(), "victim secret");

    let submitted = format!(r#"{{"webhook_secret":{exposed}}}"#);
    let error = serde_json::from_str::<ClientSettings>(&submitted)
        .err()
        .expect("an exposed envelope must be rejected as client input");
    assert!(error.to_string().contains("not accepted as secret input"));
    let optional = format!(r#"{{"webhook_secret":"plain","backup_secret":{exposed}}}"#);
    assert!(serde_json::from_str::<ClientSettings>(&optional).is_err());
    for malformed in [r#""RULLST:v2:k:AA:AA""#, r#""RULLST:""#] {
        let body = format!(r#"{{"webhook_secret":{malformed}}}"#);
        assert!(serde_json::from_str::<ClientSettings>(&body).is_err());
    }

    let accepted: ClientSettings =
        serde_json::from_str(r#"{"webhook_secret":"new secret","backup_secret":null}"#).unwrap();
    assert_eq!(accepted.webhook_secret.reveal_audited(), "new secret");
    assert!(accepted.backup_secret.is_none());
    let accepted: ClientSettings =
        serde_json::from_str(r#"{"webhook_secret":"RULLSTish","backup_secret":"spare secret"}"#)
            .unwrap();
    assert_eq!(accepted.webhook_secret.reveal_audited(), "RULLSTish");
    assert_eq!(
        accepted
            .backup_secret
            .as_ref()
            .map(SecretString::reveal_audited),
        Some("spare secret")
    );
}