tandem-server 0.6.8

HTTP server for Tandem engine APIs
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
//! Build a memory decrypt principal from a verified request context (TAN-672).
//!
//! TAN-668 sealed hosted-KMS memory rows behind an authorized
//! [`MemoryDecryptPrincipal`], threaded into `tandem-memory` via the task-local
//! `with_decrypt_principal`. This module is the missing server-side half: it
//! projects a request's [`VerifiedTenantContext`] into that principal so a hosted
//! read decrypts only the scope the caller is actually authorized for, and mirrors
//! the governed-read data boundary so the DEK scope and the access scope agree.
//!
//! Fail-closed by construction: returns `None` for a local/single-tenant request
//! (no strict projection) or one with no readable data classes. With `None` no
//! principal is scoped, so local rows (NULL `crypto_envelope`) read normally and
//! genuinely hosted-sealed rows fail closed.

use tandem_memory::types::{effective_data_boundary_for_governed_read, MemoryTenantScope};
use tandem_memory::MemoryDecryptPrincipal;
use tandem_types::ResourceKind;
use tandem_types::{
    AccessEffect, AccessPermission, ResourceRef, StrictTenantContext, TenantContext,
    VerifiedTenantContext,
};

/// Project a verified request context into a memory decrypt principal.
///
/// `None` (fail-closed) when there is no strict projection (local/single-tenant),
/// no explicit actor, or no readable data classes to grant.
pub fn memory_decrypt_principal_from_verified_context(
    verified: &VerifiedTenantContext,
    now_ms: u64,
) -> Option<MemoryDecryptPrincipal> {
    let strict = verified.strict_projection.as_ref()?;
    let principal_id = verified_actor_id(verified)?;
    let tenant_scope = MemoryTenantScope {
        org_id: verified.tenant_context.org_id.clone(),
        workspace_id: verified.tenant_context.workspace_id.clone(),
        deployment_id: verified.tenant_context.deployment_id.clone(),
    };
    // The effective allow-list of data classes the caller may read — grants ∪
    // boundary — computed exactly as the governed-read filter does, so the DEK
    // scope a row is sealed under and the class the caller is authorized for
    // agree. An empty allow-list (a pure deny-list boundary) cannot be expressed
    // as an explicit principal grant, so fail closed rather than guess "all".
    let allowed_data_classes =
        effective_data_boundary_for_governed_read(strict, now_ms).allowed_data_classes;
    if allowed_data_classes.is_empty() {
        return None;
    }
    let allowed_source_binding_ids = source_binding_grants(strict, now_ms);
    Some(MemoryDecryptPrincipal::retrieval_gateway(
        principal_id,
        tenant_scope,
        allowed_data_classes,
        allowed_source_binding_ids,
    ))
}

/// The workspace-level memory-space resource that context (tree/layer) reads
/// operate on. Context nodes and their layers are tenant/workspace-scoped and
/// seal under the tenant `Internal` key scope — not a per-resource scope — so
/// the crypto boundary cannot distinguish two same-tenant nodes. A caller must
/// therefore hold a resource scope that covers this workspace memory space
/// before we grant a scoped decrypt (TAN-672 review).
pub fn workspace_memory_space_resource(tenant_context: &TenantContext) -> ResourceRef {
    ResourceRef::new(
        tenant_context.org_id.clone(),
        tenant_context.workspace_id.clone(),
        ResourceKind::MemorySpace,
        tenant_context.workspace_id.clone(),
    )
}

/// True when the verified request's strict resource scope covers `resource`.
/// Fail-closed: `false` when there is no strict projection. Used to gate a
/// scoped decrypt so a caller whose projection is narrower than the tenant (e.g.
/// scoped to one project or data room) cannot decrypt out-of-scope same-tenant
/// content that merely shares the tenant `Internal` key scope.
pub fn verified_resource_scope_covers(
    verified: &VerifiedTenantContext,
    resource: &ResourceRef,
) -> bool {
    verified
        .strict_projection
        .as_ref()
        .map(|strict| strict.resource_scope.contains(resource))
        .unwrap_or(false)
}

