communitas-core 0.12.0

Core business logic for Communitas - PQC collaboration with virtual disks
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
// SPDX-License-Identifier: MIT OR Apache-2.0

// Copyright (c) 2025 Saorsa Labs Limited
//
// This file is part of the Saorsa P2P network.
//
// Licensed under the AGPL-3.0 license:
// <https://www.gnu.org/licenses/agpl-3.0.html>
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Input validation service for Communitas
//!
//! Centralizes all input validation logic that was previously scattered
//! across UI components. This ensures consistent validation rules and
//! keeps business logic out of the presentation layer.
use std::collections::HashSet;

/// Input type enumeration for validation rules
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InputType {
    /// Entity names (channels, groups, projects, etc.)
    EntityName,
    /// Display names for users/identities
    DisplayName,
    /// Message content
    Message,
    /// Thread reply content
    ThreadReply,
    /// Password input
    Password,
}

/// Validation result type
pub type ValidationResult<T> = Result<T, ValidationError>;

/// Validation error with user-friendly messages
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValidationError {
    pub field: String,
    pub message: String,
    pub code: ValidationErrorCode,
}

impl std::error::Error for ValidationError {}

impl std::fmt::Display for ValidationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}: {}", self.field, self.message)
    }
}

/// Validation error codes for programmatic handling
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValidationErrorCode {
    Required,
    TooShort,
    TooLong,
    InvalidFormat,
    ReservedWord,
    ContainsInvalidChars,
    Empty,
    OnlyWhitespace,
}

/// Main validation service
pub struct ValidationService {
    reserved_words: HashSet<String>,
}

impl Default for ValidationService {
    fn default() -> Self {
        Self::new()
    }
}

impl ValidationService {
    /// Create a new validation service with default reserved words
    pub fn new() -> Self {
        let mut reserved_words = HashSet::new();
        // Add common reserved words that shouldn't be used as entity names
        for word in [
            "admin",
            "system",
            "root",
            "null",
            "undefined",
            "none",
            "default",
            "public",
            "private",
            "internal",
            "external",
            "test",
            "testing",
            "example",
            "sample",
            "demo",
            "temp",
            "temporary",
        ] {
            reserved_words.insert(word.to_string());
        }

        Self { reserved_words }
    }

    /// Validate input based on type
    pub fn validate(&self, input: &str, input_type: InputType) -> ValidationResult<()> {
        match input_type {
            InputType::EntityName => self.validate_entity_name(input),
            InputType::DisplayName => self.validate_display_name(input),
            InputType::Message => self.validate_message(input),
            InputType::ThreadReply => self.validate_thread_reply(input),
            InputType::Password => self.validate_password(input),
        }
    }

    /// Validate entity name (channel, group, project names)
    pub fn validate_entity_name(&self, name: &str) -> ValidationResult<()> {
        // Check for empty or whitespace-only
        if name.trim().is_empty() {
            return Err(ValidationError {
                field: "entity_name".to_string(),
                message: "Entity name cannot be empty".to_string(),
                code: ValidationErrorCode::Required,
            });
        }

        // Check minimum length (after trimming)
        let trimmed = name.trim();
        if trimmed.len() < 2 {
            return Err(ValidationError {
                field: "entity_name".to_string(),
                message: "Entity name must be at least 2 characters long".to_string(),
                code: ValidationErrorCode::TooShort,
            });
        }

        // Check maximum length
        if trimmed.len() > 50 {
            return Err(ValidationError {
                field: "entity_name".to_string(),
                message: "Entity name cannot exceed 50 characters".to_string(),
                code: ValidationErrorCode::TooLong,
            });
        }

        // Check for reserved words (case-insensitive)
        let lower_trimmed = trimmed.to_lowercase();
        if self.reserved_words.contains(&lower_trimmed) {
            return Err(ValidationError {
                field: "entity_name".to_string(),
                message: format!("'{}' is a reserved word and cannot be used", trimmed),
                code: ValidationErrorCode::ReservedWord,
            });
        }

        // Check for invalid characters (only allow alphanumeric, spaces, hyphens, underscores)
        if !trimmed
            .chars()
            .all(|c| c.is_alphanumeric() || c.is_whitespace() || c == '-' || c == '_')
        {
            return Err(ValidationError {
                field: "entity_name".to_string(),
                message: "Entity name can only contain letters, numbers, spaces, hyphens, and underscores".to_string(),
                code: ValidationErrorCode::ContainsInvalidChars,
            });
        }

        // Check for consecutive spaces or special characters
        if trimmed.contains("  ") || trimmed.contains("--") || trimmed.contains("__") {
            return Err(ValidationError {
                field: "entity_name".to_string(),
                message: "Entity name cannot contain consecutive spaces or special characters"
                    .to_string(),
                code: ValidationErrorCode::InvalidFormat,
            });
        }

        Ok(())
    }

