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
//! Fuzzing tests for the role system to discover edge cases and security vulnerabilities.

#[cfg(test)]
mod fuzz_tests {
    use crate::{
        core::RoleSystem, permission::Permission, resource::Resource, role::Role,
        storage::MemoryStorage, subject::Subject,
    };

    // Use arbitrary for property-based testing that can be used for fuzzing
    use proptest::prelude::*;

    /// Fuzz input for role system operations.
    #[derive(Debug, Clone)]
    struct FuzzInput {
        operations: Vec<FuzzOperation>,
    }

    #[derive(Debug, Clone)]
    enum FuzzOperation {
        RegisterRole {
            name: String,
            permissions: Vec<(String, String)>,
        },
        AssignRole {
            subject_id: String,
            role_name: String,
        },
        CheckPermission {
            subject_id: String,
            action: String,
            resource_id: String,
            resource_type: String,
        },
        RemoveRole {
            subject_id: String,
            role_name: String,
        },
        AddRoleInheritance {
            child: String,
            parent: String,
        },
    }

    impl Arbitrary for FuzzOperation {
        type Parameters = ();
        type Strategy = BoxedStrategy<Self>;

        fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
            prop_oneof![
                (
                    any::<String>(),
                    prop::collection::vec((any::<String>(), any::<String>()), 0..5)
                )
                    .prop_map(|(name, permissions)| FuzzOperation::RegisterRole {
                        name,
                        permissions
                    }),
                (any::<String>(), any::<String>()).prop_map(|(subject_id, role_name)| {
                    FuzzOperation::AssignRole {
                        subject_id,
                        role_name,
                    }
                }),
                (
                    any::<String>(),
                    any::<String>(),
                    any::<String>(),
                    any::<String>()
                )
                    .prop_map(
                        |(subject_id, action, resource_id, resource_type)| {
                            FuzzOperation::CheckPermission {
                                subject_id,
                                action,
                                resource_id,
                                resource_type,
                            }
                        }
                    ),
                (any::<String>(), any::<String>()).prop_map(|(subject_id, role_name)| {
                    FuzzOperation::RemoveRole {
                        subject_id,
                        role_name,
                    }
                }),
                (any::<String>(), any::<String>()).prop_map(|(child, parent)| {
                    FuzzOperation::AddRoleInheritance { child, parent }
                }),
            ]
            .boxed()
        }
    }

    impl Arbitrary for FuzzInput {
        type Parameters = ();
        type Strategy = BoxedStrategy<Self>;

        fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy {
            prop::collection::vec(any::<FuzzOperation>(), 1..50)
                .prop_map(|operations| FuzzInput { operations })
                .boxed()
        }
    }

    /// Sanitize string input to prevent injection attacks.
    fn sanitize_string(input: &str) -> String {
        input
            .chars()
            .filter(|c| c.is_alphanumeric() || *c == '_' || *c == '-')
            .take(100) // Limit length
            .collect()
    }

    /// Execute a fuzz operation safely.
    fn execute_fuzz_operation(
        system: &mut RoleSystem<MemoryStorage>,
        operation: FuzzOperation,
    ) -> bool {
        match operation {
            FuzzOperation::RegisterRole { name, permissions } => {
                let sanitized_name = sanitize_string(&name);
                if sanitized_name.is_empty() {
                    return true; // Skip invalid names
                }

                let mut role = Role::new(sanitized_name);
                for (action, resource_type) in permissions {
                    let sanitized_action = sanitize_string(&action);
                    let sanitized_resource = sanitize_string(&resource_type);

                    if !sanitized_action.is_empty() && !sanitized_resource.is_empty() {
                        let permission = Permission::new(sanitized_action, sanitized_resource);
                        role = role.add_permission(permission);
                    }
                }

                let _ = system.register_role(role);
                true
            }
            FuzzOperation::AssignRole {
                subject_id,
                role_name,
            } => {
                let sanitized_subject = sanitize_string(&subject_id);
                let sanitized_role = sanitize_string(&role_name);

                if sanitized_subject.is_empty() || sanitized_role.is_empty() {
                    return true;
                }

                let subject = Subject::user(sanitized_subject);
                let _ = system.assign_role(&subject, &sanitized_role);
                true
            }
            FuzzOperation::CheckPermission {
                subject_id,
                action,
                resource_id,
                resource_type,
            } => {
                let sanitized_subject = sanitize_string(&subject_id);
                let sanitized_action = sanitize_string(&action);
                let sanitized_resource_id = sanitize_string(&resource_id);
                let sanitized_resource_type = sanitize_string(&resource_type);

                if sanitized_subject.is_empty()
                    || sanitized_action.is_empty()
                    || sanitized_resource_id.is_empty()
                    || sanitized_resource_type.is_empty()
                {
                    return true;
                }

                let subject = Subject::user(sanitized_subject);
                let resource = Resource::new(sanitized_resource_id, sanitized_resource_type);
                let _ = system.check_permission(&subject, &sanitized_action, &resource);
                true
            }
            FuzzOperation::RemoveRole {
                subject_id,
                role_name,
            } => {
                let sanitized_subject = sanitize_string(&subject_id);
                let sanitized_role = sanitize_string(&role_name);

                if sanitized_subject.is_empty() || sanitized_role.is_empty() {
                    return true;
                }

                let subject = Subject::user(sanitized_subject);
                let _ = system.remove_role(&subject, &sanitized_role);
                true
            }
            FuzzOperation::AddRoleInheritance { child, parent } => {
                let sanitized_child = sanitize_string(&child);
                let sanitized_parent = sanitize_string(&parent);

                if sanitized_child.is_empty()
                    || sanitized_parent.is_empty()
                    || sanitized_child == sanitized_parent
                {
                    return true;
                }

                let _ = system.add_role_inheritance(&sanitized_child, &sanitized_parent);
                true
            }
        }
    }

    proptest! {
        #[test]
        fn fuzz_role_system_never_panics(input in any::<FuzzInput>()) {
            let mut system = RoleSystem::<MemoryStorage>::new();

            // Execute all operations - should never panic
            for operation in input.operations {
                prop_assert!(execute_fuzz_operation(&mut system, operation));
            }
        }

        #[test]
        fn fuzz_permission_strings_never_panic(
            action in ".*",
            resource_type in ".*"
        ) {
            // Test that arbitrary strings don't cause panics when creating permissions
            let result = std::panic::catch_unwind(|| {
                // Only create permission if strings are not empty after basic validation
                if !action.trim().is_empty() && !resource_type.trim().is_empty() {
                    // Try to create permission - validation errors are expected and should be handled gracefully
                    if Permission::try_new(&action, &resource_type).is_err() {
                        // Validation error is expected for invalid input - not a panic
                    }
                }
            });

            // Should either succeed or fail gracefully, never panic
            prop_assert!(result.is_ok());
        }

        #[test]
        fn fuzz_role_hierarchy_stability(
            role_pairs in prop::collection::vec((any::<String>(), any::<String>()), 1..20)
        ) {
            let mut system = RoleSystem::<MemoryStorage>::new();

            // Register roles and create hierarchy
            let mut registered_roles = std::collections::HashSet::new();

            for (child, parent) in role_pairs {
                let sanitized_child = sanitize_string(&child);
                let sanitized_parent = sanitize_string(&parent);

                if sanitized_child.is_empty() || sanitized_parent.is_empty() ||
                   sanitized_child == sanitized_parent {
                    continue;
                }

                // Register roles if not already registered
                if registered_roles.insert(sanitized_child.clone()) {
                    let role = Role::new(sanitized_child.clone());
                    let _ = system.register_role(role);
                }

                if registered_roles.insert(sanitized_parent.clone()) {
                    let role = Role::new(sanitized_parent.clone());
                    let _ = system.register_role(role);
                }

                // Try to add inheritance - should handle cycles gracefully
                let _ = system.add_role_inheritance(&sanitized_child, &sanitized_parent);
            }

            // System should remain stable
            prop_assert!(true);
        }

        #[test]
        fn fuzz_concurrent_like_operations(
            operations in prop::collection::vec(any::<FuzzOperation>(), 1..100)
        ) {
            let mut system = RoleSystem::<MemoryStorage>::new();

            // Execute many operations in sequence (simulating concurrent-like load)
            for operation in operations {
                let _ = execute_fuzz_operation(&mut system, operation);
            }

            // System should remain consistent
            prop_assert!(true);
        }

        #[test]
        fn fuzz_memory_exhaustion_protection(
            large_string in prop::string::string_regex("[a-zA-Z0-9]{1000,2000}").unwrap()
        ) {
            let result = std::panic::catch_unwind(|| {
                let _permission = Permission::new(&large_string[..100], &large_string[100..200]);
            });

            // Should handle large strings gracefully
            prop_assert!(result.is_ok());
        }
    }

    #[test]
    fn fuzz_invalid_utf8_handling() {
        // Test with invalid UTF-8 sequences (converted to valid strings)
        let invalid_bytes = vec![0xFF, 0xFE, 0xFD];
        let string_from_bytes = String::from_utf8_lossy(&invalid_bytes);

        // Should not panic with converted string
        let result = std::panic::catch_unwind(|| {
            let sanitized = sanitize_string(&string_from_bytes);
            if !sanitized.is_empty() {
                let _permission = Permission::new(sanitized.clone(), sanitized);
            }
        });

        assert!(result.is_ok());
    }

    #[test]
    fn fuzz_extreme_role_hierarchy_depth() {
        let mut system = RoleSystem::<MemoryStorage>::new();

        // Create a deep hierarchy
        let mut previous_role = "root".to_string();
        let root_role = Role::new(previous_role.clone());
        system.register_role(root_role).unwrap();

        for i in 1..=20 {
            let current_role = format!("level_{}", i);
            let role = Role::new(current_role.clone());
            system.register_role(role).unwrap();

            // This should eventually hit max depth limit
            let _ = system.add_role_inheritance(&current_role, &previous_role);
            previous_role = current_role;
        }

        // System should handle deep hierarchies gracefully
    }

    #[test]
    fn fuzz_special_characters_in_permissions() {
        let special_chars = vec![
            "role\0with\0nulls",
            "role\nwith\nnewlines",
            "role\twith\ttabs",
            "role\"with\"quotes",
            "role'with'apostrophes",
            "role;with;semicolons",
            "role(with)parens",
            "role[with]brackets",
            "role{with}braces",
            "role<with>angles",
            "role|with|pipes",
            "role\\with\\backslashes",
            "role/with/slashes",
            "role?with?questions",
            "role*with*wildcards",
            "role%with%percents",
            "role&with&ampersands",
            "role=with=equals",
            "role+with+plus",
            "role-with-dashes",
            "role_with_underscores",
            "role.with.dots",
            "role,with,commas",
            "role:with:colons",
            "role#with#hashes",
            "role@with@ats",
            "role!with!exclamations",
            "role$with$dollars",
            "role^with^carets",
            "role~with~tildes",
            "role`with`backticks",
        ];

        for special_string in special_chars {
            let result = std::panic::catch_unwind(|| {
                let sanitized = sanitize_string(special_string);
                if !sanitized.is_empty() {
                    let _permission = Permission::new(sanitized.clone(), sanitized);
                }
            });

            assert!(result.is_ok(), "Failed with string: {}", special_string);
        }
    }
}