/// The verified actor id (never a client-supplied value): the tenant-context
/// actor, else the human actor. `None` when neither is explicit.
fn verified_actor_id(verified: &VerifiedTenantContext) -> Option<String> {
    normalized(verified.tenant_context.actor_id.as_deref())
        .or_else(|| normalized(Some(&verified.human_actor.actor_id)))
}

/// Source-binding ids the caller holds an active read grant for. Only these let a
/// source-bound (connector-ingested) row's envelope be unwrapped; a row with no
/// source binding needs no such grant.
fn source_binding_grants(strict: &StrictTenantContext, now_ms: u64) -> Vec<String> {
    let mut ids: Vec<String> = Vec::new();
    for grant in strict.grants.iter().filter(|grant| {
        grant.effect == AccessEffect::Allow
            && !grant.is_expired_at(now_ms)
            && grant.has_permission(AccessPermission::Read)
    }) {
        for id in source_binding_ids_in_resource(&grant.resource) {
            if !ids.contains(&id) {
                ids.push(id);
            }
        }
    }
    ids
}

fn source_binding_ids_in_resource(resource: &ResourceRef) -> Vec<String> {
    let mut ids = Vec::new();
    if resource.resource_kind == ResourceKind::SourceBinding {
        if let Some(id) = normalized(Some(&resource.resource_id)) {
            ids.push(id);
        }
    }
    for segment in &resource.parent_path {
        if segment.kind == ResourceKind::SourceBinding {
            if let Some(id) = normalized(Some(&segment.id)) {
                ids.push(id);
            }
        }
    }
    ids
}

fn normalized(value: Option<&str>) -> Option<String> {
    value
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .map(ToOwned::to_owned)
}

#[cfg(test)]
mod tests {
    use super::*;
    use tandem_enterprise_contract::DataClass;
    use tandem_types::{
        AssertionMetadata, AuthorityChain, DataBoundary, GrantSource, HumanActor, PrincipalRef,
        RequestPrincipal, ResourcePathSegment, ResourceScope, ScopedGrant, TenantContext,
    };

    fn base_verified(strict: Option<StrictTenantContext>) -> VerifiedTenantContext {
        let tenant_context = TenantContext::explicit_user_workspace(
            "acme",
            "hq",
            Some("prod".to_string()),
            "user-finance",
        );
        let principal = RequestPrincipal::authenticated_user("user-finance", "tandem-web");
        let authority_chain = AuthorityChain::from_request(principal);
        VerifiedTenantContext {
            tenant_context,
            human_actor: HumanActor::tandem_user("user-finance"),
            authority_chain,
            roles: Vec::new(),
            org_units: Vec::new(),
            capabilities: Vec::new(),
            policy_version: None,
            strict_projection: strict,
            issuer: "tandem-web".to_string(),
            audience: "tandem-runtime".to_string(),
            issued_at_ms: 1_000,
            expires_at_ms: 10_000,
            assertion_id: "assertion-a".to_string(),
            assertion_key_id: None,
        }
    }

    fn strict_with(boundary: DataBoundary, grants: Vec<ScopedGrant>) -> StrictTenantContext {
        let tenant_context = TenantContext::explicit_user_workspace(
            "acme",
            "hq",
            Some("prod".to_string()),
            "user-finance",
        );
        let principal = PrincipalRef::human_user("user-finance");
        let authority_chain = AuthorityChain::from_request(RequestPrincipal::authenticated_user(
            "user-finance",
            "web",
        ));
        let mut strict = StrictTenantContext::new(
            tenant_context.clone(),
            principal,
            authority_chain,
            ResourceScope::root(ResourceRef::new(
                "acme",
                "hq",
                ResourceKind::Workspace,
                "hq",
            )),
            AssertionMetadata::new("web", "runtime", 1_000, 10_000, "assertion-a"),
        )
        .with_data_boundary(boundary);
        strict.grants = grants;
        strict
    }

    fn read_grant(resource: ResourceRef, data_classes: Vec<DataClass>) -> ScopedGrant {
        ScopedGrant {
            grant_id: "grant-1".to_string(),
            principal: PrincipalRef::human_user("user-finance"),
            resource,
            effect: AccessEffect::Allow,
            permissions: vec![AccessPermission::Read],
            data_classes,
            tool_patterns: Vec::new(),
            grant_source: GrantSource::Direct,
            source_principal: None,
            expires_at_ms: None,
            delegation_id: None,
        }
    }

