meerkat-store 0.7.28

Session persistence for Meerkat
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
use async_trait::async_trait;
use meerkat_core::{BlobId, BlobPayload, BlobRef, BlobStore, BlobStoreError};
use std::collections::HashMap;
#[cfg(not(target_arch = "wasm32"))]
use std::path::{Path, PathBuf};
#[cfg(not(target_arch = "wasm32"))]
use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _};

#[cfg(target_arch = "wasm32")]
use crate::tokio::sync::RwLock;
#[cfg(not(target_arch = "wasm32"))]
use ::tokio::sync::RwLock;

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct StoredBlob {
    media_type: String,
    data: String,
}

/// Maximum non-payload bytes admitted by the bounded filesystem read seam.
///
/// `StoredBlob` is a tiny JSON object in normal operation. Reserving 64 KiB
/// keeps legacy media-type/JSON overhead compatible while ensuring an
/// attacker-controlled file cannot make a bounded payload read allocate an
/// arbitrarily large buffer before deserialization checks `data.len()`.
#[cfg(not(target_arch = "wasm32"))]
const MAX_STORED_BLOB_METADATA_BYTES: u64 = 64 * 1024;

fn compute_blob_id(media_type: &str, data: &str) -> BlobId {
    // Single owner of the content-addressed blob identity lives in
    // meerkat-core so transcript-identity digests and the blob store agree on
    // one definition.
    meerkat_core::blob::content_blob_id(media_type, data)
}

/// In-memory blob store for ephemeral and test paths.
pub struct MemoryBlobStore {
    blobs: RwLock<HashMap<BlobId, StoredBlob>>,
}

impl MemoryBlobStore {
    pub fn new() -> Self {
        Self {
            blobs: RwLock::new(HashMap::new()),
        }
    }
}

impl Default for MemoryBlobStore {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl BlobStore for MemoryBlobStore {
    async fn put_image(&self, media_type: &str, data: &str) -> Result<BlobRef, BlobStoreError> {
        let media_type = meerkat_core::image_generation::MediaType::canonical_str(media_type);
        let blob_id = compute_blob_id(&media_type, data);
        let stored = StoredBlob {
            media_type: media_type.clone(),
            data: data.to_string(),
        };
        // A content-addressed retry is also a repair operation. Replacing the
        // exact key is safe and ensures an earlier partial/corrupt in-memory
        // object can never be accepted merely because the path exists.
        self.blobs.write().await.insert(blob_id.clone(), stored);
        Ok(BlobRef {
            blob_id,
            media_type,
        })
    }

    async fn get(&self, blob_id: &BlobId) -> Result<BlobPayload, BlobStoreError> {
        let blobs = self.blobs.read().await;
        let stored = blobs
            .get(blob_id)
            .ok_or_else(|| BlobStoreError::NotFound(blob_id.clone()))?;
        Ok(BlobPayload {
            blob_id: blob_id.clone(),
            media_type: stored.media_type.clone(),
            data: stored.data.clone(),
        })
    }

    async fn get_with_encoded_limit(
        &self,
        blob_id: &BlobId,
        max_encoded_bytes: usize,
    ) -> Result<BlobPayload, BlobStoreError> {
        let blobs = self.blobs.read().await;
        let stored = blobs
            .get(blob_id)
            .ok_or_else(|| BlobStoreError::NotFound(blob_id.clone()))?;
        if stored.data.len() > max_encoded_bytes {
            return Err(BlobStoreError::ReadLimitExceeded {
                blob_id: blob_id.clone(),
                max_encoded_bytes,
                actual_encoded_bytes: Some(stored.data.len()),
            });
        }
        Ok(BlobPayload {
            blob_id: blob_id.clone(),
            media_type: stored.media_type.clone(),
            data: stored.data.clone(),
        })
    }

    async fn delete(&self, blob_id: &BlobId) -> Result<(), BlobStoreError> {
        self.blobs.write().await.remove(blob_id);
        Ok(())
    }

    fn is_persistent(&self) -> bool {
        false
    }
}

#[cfg(not(target_arch = "wasm32"))]
pub struct FsBlobStore {
    root: PathBuf,
    #[cfg(test)]
    parent_dir_syncs: std::sync::atomic::AtomicUsize,
}

#[cfg(not(target_arch = "wasm32"))]
impl FsBlobStore {
    pub fn new(root: PathBuf) -> Self {
        Self {
            root,
            #[cfg(test)]
            parent_dir_syncs: std::sync::atomic::AtomicUsize::new(0),
        }
    }

