aft-base 0.67.1

Shared foundation for Agent Foundational Toolkit
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
#![cfg(test)]

//! The `secret.use` seam against the contract's golden examples and the pinned
//! approval vectors.
//!
//! `crates/aft/tests/fixtures/secret/secret-approval-v1.json` is a byte-for-byte
//! copy of claustrum's `crates/credentials-core/tests/vectors/secret-approval-v1.json`
//! (sha256 `efd4ba6064…495cc27`). Reading it here proves three things at once:
//! the request AFT builds carries exactly the contract's key set, a reply is
//! read by exactly the contract's rules, and the statement AFT hands to the
//! consent ask is the signed bytes the phone will answer.

use super::*;
use serde_json::json;
use serde_json::Value;
use std::collections::BTreeSet;

/// The pinned vector file, as shipped in this repository.
const VECTORS: &str = include_str!("../../../aft/tests/fixtures/secret/secret-approval-v1.json");

/// The sha256 the campaign pinned for the vector file.
const VECTORS_SHA256: &str = "efd4ba60641586e143bce81087e4f9f5aaf8a99ffede74190303da7d4495cc27";

// The six agent-sender request/result examples in claustrum's
// docs/wire-contract-v1.md, §15 "secret.use: the sender contract", in document
// order. Keep their literal JSON bytes, including statement bytes.
pub(super) const USE_GOLDENS: &str =
    include_str!("../../tests/fixtures/secret/secret-use-v1.jsonl");

#[test]
fn every_sender_golden_round_trips_through_the_wire_types() {
    let examples: Vec<_> = USE_GOLDENS.lines().collect();
    assert_eq!(examples.len(), 6);
    for (index, example) in examples.into_iter().enumerate() {
        let value: Value = serde_json::from_str(example).unwrap();
        let encoded = if index < 3 {
            let request: SecretUseRequest = serde_json::from_value(value).unwrap();
            serde_json::to_string(&request).unwrap()
        } else {
            let reply = parse_reply(&value).unwrap();
            serde_json::to_string(&reply).unwrap()
        };
        assert_eq!(encoded, example, "sender golden {index}");
    }
}

#[test]
fn sender_refusals_preserve_the_contract_code_class_and_error_frame() {
    use SecretUseRefusal::*;
    for (code, class, refusal) in [
        ("not_found", "permanent", NotFound),
        ("limit_exceeded", "context_overflow", LimitExceeded),
        ("secret_setup_incomplete", "permanent", SetupIncomplete),
        ("pending_limit", "transient", PendingLimit),
        ("store_error", "transient", StoreError),
    ] {
        let answer =
            parse_answer(&json!({"result":{"error":{"code":code,"class":class}}})).unwrap();
        assert_eq!(answer.refusal(), Some(refusal));
        let SecretUseAnswer::Refused(error) = answer else {
            panic!("result refusal")
        };
        assert_eq!(error.code, code);
        assert_eq!(error.class.as_deref(), Some(class));
    }
    let answer =
        parse_answer(&json!({"code":"invalid_params","message":"invalid params"})).unwrap();
    assert_eq!(answer.refusal(), Some(InvalidParams));
    assert!(matches!(answer, SecretUseAnswer::Rejected(_)));
}

fn vectors() -> Value {
    serde_json::from_str(VECTORS).expect("the pinned vector file is JSON")
}

fn hex(bytes: &[u8]) -> String {
    bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}

fn base64url_decode(text: &str) -> Vec<u8> {
    use base64::Engine;
    base64::engine::general_purpose::URL_SAFE_NO_PAD
        .decode(text)
        .unwrap_or_else(|error| panic!("strict unpadded base64url: {error}"))
}

fn sha256_hex(bytes: &[u8]) -> String {
    use sha2::Digest;
    hex(&sha2::Sha256::digest(bytes))
}

/// The daemon stamp the contract's golden request relays.
fn golden_stamp() -> subc_protocol::scope::ScopeStamp {
    serde_json::from_value(json!({
        "owner": {"kind": "reserved", "module_id": "prefrontal-core"},
        "ref": "H",
        "scope_epoch": 7,
        "kind": "head",
        "attributes": {"agent_id": "agent-A"},
        "owner_authorized": true,
    }))
    .unwrap()
}

