role-system 1.1.1

A flexible and powerful role-based access control (RBAC) library for Rust applications
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
//! Security-focused tests for the role system.
//! These tests ensure the system cannot be compromised through various attack vectors.

use role_system::{
    core::{RoleSystem, RoleSystemConfig},
    permission::Permission,
    resource::Resource,
    role::Role,
    subject::Subject,
};
use std::{
    collections::HashMap,
    sync::{Arc, Mutex},
    thread,
    time::{Duration, Instant},
};

#[test]
fn test_privilege_escalation_prevention() {
    let mut system = RoleSystem::new();

    // Create permissions with different levels
    let read_docs = Permission::new("read", "documents");
    let admin_perm = Permission::new("admin", "system");

    // Create roles
    let user_role = Role::new("user").add_permission(read_docs);
    let admin_role = Role::new("admin").add_permission(admin_perm);

    system.register_role(user_role).unwrap();
    system.register_role(admin_role).unwrap();

    let user = Subject::user("normal_user");
    system.assign_role(&user, "user").unwrap();

    let admin_resource = Resource::new("critical_system", "system");

    // User should NOT have admin permissions
    assert!(
        !system
            .check_permission(&user, "admin", &admin_resource)
            .unwrap()
    );

    // Even if we try to manipulate the system, user should not gain admin access
    let user_roles = system.get_subject_roles(&user).unwrap();
    assert!(!user_roles.contains("admin"));
    assert_eq!(user_roles.len(), 1);
    assert!(user_roles.contains("user"));
}

#[test]
fn test_role_hierarchy_cycle_prevention() {
    let mut system = RoleSystem::new();

    // Create roles
    let role_a = Role::new("role_a");
    let role_b = Role::new("role_b");
    let role_c = Role::new("role_c");

    system.register_role(role_a).unwrap();
    system.register_role(role_b).unwrap();
    system.register_role(role_c).unwrap();

    // Create a chain: role_a -> role_b -> role_c
    system.add_role_inheritance("role_a", "role_b").unwrap();
    system.add_role_inheritance("role_b", "role_c").unwrap();

    // Attempting to create a cycle should fail
    assert!(system.add_role_inheritance("role_c", "role_a").is_err());
    assert!(system.add_role_inheritance("role_c", "role_b").is_err());

    // Direct self-reference should also fail
    assert!(system.add_role_inheritance("role_a", "role_a").is_err());
}

#[test]
fn test_conditional_permission_bypass_prevention() {
    let mut system = RoleSystem::new();

    // Create a permission that should only work during business hours
    let conditional_perm = Permission::with_condition("access", "vault", |context| {
        context.get("time") == Some(&"business_hours".to_string())
            && context.get("authorized") == Some(&"true".to_string())
    });

    let role = Role::new("vault_user").add_permission(conditional_perm);
    system.register_role(role).unwrap();

    let user = Subject::user("employee");
    system.assign_role(&user, "vault_user").unwrap();

    let vault = Resource::new("main_vault", "vault");

    // Test with correct context
    let mut valid_context = HashMap::new();
    valid_context.insert("time".to_string(), "business_hours".to_string());
    valid_context.insert("authorized".to_string(), "true".to_string());
    assert!(
        system
            .check_permission_with_context(&user, "access", &vault, &valid_context)
            .unwrap()
    );

    // Test bypass attempts with malicious contexts
    let malicious_contexts = vec![
        HashMap::new(), // Empty context
        {
            let mut ctx = HashMap::new();
            ctx.insert("time".to_string(), "business_hours".to_string());
            // Missing authorized field
            ctx
        },
        {
            let mut ctx = HashMap::new();
            ctx.insert("time".to_string(), "after_hours".to_string());
            ctx.insert("authorized".to_string(), "true".to_string());
            ctx
        },
        {
            let mut ctx = HashMap::new();
            ctx.insert("time".to_string(), "business_hours".to_string());
            ctx.insert("authorized".to_string(), "false".to_string());
            ctx
        },
        {
            let mut ctx = HashMap::new();
            // Injection attempt
            ctx.insert(
                "time".to_string(),
                "business_hours\"; DROP TABLE permissions; --".to_string(),
            );
            ctx.insert("authorized".to_string(), "true".to_string());
            ctx
        },
    ];

    for malicious_context in malicious_contexts {
        assert!(
            !system
                .check_permission_with_context(&user, "access", &vault, &malicious_context)
                .unwrap(),
            "Permission should be denied for malicious context: {:?}",
            malicious_context
        );
    }
}