    #[test]
    fn no_strict_projection_is_fail_closed_none() {
        assert!(
            memory_decrypt_principal_from_verified_context(&base_verified(None), 2_000).is_none()
        );
    }

    #[test]
    fn explicit_boundary_becomes_allowed_data_classes() {
        let strict = strict_with(
            DataBoundary::allow(vec![DataClass::FinancialRecord]),
            Vec::new(),
        );
        let principal =
            memory_decrypt_principal_from_verified_context(&base_verified(Some(strict)), 2_000)
                .expect("hosted principal");
        assert_eq!(principal.tenant_scope.org_id, "acme");
        assert_eq!(
            principal.tenant_scope.deployment_id.as_deref(),
            Some("prod")
        );
        assert_eq!(
            principal.allowed_data_classes,
            vec![DataClass::FinancialRecord]
        );
        assert_eq!(principal.principal_id, "user-finance");
    }

    #[test]
    fn unrestricted_boundary_derives_classes_from_read_grants() {
        let grant = read_grant(
            ResourceRef::new("acme", "hq", ResourceKind::MemorySpace, "mem"),
            vec![DataClass::Confidential],
        );
        let strict = strict_with(DataBoundary::unrestricted(), vec![grant]);
        let principal =
            memory_decrypt_principal_from_verified_context(&base_verified(Some(strict)), 2_000)
                .expect("hosted principal");
        assert_eq!(
            principal.allowed_data_classes,
            vec![DataClass::Confidential]
        );
    }

    #[test]
    fn source_binding_read_grant_is_extracted() {
        let mut resource = ResourceRef::new(
            "acme",
            "hq",
            ResourceKind::SourceBinding,
            "notion-finance-db",
        );
        resource.parent_path = vec![ResourcePathSegment::new(
            ResourceKind::ConnectorInstance,
            "notion",
        )];
        let strict = strict_with(
            DataBoundary::allow(vec![DataClass::FinancialRecord]),
            vec![read_grant(resource, vec![DataClass::FinancialRecord])],
        );
        let principal =
            memory_decrypt_principal_from_verified_context(&base_verified(Some(strict)), 2_000)
                .expect("hosted principal");
        assert_eq!(
            principal.allowed_source_binding_ids,
            vec!["notion-finance-db".to_string()]
        );
    }

    #[test]
    fn deny_only_boundary_is_fail_closed_none() {
        // allowed empty + denied non-empty is not unrestricted, so the effective
        // boundary keeps an empty allow-list — inexpressible as a principal grant.
        let boundary = DataBoundary {
            allowed_data_classes: Vec::new(),
            denied_data_classes: vec![DataClass::Restricted],
        };
        let strict = strict_with(boundary, Vec::new());
        assert!(memory_decrypt_principal_from_verified_context(
            &base_verified(Some(strict)),
            2_000
        )
        .is_none());
    }

    fn project_scoped_strict(project_id: &str) -> StrictTenantContext {
        let tenant_context = TenantContext::explicit_user_workspace(
            "acme",
            "hq",
            Some("prod".to_string()),
            "user-finance",
        );
        let authority_chain = AuthorityChain::from_request(RequestPrincipal::authenticated_user(
            "user-finance",
            "web",
        ));
        let mut project_ref = ResourceRef::new("acme", "hq", ResourceKind::Project, project_id);
        project_ref.project_id = Some(project_id.to_string());
        StrictTenantContext::new(
            tenant_context,
            PrincipalRef::human_user("user-finance"),
            authority_chain,
            ResourceScope::root(project_ref),
            AssertionMetadata::new("web", "runtime", 1_000, 10_000, "assertion-a"),
        )
        .with_data_boundary(DataBoundary::allow(vec![DataClass::Internal]))
    }

    #[test]
    fn workspace_scope_covers_workspace_memory() {
        let verified = base_verified(Some(strict_with(
            DataBoundary::allow(vec![DataClass::Internal]),
            Vec::new(),
        )));
        let workspace_memory = workspace_memory_space_resource(&verified.tenant_context);
        assert!(verified_resource_scope_covers(&verified, &workspace_memory));
    }

