matrixcode-core 0.4.43

MatrixCode Agent Core - Pure logic, no UI
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
//! Security Validator
//!
//! Validates callback requests from external services.
//! Ensures request_id + service_id + token combination is valid.

use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};

use tokio::sync::RwLock;

use crate::matrixrpc::ServiceId;

/// Security error types
#[derive(Debug, thiserror::Error)]
pub enum SecurityError {
    /// Invalid token
    #[error("Invalid or expired token")]
    InvalidToken,

    /// Token expired
    #[error("Token expired at {0}")]
    TokenExpired(String),

    /// Service not authorized
    #[error("Service '{0}' is not authorized for this callback")]
    ServiceNotAuthorized(String),

    /// Request ID mismatch
    #[error("Request ID '{0}' does not match the token")]
    RequestIdMismatch(String),

    /// Missing required field
    #[error("Missing required field: {0}")]
    MissingField(String),

    /// Token generation failed
    #[error("Token generation failed: {0}")]
    TokenGenerationFailed(String),

    /// Rate limit exceeded
    #[error("Rate limit exceeded for service '{0}'")]
    RateLimitExceeded(String),

    /// Internal error
    #[error("Internal security error: {0}")]
    Internal(String),
}

/// Token information
#[derive(Debug, Clone)]
pub struct TokenInfo {
    /// The token value
    pub token: String,

    /// Service ID that owns this token
    pub service_id: ServiceId,

    /// Request ID this token is associated with
    pub request_id: String,

    /// When the token was created
    pub created_at: Instant,

    /// When the token expires
    pub expires_at: Instant,

    /// Allowed callback types
    pub allowed_types: Vec<String>,

    /// Usage count
    pub usage_count: u32,

    /// Maximum allowed uses
    pub max_uses: u32,
}

impl TokenInfo {
    /// Create a new token info
    pub fn new(
        token: String,
        service_id: ServiceId,
        request_id: String,
        lifetime_secs: u64,
    ) -> Self {
        let now = Instant::now();
        Self {
            token,
            service_id,
            request_id,
            created_at: now,
            expires_at: now + Duration::from_secs(lifetime_secs),
            allowed_types: vec![
                "ai".to_string(), "tool".to_string(), "context".to_string(),
            ],
            usage_count: 0,
            max_uses: 10,
        }
    }

    /// Set allowed callback types
    pub fn with_allowed_types(mut self, types: Vec<String>) -> Self {
        self.allowed_types = types;
        self
    }

    /// Set maximum uses
    pub fn with_max_uses(mut self, max: u32) -> Self {
        self.max_uses = max;
        self
    }

    /// Check if token is expired
    pub fn is_expired(&self) -> bool {
        Instant::now() > self.expires_at
    }

    /// Check if token has remaining uses
    pub fn has_remaining_uses(&self) -> bool {
        self.usage_count < self.max_uses
    }

    /// Check if callback type is allowed
    pub fn is_type_allowed(&self, callback_type: &str) -> bool {
        self.allowed_types.contains(&callback_type.to_string())
    }

    /// Increment usage count
    pub fn increment_usage(&mut self) {
        self.usage_count += 1;
    }
}

/// Validation result
#[derive(Debug, Clone)]
pub struct ValidationResult {
    /// Whether validation passed
    pub is_valid: bool,

    /// The validated token info (if valid)
    pub token_info: Option<TokenInfo>,

    /// Error message (if invalid)
    pub error: Option<String>,
}

impl ValidationResult {
    /// Create a successful validation result
    pub fn success(token_info: TokenInfo) -> Self {
        Self {
            is_valid: true,
            token_info: Some(token_info),
            error: None,
        }
    }

    /// Create a failed validation result
    pub fn failure(error: impl Into<String>) -> Self {
        Self {
            is_valid: false,
            token_info: None,
            error: Some(error.into()),
        }
    }
}

/// Security configuration
#[derive(Debug, Clone)]
pub struct SecurityConfig {
    /// Token lifetime in seconds
    pub token_lifetime_secs: u64,

    /// Maximum uses per token
    pub max_token_uses: u32,

    /// Maximum tokens per service
    pub max_tokens_per_service: u32,

    /// Rate limit: requests per minute per service
    pub rate_limit_per_minute: u32,

    /// Enable strict validation
    pub strict_validation: bool,
}

impl Default for SecurityConfig {
    fn default() -> Self {
        Self {
            token_lifetime_secs: 300, // 5 minutes
            max_token_uses: 10,
            max_tokens_per_service: 100,
            rate_limit_per_minute: 60,
            strict_validation: true,
        }
    }
}

/// Rate limit tracker
#[derive(Debug, Clone)]
struct RateLimitEntry {
    /// Request timestamps
    timestamps: Vec<Instant>,
    /// Last cleanup time
    last_cleanup: Instant,
}

impl RateLimitEntry {
    fn new() -> Self {
        Self {
            timestamps: Vec::new(),
            last_cleanup: Instant::now(),
        }
    }

