forgelib 1.1.0

The standard library for agent-built SaaS: hardened infra primitives on Postgres
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
use super::common;
use super::{
    Blob, BlobInfo, BlobSummary, ConditionalGet, DEFAULT_CONTENT_TYPE, ListPage, ProxyPresign,
    PutOpts, PutPrecondition,
};
use crate::backend::{BackendLifecycle, Primitive};
use crate::error::Result;
use crate::types::Cursor;
use crate::util::sha256_hex;
use async_trait::async_trait;
use bytes::Bytes;
use std::collections::{BTreeMap, HashMap};
use std::sync::{Mutex, PoisonError};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

/// One stored object: bytes plus the metadata `head`/`list` echo back. `size` and `etag`
/// derive from `data`, the source of truth for both.
struct Object {
    data: Bytes,
    content_type: String,
    etag: String,
    last_modified: SystemTime,
    metadata: BTreeMap<String, String>,
    cache_control: Option<String>,
    content_disposition: Option<String>,
    checksum_sha256: String,
}

impl Object {
    /// Body-less metadata view for `head`/`list`. `key` is the logical
    /// (namespace-stripped) key the caller used.
    fn info(&self, key: String) -> BlobInfo {
        BlobInfo::new(
            key,
            u64::try_from(self.data.len()).unwrap_or(u64::MAX),
            self.content_type.clone(),
            self.etag.clone(),
            self.last_modified,
            self.metadata.clone(),
        )
        .with_storage_metadata(
            self.cache_control.clone(),
            self.content_disposition.clone(),
            Some(self.checksum_sha256.clone()),
            None,
        )
    }

    fn summary(&self, key: String) -> BlobSummary {
        BlobSummary::new(
            key,
            u64::try_from(self.data.len()).unwrap_or(u64::MAX),
            self.etag.clone(),
            self.last_modified,
        )
    }
}

/// In-memory [`Blob`]: bytes + metadata in a map, presigning via the shared HMAC scheme.
pub(crate) struct MemBlob {
    state: Mutex<HashMap<String, Object>>,
    /// Namespace + presign config, via the same helper the Postgres and filesystem
    /// backends use so key mapping and URL signing never diverge.
    shared: common::Shared,
}

impl MemBlob {
    pub(crate) fn new(namespace: String, secret: Option<Vec<u8>>, base_url: String) -> Self {
        Self {
            state: Mutex::new(HashMap::new()),
            shared: common::Shared::new(namespace, secret, base_url),
        }
    }

    /// Take the map lock, recovering the guard if a previous holder panicked. Critical
    /// sections are short and synchronous (no `await` across the lock), so a poisoned lock
    /// never reflects a half-updated invariant worth aborting for.
    fn lock(&self) -> std::sync::MutexGuard<'_, HashMap<String, Object>> {
        self.state.lock().unwrap_or_else(PoisonError::into_inner)
    }
}

/// `put` commit time truncated to whole seconds, matching the seconds precision
/// documented for `BlobInfo::last_modified`.
fn commit_time() -> SystemTime {
    let now = SystemTime::now();
    now.duration_since(UNIX_EPOCH)
        .map(|d| UNIX_EPOCH + Duration::from_secs(d.as_secs()))
        .unwrap_or(now)
}

#[async_trait]
impl Blob for MemBlob {
    async fn put(&self, key: &str, data: Bytes, opts: PutOpts) -> Result<()> {
        common::check_key(key)?;
        common::check_put(&data, &opts)?;
        common::reject_s3_encryption(&opts)?;
        let pk = self.shared.physical(key);
        let etag = sha256_hex(&data);
        let checksum_sha256 = etag.clone();
        let content_type = opts
            .content_type
            .unwrap_or_else(|| DEFAULT_CONTENT_TYPE.to_string());
        let mut state = self.lock();
        match &opts.precondition {
            Some(PutPrecondition::CreateOnly) if state.contains_key(&pk) => {
                return Err(crate::error::ForgeError::precondition(
                    "blob already exists",
                ));
            }
            Some(PutPrecondition::MatchVersion(expected))
                if state.get(&pk).map(|object| &object.etag) != Some(expected) =>
            {
                return Err(crate::error::ForgeError::precondition(
                    "blob version does not match",
                ));
            }
            _ => {}
        }
        state.insert(
            pk,
            Object {
                data,
                content_type,
                etag,
                last_modified: commit_time(),
                metadata: opts.metadata,
                cache_control: opts.cache_control,
                content_disposition: opts.content_disposition,
                checksum_sha256,
            },
        );
        Ok(())
    }

    async fn get(&self, key: &str) -> Result<Option<Bytes>> {
        common::check_key(key)?;
        let pk = self.shared.physical(key);
        Ok(self.lock().get(&pk).map(|o| o.data.clone()))
    }