    fn path_for(&self, blob_id: &BlobId) -> Result<PathBuf, BlobStoreError> {
        if !blob_id.is_canonical_sha256() {
            return Err(BlobStoreError::InvalidId(blob_id.clone()));
        }
        let Some(key) = blob_id.as_str().strip_prefix("sha256:") else {
            return Err(BlobStoreError::InvalidId(blob_id.clone()));
        };
        let prefix = key.get(0..2).unwrap_or("xx");
        Ok(self.root.join(prefix).join(format!("{key}.json")))
    }

    async fn ensure_parent_dir(path: &Path) -> Result<(), BlobStoreError> {
        if let Some(parent) = path.parent() {
            tokio::fs::create_dir_all(parent)
                .await
                .map_err(|err| BlobStoreError::WriteFailed(err.to_string()))?;
        }
        Ok(())
    }

    async fn stored_object_matches(&self, blob_id: &BlobId, media_type: &str, data: &str) -> bool {
        self.get_with_encoded_limit(blob_id, data.len())
            .await
            .is_ok_and(|stored| {
                stored.media_type == media_type
                    && stored.data == data
                    && compute_blob_id(&stored.media_type, &stored.data) == *blob_id
            })
    }

    async fn write_atomic(path: &Path, bytes: &[u8]) -> Result<(), BlobStoreError> {
        let temp_path = path.with_extension(format!("{}.tmp", uuid::Uuid::new_v4()));
        let result = async {
            let mut file = tokio::fs::OpenOptions::new()
                .create_new(true)
                .write(true)
                .open(&temp_path)
                .await
                .map_err(|err| BlobStoreError::WriteFailed(err.to_string()))?;
            file.write_all(bytes)
                .await
                .map_err(|err| BlobStoreError::WriteFailed(err.to_string()))?;
            file.sync_all()
                .await
                .map_err(|err| BlobStoreError::WriteFailed(err.to_string()))?;
            drop(file);
            tokio::fs::rename(&temp_path, path)
                .await
                .map_err(|err| BlobStoreError::WriteFailed(err.to_string()))
        }
        .await;
        if result.is_err() {
            // The uniquely named temporary object is ours. Never unlink the
            // content-addressed destination: another writer may own it.
            let _ = tokio::fs::remove_file(&temp_path).await;
        }
        result
    }

    async fn sync_parent_dir(&self, path: &Path) -> Result<(), BlobStoreError> {
        #[cfg(unix)]
        {
            let parent = path.parent().ok_or_else(|| {
                BlobStoreError::WriteFailed("blob path has no parent directory".to_string())
            })?;
            let mut directories = vec![parent];
            if parent != self.root {
                directories.push(self.root.as_path());
            }
            if let Some(root_parent) = self.root.parent()
                && !root_parent.as_os_str().is_empty()
                && !directories.contains(&root_parent)
            {
                directories.push(root_parent);
            }
            for directory_path in directories {
                let directory = tokio::fs::File::open(directory_path)
                    .await
                    .map_err(|err| BlobStoreError::WriteFailed(err.to_string()))?;
                directory
                    .sync_all()
                    .await
                    .map_err(|err| BlobStoreError::WriteFailed(err.to_string()))?;
            }
        }
        #[cfg(test)]
        self.parent_dir_syncs
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        Ok(())
    }
}

#[cfg(not(target_arch = "wasm32"))]
#[async_trait]
impl BlobStore for FsBlobStore {
    async fn put_image(&self, media_type: &str, data: &str) -> Result<BlobRef, BlobStoreError> {
        let media_type = meerkat_core::image_generation::MediaType::canonical_str(media_type);
        let blob_id = compute_blob_id(&media_type, data);
        let path = self.path_for(&blob_id)?;
        if self
            .stored_object_matches(&blob_id, &media_type, data)
            .await
        {
            // The existing bytes are not a durable success until the shard
            // directory entry itself is synchronized. This also repairs the
            // uncertain outcome of a prior rename whose directory fsync
            // failed or whose process crashed before it.
            self.sync_parent_dir(&path).await?;
            return Ok(BlobRef {
                blob_id,
                media_type,
            });
        }

        Self::ensure_parent_dir(&path).await?;
        let stored = StoredBlob {
            media_type: media_type.clone(),
            data: data.to_string(),
        };
        let bytes = serde_json::to_vec(&stored)
            .map_err(|err| BlobStoreError::WriteFailed(err.to_string()))?;
        if let Err(write_error) = Self::write_atomic(&path, &bytes).await {
            // On platforms where rename cannot replace an existing file, a
            // concurrent exact writer may have won. Accept only after bounded
            // byte-for-byte verification; otherwise surface the write fault.
            if !self
                .stored_object_matches(&blob_id, &media_type, data)
                .await
            {
                return Err(write_error);
            }
        }
        self.sync_parent_dir(&path).await?;
        if !self
            .stored_object_matches(&blob_id, &media_type, data)
            .await
        {
            return Err(BlobStoreError::WriteFailed(format!(
                "atomic blob write failed read-back verification for {blob_id}"
            )));
        }
        Ok(BlobRef {
            blob_id,
            media_type,
        })
    }