fn keys(value: &Value) -> BTreeSet<String> {
    value
        .as_object()
        .expect("an object")
        .keys()
        .cloned()
        .collect()
}

fn set(names: &[&str]) -> BTreeSet<String> {
    names.iter().map(|name| (*name).to_string()).collect()
}

#[test]
fn the_shipped_vector_file_is_the_pinned_one() {
    assert_eq!(sha256_hex(VECTORS.as_bytes()), VECTORS_SHA256);
}

#[test]
fn the_golden_api_key_request_has_exactly_the_contract_key_set() {
    let request = SecretUseRequest::new(
        "key",
        &golden_stamp(),
        Target::ApiKey(ApiKeyTarget {
            scheme: "https".into(),
            host: "x.test".into(),
            port: Some(443),
            path: "/repos/a/issues/1".into(),
        }),
    )
    .with_coverage(Coverage::Scope);
    let params = request.to_params();
    assert_eq!(keys(&params), set(&["name", "coverage", "stamp", "target"]));
    assert_eq!(
        params["target"],
        json!({"scheme": "https", "host": "x.test", "port": 443, "path": "/repos/a/issues/1"})
    );
    // The stamp is relayed unchanged, attribute object included.
    assert_eq!(params["stamp"], json!(golden_stamp()));
    assert_eq!(params["stamp"]["attributes"]["agent_id"], json!("agent-A"));
    // No agent name is ever sent: AFT holds no trusted one, and the contract's
    // key set is closed.
    assert!(params.get("display_name").is_none());
}

#[test]
fn the_default_request_matches_the_contracts_defaults_example() {
    // `coverage` and `port` omitted is the same call as the golden example
    // with them, per the contract's own "defaults" example.
    let request = SecretUseRequest::new(
        "key",
        &golden_stamp(),
        Target::ApiKey(ApiKeyTarget {
            scheme: "https".into(),
            host: "x.test".into(),
            port: None,
            path: "/repos/a/issues/1".into(),
        }),
    );
    let params = request.to_params();
    assert_eq!(keys(&params), set(&["name", "stamp", "target"]));
    assert_eq!(
        params["target"],
        json!({"scheme": "https", "host": "x.test", "path": "/repos/a/issues/1"})
    );
}

#[test]
fn a_golden_released_api_key_reply_parses_to_the_key_and_its_location() {
    let reply = parse_reply(&json!({
        "status": "released",
        "kind": "agent_api_key",
        "key_b64url": "c2stZXhhbXBsZQ",
        "location": "header:X-Api-Key",
    }))
    .expect("the contract's released api key example parses");
    let SecretUseReply::ReleasedApiKey(key) = reply else {
        panic!("a released api key reply is the key form");
    };
    assert_eq!(key.material(), b"sk-example");
    assert_eq!(key.location, "header:X-Api-Key");
    // The location is one the rules crate can act on.
    assert!(secret_rules::location::parse_vault_location(&key.location).is_some());
    // Nothing in the debug form prints the key.
    assert!(!format!("{key:?}").contains("sk-example"));
}

#[test]
fn a_golden_released_login_reply_parses_with_and_without_totp() {
    let reply = parse_reply(&json!({
        "status": "released",
        "kind": "site_login",
        "username": "alice",
        "password": "correct horse",
        "totp": "123456",
    }))
    .expect("the contract's site login example parses");
    let SecretUseReply::ReleasedLogin(login) = reply else {
        panic!("a site login reply is the login form");
    };
    assert_eq!(login.username, "alice");
    assert_eq!(login.password(), b"correct horse");
    assert_eq!(
        login.totp.as_ref().map(|code| code.as_str()),
        Some("123456")
    );

    let without = parse_reply(&json!({
        "status": "released",
        "kind": "site_login",
        "username": "alice",
        "password": "correct horse",
    }))
    .expect("a login without a TOTP seed carries no totp key");
    let SecretUseReply::ReleasedLogin(login) = without else {
        panic!("a site login reply is the login form");
    };
    assert_eq!(login.totp, None);
}