    /// Validate display name
    pub fn validate_display_name(&self, name: &str) -> ValidationResult<()> {
        let trimmed = name.trim();

        // Check for empty or whitespace-only
        if trimmed.is_empty() {
            return Err(ValidationError {
                field: "display_name".to_string(),
                message: "Display name cannot be empty".to_string(),
                code: ValidationErrorCode::Required,
            });
        }

        // Check maximum length
        if trimmed.len() > 100 {
            return Err(ValidationError {
                field: "display_name".to_string(),
                message: "Display name cannot exceed 100 characters".to_string(),
                code: ValidationErrorCode::TooLong,
            });
        }

        // Allow more characters for display names (including emojis, special chars)
        // Just check for control characters
        if trimmed.chars().any(|c| c.is_control()) {
            return Err(ValidationError {
                field: "display_name".to_string(),
                message: "Display name cannot contain control characters".to_string(),
                code: ValidationErrorCode::ContainsInvalidChars,
            });
        }

        Ok(())
    }

    /// Validate message content
    pub fn validate_message(&self, message: &str) -> ValidationResult<()> {
        // Allow empty messages for drafts, but not whitespace-only if non-empty
        let trimmed = message.trim();

        if !message.is_empty() && trimmed.is_empty() {
            return Err(ValidationError {
                field: "message".to_string(),
                message: "Message cannot be only whitespace".to_string(),
                code: ValidationErrorCode::OnlyWhitespace,
            });
        }

        // Check maximum length (reasonable limit for messages)
        if message.len() > 10000 {
            return Err(ValidationError {
                field: "message".to_string(),
                message: "Message cannot exceed 10,000 characters".to_string(),
                code: ValidationErrorCode::TooLong,
            });
        }

        // Allow all characters in messages (including newlines, emojis, etc.)
        // Just check for null bytes or other problematic characters
        if message.chars().any(|c| c == '\0') {
            return Err(ValidationError {
                field: "message".to_string(),
                message: "Message cannot contain null characters".to_string(),
                code: ValidationErrorCode::ContainsInvalidChars,
            });
        }

        Ok(())
    }

    /// Validate thread reply content (same rules as messages)
    pub fn validate_thread_reply(&self, reply: &str) -> ValidationResult<()> {
        self.validate_message(reply)
    }

    /// Validate password
    pub fn validate_password(&self, password: &str) -> ValidationResult<()> {
        // Check minimum length
        if password.len() < 8 {
            return Err(ValidationError {
                field: "password".to_string(),
                message: "Password must be at least 8 characters long".to_string(),
                code: ValidationErrorCode::TooShort,
            });
        }

        // Check maximum length (reasonable limit for security)
        if password.len() > 128 {
            return Err(ValidationError {
                field: "password".to_string(),
                message: "Password cannot exceed 128 characters".to_string(),
                code: ValidationErrorCode::TooLong,
            });
        }

        // Check for control characters
        if password.chars().any(|c| c.is_control()) {
            return Err(ValidationError {
                field: "password".to_string(),
                message: "Password cannot contain control characters".to_string(),
                code: ValidationErrorCode::ContainsInvalidChars,
            });
        }

        Ok(())
    }

    /// Sanitize input by trimming whitespace
    pub fn sanitize(&self, input: &str, input_type: InputType) -> String {
        match input_type {
            InputType::EntityName | InputType::DisplayName => input.trim().to_string(),
            InputType::Message | InputType::ThreadReply | InputType::Password => input.to_string(),
        }
    }

