secretspec 0.19.1

A declarative interface for every secret provider.
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
//! Cache-entry encoding, ownership, and freshness policy.
//!
//! Provider I/O, auditing, warning output, and remediation remain in
//! [`crate::secrets`]. This module owns the provider-independent envelope
//! format and the decisions that can be made from a stored value alone.

use secrecy::zeroize::Zeroizing;
use secrecy::{ExposeSecret, SecretString};
use std::time::{SystemTime, UNIX_EPOCH};

/// Marker every cache entry starts with, identifying the value as SecretSpec's
/// own and naming the format version — without parsing it.
///
/// Ownership has to be decidable even when the payload is not readable. A
/// truncated write leaves something only SecretSpec could have put there, which
/// is safe to replace; a value with no marker belongs to someone else and must
/// never be touched.
pub(crate) const CACHE_ENVELOPE_MARKER: &str = "secretspec-cache-v3:";

/// The 0.17 envelope recorded when an entry was written rather than when it
/// expires. Keep recognizing it so an upgrade can replace its own entries
/// without mistaking another project or profile's entry for ours.
const LEGACY_CACHE_ENVELOPE_MARKER: &str = "secretspec-cache-v2:";

/// Value stored inside the configured cache provider. The provider remains
/// responsible for encryption; the envelope adds freshness, route invalidation,
/// and ownership metadata.
#[derive(serde::Serialize, serde::Deserialize)]
struct CacheEnvelope {
    project: String,
    profile: String,
    expires_at: u64,
    max_age_secs: u64,
    route_fingerprint: String,
    /// The cached plaintext stays in a zeroizing buffer on both serialization
    /// and deserialization.
    #[serde(with = "zeroizing_string")]
    value: Zeroizing<String>,
}

/// The 0.17 envelope can still serve its owner while it remains fresh under the
/// active route's policy. Another owner cannot infer its expiry because v2 did
/// not store the `max_age` that created it, so foreign v2 entries remain
/// untouched.
#[derive(serde::Deserialize)]
struct LegacyCacheEnvelope {
    project: String,
    profile: String,
    cached_at: u64,
    route_fingerprint: String,
    #[serde(with = "zeroizing_string")]
    value: Zeroizing<String>,
}

enum DecodedEnvelope {
    Current(CacheEnvelope),
    Legacy(LegacyCacheEnvelope),
}

/// Serde for the envelope's plaintext, keeping it in a zeroizing buffer in both
/// directions. Deserialization moves serde's `String` directly into the buffer.
mod zeroizing_string {
    use secrecy::zeroize::Zeroizing;
    use serde::{Deserialize, Deserializer, Serializer};

    pub(super) fn serialize<S: Serializer>(
        value: &Zeroizing<String>,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(value)
    }

    pub(super) fn deserialize<'de, D: Deserializer<'de>>(
        deserializer: D,
    ) -> Result<Zeroizing<String>, D::Error> {
        String::deserialize(deserializer).map(Zeroizing::new)
    }
}

/// Whether the caller may change the value sitting at a cache address.
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum CacheOwnership {
    /// This project and profile wrote a readable entry.
    Ours,
    /// A readable SecretSpec entry whose declared lifetime has ended. Its
    /// original owner no longer has an interest in preserving it.
    Expired,
    /// Another project or profile wrote the entry.
    Foreign { project: String, profile: String },
    /// The ownership marker is ours, but the payload is damaged or incompatible.
    OursUnreadable,
    /// No SecretSpec ownership marker is present.
    Unrecognized,
}

/// What a stored cache entry can do for the read that found it.
pub(crate) enum CacheEntryStatus {
    /// Fresh, and written for the expected authoritative route.
    Fresh(SecretString),
    /// Expired (regardless of owner), or ours but no longer usable because its
    /// authoritative route or freshness policy changed.
    Stale,
    /// Marked as ours but not readable as this envelope version.
    OursUnreadable,
    /// Owned by another project or profile.
    Foreign { project: String, profile: String },
    /// Not a SecretSpec cache entry.
    Unrecognized,
}