#[test]
fn a_released_reply_carrying_a_key_the_contract_does_not_show_is_refused() {
    // Each key set is closed, so an extra member is a protocol error rather
    // than something to ignore.
    for reply in [
        json!({"status":"released","kind":"agent_api_key","key_b64url":"c2stZXhhbXBsZQ","location":"header:X-Api-Key","expires_at_ms":1}),
        json!({"status":"released","kind":"site_login","username":"alice","password":"correct horse","totp":"123456","expires_at_ms":1}),
        json!({"status":"approval_required","request_id":"01HF7YAT000000000000000001","statement_b64url":"eyJ2IjoxfQ","expires_at_ms":1,"note":"x"}),
    ] {
        assert!(parse_reply(&reply).is_err(), "{reply}");
    }
    // A missing member is refused too.
    assert!(parse_reply(&json!({
        "status": "released", "kind": "agent_api_key", "key_b64url": "c2stZXhhbXBsZQ"
    }))
    .is_err());
}

#[test]
fn a_key_that_is_not_strict_unpadded_base64url_is_refused() {
    for key in ["c2stZXhhbXBsZQ==", "c2stZXhh bXBsZQ", "c2stZXhhbXBsZR"] {
        let reply = json!({
            "status": "released",
            "kind": "agent_api_key",
            "key_b64url": key,
            "location": "authorization_bearer",
        });
        assert_eq!(
            parse_reply(&reply).err(),
            Some(ReplyError::BadKeyEncoding),
            "{key}"
        );
    }
}

#[test]
fn the_golden_approval_required_reply_parses_to_its_statement() {
    let file = vectors();
    let statement = &file["vectors"][0]["statements"][0];
    let reply = parse_reply(&json!({
        "status": "approval_required",
        "request_id": statement["request_id"],
        "statement_b64url": statement["statement_b64url"],
        "expires_at_ms": 1700000600000u64,
    }))
    .expect("the golden approval_required reply parses");
    let SecretUseReply::ApprovalRequired(request) = reply else {
        panic!("an approval_required reply is the approval form");
    };
    assert_eq!(
        request.request_id,
        statement["request_id"].as_str().unwrap()
    );
    assert_eq!(request.expires_at_ms, 1700000600000);
    // The statement AFT hands to the consent ask is the exact bytes the phone
    // signs: decoding and hashing it must reproduce the vector's digest.
    assert_eq!(
        sha256_hex(&base64url_decode(&request.statement_b64url)),
        statement["statement_sha256"].as_str().unwrap()
    );
}

#[test]
fn every_approval_vector_statement_decodes_the_documented_fields() {
    let file = vectors();
    for vector in file["vectors"].as_array().expect("vectors is an array") {
        let case = vector["case"].as_str().unwrap();
        for statement in vector["statements"].as_array().unwrap() {
            let raw = base64url_decode(statement["statement_b64url"].as_str().unwrap());
            assert_eq!(
                sha256_hex(&raw),
                statement["statement_sha256"].as_str().unwrap(),
                "{case}"
            );
            let decoded: Value = serde_json::from_slice(&raw).expect("a statement is JSON");
            assert_eq!(
                decoded["request_id"],
                statement["request_id"].clone(),
                "{case}"
            );
            // The two statement domains the vectors cover.
            let version = decoded["v"].as_str().unwrap();
            assert!(
                version == "ck-secret-use-statement/v1"
                    || version == "ck-secret-delete-statement/v1",
                "{case}: {version}"
            );
            if version == "ck-secret-use-statement/v1" {
                // The approval statement's own scope names the mapped asking
                // scope, which is what AFT relays in the stamp.
                assert_eq!(decoded["scope"]["kind"], json!("head"), "{case}");
                assert_eq!(
                    decoded["scope"]["owner"],
                    json!("reserved:prefrontal-core"),
                    "{case}"
                );
                assert_eq!(decoded["scope"]["ref"], json!("H"), "{case}");
                assert_eq!(decoded["scope"]["epoch"], json!(7), "{case}");
                assert_eq!(decoded["agent_id"], json!("agent-A"), "{case}");
                assert_eq!(decoded["coverage"], json!("scope"), "{case}");
            }
        }
        // The envelope carries the statement digests, and it is the bytes the
        // phone signs: its own hash must match.
        let envelope = base64url_decode(vector["envelope_b64url"].as_str().unwrap());
        assert_eq!(
            sha256_hex(&envelope),
            vector["envelope_sha256"].as_str().unwrap(),
            "{case}"
        );
        let envelope: Value = serde_json::from_slice(&envelope).expect("an envelope is JSON");
        assert_eq!(envelope["v"], json!("ck-secret-use-envelope/v1"), "{case}");
        let items = envelope["items"].as_array().unwrap();
        // Every listed item is one of the case's statements. An envelope may
        // leave a pending request out (the `batch-omitted` case exists to prove
        // it), and it never invents a request id.
        for item in items {
            if item["request_id"] == json!("00000000000000000000000000") {
                // `batch-unknown`'s deliberately unknown item.
                continue;
            }
            assert!(
                vector["statements"]
                    .as_array()
                    .unwrap()
                    .iter()
                    .any(|statement| {
                        item["request_id"] == statement["request_id"]
                            && item["statement_sha256"] == statement["statement_sha256"]
                    }),
                "{case}: the envelope lists an item no statement backs"
            );
        }
        assert!(
            !items.is_empty(),
            "{case}: an envelope carries at least one item"
        );
    }
}