    async fn get_if(
        &self,
        key: &str,
        if_match: Option<&str>,
        if_none_match: Option<&str>,
    ) -> Result<ConditionalGet> {
        common::check_key(key)?;
        common::check_get_conditions(if_match, if_none_match)?;
        let pk = self.shared.physical(key);
        let state = self.lock();
        let Some(object) = state.get(&pk) else {
            return Ok(ConditionalGet::Missing);
        };
        if if_match.is_some_and(|expected| expected != object.etag) {
            return Err(crate::ForgeError::precondition(
                "blob read version does not match",
            ));
        }
        if if_none_match.is_some_and(|version| version == object.etag) {
            return Ok(ConditionalGet::NotModified {
                etag: object.etag.clone(),
            });
        }
        Ok(ConditionalGet::Found {
            body: object.data.clone(),
            etag: object.etag.clone(),
        })
    }

    async fn head(&self, key: &str) -> Result<Option<BlobInfo>> {
        common::check_key(key)?;
        let pk = self.shared.physical(key);
        Ok(self.lock().get(&pk).map(|o| o.info(key.to_string())))
    }

    async fn delete(&self, key: &str) -> Result<()> {
        common::check_key(key)?;
        let pk = self.shared.physical(key);
        self.lock().remove(&pk);
        Ok(())
    }

    async fn list(&self, prefix: &str, cursor: Option<Cursor>, limit: u32) -> Result<ListPage> {
        let physical_prefix = self.shared.physical(prefix);
        let limit = limit.clamp(1, 1000) as usize;
        // Keyset pagination over the physical key, like the Postgres backend: the cursor
        // token is the last physical key returned; the next page starts after it.
        let after = cursor.map(|c| c.token().to_string());
        let state = self.lock();
        let mut matched: Vec<(String, BlobSummary)> = state
            .iter()
            .filter(|(k, _)| k.starts_with(physical_prefix.as_str()))
            .filter(|(k, _)| after.as_deref().is_none_or(|a| k.as_str() > a))
            .map(|(k, o)| (k.clone(), o.summary(self.shared.logical(k).to_string())))
            .collect();
        matched.sort_by(|a, b| a.0.cmp(&b.0));
        matched.truncate(limit.saturating_add(1));
        let next = (matched.len() > limit).then(|| {
            Cursor::from_token(
                matched
                    .get(limit - 1)
                    .map(|item| item.0.clone())
                    .unwrap_or_default(),
            )
        });
        matched.truncate(limit);
        let items = matched.into_iter().map(|(_, info)| info).collect();
        Ok(ListPage::new(items, next))
    }

    async fn presign_upload(
        &self,
        key: &str,
        expires: Duration,
        max_bytes: u64,
    ) -> Result<ProxyPresign> {
        self.shared.presign_upload(key, expires, max_bytes).await
    }

    async fn presign_download(&self, key: &str, expires: Duration) -> Result<ProxyPresign> {
        self.shared.presign_download(key, expires).await
    }

    async fn verify_presigned(
        &self,
        method: &str,
        key: &str,
        expires_epoch: i64,
        max_bytes: u64,
        sig: &str,
    ) -> Result<bool> {
        self.shared
            .verify_presigned(method, key, expires_epoch, max_bytes, sig)
    }
}