    /// Validate and sanitize input in one step
    pub fn validate_and_sanitize(
        &self,
        input: &str,
        input_type: InputType,
    ) -> ValidationResult<String> {
        let sanitized = self.sanitize(input, input_type);
        self.validate(&sanitized, input_type)?;
        Ok(sanitized)
    }
}

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

    fn create_validator() -> ValidationService {
        ValidationService::new()
    }

    mod entity_name_validation {
        use super::*;

        #[test]
        fn test_valid_entity_names() {
            let validator = create_validator();

            // Valid names should pass
            assert!(validator.validate_entity_name("My Channel").is_ok());
            assert!(validator.validate_entity_name("project-alpha").is_ok());
            assert!(validator.validate_entity_name("Group_123").is_ok());
            assert!(validator.validate_entity_name("AB").is_ok()); // Minimum 2 chars after trim
            assert!(
                validator
                    .validate_entity_name("Valid Name With Spaces")
                    .is_ok()
            );
        }

        #[test]
        fn test_empty_entity_names() {
            let validator = create_validator();

            let result = validator.validate_entity_name("");
            assert!(result.is_err());
            assert_eq!(result.unwrap_err().code, ValidationErrorCode::Required);

            let result = validator.validate_entity_name("   ");
            assert!(result.is_err());
            assert_eq!(result.unwrap_err().code, ValidationErrorCode::Required);
        }

        #[test]
        fn test_too_short_entity_names() {
            let validator = create_validator();

            let result = validator.validate_entity_name("A");
            assert!(result.is_err());
            assert_eq!(result.unwrap_err().code, ValidationErrorCode::TooShort);
        }

        #[test]
        fn test_too_long_entity_names() {
            let validator = create_validator();

            let long_name = "A".repeat(51);
            let result = validator.validate_entity_name(&long_name);
            assert!(result.is_err());
            assert_eq!(result.unwrap_err().code, ValidationErrorCode::TooLong);
        }

        #[test]
        fn test_reserved_words() {
            let validator = create_validator();

            let result = validator.validate_entity_name("admin");
            assert!(result.is_err());
            assert_eq!(result.unwrap_err().code, ValidationErrorCode::ReservedWord);

            let result = validator.validate_entity_name("ADMIN"); // case insensitive
            assert!(result.is_err());
            assert_eq!(result.unwrap_err().code, ValidationErrorCode::ReservedWord);
        }

        #[test]
        fn test_invalid_characters() {
            let validator = create_validator();

            let result = validator.validate_entity_name("Channel@Name");
            assert!(result.is_err());
            assert_eq!(
                result.unwrap_err().code,
                ValidationErrorCode::ContainsInvalidChars
            );

            let result = validator.validate_entity_name("Channel.Name");
            assert!(result.is_err());
            assert_eq!(
                result.unwrap_err().code,
                ValidationErrorCode::ContainsInvalidChars
            );
        }

        #[test]
        fn test_consecutive_special_chars() {
            let validator = create_validator();

            let result = validator.validate_entity_name("Channel--Name");
            assert!(result.is_err());
            assert_eq!(result.unwrap_err().code, ValidationErrorCode::InvalidFormat);

            let result = validator.validate_entity_name("Channel  Name");
            assert!(result.is_err());
            assert_eq!(result.unwrap_err().code, ValidationErrorCode::InvalidFormat);
        }
    }

    mod display_name_validation {
        use super::*;

        #[test]
        fn test_valid_display_names() {
            let validator = create_validator();

            assert!(validator.validate_display_name("John Doe").is_ok());
            assert!(validator.validate_display_name("Alice").is_ok());
            assert!(validator.validate_display_name("用户123").is_ok()); // Unicode
            assert!(
                validator
                    .validate_display_name("Name with émojis 🎉")
                    .is_ok()
            );
        }

        #[test]
        fn test_empty_display_names() {
            let validator = create_validator();

            let result = validator.validate_display_name("");
            assert!(result.is_err());
            assert_eq!(result.unwrap_err().code, ValidationErrorCode::Required);
        }

        #[test]
        fn test_too_long_display_names() {
            let validator = create_validator();

            let long_name = "A".repeat(101);
            let result = validator.validate_display_name(&long_name);
            assert!(result.is_err());
            assert_eq!(result.unwrap_err().code, ValidationErrorCode::TooLong);
        }

        #[test]
        fn test_control_characters_in_display_names() {
            let validator = create_validator();

            let result = validator.validate_display_name("Name\nwith\nnewlines");
            assert!(result.is_err());
            assert_eq!(
                result.unwrap_err().code,
                ValidationErrorCode::ContainsInvalidChars
            );
        }
    }

    mod message_validation {
        use super::*;

        #[test]
        fn test_valid_messages() {
            let validator = create_validator();

            assert!(validator.validate_message("").is_ok()); // Empty messages allowed
            assert!(validator.validate_message("Hello world!").is_ok());
            assert!(validator.validate_message("Multi\nline\nmessage").is_ok());
            assert!(validator.validate_message("Message with émojis 🎉").is_ok());
        }

        #[test]
        fn test_whitespace_only_messages() {
            let validator = create_validator();

            let result = validator.validate_message("   ");
            assert!(result.is_err());
            assert_eq!(
                result.unwrap_err().code,
                ValidationErrorCode::OnlyWhitespace
            );

            let result = validator.validate_message("\t\n  \t");
            assert!(result.is_err());
            assert_eq!(
                result.unwrap_err().code,
                ValidationErrorCode::OnlyWhitespace
            );
        }

        #[test]
        fn test_too_long_messages() {
            let validator = create_validator();

            let long_message = "A".repeat(10001);
            let result = validator.validate_message(&long_message);
            assert!(result.is_err());
            assert_eq!(result.unwrap_err().code, ValidationErrorCode::TooLong);
        }

        #[test]
        fn test_null_characters_in_messages() {
            let validator = create_validator();

            let result = validator.validate_message("Message with \0 null");
            assert!(result.is_err());
            assert_eq!(
                result.unwrap_err().code,
                ValidationErrorCode::ContainsInvalidChars
            );
        }
    }

    mod password_validation {
        use super::*;

        #[test]
        fn test_valid_passwords() {
            let validator = create_validator();

            assert!(validator.validate_password("password123").is_ok());
            assert!(validator.validate_password("A".repeat(8).as_str()).is_ok());
            assert!(validator.validate_password("Complex!@#$%^&*()").is_ok());
        }

        #[test]
        fn test_too_short_passwords() {
            let validator = create_validator();

            let result = validator.validate_password("short");
            assert!(result.is_err());
            assert_eq!(result.unwrap_err().code, ValidationErrorCode::TooShort);
        }

        #[test]
        fn test_too_long_passwords() {
            let validator = create_validator();

            let long_password = "A".repeat(129);
            let result = validator.validate_password(&long_password);
            assert!(result.is_err());
            assert_eq!(result.unwrap_err().code, ValidationErrorCode::TooLong);
        }

        #[test]
        fn test_control_characters_in_passwords() {
            let validator = create_validator();

            let result = validator.validate_password("password\n123");
            assert!(result.is_err());
            assert_eq!(
                result.unwrap_err().code,
                ValidationErrorCode::ContainsInvalidChars
            );
        }
    }

    mod sanitization {
        use super::*;

        #[test]
        fn test_entity_name_sanitization() {
            let validator = create_validator();

            assert_eq!(
                validator.sanitize("  My Channel  ", InputType::EntityName),
                "My Channel"
            );
            assert_eq!(
                validator.sanitize("project", InputType::EntityName),
                "project"
            );
        }

        #[test]
        fn test_message_sanitization() {
            let validator = create_validator();

            // Messages preserve whitespace
            assert_eq!(
                validator.sanitize("  Hello  ", InputType::Message),
                "  Hello  "
            );
            assert_eq!(
                validator.sanitize("Multi\nline", InputType::Message),
                "Multi\nline"
            );
        }

        #[test]
        fn test_validate_and_sanitize() {
            let validator = create_validator();

            let result = validator.validate_and_sanitize("  My Channel  ", InputType::EntityName);
            assert!(result.is_ok());
            assert_eq!(result.unwrap(), "My Channel");

            let result = validator.validate_and_sanitize("", InputType::EntityName);
            assert!(result.is_err());
        }
    }
}