#[test]
fn a_tampered_envelope_vector_no_longer_matches_its_recorded_hash() {
    // The vector file ships a deliberately altered envelope for each case; it
    // must not hash to the accepted one, which is what makes the digest a
    // signature input rather than a decoration.
    for vector in vectors()["vectors"].as_array().unwrap() {
        let case = vector["case"].as_str().unwrap();
        let tampered = base64url_decode(vector["tampered_envelope_b64url"].as_str().unwrap());
        assert_ne!(
            sha256_hex(&tampered),
            vector["envelope_sha256"].as_str().unwrap(),
            "{case}"
        );
    }
}

#[test]
fn a_result_error_and_a_transport_error_read_as_the_contract_spells_them() {
    let file = vectors();
    // A result error carries the code and class (§15's table).
    let answer =
        parse_answer(&json!({"result":{"error":{"code":"not_found","class":"permanent"}}}))
            .expect("a result error parses");
    let SecretUseAnswer::Refused(error) = answer else {
        panic!("a result error is a refusal");
    };
    assert_eq!(error.code, "not_found");
    assert_eq!(error.class.as_deref(), Some("permanent"));
    assert!(!error.is_transient());

    // A transport error carries a fixed message and no class (§1a).
    let answer = parse_answer(&json!({
        "code": "invalid_params",
        "message": "invalid_params"
    }))
    .expect("a transport error parses");
    let SecretUseAnswer::Rejected(error) = answer else {
        panic!("a transport error is a rejection");
    };
    assert_eq!(error.code, "invalid_params");

    // The reply forms ride inside `result`.
    let answer = parse_answer(&json!({"result":{
        "status": "released",
        "kind": "agent_api_key",
        "key_b64url": "c2stZXhhbXBsZQ",
        "location": "header:X-Api-Key"
    }}))
    .expect("a wrapped release parses");
    assert!(matches!(
        answer,
        SecretUseAnswer::Reply(SecretUseReply::ReleasedApiKey(_))
    ));
    assert!(!file["vectors"].as_array().unwrap().is_empty());
}