/// Load testing utilities for performance validation.
#[cfg(test)]
pub mod load_tests {
    use crate::{
        core::RoleSystem, permission::Permission, resource::Resource, role::Role,
        storage::MemoryStorage, subject::Subject,
    };
    use std::time::Instant;

    /// Run a basic load test with many permission checks.
    pub fn load_test_permission_checks(
        num_subjects: usize,
        num_roles: usize,
        num_permissions_per_role: usize,
        num_checks: usize,
    ) -> (std::time::Duration, f64) {
        let mut system = RoleSystem::<MemoryStorage>::new();

        // Setup: Create roles with permissions
        for role_idx in 0..num_roles {
            let mut role = Role::new(format!("role_{}", role_idx));

            for perm_idx in 0..num_permissions_per_role {
                let permission = Permission::new(
                    format!("action_{}", perm_idx),
                    format!("resource_type_{}", perm_idx % 10), // Group resources
                );
                role = role.add_permission(permission);
            }

            system.register_role(role).unwrap();
        }

        // Setup: Create subjects and assign roles
        let subjects: Vec<Subject> = (0..num_subjects)
            .map(|i| Subject::user(format!("user_{}", i)))
            .collect();

        for (i, subject) in subjects.iter().enumerate() {
            let role_name = format!("role_{}", i % num_roles);
            system.assign_role(subject, &role_name).unwrap();
        }

        // Load test: Perform many permission checks
        let start = Instant::now();
        let mut granted_count = 0;

        for i in 0..num_checks {
            let subject = &subjects[i % num_subjects];
            let action = format!("action_{}", i % num_permissions_per_role);
            let resource = Resource::new(
                format!("resource_{}", i),
                format!("resource_type_{}", i % 10),
            );

            if system
                .check_permission(subject, &action, &resource)
                .unwrap_or(false)
            {
                granted_count += 1;
            }
        }

        let duration = start.elapsed();
        let success_rate = granted_count as f64 / num_checks as f64;

        (duration, success_rate)
    }