    #[test]
    fn project_scope_does_not_cover_workspace_memory() {
        // A projection scoped to one project is narrower than the workspace memory
        // space context layers seal under, so a scoped decrypt is denied.
        let verified = base_verified(Some(project_scoped_strict("project-x")));
        let workspace_memory = workspace_memory_space_resource(&verified.tenant_context);
        assert!(!verified_resource_scope_covers(
            &verified,
            &workspace_memory
        ));
    }

    /// End-to-end server-boundary test: the principal this module derives from a
    /// verified context actually authorizes (or denies) a real hosted-KMS sealed
    /// memory read via `with_decrypt_principal` — the exact composition the
    /// `/memory/context/*` handlers perform.
    mod hosted {
        use super::*;
        use tandem_memory::db::MemoryDatabase;
        use tandem_memory::decrypt_context::with_decrypt_principal;
        use tandem_memory::dek_cache::MemoryDekCache;
        use tandem_memory::envelope_crypto::HostedMemoryEnvelopeCrypto;
        use tandem_memory::types::{LayerType, MemoryResult, MemoryTenantScope, NodeType};
        use tandem_memory::{
            GoogleCloudKmsDecryptClient, GoogleCloudKmsDecryptRequest,
            GoogleCloudKmsDekUnwrapProvider, GoogleCloudKmsDekWrapProvider,
            GoogleCloudKmsEncryptClient, GoogleCloudKmsEncryptRequest, MemoryCryptoProvider,
            MemoryDecryptBroker, MemoryDecryptBrokerConfig,
        };

        const RUNTIME_PRINCIPAL: &str = "runtime-memory-decryptor";
        const PROVIDER_ID: &str = "google_cloud_kms";
        const KEK_ID: &str = "projects/acme/locations/global/keyRings/memory/cryptoKeys/finance";

        /// Reversible keyed-XOR KMS so a DEK round-trips in-process.
        #[derive(Clone)]
        struct XorFixtureKms {
            fingerprint: u8,
        }
        impl GoogleCloudKmsEncryptClient for XorFixtureKms {
            fn encrypt(&self, request: &GoogleCloudKmsEncryptRequest) -> MemoryResult<Vec<u8>> {
                Ok(request
                    .plaintext
                    .iter()
                    .map(|byte| byte ^ self.fingerprint)
                    .collect())
            }
        }
        impl GoogleCloudKmsDecryptClient for XorFixtureKms {
            fn decrypt(&self, request: &GoogleCloudKmsDecryptRequest) -> MemoryResult<Vec<u8>> {
                Ok(request
                    .ciphertext
                    .iter()
                    .map(|byte| byte ^ self.fingerprint)
                    .collect())
            }
        }

        fn hosted_provider() -> MemoryCryptoProvider {
            let config = MemoryDecryptBrokerConfig::hosted(PROVIDER_ID, RUNTIME_PRINCIPAL).unwrap();
            let broker = MemoryDecryptBroker::new(config).unwrap();
            let kms = XorFixtureKms { fingerprint: 0x5A };
            let wrap = GoogleCloudKmsDekWrapProvider::new(kms.clone(), RUNTIME_PRINCIPAL).unwrap();
            let unwrap = GoogleCloudKmsDekUnwrapProvider::new(kms, RUNTIME_PRINCIPAL).unwrap();
            let hosted = HostedMemoryEnvelopeCrypto::new(
                broker,
                Box::new(wrap),
                Box::new(unwrap),
                MemoryDekCache::new(64),
                PROVIDER_ID,
                RUNTIME_PRINCIPAL,
                KEK_ID,
                "1",
                0,
            );
            MemoryCryptoProvider::hosted(hosted)
        }