#[test]
fn test_input_validation_and_injection_prevention() {
    let mut system = RoleSystem::new();

    // Test malicious inputs for role names
    let long_string = "a".repeat(10000);
    let malicious_inputs = vec![
        "",
        " ",
        "\n",
        "\t",
        "role\x00name",
        "role'; DROP TABLE roles; --",
        "../../../etc/passwd",
        "role<script>alert('xss')</script>",
        "role\u{0000}name",
        &long_string, // Very long string
    ];

    for malicious_input in malicious_inputs {
        // Creating roles with malicious names should either fail or be sanitized
        let result = Role::new(malicious_input);
        if !malicious_input.is_empty() && !malicious_input.trim().is_empty() {
            // If the role is created, it should not cause issues when registered
            let register_result = system.register_role(result);
            // System should handle this gracefully without crashing
            if register_result.is_ok() {
                // If registered successfully, ensure it doesn't break permission checks
                let user = Subject::user("test_user");
                let _ = system.assign_role(&user, malicious_input);
                let resource = Resource::new("test", "test");
                let _ = system.check_permission(&user, "test", &resource);
            }
        }
    }
}

#[test]
fn test_concurrent_access_safety() {
    let system = Arc::new(Mutex::new(RoleSystem::new()));

    // Register initial role
    {
        let mut sys = system.lock().unwrap();
        let role = Role::new("test_role").add_permission(Permission::new("read", "documents"));
        sys.register_role(role).unwrap();
    }

    let handles: Vec<_> = (0..10)
        .map(|i| {
            let system_clone = Arc::clone(&system);
            thread::spawn(move || {
                let user = Subject::user(format!("user_{}", i));
                let resource = Resource::new("doc", "documents");

                // Each thread tries to assign role and check permission
                {
                    let mut sys = system_clone.lock().unwrap();
                    let _ = sys.assign_role(&user, "test_role");
                }

                // Check permission
                {
                    let sys = system_clone.lock().unwrap();
                    let result = sys.check_permission(&user, "read", &resource);
                    assert!(result.is_ok());
                }
            })
        })
        .collect();

    // Wait for all threads to complete
    for handle in handles {
        handle.join().unwrap();
    }

    // Verify system state is consistent
    let sys = system.lock().unwrap();
    for i in 0..10 {
        let user = Subject::user(format!("user_{}", i));
        let roles = sys.get_subject_roles(&user).unwrap();
        assert!(roles.contains("test_role"));
    }
}

#[test]
fn test_cache_integrity() {
    let mut system = RoleSystem::new();

    let permission = Permission::new("read", "documents");
    let role = Role::new("reader").add_permission(permission);
    system.register_role(role).unwrap();

    let user = Subject::user("cache_test_user");
    system.assign_role(&user, "reader").unwrap();

    let resource = Resource::new("doc1", "documents");

    // Prime the cache
    assert!(system.check_permission(&user, "read", &resource).unwrap());

    // Remove the role assignment
    system.remove_role(&user, "reader").unwrap();

    // Cache should be invalidated, permission should be denied
    assert!(!system.check_permission(&user, "read", &resource).unwrap());

    // Re-assign role
    system.assign_role(&user, "reader").unwrap();

    // Permission should be granted again
    assert!(system.check_permission(&user, "read", &resource).unwrap());
}

#[test]
fn test_role_elevation_expiry() {
    // Create a config with caching disabled to ensure fresh permission checks
    let config = RoleSystemConfig {
        max_hierarchy_depth: 10,
        enable_caching: false,
        cache_ttl_seconds: 300,
        enable_audit: true,
    };

    let mut system = RoleSystem::with_config(config);

    let admin_perm = Permission::new("admin", "system");
    let admin_role = Role::new("admin").add_permission(admin_perm);
    system.register_role(admin_role).unwrap();

    let user = Subject::user("temp_admin");
    let resource = Resource::new("system", "system");

    // User should not have admin permission initially
    assert!(!system.check_permission(&user, "admin", &resource).unwrap());

    // Elevate user to admin for a very short duration
    system
        .elevate_role(&user, "admin", Some(Duration::from_millis(10)))
        .unwrap();

    // User should have admin permission immediately after elevation
    assert!(system.check_permission(&user, "admin", &resource).unwrap());

    // Wait for elevation to expire with a longer delay to ensure expiry
    thread::sleep(Duration::from_millis(100));

    // User should no longer have admin permission
    assert!(!system.check_permission(&user, "admin", &resource).unwrap());
}