    async fn get(&self, blob_id: &BlobId) -> Result<BlobPayload, BlobStoreError> {
        let path = self.path_for(blob_id)?;
        let bytes = tokio::fs::read(&path).await.map_err(|err| {
            if err.kind() == std::io::ErrorKind::NotFound {
                BlobStoreError::NotFound(blob_id.clone())
            } else {
                BlobStoreError::ReadFailed(err.to_string())
            }
        })?;
        let stored: StoredBlob =
            serde_json::from_slice(&bytes).map_err(|err| BlobStoreError::Corrupt {
                blob_id: blob_id.clone(),
                detail: err.to_string(),
            })?;
        Ok(BlobPayload {
            blob_id: blob_id.clone(),
            media_type: stored.media_type,
            data: stored.data,
        })
    }

    async fn get_with_encoded_limit(
        &self,
        blob_id: &BlobId,
        max_encoded_bytes: usize,
    ) -> Result<BlobPayload, BlobStoreError> {
        let path = self.path_for(blob_id)?;
        let file = tokio::fs::File::open(&path).await.map_err(|err| {
            if err.kind() == std::io::ErrorKind::NotFound {
                BlobStoreError::NotFound(blob_id.clone())
            } else {
                BlobStoreError::ReadFailed(err.to_string())
            }
        })?;
        let metadata = file
            .metadata()
            .await
            .map_err(|err| BlobStoreError::ReadFailed(err.to_string()))?;
        let max_file_bytes = u64::try_from(max_encoded_bytes)
            .unwrap_or(u64::MAX)
            .saturating_add(MAX_STORED_BLOB_METADATA_BYTES);
        if metadata.len() > max_file_bytes {
            return Err(BlobStoreError::ReadLimitExceeded {
                blob_id: blob_id.clone(),
                max_encoded_bytes,
                actual_encoded_bytes: None,
            });
        }

        // The metadata preflight bounds this allocation even for malformed
        // legacy files. `take` also closes the metadata/read TOCTOU window: a
        // file that grows after the preflight can contribute at most one byte
        // beyond the bound, which is rejected before JSON parsing.
        let mut bytes = Vec::new();
        let mut bounded = file.take(max_file_bytes.saturating_add(1));
        bounded
            .read_to_end(&mut bytes)
            .await
            .map_err(|err| BlobStoreError::ReadFailed(err.to_string()))?;
        if u64::try_from(bytes.len()).unwrap_or(u64::MAX) > max_file_bytes {
            return Err(BlobStoreError::ReadLimitExceeded {
                blob_id: blob_id.clone(),
                max_encoded_bytes,
                actual_encoded_bytes: None,
            });
        }
        // The exact encoded-payload check follows JSON parsing, because total
        // file bytes also include the media type and JSON syntax.
        let stored: StoredBlob =
            serde_json::from_slice(&bytes).map_err(|err| BlobStoreError::Corrupt {
                blob_id: blob_id.clone(),
                detail: err.to_string(),
            })?;
        if stored.data.len() > max_encoded_bytes {
            return Err(BlobStoreError::ReadLimitExceeded {
                blob_id: blob_id.clone(),
                max_encoded_bytes,
                actual_encoded_bytes: Some(stored.data.len()),
            });
        }
        Ok(BlobPayload {
            blob_id: blob_id.clone(),
            media_type: stored.media_type,
            data: stored.data,
        })
    }

    async fn delete(&self, blob_id: &BlobId) -> Result<(), BlobStoreError> {
        let path = self.path_for(blob_id)?;
        match tokio::fs::remove_file(path).await {
            Ok(()) => Ok(()),
            Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(()),
            Err(err) => Err(BlobStoreError::DeleteFailed(err.to_string())),
        }
    }

    fn is_persistent(&self) -> bool {
        true
    }
}

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