/// Errors that can prevent encoding a cache entry.
#[derive(Debug, thiserror::Error)]
pub(crate) enum CacheEncodeError {
    #[error(transparent)]
    Clock(#[from] std::time::SystemTimeError),
    #[error("cache expiration timestamp is too large")]
    ExpirationOverflow,
    #[error(transparent)]
    Serialize(#[from] serde_json::Error),
}

fn decode(stored: &SecretString) -> Option<Result<DecodedEnvelope, serde_json::Error>> {
    let stored = stored.expose_secret();
    if let Some(payload) = stored.strip_prefix(CACHE_ENVELOPE_MARKER) {
        return Some(serde_json::from_str(payload).map(DecodedEnvelope::Current));
    }
    stored
        .strip_prefix(LEGACY_CACHE_ENVELOPE_MARKER)
        .map(|payload| serde_json::from_str(payload).map(DecodedEnvelope::Legacy))
}

fn unix_timestamp() -> Result<u64, std::time::SystemTimeError> {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|duration| duration.as_secs())
}

/// Classify cache ownership without trusting the provider address.
pub(crate) fn ownership(stored: &SecretString, project: &str, profile: &str) -> CacheOwnership {
    ownership_at(stored, project, profile, unix_timestamp().ok())
}

fn ownership_at(
    stored: &SecretString,
    project: &str,
    profile: &str,
    now: Option<u64>,
) -> CacheOwnership {
    match decode(stored) {
        None => CacheOwnership::Unrecognized,
        Some(Err(_)) => CacheOwnership::OursUnreadable,
        Some(Ok(DecodedEnvelope::Current(envelope))) => {
            if now.is_some_and(|now| now >= envelope.expires_at) {
                CacheOwnership::Expired
            } else if envelope.project == project && envelope.profile == profile {
                CacheOwnership::Ours
            } else {
                CacheOwnership::Foreign {
                    project: envelope.project,
                    profile: envelope.profile,
                }
            }
        }
        Some(Ok(DecodedEnvelope::Legacy(envelope)))
            if envelope.project == project && envelope.profile == profile =>
        {
            CacheOwnership::Ours
        }
        Some(Ok(DecodedEnvelope::Legacy(envelope))) => CacheOwnership::Foreign {
            project: envelope.project,
            profile: envelope.profile,
        },
    }
}

/// Inspect an entry using the current wall clock.
///
/// Clock errors are returned separately so the caller can preserve the
/// fail-open cache policy while deciding how to report the failure.
pub(crate) fn inspect_entry(
    stored: &SecretString,
    project: &str,
    profile: &str,
    route_fingerprint: &str,
    max_age_secs: u64,
) -> Result<CacheEntryStatus, std::time::SystemTimeError> {
    inspect_entry_with_clock(
        stored,
        project,
        profile,
        route_fingerprint,
        max_age_secs,
        unix_timestamp,
    )
}

fn inspect_entry_with_clock<E>(
    stored: &SecretString,
    project: &str,
    profile: &str,
    route_fingerprint: &str,
    max_age_secs: u64,
    clock: impl FnOnce() -> Result<u64, E>,
) -> Result<CacheEntryStatus, E> {
    let Some(decoded) = decode(stored) else {
        return Ok(CacheEntryStatus::Unrecognized);
    };
    let envelope = match decoded {
        Ok(DecodedEnvelope::Current(envelope)) => envelope,
        Ok(DecodedEnvelope::Legacy(envelope)) => {
            if envelope.project != project || envelope.profile != profile {
                return Ok(CacheEntryStatus::Foreign {
                    project: envelope.project,
                    profile: envelope.profile,
                });
            }
            if envelope.route_fingerprint != route_fingerprint {
                return Ok(CacheEntryStatus::Stale);
            }
            let now = clock()?;
            if envelope.cached_at > now || now.saturating_sub(envelope.cached_at) > max_age_secs {
                return Ok(CacheEntryStatus::Stale);
            }
            return Ok(CacheEntryStatus::Fresh(SecretString::new(
                envelope.value.as_str().into(),
            )));
        }
        Err(_) => return Ok(CacheEntryStatus::OursUnreadable),
    };
    let now = clock()?;
    if now >= envelope.expires_at {
        // Expiration is intrinsic to a v3 entry, so whoever encounters it can
        // discard it even when its project/profile no longer has a manifest.
        return Ok(CacheEntryStatus::Stale);
    }
    if envelope.project != project || envelope.profile != profile {
        return Ok(CacheEntryStatus::Foreign {
            project: envelope.project,
            profile: envelope.profile,
        });
    }
    // Reconstructing the write time from the self-contained v3 policy preserves
    // clock-rollback detection without retaining `cached_at` in the envelope.
    let Some(cached_at) = envelope.expires_at.checked_sub(envelope.max_age_secs) else {
        return Ok(CacheEntryStatus::Stale);
    };
    if cached_at > now || envelope.max_age_secs != max_age_secs {
        return Ok(CacheEntryStatus::Stale);
    }
    if envelope.route_fingerprint != route_fingerprint {
        return Ok(CacheEntryStatus::Stale);
    }
    Ok(CacheEntryStatus::Fresh(SecretString::new(
        envelope.value.as_str().into(),
    )))
}

#[cfg(test)]
fn inspect_entry_at(
    stored: &SecretString,
    project: &str,
    profile: &str,
    route_fingerprint: &str,
    max_age_secs: u64,
    now: u64,
) -> CacheEntryStatus {
    inspect_entry_with_clock(
        stored,
        project,
        profile,
        route_fingerprint,
        max_age_secs,
        || Ok::<u64, std::convert::Infallible>(now),
    )
    .expect("an infallible test clock cannot fail")
}

/// Encode an entry using the current wall clock.
pub(crate) fn encode_entry(
    project: &str,
    profile: &str,
    max_age_secs: u64,
    route_fingerprint: String,
    value: &SecretString,
) -> Result<SecretString, CacheEncodeError> {
    encode_entry_at(
        project,
        profile,
        unix_timestamp()?,
        max_age_secs,
        route_fingerprint,
        value,
    )
}

fn encode_entry_at(
    project: &str,
    profile: &str,
    now: u64,
    max_age_secs: u64,
    route_fingerprint: String,
    value: &SecretString,
) -> Result<SecretString, CacheEncodeError> {
    let expires_at = now
        .checked_add(max_age_secs)
        .ok_or(CacheEncodeError::ExpirationOverflow)?;
    let envelope = CacheEnvelope {
        project: project.to_string(),
        profile: profile.to_string(),
        expires_at,
        max_age_secs,
        route_fingerprint,
        value: Zeroizing::new(value.expose_secret().to_string()),
    };
    // Both plaintext renderings of the envelope are held in buffers that
    // zeroize on drop.
    let json = serde_json::to_string(&envelope)?;
    let serialized = Zeroizing::new(format!("{CACHE_ENVELOPE_MARKER}{json}"));
    Ok(SecretString::new(serialized.as_str().into()))
}

#[cfg(test)]
mod tests {
    use super::*;