#[test]
fn test_resource_pattern_security() {
    let mut system = RoleSystem::new();

    // Create permissions for specific patterns
    let user_docs_perm = Permission::new("read", "documents");
    let admin_docs_perm = Permission::new("read", "admin_documents");

    let user_role = Role::new("user").add_permission(user_docs_perm);
    let admin_role = Role::new("admin").add_permission(admin_docs_perm);

    system.register_role(user_role).unwrap();
    system.register_role(admin_role).unwrap();

    let user = Subject::user("normal_user");
    system.assign_role(&user, "user").unwrap();

    // User should access regular documents
    let user_doc = Resource::new("user_file.txt", "documents");
    assert!(system.check_permission(&user, "read", &user_doc).unwrap());

    // User should NOT access admin documents
    let admin_doc = Resource::new("admin_file.txt", "admin_documents");
    assert!(!system.check_permission(&user, "read", &admin_doc).unwrap());

    // Test path traversal attempts - these should fail to create
    let malicious_resource_specs = vec![
        ("../admin_file.txt", "documents"),
        ("../../etc/passwd", "documents"),
        ("admin_file.txt", "documents/../admin_documents"),
    ];

    for (id, resource_type) in malicious_resource_specs {
        let result = std::panic::catch_unwind(|| Resource::new(id, resource_type));
        assert!(
            result.is_err(),
            "Should fail to create malicious resource with ID: '{}'",
            id
        );
    }
}

#[test]
fn test_error_information_leakage() {
    let mut system = RoleSystem::new();

    let user = Subject::user("test_user");
    let resource = Resource::new("test_resource", "test_type");

    // Test permission check on non-existent role
    let result = system.check_permission(&user, "read", &resource);
    assert!(result.is_ok());
    assert!(!result.unwrap()); // Should return false, not error

    // Test assigning non-existent role
    let result = system.assign_role(&user, "non_existent_role");
    assert!(result.is_err());

    // Error message should not leak sensitive information
    if let Err(error) = result {
        let error_msg = error.to_string();
        // Should not contain file paths, memory addresses, or other sensitive data
        assert!(!error_msg.contains("/"));
        assert!(!error_msg.contains("\\"));
        assert!(!error_msg.contains("0x"));
        assert!(!error_msg.contains("password"));
        assert!(!error_msg.contains("secret"));
    }
}

#[test]
fn test_boundary_conditions() {
    let _system = RoleSystem::new();

    // Test with maximum hierarchy depth
    let config = RoleSystemConfig {
        max_hierarchy_depth: 3,
        enable_caching: true,
        cache_ttl_seconds: 300,
        enable_audit: false,
    };
    let mut limited_system = RoleSystem::with_config(config);

    // Create a hierarchy at the limit
    for i in 0..=3 {
        let role = Role::new(format!("role_{}", i));
        limited_system.register_role(role).unwrap();
    }

    // Build hierarchy: role_0 -> role_1 -> role_2 -> role_3
    for i in 0..3 {
        limited_system
            .add_role_inheritance(&format!("role_{}", i), &format!("role_{}", i + 1))
            .unwrap();
    }

    // Adding one more level should fail
    let role_4 = Role::new("role_4");
    limited_system.register_role(role_4).unwrap();
    assert!(
        limited_system
            .add_role_inheritance("role_3", "role_4")
            .is_err()
    );
}

#[test]
fn test_memory_exhaustion_protection() {
    let mut system = RoleSystem::new();

    // Create a large number of roles and permissions
    for i in 0..1000 {
        let permission = Permission::new(format!("action_{}", i), format!("resource_{}", i));
        let role = Role::new(format!("role_{}", i)).add_permission(permission);
        system.register_role(role).unwrap();
    }

    // Create users and assign roles
    for i in 0..100 {
        let user = Subject::user(format!("user_{}", i));
        for j in 0..10 {
            let role_index = (i * 10 + j) % 1000;
            system
                .assign_role(&user, &format!("role_{}", role_index))
                .unwrap();
        }
    }

    // System should still be responsive
    let test_user = Subject::user("user_0");
    let test_resource = Resource::new("resource_0", "resource_0");
    let start = Instant::now();
    let result = system.check_permission(&test_user, "action_0", &test_resource);
    let duration = start.elapsed();

    assert!(result.is_ok());
    assert!(
        duration < Duration::from_millis(100),
        "Permission check took too long: {:?}",
        duration
    );
}