#[async_trait]
impl BackendLifecycle for MemBlob {
    fn name(&self) -> &'static str {
        "memory"
    }
    fn primitive(&self) -> Primitive {
        Primitive::Blob
    }
    fn durable(&self) -> bool {
        false
    }
    fn caveats(&self) -> &'static str {
        "in-process, not durable"
    }
    // Blobs carry no TTL, so there is nothing to sweep: the no-op `maintain` default applies.
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use crate::S3Encryption;
    use crate::blob::MAX_CONTENT_TYPE_BYTES;
    use crate::error::ForgeError;

    fn b(s: &str) -> Bytes {
        Bytes::from(s.as_bytes().to_vec())
    }

    #[tokio::test]
    async fn put_then_get_and_head_roundtrip() {
        let blob = MemBlob::new(String::new(), None, "/files".to_string());
        let opts = PutOpts::new()
            .with_content_type("text/csv")
            .with_metadata("owner", "alice");
        blob.put("exports/data.csv", b("a,b,c"), opts)
            .await
            .unwrap();

        assert_eq!(
            blob.get("exports/data.csv").await.unwrap(),
            Some(b("a,b,c"))
        );
        let info = blob.head("exports/data.csv").await.unwrap().unwrap();
        assert_eq!(info.key, "exports/data.csv");
        assert_eq!(info.size, 5);
        assert_eq!(info.content_type, "text/csv");
        assert_eq!(info.etag, sha256_hex(b"a,b,c"), "etag is the content hash");
        assert_eq!(
            info.metadata.get("owner").map(String::as_str),
            Some("alice")
        );

        assert_eq!(blob.get("missing").await.unwrap(), None);
        assert!(blob.head("missing").await.unwrap().is_none());
    }

    #[tokio::test]
    async fn head_defaults_content_type_when_unset() {
        let blob = MemBlob::new(String::new(), None, "/files".to_string());
        blob.put("k", b("x"), PutOpts::new()).await.unwrap();
        let info = blob.head("k").await.unwrap().unwrap();
        assert_eq!(info.content_type, DEFAULT_CONTENT_TYPE);
        assert!(info.metadata.is_empty());
    }

    #[tokio::test]
    async fn put_overwrites_last_write_wins() {
        let blob = MemBlob::new(String::new(), None, "/files".to_string());
        blob.put(
            "k",
            b("v1"),
            PutOpts::new()
                .with_content_type("text/plain")
                .with_metadata("a", "1"),
        )
        .await
        .unwrap();
        let first = blob.head("k").await.unwrap().unwrap();

        blob.put(
            "k",
            b("v2-longer"),
            PutOpts::new().with_content_type("application/json"),
        )
        .await
        .unwrap();
        let second = blob.head("k").await.unwrap().unwrap();

        assert_eq!(blob.get("k").await.unwrap(), Some(b("v2-longer")));
        assert_eq!(second.content_type, "application/json");
        assert!(
            second.metadata.is_empty(),
            "an overwrite replaces metadata wholesale, it does not merge"
        );
        assert_eq!(second.size, 9);
        assert_ne!(first.etag, second.etag, "etag tracks the bytes");
    }

    #[tokio::test]
    async fn conditional_reads_copy_headers_and_checksum_compose() {
        let blob = MemBlob::new(String::new(), None, "/files".to_string());
        let body = b("portable bytes");
        let checksum = sha256_hex(&body);
        blob.put(
            "source",
            body.clone(),
            PutOpts::new()
                .with_cache_control("public, max-age=60")
                .with_content_disposition("attachment; filename=report.txt")
                .with_checksum_sha256(checksum.clone()),
        )
        .await
        .unwrap();
        let info = blob.head("source").await.unwrap().unwrap();
        assert_eq!(info.cache_control.as_deref(), Some("public, max-age=60"));
        assert_eq!(info.checksum_sha256.as_deref(), Some(checksum.as_str()));
        assert!(
            blob.verify_checksum_sha256("source", &checksum)
                .await
                .unwrap()
        );

        assert!(matches!(
            blob.get_if("source", None, Some(&info.etag)).await.unwrap(),
            ConditionalGet::NotModified { .. }
        ));
        assert!(matches!(
            blob.get_if("source", Some("wrong"), None).await,
            Err(ForgeError::Precondition(_))
        ));
        let copied = blob
            .copy("source", "copy", PutOpts::new().create_only())
            .await
            .unwrap();
        assert_eq!(blob.get("copy").await.unwrap(), Some(body));
        assert_eq!(copied.cache_control, info.cache_control);
        assert!(matches!(
            blob.create_multipart("large", PutOpts::new()).await,
            Err(ForgeError::NotConfigured(_))
        ));
        assert!(matches!(
            blob.put(
                "encrypted",
                b("x"),
                PutOpts::new().with_s3_encryption(S3Encryption::S3Managed),
            )
            .await,
            Err(ForgeError::NotConfigured(_))
        ));
    }

    #[tokio::test]
    async fn delete_is_idempotent() {
        let blob = MemBlob::new(String::new(), None, "/files".to_string());
        blob.put("k", b("v"), PutOpts::new()).await.unwrap();
        blob.delete("k").await.unwrap();
        blob.delete("k").await.unwrap();
        assert_eq!(blob.get("k").await.unwrap(), None);
    }

    #[tokio::test]
    async fn rejects_empty_key_and_oversized_metadata() {
        let blob = MemBlob::new(String::new(), None, "/files".to_string());
        assert!(
            matches!(
                blob.put("", b("x"), PutOpts::new()).await,
                Err(ForgeError::Invalid(_))
            ),
            "empty key is Invalid"
        );
        let huge_ct = "x".repeat(MAX_CONTENT_TYPE_BYTES + 1);
        assert!(
            matches!(
                blob.put("k", b("v"), PutOpts::new().with_content_type(huge_ct))
                    .await,
                Err(ForgeError::Limit(_))
            ),
            "content type over the cap is Limit"
        );
    }

    #[tokio::test]
    async fn list_paginates_by_prefix() {
        let blob = MemBlob::new(String::new(), None, "/files".to_string());
        for i in 0..10 {
            blob.put(&format!("img/{i:02}.png"), b("x"), PutOpts::new())
                .await
                .unwrap();
        }
        blob.put("other/x", b("y"), PutOpts::new()).await.unwrap();

        let mut seen = Vec::new();
        let mut cursor = None;
        loop {
            let page = blob.list("img/", cursor, 3).await.unwrap();
            seen.extend(page.items.into_iter().map(|i| i.key));
            match page.next {
                Some(c) => cursor = Some(c),
                None => break,
            }
        }
        assert_eq!(seen.len(), 10, "exactly the 10 img/* keys");
        assert!(seen.is_sorted(), "list returns keys in lexicographic order");
        assert_eq!(seen.first().map(String::as_str), Some("img/00.png"));
        assert!(!seen.iter().any(|k| k.starts_with("other/")));
    }

    #[tokio::test]
    async fn namespace_prefixes_storage_but_exposes_logical_keys() {
        let blob = MemBlob::new("tenant_a".to_string(), None, "/files".to_string());
        blob.put("docs/a.txt", b("hi"), PutOpts::new())
            .await
            .unwrap();
        {
            // Reaching into the map proves the stored key carries the namespace prefix.
            let state = blob.lock();
            assert!(state.contains_key("tenant_a:docs/a.txt"));
        }
        let info = blob.head("docs/a.txt").await.unwrap().unwrap();
        assert_eq!(info.key, "docs/a.txt", "head exposes the logical key");
        let page = blob.list("", None, 100).await.unwrap();
        assert_eq!(
            page.items.first().map(|i| i.key.as_str()),
            Some("docs/a.txt"),
            "list strips the namespace back to the logical key"
        );
    }

    #[tokio::test]
    async fn namespaces_isolate_keys() {
        let a = MemBlob::new("app_a".to_string(), None, "/files".to_string());
        let b2 = MemBlob::new("app_b".to_string(), None, "/files".to_string());
        a.put("shared", b("from-a"), PutOpts::new()).await.unwrap();
        b2.put("shared", b("from-b"), PutOpts::new()).await.unwrap();
        assert_eq!(a.get("shared").await.unwrap(), Some(b("from-a")));
        assert_eq!(b2.get("shared").await.unwrap(), Some(b("from-b")));
    }

    #[tokio::test]
    async fn presign_roundtrips_and_requires_a_secret() {
        let unsigned = MemBlob::new(String::new(), None, "/files".to_string());
        // No signing secret => presign and verify are Config errors (a deployment problem).
        assert!(matches!(
            unsigned
                .presign_download("k", Duration::from_secs(60))
                .await,
            Err(ForgeError::Config(_))
        ));
        assert!(matches!(
            unsigned
                .verify_presigned("GET", "k", 0, 0, "deadbeef")
                .await,
            Err(ForgeError::Config(_))
        ));

        let blob = MemBlob::new(
            String::new(),
            Some(b"sign-key".to_vec()),
            "/files".to_string(),
        );
        let url = blob
            .presign_download("docs/report.pdf", Duration::from_secs(300))
            .await
            .unwrap();
        let expires = url.expires_epoch;
        let sig = url.signature;

        assert!(
            blob.verify_presigned("GET", "docs/report.pdf", expires, 0, &sig)
                .await
                .unwrap(),
            "a freshly minted download URL verifies"
        );
        assert!(
            !blob
                .verify_presigned("GET", "docs/other.pdf", expires, 0, &sig)
                .await
                .unwrap(),
            "tampering with the key invalidates the signature"
        );
        assert!(
            !blob
                .verify_presigned("PUT", "docs/report.pdf", expires, 0, &sig)
                .await
                .unwrap(),
            "a GET URL cannot be replayed as a PUT"
        );

        let other_namespace = MemBlob::new(
            "other-app".to_string(),
            Some(b"sign-key".to_vec()),
            "/files".to_string(),
        );
        assert!(
            !other_namespace
                .verify_presigned("GET", "docs/report.pdf", expires, 0, &sig)
                .await
                .unwrap(),
            "a URL cannot be replayed into another application namespace"
        );
    }

    #[tokio::test]
    async fn presign_upload_signs_the_size_cap() {
        let blob = MemBlob::new(
            String::new(),
            Some(b"sign-key".to_vec()),
            "/files".to_string(),
        );
        let url = blob
            .presign_upload("uploads/x.bin", Duration::from_secs(120), 4096)
            .await
            .unwrap();
        assert!(url.url.contains("max_bytes=4096"));
        let expires = url.expires_epoch;
        let sig = url.signature;

        assert!(
            blob.verify_presigned("PUT", "uploads/x.bin", expires, 4096, &sig)
                .await
                .unwrap()
        );
        assert!(
            !blob
                .verify_presigned("PUT", "uploads/x.bin", expires, 8192, &sig)
                .await
                .unwrap(),
            "a size cap other than the one signed must fail"
        );
    }
}