#[test]
fn the_secret_use_client_sends_the_pinned_params_and_reads_a_release() {
    struct Recording(std::sync::Mutex<Vec<Value>>);
    impl SecretUseTransport for Recording {
        fn secret_use(&self, params: Value) -> Result<zeroize::Zeroizing<Vec<u8>>, String> {
            self.0.lock().unwrap().push(params);
            Ok(zeroize::Zeroizing::new(
                serde_json::to_vec(&json!({"result":{
                    "status": "released",
                    "kind": "agent_api_key",
                    "key_b64url": "c2stZXhhbXBsZQ",
                    "location": "header:X-Api-Key"
                }}))
                .unwrap(),
            ))
        }
    }
    let transport = std::sync::Arc::new(Recording(std::sync::Mutex::new(Vec::new())));
    let vault = SubcSecretVault::new(transport.clone());
    let scope = SecretScope::from_stamp(&golden_stamp());
    let answer = vault.use_secret(
        &scope,
        "key",
        &ApiKeyTarget {
            scheme: "https".into(),
            host: "x.test".into(),
            port: Some(443),
            path: "/repos/a".into(),
        },
    );
    let UseAnswer::Released(released) = answer else {
        panic!("a release keeps material for this request only");
    };
    assert_eq!(released.location, "header:X-Api-Key");
    assert_eq!(released.material(), b"sk-example");
    let sent = transport.0.lock().unwrap();
    assert_eq!(sent.len(), 1);
    assert_eq!(keys(&sent[0]), set(&["name", "stamp", "target"]));
    assert_eq!(sent[0]["stamp"], json!(golden_stamp()));
    assert_eq!(sent[0]["name"], json!("key"));
}

#[test]
fn the_secret_use_client_reports_an_approval_as_pending() {
    struct Pending;
    impl SecretUseTransport for Pending {
        fn secret_use(&self, _params: Value) -> Result<zeroize::Zeroizing<Vec<u8>>, String> {
            let file = vectors();
            Ok(zeroize::Zeroizing::new(
                serde_json::to_vec(&json!({"result":{
                    "status": "approval_required",
                    "request_id": file["vectors"][0]["statements"][0]["request_id"],
                    "statement_b64url": file["vectors"][0]["statements"][0]["statement_b64url"],
                    "expires_at_ms": 1700000600000u64,
                }}))
                .unwrap(),
            ))
        }
    }
    let vault = SubcSecretVault::new(std::sync::Arc::new(Pending));
    let scope = SecretScope::from_stamp(&golden_stamp());
    let UseAnswer::ApprovalRequired(request) = vault.use_secret(
        &scope,
        "key",
        &ApiKeyTarget {
            scheme: "https".into(),
            host: "x.test".into(),
            port: Some(443),
            path: "/repos/a".into(),
        },
    ) else {
        panic!("an approval_required answer is a pending resolution");
    };
    assert_eq!(request.request_id, "01HF7YAT000000000000000001");
    assert!(!request.statement_b64url.is_empty());
}

#[test]
fn the_secret_use_client_preserves_uniform_not_found_for_literal_forwarding() {
    struct NotFound;
    impl SecretUseTransport for NotFound {
        fn secret_use(&self, _params: Value) -> Result<zeroize::Zeroizing<Vec<u8>>, String> {
            Ok(zeroize::Zeroizing::new(
                serde_json::to_vec(
                    &json!({"result":{"error":{"code":"not_found","class":"permanent"}}}),
                )
                .unwrap(),
            ))
        }
    }
    let vault = SubcSecretVault::new(std::sync::Arc::new(NotFound));
    let scope = SecretScope::from_stamp(&golden_stamp());
    assert!(matches!(
        vault.use_secret(
            &scope,
            "key",
            &ApiKeyTarget {
                scheme: "https".into(),
                host: "x.test".into(),
                port: Some(443),
                path: "/repos/a".into()
            }
        ),
        UseAnswer::NotFound
    ));
}

#[test]
fn a_deposit_location_token_reads_as_the_rules_location() {
    // The deposit's four location spellings, as §15 lists them.
    assert_eq!(
        secret_rules::location::parse_vault_location("authorization_bearer"),
        Some(secret_rules::location::Location::Bearer)
    );
    assert_eq!(
        secret_rules::location::parse_vault_location("header:X-Api-Key"),
        Some(secret_rules::location::Location::Header("X-Api-Key".into()))
    );
    assert_eq!(
        secret_rules::location::parse_vault_location("query:token"),
        Some(secret_rules::location::Location::Query("token".into()))
    );
    assert_eq!(
        secret_rules::location::parse_vault_location("json_pointer:/a/b"),
        Some(secret_rules::location::Location::Json("/a/b".into()))
    );
    assert_eq!(
        secret_rules::location::parse_vault_location("header:"),
        None
    );
    assert_eq!(
        secret_rules::location::parse_vault_location("cookie:x"),
        None
    );
}