#[test]
fn test_wildcard_permission_security() {
    let mut system = RoleSystem::new();

    // Create a role with wildcard action but specific resource
    let limited_wildcard = Permission::wildcard("documents");
    let role = Role::new("doc_admin").add_permission(limited_wildcard);
    system.register_role(role).unwrap();

    let user = Subject::user("doc_admin_user");
    system.assign_role(&user, "doc_admin").unwrap();

    let doc_resource = Resource::new("test.txt", "documents");
    let system_resource = Resource::new("config", "system");

    // User should have all actions on documents
    assert!(
        system
            .check_permission(&user, "read", &doc_resource)
            .unwrap()
    );
    assert!(
        system
            .check_permission(&user, "write", &doc_resource)
            .unwrap()
    );
    assert!(
        system
            .check_permission(&user, "delete", &doc_resource)
            .unwrap()
    );

    // But NOT on system resources
    assert!(
        !system
            .check_permission(&user, "read", &system_resource)
            .unwrap()
    );
    assert!(
        !system
            .check_permission(&user, "write", &system_resource)
            .unwrap()
    );
    assert!(
        !system
            .check_permission(&user, "delete", &system_resource)
            .unwrap()
    );
}

#[test]
fn test_super_admin_isolation() {
    let mut system = RoleSystem::new();

    // Create super admin and regular user
    let super_admin_role = Role::new("super_admin").add_permission(Permission::super_admin());
    let user_role = Role::new("user").add_permission(Permission::new("read", "documents"));

    system.register_role(super_admin_role).unwrap();
    system.register_role(user_role).unwrap();

    let admin = Subject::user("admin");
    let user = Subject::user("user");

    system.assign_role(&admin, "super_admin").unwrap();
    system.assign_role(&user, "user").unwrap();

    let critical_resource = Resource::new("nuclear_codes", "top_secret");
    let user_resource = Resource::new("readme.txt", "documents");

    // Super admin should have access to everything
    assert!(
        system
            .check_permission(&admin, "read", &critical_resource)
            .unwrap()
    );
    assert!(
        system
            .check_permission(&admin, "launch", &critical_resource)
            .unwrap()
    );
    assert!(
        system
            .check_permission(&admin, "read", &user_resource)
            .unwrap()
    );

    // Regular user should only have limited access
    assert!(
        !system
            .check_permission(&user, "read", &critical_resource)
            .unwrap()
    );
    assert!(
        !system
            .check_permission(&user, "launch", &critical_resource)
            .unwrap()
    );
    assert!(
        system
            .check_permission(&user, "read", &user_resource)
            .unwrap()
    );
    assert!(
        !system
            .check_permission(&user, "write", &user_resource)
            .unwrap()
    );
}

#[cfg(feature = "persistence")]
#[test]
fn test_serialization_integrity() {
    use serde_json;

    let mut system = RoleSystem::new();

    let permission = Permission::new("read", "documents");
    let role = Role::new("reader").add_permission(permission);
    system.register_role(role).unwrap();

    let user = Subject::user("test_user");
    system.assign_role(&user, "reader").unwrap();

    // Serialize a permission
    let original_permission = Permission::new("write", "files");
    let serialized = serde_json::to_string(&original_permission).unwrap();

    // Verify it can be deserialized correctly
    let deserialized: Permission = serde_json::from_str(&serialized).unwrap();
    assert_eq!(deserialized.action(), original_permission.action());
    assert_eq!(
        deserialized.resource_type(),
        original_permission.resource_type()
    );

    // Test that malicious JSON cannot create invalid permissions
    let malicious_json =
        r#"{"action":"admin","resource_type":"system","condition":"malicious_code"}"#;
    let result: Result<Permission, _> = serde_json::from_str(malicious_json);
    // Should either deserialize safely (ignoring malicious fields) or fail
    assert!(result.is_ok() || result.is_err());
}