    const PROJECT: &str = "project";
    const PROFILE: &str = "default";
    const FINGERPRINT: &str = "route-v1";
    const WRITTEN_AT: u64 = 1_000;
    const MAX_AGE: u64 = 60;
    const EXPIRES_AT: u64 = 1_060;

    fn entry() -> SecretString {
        encode_entry_at(
            PROJECT,
            PROFILE,
            WRITTEN_AT,
            MAX_AGE,
            FINGERPRINT.to_string(),
            &SecretString::new("sensitive".into()),
        )
        .expect("cache envelope serializes")
    }

    #[test]
    fn encoded_entry_round_trips_before_expiration() {
        let decoded = decode(&entry())
            .expect("marker present")
            .expect("valid envelope");
        let DecodedEnvelope::Current(envelope) = decoded else {
            panic!("new entries use the current envelope");
        };
        let status = inspect_entry_at(
            &entry(),
            PROJECT,
            PROFILE,
            FINGERPRINT,
            MAX_AGE,
            EXPIRES_AT - 1,
        );
        let CacheEntryStatus::Fresh(value) = status else {
            panic!("an entry is fresh before its expiration timestamp");
        };
        assert_eq!(envelope.expires_at, EXPIRES_AT);
        assert_eq!(envelope.max_age_secs, MAX_AGE);
        assert_eq!(value.expose_secret(), "sensitive");
    }

    #[test]
    fn entry_is_stale_at_its_expiration() {
        assert!(matches!(
            inspect_entry_at(&entry(), PROJECT, PROFILE, FINGERPRINT, MAX_AGE, EXPIRES_AT),
            CacheEntryStatus::Stale
        ));
    }

    #[test]
    fn clock_rollback_makes_an_implausibly_distant_expiration_stale() {
        assert!(matches!(
            inspect_entry_at(
                &entry(),
                PROJECT,
                PROFILE,
                FINGERPRINT,
                MAX_AGE,
                WRITTEN_AT - 1
            ),
            CacheEntryStatus::Stale
        ));
    }

    #[test]
    fn changed_max_age_invalidates_an_unexpired_entry() {
        assert!(matches!(
            inspect_entry_at(
                &entry(),
                PROJECT,
                PROFILE,
                FINGERPRINT,
                MAX_AGE / 2,
                WRITTEN_AT
            ),
            CacheEntryStatus::Stale
        ));
    }