    #[test]
    fn test_load_performance() {
        let (duration, success_rate) = load_test_permission_checks(
            100,   // subjects
            50,    // roles
            20,    // permissions per role
            10000, // permission checks
        );

        println!("Load test results:");
        println!("  Duration: {:?}", duration);
        println!("  Success rate: {:.2}%", success_rate * 100.0);
        println!(
            "  Checks per second: {:.0}",
            10000.0 / duration.as_secs_f64()
        );

        // Performance assertions
        assert!(
            duration.as_millis() < 5000,
            "Load test took too long: {:?}",
            duration
        );
        assert!(success_rate > 0.0, "No permissions were granted");
    }

    #[test]
    fn test_memory_usage_scaling() {
        use std::alloc::{GlobalAlloc, Layout, System};
        use std::sync::atomic::{AtomicUsize, Ordering};

        // Simple memory tracking (approximate)
        static ALLOCATED: AtomicUsize = AtomicUsize::new(0);

        #[allow(dead_code)]
        struct TrackingAllocator;

        unsafe impl GlobalAlloc for TrackingAllocator {
            unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
                let ptr = unsafe { System.alloc(layout) };
                if !ptr.is_null() {
                    ALLOCATED.fetch_add(layout.size(), Ordering::Relaxed);
                }
                ptr
            }

            unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
                unsafe { System.dealloc(ptr, layout) };
                ALLOCATED.fetch_sub(layout.size(), Ordering::Relaxed);
            }
        }

        // Test memory usage with increasing scale
        let initial_memory = ALLOCATED.load(Ordering::Relaxed);

        let system = RoleSystem::<MemoryStorage>::new();
        let memory_after_creation = ALLOCATED.load(Ordering::Relaxed);

        println!("Memory usage:");
        println!("  Initial: {} bytes", initial_memory);
        println!("  After creation: {} bytes", memory_after_creation);
        println!(
            "  System overhead: {} bytes",
            memory_after_creation - initial_memory
        );

        // Basic memory usage should be reasonable
        let overhead = memory_after_creation - initial_memory;
        assert!(
            overhead < 1024 * 1024,
            "System uses too much memory: {} bytes",
            overhead
        );

        drop(system);
    }
}