agent-uri-attestation 0.2.1

PASETO v4.public attestation for agent-uri
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
//! Pure verification functions for formal verification support.
//!
//! This module contains pure functions that implement the core verification
//! logic for attestation tokens. These functions are designed to be:
//!
//! - **Deterministic**: Same inputs always produce the same output
//! - **Side-effect free**: No I/O, no mutation of external state
//! - **Individually verifiable**: Small enough for formal verification tools
//!
//! # Design Rationale
//!
//! By extracting verification logic into pure functions, we enable:
//! - Unit testing without mocks or test fixtures
//! - Formal verification with tools like Kani
//! - Clear documentation of security invariants
//!
//! # Security Properties
//!
//! | Function | Property Verified |
//! |----------|-------------------|
//! | [`validate_issuer`] | Token issuer equals URI trust root (exact match) |
//! | [`validate_subject`] | Token subject equals presented URI (exact match) |
//! | [`check_expiration`] | Current time is strictly less than expiration |
//! | [`capability_covers`] | Attested capability is prefix of or equals required |

use chrono::{DateTime, Utc};

use agent_uri::CapabilityPath;

use crate::error::AttestationError;

/// Pure function: checks if any attested capability covers the required path.
///
/// A capability covers a required path if:
/// - The capability exactly equals the required path, OR
/// - The capability is a prefix of the required path (hierarchical coverage)
///
/// # Arguments
///
/// * `attested_capabilities` - The capabilities granted in the attestation token
/// * `required` - The capability path required for the operation
///
/// # Returns
///
/// `true` if any attested capability covers the required path, `false` otherwise
///
/// # Examples
///
/// ```
/// use agent_uri::CapabilityPath;
/// use agent_uri_attestation::capability_covers;
///
/// let attested = vec!["workflow".to_string(), "assistant/chat".to_string()];
/// let required = CapabilityPath::parse("workflow/approval").unwrap();
///
/// // "workflow" is a prefix of "workflow/approval"
/// assert!(capability_covers(&attested, &required));
///
/// // Exact match also works
/// let exact = CapabilityPath::parse("assistant/chat").unwrap();
/// assert!(capability_covers(&attested, &exact));
/// ```
#[must_use]
pub fn capability_covers(attested_capabilities: &[String], required: &CapabilityPath) -> bool {
    attested_capabilities.iter().any(|cap| {
        let required_str = required.as_str();
        // Capability covers required if:
        // 1. They are exactly equal, OR
        // 2. Capability is a proper prefix (required starts with cap + "/")
        required_str == cap || required_str.starts_with(&format!("{cap}/"))
    })
}

/// Pure function: validates that the token issuer matches the URI trust root.
///
/// # Arguments
///
/// * `uri_trust_root` - The trust root extracted from the agent URI
/// * `token_issuer` - The issuer claim from the token
///
/// # Returns
///
/// `Ok(())` if the issuer matches, or `Err(AttestationError::TrustRootMismatch)` otherwise
///
/// # Errors
///
/// Returns `AttestationError::TrustRootMismatch` if `uri_trust_root` does not equal `token_issuer`.
///
/// # Examples
///
/// ```
/// use agent_uri_attestation::validate_issuer;
///
/// assert!(validate_issuer("acme.com", "acme.com").is_ok());
/// assert!(validate_issuer("acme.com", "evil.com").is_err());
/// ```
pub fn validate_issuer(
    uri_trust_root: &str,
    token_issuer: &str,
) -> Result<(), AttestationError> {
    if uri_trust_root == token_issuer {
        Ok(())
    } else {
        Err(AttestationError::TrustRootMismatch {
            token_root: token_issuer.to_string(),
            expected_root: uri_trust_root.to_string(),
        })
    }
}

/// Pure function: validates that the token subject matches the presented URI.
///
/// # Arguments
///
/// * `presented_uri` - The agent URI being verified, as a string
/// * `token_subject` - The `agent_uri` claim from the token
///
/// # Returns
///
/// `Ok(())` if they match, or `Err(AttestationError::UriMismatch)` otherwise
///
/// # Errors
///
/// Returns `AttestationError::UriMismatch` if `presented_uri` does not equal `token_subject`.
///
/// # Examples
///
/// ```
/// use agent_uri_attestation::validate_subject;
///
/// let uri = "agent://acme.com/workflow/agent_01h455vb4pex5vsknk084sn02q";
/// assert!(validate_subject(uri, uri).is_ok());
/// assert!(validate_subject(uri, "agent://evil.com/test/agent_xyz").is_err());
/// ```
pub fn validate_subject(
    presented_uri: &str,
    token_subject: &str,
) -> Result<(), AttestationError> {
    if presented_uri == token_subject {
        Ok(())
    } else {
        Err(AttestationError::UriMismatch {
            token_uri: token_subject.to_string(),
            expected_uri: presented_uri.to_string(),
        })
    }
}