    fn add_request(&mut self) {
        self.timestamps.push(Instant::now());
        // Cleanup old entries every minute
        if self.last_cleanup.elapsed() > Duration::from_secs(60) {
            self.cleanup();
            self.last_cleanup = Instant::now();
        }
    }

    fn cleanup(&mut self) {
        let cutoff = Instant::now() - Duration::from_secs(60);
        self.timestamps.retain(|t| *t > cutoff);
    }

    fn count_last_minute(&self) -> u32 {
        let cutoff = Instant::now() - Duration::from_secs(60);
        self.timestamps.iter().filter(|t| **t > cutoff).count() as u32
    }
}

/// Security Validator
///
/// Validates callback requests from external services.
/// Manages token generation, validation, and rate limiting.
pub struct SecurityValidator {
    /// Configuration
    config: SecurityConfig,

    /// Active tokens
    tokens: Arc<RwLock<HashMap<String, TokenInfo>>>,

    /// Service to token mapping
    service_tokens: Arc<RwLock<HashMap<ServiceId, Vec<String>>>>,

    /// Rate limit tracking
    rate_limits: Arc<RwLock<HashMap<ServiceId, RateLimitEntry>>>,
}

impl SecurityValidator {
    /// Create a new security validator
    pub fn new() -> Self {
        Self::with_config(SecurityConfig::default())
    }