    #[test]
    fn encoded_entry_stores_expiration_instead_of_write_time() {
        let entry = entry();
        let payload = entry
            .expose_secret()
            .strip_prefix(CACHE_ENVELOPE_MARKER)
            .expect("marker present");
        let envelope: serde_json::Value = serde_json::from_str(payload).unwrap();
        assert_eq!(envelope["expires_at"], EXPIRES_AT);
        assert_eq!(envelope["max_age_secs"], MAX_AGE);
        assert!(envelope.get("cached_at").is_none());
    }

    #[test]
    fn expiration_timestamp_overflow_refuses_the_cache_entry() {
        assert!(matches!(
            encode_entry_at(
                PROJECT,
                PROFILE,
                u64::MAX,
                MAX_AGE,
                FINGERPRINT.to_string(),
                &SecretString::new("sensitive".into()),
            ),
            Err(CacheEncodeError::ExpirationOverflow)
        ));
    }

    #[test]
    fn ownership_distinguishes_ours_foreign_unreadable_and_unrecognized() {
        assert_eq!(
            ownership_at(&entry(), PROJECT, PROFILE, Some(EXPIRES_AT - 1)),
            CacheOwnership::Ours
        );
        assert_eq!(
            ownership_at(&entry(), "other-project", PROFILE, Some(EXPIRES_AT - 1)),
            CacheOwnership::Foreign {
                project: PROJECT.to_string(),
                profile: PROFILE.to_string(),
            }
        );
        assert_eq!(
            ownership(
                &SecretString::new(format!("{CACHE_ENVELOPE_MARKER}{{truncated").into()),
                PROJECT,
                PROFILE
            ),
            CacheOwnership::OursUnreadable
        );
        assert_eq!(
            ownership(
                &SecretString::new("someone else's value".into()),
                PROJECT,
                PROFILE
            ),
            CacheOwnership::Unrecognized
        );
    }

    #[test]
    fn expired_entry_can_be_removed_by_whichever_project_encounters_it() {
        assert_eq!(
            ownership_at(&entry(), "other-project", "other-profile", Some(EXPIRES_AT)),
            CacheOwnership::Expired
        );
        assert!(matches!(
            inspect_entry_at(
                &entry(),
                "other-project",
                "other-profile",
                "different-route",
                MAX_AGE,
                EXPIRES_AT,
            ),
            CacheEntryStatus::Stale
        ));
    }

    fn legacy_entry() -> SecretString {
        SecretString::new(
            format!(
                "{LEGACY_CACHE_ENVELOPE_MARKER}{}",
                serde_json::json!({
                    "project": PROJECT,
                    "profile": PROFILE,
                    "cached_at": WRITTEN_AT,
                    "route_fingerprint": FINGERPRINT,
                    "value": "sensitive",
                })
            )
            .into(),
        )
    }

    #[test]
    fn legacy_entries_preserve_ownership_during_migration() {
        let legacy = legacy_entry();
        assert_eq!(
            ownership_at(&legacy, PROJECT, PROFILE, Some(EXPIRES_AT)),
            CacheOwnership::Ours
        );
        assert_eq!(
            ownership_at(&legacy, "other-project", PROFILE, Some(EXPIRES_AT)),
            CacheOwnership::Foreign {
                project: PROJECT.to_string(),
                profile: PROFILE.to_string(),
            }
        );
    }

    #[test]
    fn fresh_legacy_entry_remains_usable_during_migration() {
        let legacy = legacy_entry();
        let status = inspect_entry_at(&legacy, PROJECT, PROFILE, FINGERPRINT, MAX_AGE, EXPIRES_AT);
        let CacheEntryStatus::Fresh(value) = status else {
            panic!("v2 preserves its original inclusive freshness boundary");
        };
        assert_eq!(value.expose_secret(), "sensitive");
    }

    #[test]
    fn expired_legacy_entry_is_stale_for_its_owner() {
        assert!(matches!(
            inspect_entry_at(
                &legacy_entry(),
                PROJECT,
                PROFILE,
                FINGERPRINT,
                MAX_AGE,
                EXPIRES_AT + 1
            ),
            CacheEntryStatus::Stale
        ));
    }

    #[test]
    fn changed_route_is_stale_even_inside_the_time_window() {
        assert!(matches!(
            inspect_entry_at(
                &entry(),
                PROJECT,
                PROFILE,
                "different-route",
                MAX_AGE,
                EXPIRES_AT - 1
            ),
            CacheEntryStatus::Stale
        ));
    }
}