    #[tokio::test]
    async fn memory_bounded_read_rejects_before_cloning_payload() {
        let store = MemoryBlobStore::new();
        let blob = store
            .put_image("image/png", "AQID")
            .await
            .expect("store test blob");

        let error = store
            .get_with_encoded_limit(&blob.blob_id, 3)
            .await
            .expect_err("four encoded bytes must not fit a three-byte read limit");
        assert!(matches!(
            error,
            BlobStoreError::ReadLimitExceeded {
                blob_id,
                max_encoded_bytes: 3,
                actual_encoded_bytes: Some(4),
            } if blob_id == blob.blob_id
        ));
    }

    #[cfg(not(target_arch = "wasm32"))]
    #[tokio::test]
    async fn filesystem_bounded_read_preflights_oversized_file_before_json_parse() {
        let temp = tempfile::tempdir().expect("temporary blob root");
        let store = FsBlobStore::new(temp.path().to_path_buf());
        let blob_id = BlobId::new(format!("sha256:{}", "ab".repeat(32)));
        let path = store.path_for(&blob_id).expect("canonical blob path");
        FsBlobStore::ensure_parent_dir(&path)
            .await
            .expect("create blob shard directory");
        // Deliberately malformed JSON: if the bounded seam reads and parses
        // the object before checking metadata, the result is ReadFailed.
        tokio::fs::write(
            &path,
            vec![b'x'; MAX_STORED_BLOB_METADATA_BYTES as usize + 2],
        )
        .await
        .expect("write oversized malformed blob");

        let error = store
            .get_with_encoded_limit(&blob_id, 1)
            .await
            .expect_err("metadata preflight must reject the oversized file");
        assert!(matches!(
            error,
            BlobStoreError::ReadLimitExceeded {
                blob_id: rejected,
                max_encoded_bytes: 1,
                actual_encoded_bytes: None,
            } if rejected == blob_id
        ));
    }

    #[cfg(not(target_arch = "wasm32"))]
    #[tokio::test]
    async fn filesystem_put_repairs_corrupt_existing_object_atomically() {
        let temp = tempfile::tempdir().expect("temporary blob root");
        let store = FsBlobStore::new(temp.path().to_path_buf());
        let data = "iVBORw0KGgo=";
        let blob_id = compute_blob_id("image/png", data);
        let path = store.path_for(&blob_id).expect("canonical blob path");
        FsBlobStore::ensure_parent_dir(&path)
            .await
            .expect("create blob shard directory");
        tokio::fs::write(&path, b"{corrupt")
            .await
            .expect("seed corrupt content-addressed destination");

        let repaired = store
            .put_image(" Image/PNG; charset=binary ", data)
            .await
            .expect("exact retry must repair a corrupt destination");
        assert_eq!(repaired.blob_id, blob_id);
        assert_eq!(repaired.media_type, "image/png");
        let payload = store
            .get_with_encoded_limit(&blob_id, data.len())
            .await
            .expect("repaired object must pass bounded readback");
        assert_eq!(payload.media_type, "image/png");
        assert_eq!(payload.data, data);
        assert_eq!(compute_blob_id(&payload.media_type, &payload.data), blob_id);
        assert!(
            store
                .parent_dir_syncs
                .load(std::sync::atomic::Ordering::Relaxed)
                >= 1,
            "a successful atomic repair must fsync its shard directory"
        );

        let entries = std::fs::read_dir(path.parent().expect("shard parent"))
            .expect("read shard directory")
            .collect::<Result<Vec<_>, _>>()
            .expect("collect shard directory entries");
        assert_eq!(entries.len(), 1, "atomic repair must not leak temp files");
    }

    #[cfg(not(target_arch = "wasm32"))]
    #[tokio::test]
    async fn filesystem_reads_reject_noncanonical_traversal_ids_before_path_lookup() {
        let temp = tempfile::tempdir().expect("temporary blob root");
        let store = FsBlobStore::new(temp.path().join("blobs"));
        let traversal = BlobId::new("sha256:../../outside");

        assert!(matches!(
            store.get(&traversal).await,
            Err(BlobStoreError::InvalidId(ref rejected)) if rejected == &traversal
        ));
        assert!(matches!(
            store.get_with_encoded_limit(&traversal, 1024).await,
            Err(BlobStoreError::InvalidId(ref rejected)) if rejected == &traversal
        ));
        assert!(matches!(
            store.delete(&traversal).await,
            Err(BlobStoreError::InvalidId(ref rejected)) if rejected == &traversal
        ));
        assert!(
            !temp.path().join("blobs").exists(),
            "invalid ids must be rejected before filesystem traversal"
        );
    }
}