/// Pure function: checks if a token has expired at a given time.
///
/// # Arguments
///
/// * `exp` - The expiration time from the token
/// * `now` - The current time to check against
///
/// # Returns
///
/// `Ok(())` if the token is still valid (now < exp), or
/// `Err(AttestationError::TokenExpired)` if expired (now >= exp)
///
/// # Errors
///
/// Returns `AttestationError::TokenExpired` if `now >= exp`.
///
/// # Examples
///
/// ```
/// use chrono::{Utc, Duration};
/// use agent_uri_attestation::check_expiration;
///
/// let now = Utc::now();
/// let future = now + Duration::hours(1);
/// let past = now - Duration::hours(1);
///
/// assert!(check_expiration(future, now).is_ok());
/// assert!(check_expiration(past, now).is_err());
/// assert!(check_expiration(now, now).is_err()); // Boundary: now >= exp is expired
/// ```
pub fn check_expiration(exp: DateTime<Utc>, now: DateTime<Utc>) -> Result<(), AttestationError> {
    if now < exp {
        Ok(())
    } else {
        Err(AttestationError::TokenExpired {
            expired_at: exp.to_rfc3339(),
        })
    }
}

/// Pure function: checks capability coverage and returns a structured error if insufficient.
///
/// # Arguments
///
/// * `attested_capabilities` - The capabilities from the token
/// * `required` - The required capability path
///
/// # Returns
///
/// `Ok(())` if capabilities are sufficient, or
/// `Err(AttestationError::InsufficientCapabilities)` if not
///
/// # Errors
///
/// Returns `AttestationError::InsufficientCapabilities` if no attested capability covers
/// the required path.
///
/// # Examples
///
/// ```
/// use agent_uri::CapabilityPath;
/// use agent_uri_attestation::check_capability_coverage;
///
/// let attested = vec!["workflow".to_string()];
/// let required = CapabilityPath::parse("workflow/approval").unwrap();
/// assert!(check_capability_coverage(&attested, &required).is_ok());
///
/// let unrelated = CapabilityPath::parse("assistant/chat").unwrap();
/// assert!(check_capability_coverage(&attested, &unrelated).is_err());
/// ```
pub fn check_capability_coverage(
    attested_capabilities: &[String],
    required: &CapabilityPath,
) -> Result<(), AttestationError> {
    if capability_covers(attested_capabilities, required) {
        Ok(())
    } else {
        Err(AttestationError::InsufficientCapabilities {
            required: required.to_string(),
            attested: attested_capabilities.to_vec(),
        })
    }
}

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

    mod capability_covers_tests {
        use super::*;

        #[test]
        fn exact_match_is_covered() {
            let attested = vec!["workflow/approval".to_string()];
            let required = CapabilityPath::parse("workflow/approval").unwrap();
            assert!(capability_covers(&attested, &required));
        }

        #[test]
        fn prefix_covers_child_path() {
            let attested = vec!["workflow".to_string()];
            let required = CapabilityPath::parse("workflow/approval").unwrap();
            assert!(capability_covers(&attested, &required));
        }

        #[test]
        fn prefix_covers_grandchild_path() {
            let attested = vec!["workflow".to_string()];
            let required = CapabilityPath::parse("workflow/approval/invoice").unwrap();
            assert!(capability_covers(&attested, &required));
        }

        #[test]
        fn sibling_path_not_covered() {
            let attested = vec!["workflow/approval".to_string()];
            let required = CapabilityPath::parse("workflow/rejection").unwrap();
            assert!(!capability_covers(&attested, &required));
        }

        #[test]
        fn unrelated_path_not_covered() {
            let attested = vec!["assistant/chat".to_string()];
            let required = CapabilityPath::parse("workflow/approval").unwrap();
            assert!(!capability_covers(&attested, &required));
        }

        #[test]
        fn empty_capabilities_cover_nothing() {
            let attested: Vec<String> = vec![];
            let required = CapabilityPath::parse("workflow").unwrap();
            assert!(!capability_covers(&attested, &required));
        }

        #[test]
        fn reverse_prefix_not_covered() {
            // "workflow/approval" does NOT cover "workflow"
            let attested = vec!["workflow/approval".to_string()];
            let required = CapabilityPath::parse("workflow").unwrap();
            assert!(!capability_covers(&attested, &required));
        }

        #[test]
        fn partial_segment_match_not_covered() {
            // "work" should NOT cover "workflow"
            let attested = vec!["work".to_string()];
            let required = CapabilityPath::parse("workflow").unwrap();
            assert!(!capability_covers(&attested, &required));
        }

        #[test]
        fn multiple_capabilities_any_covers() {
            let attested = vec![
                "assistant/chat".to_string(),
                "workflow".to_string(),
                "storage/read".to_string(),
            ];
            let required = CapabilityPath::parse("workflow/approval").unwrap();
            assert!(capability_covers(&attested, &required));
        }
    }

    mod issuer_validation_tests {
        use super::*;

        #[test]
        fn exact_match_succeeds() {
            assert!(validate_issuer("acme.com", "acme.com").is_ok());
        }

        #[test]
        fn mismatch_fails() {
            let result = validate_issuer("acme.com", "evil.com");
            assert!(matches!(
                result,
                Err(AttestationError::TrustRootMismatch { .. })
            ));
        }

        #[test]
        fn case_difference_fails() {
            // Issuers are case-sensitive
            let result = validate_issuer("acme.com", "ACME.COM");
            assert!(matches!(
                result,
                Err(AttestationError::TrustRootMismatch { .. })
            ));
        }

        #[test]
        fn with_port_exact_match() {
            assert!(validate_issuer("localhost:8472", "localhost:8472").is_ok());
        }
    }

    mod subject_validation_tests {
        use super::*;

        #[test]
        fn exact_match_succeeds() {
            let uri = "agent://acme.com/workflow/agent_01h455vb4pex5vsknk084sn02q";
            assert!(validate_subject(uri, uri).is_ok());
        }

        #[test]
        fn mismatch_fails() {
            let presented = "agent://acme.com/workflow/agent_01h455vb4pex5vsknk084sn02q";
            let token_sub = "agent://acme.com/other/agent_01h455vb4pex5vsknk084sn02q";
            let result = validate_subject(presented, token_sub);
            assert!(matches!(result, Err(AttestationError::UriMismatch { .. })));
        }
    }

    mod expiration_tests {
        use super::*;
        use chrono::Duration;

        #[test]
        fn future_expiration_is_valid() {
            let now = Utc::now();
            let exp = now + Duration::hours(1);
            assert!(check_expiration(exp, now).is_ok());
        }

        #[test]
        fn exact_expiration_is_expired() {
            let now = Utc::now();
            let result = check_expiration(now, now);
            assert!(matches!(result, Err(AttestationError::TokenExpired { .. })));
        }

        #[test]
        fn past_expiration_is_expired() {
            let now = Utc::now();
            let exp = now - Duration::hours(1);
            let result = check_expiration(exp, now);
            assert!(matches!(result, Err(AttestationError::TokenExpired { .. })));
        }

        #[test]
        fn one_second_before_expiration_is_valid() {
            let exp = Utc::now() + Duration::seconds(1);
            let now = exp - Duration::seconds(1);
            assert!(check_expiration(exp, now).is_ok());
        }
    }

    mod check_capability_coverage_tests {
        use super::*;

        #[test]
        fn returns_ok_when_covered() {
            let attested = vec!["workflow".to_string()];
            let required = CapabilityPath::parse("workflow/approval").unwrap();
            assert!(check_capability_coverage(&attested, &required).is_ok());
        }

        #[test]
        fn returns_err_with_details_when_not_covered() {
            let attested = vec!["assistant/chat".to_string()];
            let required = CapabilityPath::parse("workflow/approval").unwrap();
            let result = check_capability_coverage(&attested, &required);
            match result {
                Err(AttestationError::InsufficientCapabilities {
                    required: req,
                    attested: att,
                }) => {
                    assert_eq!(req, "workflow/approval");
                    assert_eq!(att, vec!["assistant/chat".to_string()]);
                }
                _ => panic!("Expected InsufficientCapabilities error"),
            }
        }
    }
}