    /// Create a new security validator with configuration
    pub fn with_config(config: SecurityConfig) -> Self {
        Self {
            config,
            tokens: Arc::new(RwLock::new(HashMap::new())),
            service_tokens: Arc::new(RwLock::new(HashMap::new())),
            rate_limits: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Generate a new token for a callback request
    pub async fn generate_token(
        &self,
        service_id: ServiceId,
        request_id: String,
        allowed_types: Vec<String>,
    ) -> Result<String, SecurityError> {
        // Check if service has too many tokens
        {
            let service_tokens = self.service_tokens.read().await;
            if let Some(tokens) = service_tokens.get(&service_id) {
                if tokens.len() >= self.config.max_tokens_per_service as usize {
                    return Err(SecurityError::RateLimitExceeded(service_id.to_string()));
                }
            }
        }

        // Generate unique token
        let token = format!(
            "cb_{}_{}",
            uuid::Uuid::new_v4().to_string(),
            &request_id[..8.min(request_id.len())]
        );

        // Create token info
        let token_info = TokenInfo::new(
            token.clone(),
            service_id.clone(),
            request_id,
            self.config.token_lifetime_secs,
        )
        .with_allowed_types(allowed_types)
        .with_max_uses(self.config.max_token_uses);

        // Store token
        {
            let mut tokens = self.tokens.write().await;
            tokens.insert(token.clone(), token_info);
        }

        // Update service token mapping
        {
            let mut service_tokens = self.service_tokens.write().await;
            service_tokens
                .entry(service_id)
                .or_insert_with(Vec::new)
                .push(token.clone());
        }

        Ok(token)
    }

    /// Validate a callback request
    pub async fn validate(
        &self,
        token: &str,
        service_id: &ServiceId,
        request_id: &str,
        callback_type: &str,
    ) -> ValidationResult {
        // Check rate limit first
        {
            let mut rate_limits = self.rate_limits.write().await;
            let entry = rate_limits
                .entry(service_id.clone())
                .or_insert_with(RateLimitEntry::new);

            if entry.count_last_minute() >= self.config.rate_limit_per_minute {
                return ValidationResult::failure(SecurityError::RateLimitExceeded(
                    service_id.to_string(),
                ).to_string());
            }

            entry.add_request();
        }

        // Look up token
        let mut tokens = self.tokens.write().await;
        let token_info = match tokens.get_mut(token) {
            Some(info) => info,
            None => return ValidationResult::failure(SecurityError::InvalidToken.to_string()),
        };

        // Check expiration
        if token_info.is_expired() {
            tokens.remove(token);
            return ValidationResult::failure(
                SecurityError::TokenExpired("token has expired".to_string()).to_string(),
            );
        }

        // Check remaining uses
        if !token_info.has_remaining_uses() {
            return ValidationResult::failure("Token usage limit exceeded".to_string());
        }

        // Validate service ID
        if token_info.service_id != *service_id {
            return ValidationResult::failure(
                SecurityError::ServiceNotAuthorized(service_id.to_string()).to_string(),
            );
        }

        // Validate request ID
        if self.config.strict_validation && token_info.request_id != request_id {
            return ValidationResult::failure(
                SecurityError::RequestIdMismatch(request_id.to_string()).to_string(),
            );
        }

        // Validate callback type
        if !token_info.is_type_allowed(callback_type) {
            return ValidationResult::failure(format!(
                "Callback type '{}' is not allowed for this token",
                callback_type
            ));
        }

        // Increment usage
        token_info.increment_usage();

        ValidationResult::success(token_info.clone())
    }

    /// Invalidate a token
    pub async fn invalidate_token(&self, token: &str) -> Result<(), SecurityError> {
        let token_info = {
            let mut tokens = self.tokens.write().await;
            tokens.remove(token)
        };

        if let Some(info) = token_info {
            // Remove from service mapping
            let mut service_tokens = self.service_tokens.write().await;
            if let Some(tokens) = service_tokens.get_mut(&info.service_id) {
                tokens.retain(|t| t != token);
            }
        }

        Ok(())
    }

    /// Invalidate all tokens for a service
    pub async fn invalidate_service_tokens(&self, service_id: &ServiceId) {
        let tokens_to_remove = {
            let service_tokens = self.service_tokens.read().await;
            service_tokens.get(service_id).cloned().unwrap_or_default()
        };

        {
            let mut tokens = self.tokens.write().await;
            for token in &tokens_to_remove {
                tokens.remove(token);
            }
        }

        {
            let mut service_tokens = self.service_tokens.write().await;
            service_tokens.remove(service_id);
        }
    }

    /// Clean up expired tokens
    pub async fn cleanup_expired(&self) -> usize {
        let expired_tokens: Vec<String> = {
            let tokens = self.tokens.read().await;
            tokens
                .iter()
                .filter(|(_, info)| info.is_expired())
                .map(|(token, _)| token.clone())
                .collect()
        };

        let count = expired_tokens.len();

        for token in &expired_tokens {
            self.invalidate_token(token).await.ok();
        }

        count
    }

    /// Get active token count
    pub async fn token_count(&self) -> usize {
        self.tokens.read().await.len()
    }

    /// Get token info
    pub async fn get_token_info(&self, token: &str) -> Option<TokenInfo> {
        self.tokens.read().await.get(token).cloned()
    }
}

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

    #[tokio::test]
    async fn test_generate_token() {
        let validator = SecurityValidator::new();
        let service_id = ServiceId::new("test-service");
        let request_id = "req-001".to_string();

        let token = validator
            .generate_token(service_id.clone(), request_id.clone(), vec!["ai".to_string(), "tool".to_string()])
            .await
            .unwrap();

        assert!(token.starts_with("cb_"));
        assert!(validator.token_count().await == 1);
    }

    #[tokio::test]
    async fn test_validate_token() {
        let validator = SecurityValidator::new();
        let service_id = ServiceId::new("test-service");
        let request_id = "req-001".to_string();

        let token = validator
            .generate_token(service_id.clone(), request_id.clone(), vec!["ai".to_string()])
            .await
            .unwrap();

        let result = validator
            .validate(&token, &service_id, &request_id, "ai")
            .await;

        assert!(result.is_valid);
        assert!(result.token_info.is_some());
    }

    #[tokio::test]
    async fn test_validate_invalid_token() {
        let validator = SecurityValidator::new();
        let service_id = ServiceId::new("test-service");

        let result = validator
            .validate("invalid_token", &service_id, "req-001", "ai")
            .await;

        assert!(!result.is_valid);
        assert!(result.error.is_some());
    }

    #[tokio::test]
    async fn test_validate_wrong_service() {
        let validator = SecurityValidator::new();
        let service_id1 = ServiceId::new("service1");
        let service_id2 = ServiceId::new("service2");
        let request_id = "req-001".to_string();

        let token = validator
            .generate_token(service_id1.clone(), request_id.clone(), vec!["ai".to_string()])
            .await
            .unwrap();

        let result = validator
            .validate(&token, &service_id2, &request_id, "ai")
            .await;

        assert!(!result.is_valid);
    }

    #[tokio::test]
    async fn test_validate_wrong_callback_type() {
        let validator = SecurityValidator::new();
        let service_id = ServiceId::new("test-service");
        let request_id = "req-001".to_string();

        let token = validator
            .generate_token(service_id.clone(), request_id.clone(), vec!["ai".to_string()])
            .await
            .unwrap();

        let result = validator
            .validate(&token, &service_id, &request_id, "tool")
            .await;

        assert!(!result.is_valid);
    }

    #[tokio::test]
    async fn test_invalidate_token() {
        let validator = SecurityValidator::new();
        let service_id = ServiceId::new("test-service");
        let request_id = "req-001".to_string();

        let token = validator
            .generate_token(service_id.clone(), request_id.clone(), vec!["ai".to_string()])
            .await
            .unwrap();

        validator.invalidate_token(&token).await.unwrap();
        assert!(validator.token_count().await == 0);
    }

    #[tokio::test]
    async fn test_token_usage_limit() {
        let config = SecurityConfig {
            max_token_uses: 2,
            ..Default::default()
        };
        let validator = SecurityValidator::with_config(config);
        let service_id = ServiceId::new("test-service");
        let request_id = "req-001".to_string();

        let token = validator
            .generate_token(service_id.clone(), request_id.clone(), vec!["ai".to_string()])
            .await
            .unwrap();

        // First use
        let result1 = validator.validate(&token, &service_id, &request_id, "ai").await;
        assert!(result1.is_valid);

        // Second use
        let result2 = validator.validate(&token, &service_id, &request_id, "ai").await;
        assert!(result2.is_valid);

        // Third use should fail
        let result3 = validator.validate(&token, &service_id, &request_id, "ai").await;
        assert!(!result3.is_valid);
    }
}