        /// A verified context for `org` with an Internal-class data boundary — the
        /// class layers seal under.
        fn verified_internal(org: &str) -> VerifiedTenantContext {
            let tenant_context = TenantContext::explicit_user_workspace(
                org,
                "hq",
                Some("prod".to_string()),
                "user-finance",
            );
            let authority_chain = AuthorityChain::from_request(
                RequestPrincipal::authenticated_user("user-finance", "web"),
            );
            let strict = StrictTenantContext::new(
                tenant_context.clone(),
                PrincipalRef::human_user("user-finance"),
                authority_chain.clone(),
                ResourceScope::root(ResourceRef::new(org, "hq", ResourceKind::Workspace, "hq")),
                AssertionMetadata::new("web", "runtime", 1_000, 10_000, "assertion-a"),
            )
            .with_data_boundary(DataBoundary::allow(vec![DataClass::Internal]));
            VerifiedTenantContext {
                tenant_context,
                human_actor: HumanActor::tandem_user("user-finance"),
                authority_chain,
                roles: Vec::new(),
                org_units: Vec::new(),
                capabilities: Vec::new(),
                policy_version: None,
                strict_projection: Some(strict),
                issuer: "web".to_string(),
                audience: "runtime".to_string(),
                issued_at_ms: 1_000,
                expires_at_ms: 10_000,
                assertion_id: "assertion-a".to_string(),
                assertion_key_id: None,
            }
        }

        async fn hosted_db_with_layer(
        ) -> (tempfile::TempDir, MemoryDatabase, String, MemoryTenantScope) {
            let temp = tempfile::TempDir::new().unwrap();
            let db = MemoryDatabase::new(&temp.path().join("hosted.db"))
                .await
                .unwrap()
                .with_crypto_provider(hosted_provider());
            let tenant = MemoryTenantScope {
                org_id: "acme".to_string(),
                workspace_id: "hq".to_string(),
                deployment_id: Some("prod".to_string()),
            };
            let node = db
                .create_node(
                    "memory://acme/hq/summary.md",
                    None,
                    NodeType::File,
                    None,
                    &tenant,
                )
                .await
                .unwrap();
            db.create_layer(
                &node,
                LayerType::L2,
                "Summary: ACME owes $120k on invoice INV-2043",
                8,
                None,
                &tenant,
            )
            .await
            .unwrap();
            (temp, db, node, tenant)
        }

        #[tokio::test]
        async fn verified_principal_decrypts_a_hosted_sealed_layer() {
            let (_temp, db, node, tenant) = hosted_db_with_layer().await;

            // No principal → the sealed layer fails closed.
            assert!(db.get_layer(&node, LayerType::L2, &tenant).await.is_err());

            // The principal derived from the ACME verified context decrypts it.
            let principal =
                memory_decrypt_principal_from_verified_context(&verified_internal("acme"), 2_000)
                    .expect("hosted principal");
            let layer =
                with_decrypt_principal(principal, db.get_layer(&node, LayerType::L2, &tenant))
                    .await
                    .unwrap()
                    .expect("layer present");
            assert!(layer.content.contains("120k"));
        }

        #[tokio::test]
        async fn cross_tenant_verified_principal_is_denied() {
            let (_temp, db, node, tenant) = hosted_db_with_layer().await;

            // A principal built from a different tenant's verified context cannot
            // decrypt ACME's sealed layer — denied at the broker.
            let other =
                memory_decrypt_principal_from_verified_context(&verified_internal("hooli"), 2_000)
                    .expect("hosted principal");
            let result =
                with_decrypt_principal(other, db.get_layer(&node, LayerType::L2, &tenant)).await;
            assert!(result.is_err(), "cross-tenant hosted read must be denied");
        }

        #[tokio::test]
        async fn project_scoped_caller_cannot_decrypt_workspace_layer() {
            let (_temp, db, node, tenant) = hosted_db_with_layer().await;

            // A same-tenant caller whose projection is scoped to one project (not
            // the workspace) holds Internal access, but the handler gate refuses to
            // scope a decrypt principal for the workspace memory space it doesn't
            // cover — so the sealed layer stays fail-closed.
            let verified = base_verified(Some(project_scoped_strict("project-x")));
            let workspace_memory = workspace_memory_space_resource(&verified.tenant_context);
            let principal = Some(&verified)
                .filter(|verified| verified_resource_scope_covers(verified, &workspace_memory))
                .and_then(|verified| {
                    memory_decrypt_principal_from_verified_context(verified, 2_000)
                });
            assert!(
                principal.is_none(),
                "a project-scoped projection must not yield a workspace decrypt principal"
            );
            // With no principal scoped, the sealed workspace layer fails closed.
            assert!(db.get_layer(&node, LayerType::L2, &tenant).await.is_err());
        }
    }
}