oxihuman-core 0.1.2

Core data structures, algorithms, and asset management for OxiHuman
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0
#![allow(dead_code)]

//! Security validation utilities for OxiHuman.
//!
//! Provides path sanitization, file size validation, safe stride/offset
//! arithmetic, and content-type detection for 3D asset files.
//!
//! # Usage
//!
//! ```rust
//! use oxihuman_core::security::{sanitize_path, validate_file_size, SecurityError};
//!
//! // Reject path traversal attempts
//! assert!(sanitize_path("../etc/passwd").is_err());
//! assert!(sanitize_path("models/human.glb").is_ok());
//!
//! // Reject oversized uploads
//! assert!(validate_file_size(200 * 1024 * 1024, 100).is_err());
//! ```

use std::fmt;
use std::path::PathBuf;

// ---------------------------------------------------------------------------
// SecurityError
// ---------------------------------------------------------------------------

/// Errors produced by OxiHuman security validation functions.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SecurityError {
    /// Path contains a `..` component that could escape the intended root.
    PathTraversal,
    /// Path is absolute (starts with `/`, `\`, or a Windows drive letter like `C:\`).
    AbsolutePath,
    /// Path contains a null byte (`\0`).
    NullByte,
    /// Path exceeds the maximum allowed length (512 characters).
    TooLong,
    /// Path component matches a Windows reserved device name (e.g. `CON`, `NUL`).
    ReservedName(String),
    /// Path bytes are not valid UTF-8.
    InvalidUtf8,
    /// File size exceeds the configured maximum.
    FileTooLarge {
        /// Actual file size in bytes.
        size_bytes: usize,
        /// Maximum allowed size in bytes.
        max_bytes: usize,
    },
    /// An arithmetic overflow occurred during stride/offset computation.
    OverflowError,
    /// A computed index is out of bounds for the given buffer.
    OutOfBounds {
        /// The computed byte index.
        index: usize,
        /// The total buffer size in bytes.
        total: usize,
    },
}

impl fmt::Display for SecurityError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SecurityError::PathTraversal => {
                write!(
                    f,
                    "security: path traversal detected (contains '..' component)"
                )
            }
            SecurityError::AbsolutePath => {
                write!(f, "security: absolute paths are not allowed")
            }
            SecurityError::NullByte => {
                write!(f, "security: path contains a null byte")
            }
            SecurityError::TooLong => {
                write!(
                    f,
                    "security: path exceeds maximum allowed length of 512 characters"
                )
            }
            SecurityError::ReservedName(name) => {
                write!(
                    f,
                    "security: '{}' is a reserved OS name and cannot be used as a path component",
                    name
                )
            }
            SecurityError::InvalidUtf8 => {
                write!(f, "security: path contains non-UTF-8 bytes")
            }
            SecurityError::FileTooLarge {
                size_bytes,
                max_bytes,
            } => {
                write!(
                    f,
                    "security: file size {} bytes exceeds maximum of {} bytes",
                    size_bytes, max_bytes
                )
            }
            SecurityError::OverflowError => {
                write!(
                    f,
                    "security: arithmetic overflow in stride/offset calculation"
                )
            }
            SecurityError::OutOfBounds { index, total } => {
                write!(
                    f,
                    "security: computed index {} is out of bounds for buffer of size {}",
                    index, total
                )
            }
        }
    }
}

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

// ---------------------------------------------------------------------------
// Windows reserved device names
// ---------------------------------------------------------------------------

/// Returns `true` if `name` (without extension, case-insensitive) is a
/// Windows-reserved device name that must not appear as a path component.
fn is_reserved_name(component: &str) -> bool {
    // Strip any extension to get the bare stem.
    let stem = match component.rfind('.') {
        Some(dot) if dot > 0 => &component[..dot],
        _ => component,
    };

    let upper = stem.to_ascii_uppercase();
    matches!(
        upper.as_str(),
        "CON"
            | "PRN"
            | "AUX"
            | "NUL"
            | "COM0"
            | "COM1"
            | "COM2"
            | "COM3"
            | "COM4"
            | "COM5"
            | "COM6"
            | "COM7"
            | "COM8"
            | "COM9"
            | "LPT0"
            | "LPT1"
            | "LPT2"
            | "LPT3"
            | "LPT4"
            | "LPT5"
            | "LPT6"
            | "LPT7"
            | "LPT8"
            | "LPT9"
    )
}

// ---------------------------------------------------------------------------
// sanitize_path
// ---------------------------------------------------------------------------

/// Validates a path string supplied from untrusted input and returns a safe
/// [`PathBuf`] if all checks pass.
///
/// # Checks performed (in order)
///
/// 1. Null bytes → [`SecurityError::NullByte`]
/// 2. Length > 512 → [`SecurityError::TooLong`]
/// 3. Starts with `/`, `\`, or `X:` (Windows drive) → [`SecurityError::AbsolutePath`]
/// 4. Any path component equals `..` → [`SecurityError::PathTraversal`]
/// 5. Any path component (stem, case-insensitive) is a reserved OS name →
///    [`SecurityError::ReservedName`]
///
/// # Example
///
/// ```rust
/// use oxihuman_core::security::sanitize_path;
///
/// assert!(sanitize_path("models/body.glb").is_ok());
/// assert!(sanitize_path("../secret").is_err());
/// assert!(sanitize_path("/etc/passwd").is_err());
/// ```
pub fn sanitize_path(input: &str) -> Result<PathBuf, SecurityError> {
    // Check 1: null bytes
    if input.contains('\0') {
        return Err(SecurityError::NullByte);
    }

    // Check 2: length
    if input.len() > 512 {
        return Err(SecurityError::TooLong);
    }

    // Check 3: absolute paths
    // Unix-style absolute
    if input.starts_with('/') {
        return Err(SecurityError::AbsolutePath);
    }
    // Windows UNC or absolute backslash
    if input.starts_with('\\') {
        return Err(SecurityError::AbsolutePath);
    }
    // Windows drive letter (e.g. "C:" or "C:\")
    {
        let mut chars = input.chars();
        let first = chars.next();
        let second = chars.next();
        if let (Some(c), Some(':')) = (first, second) {
            if c.is_ascii_alphabetic() {
                return Err(SecurityError::AbsolutePath);
            }
        }
    }

    // Check 4 & 5: iterate components split on '/' and '\'
    for component in input.split(['/', '\\']) {
        // Skip empty segments (e.g. trailing slash or double slash)
        if component.is_empty() || component == "." {
            continue;
        }

        // Path traversal
        if component == ".." {
            return Err(SecurityError::PathTraversal);
        }

        // Reserved OS names
        if is_reserved_name(component) {
            return Err(SecurityError::ReservedName(component.to_string()));
        }
    }

    Ok(PathBuf::from(input))
}

// ---------------------------------------------------------------------------
// validate_file_size
// ---------------------------------------------------------------------------

/// Validates that `bytes` does not exceed `max_mb` megabytes.
///
/// # Example
///
/// ```rust
/// use oxihuman_core::security::validate_file_size;
///
/// assert!(validate_file_size(1024 * 1024, 10).is_ok());   // 1 MB, limit 10 MB
/// assert!(validate_file_size(200 * 1024 * 1024, 100).is_err()); // 200 MB > 100 MB
/// ```
pub fn validate_file_size(bytes: usize, max_mb: u32) -> Result<(), SecurityError> {
    let max_bytes = (max_mb as usize).saturating_mul(1024).saturating_mul(1024);
    if bytes > max_bytes {
        Err(SecurityError::FileTooLarge {
            size_bytes: bytes,
            max_bytes,
        })
    } else {
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// checked_stride_offset
// ---------------------------------------------------------------------------

/// Computes `index * stride` with overflow detection and bounds checking.
///
/// Returns the byte offset if it is strictly less than `total`, otherwise
/// returns an error.
///
/// # Example
///
/// ```rust
/// use oxihuman_core::security::checked_stride_offset;
///
/// assert_eq!(checked_stride_offset(3, 4, 20).unwrap(), 12);
/// assert!(checked_stride_offset(usize::MAX, 2, 100).is_err()); // overflow
/// assert!(checked_stride_offset(5, 4, 16).is_err());           // out of bounds
/// ```
pub fn checked_stride_offset(
    index: usize,
    stride: usize,
    total: usize,
) -> Result<usize, SecurityError> {
    let offset = index
        .checked_mul(stride)
        .ok_or(SecurityError::OverflowError)?;
    if offset >= total {
        return Err(SecurityError::OutOfBounds {
            index: offset,
            total,
        });
    }
    Ok(offset)
}

// ---------------------------------------------------------------------------
// is_safe_content_type
// ---------------------------------------------------------------------------

/// Validates the magic bytes of a 3D asset file against known-good signatures.
///
/// Supported formats:
/// - **GLB** (binary glTF): magic `glTF` at offset 0
/// - **OBJ** (Wavefront): starts with `v `, `# `, `mtllib`, `usemtl`, `o `, or `g `
/// - **STL binary**: at least 84 bytes (80-byte header + 4-byte count)
/// - **STL ASCII**: starts with `solid` (case-insensitive)
/// - **PLY**: starts with `ply\n` or `ply\r`
/// - **FBX binary**: starts with `Kaydara FBX Binary  \x00`
///
/// Returns `false` for empty slices and unrecognized formats.
///
/// # Example
///
/// ```rust
/// use oxihuman_core::security::is_safe_content_type;
///
/// assert!(is_safe_content_type(b"glTF\x02\x00\x00\x00"));
/// assert!(is_safe_content_type(b"ply\nformat ascii 1.0\n"));
/// assert!(!is_safe_content_type(b"\x00\x01\x02\x03"));
/// ```
pub fn is_safe_content_type(bytes: &[u8]) -> bool {
    if bytes.is_empty() {
        return false;
    }

    // GLB: binary glTF magic "glTF"
    if bytes.starts_with(b"glTF") {
        return true;
    }

    // PLY
    if bytes.starts_with(b"ply\n") || bytes.starts_with(b"ply\r") {
        return true;
    }

    // FBX binary
    if bytes.starts_with(b"Kaydara FBX Binary  \x00") {
        return true;
    }

    // STL ASCII: starts with "solid" (case-insensitive, first 5 bytes)
    if bytes.len() >= 5 {
        let prefix: Vec<u8> = bytes[..5].iter().map(|b| b.to_ascii_lowercase()).collect();
        if prefix == b"solid" {
            return true;
        }
    }

    // OBJ: check first 16 bytes for known OBJ line starters
    {
        let probe = if bytes.len() >= 16 {
            &bytes[..16]
        } else {
            bytes
        };
        let obj_prefixes: &[&[u8]] = &[b"v ", b"# ", b"mtllib", b"usemtl", b"o ", b"g "];
        for prefix in obj_prefixes {
            if probe.starts_with(prefix) {
                return true;
            }
        }
    }

    // STL binary: minimum 84 bytes (80-byte header + 4-byte triangle count)
    // Only matched when the above ASCII checks have already failed.
    // A binary STL cannot start with "solid" (handled above), so if we reach
    // here with ≥ 84 bytes and no other match, treat as potentially valid
    // binary STL only if the header does NOT start with "solid".
    if bytes.len() >= 84 {
        // Already excluded ASCII STL above. Accept as binary STL candidate.
        return true;
    }

    false
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // --- sanitize_path ---

    #[test]
    fn sanitize_path_valid_relative() {
        let result = sanitize_path("models/human.glb").expect("should succeed");
        assert_eq!(result, PathBuf::from("models/human.glb"));
    }

    #[test]
    fn sanitize_path_valid_nested() {
        let result = sanitize_path("assets/v1/mesh.obj").expect("should succeed");
        assert_eq!(result, PathBuf::from("assets/v1/mesh.obj"));
    }

    #[test]
    fn sanitize_path_rejects_null_byte() {
        let err = sanitize_path("foo\0bar").unwrap_err();
        assert_eq!(err, SecurityError::NullByte);
    }

    #[test]
    fn sanitize_path_rejects_too_long() {
        let long = "a".repeat(513);
        let err = sanitize_path(&long).unwrap_err();
        assert_eq!(err, SecurityError::TooLong);
    }

    #[test]
    fn sanitize_path_512_chars_ok() {
        // Exactly 512 characters must pass
        let ok = "a".repeat(512);
        assert!(sanitize_path(&ok).is_ok());
    }

    #[test]
    fn sanitize_path_rejects_dotdot() {
        let err = sanitize_path("../etc/passwd").unwrap_err();
        assert_eq!(err, SecurityError::PathTraversal);
    }

    #[test]
    fn sanitize_path_rejects_dotdot_middle() {
        let err = sanitize_path("assets/../../../etc/shadow").unwrap_err();
        assert_eq!(err, SecurityError::PathTraversal);
    }

    #[test]
    fn sanitize_path_rejects_absolute_unix() {
        let err = sanitize_path("/etc/passwd").unwrap_err();
        assert_eq!(err, SecurityError::AbsolutePath);
    }

    #[test]
    fn sanitize_path_rejects_absolute_backslash() {
        let err = sanitize_path("\\\\server\\share").unwrap_err();
        assert_eq!(err, SecurityError::AbsolutePath);
    }

    #[test]
    fn sanitize_path_rejects_windows_drive() {
        let err = sanitize_path("C:\\Windows\\System32").unwrap_err();
        assert_eq!(err, SecurityError::AbsolutePath);
    }

    #[test]
    fn sanitize_path_rejects_windows_drive_lowercase() {
        let err = sanitize_path("c:/Users/Admin").unwrap_err();
        assert_eq!(err, SecurityError::AbsolutePath);
    }

    #[test]
    fn sanitize_path_rejects_reserved_con() {
        let err = sanitize_path("CON").unwrap_err();
        assert!(matches!(err, SecurityError::ReservedName(_)));
    }

    #[test]
    fn sanitize_path_rejects_reserved_nul_with_ext() {
        // NUL.txt — strip extension before checking
        let err = sanitize_path("NUL.txt").unwrap_err();
        assert!(matches!(err, SecurityError::ReservedName(_)));
    }

    #[test]
    fn sanitize_path_rejects_reserved_com1() {
        let err = sanitize_path("COM1").unwrap_err();
        assert!(matches!(err, SecurityError::ReservedName(_)));
    }

    #[test]
    fn sanitize_path_rejects_reserved_lpt9_lowercase() {
        let err = sanitize_path("lpt9").unwrap_err();
        assert!(matches!(err, SecurityError::ReservedName(_)));
    }

    #[test]
    fn sanitize_path_rejects_reserved_in_subdir() {
        // Reserved name as a subdirectory component
        let err = sanitize_path("assets/NUL/mesh.obj").unwrap_err();
        assert!(matches!(err, SecurityError::ReservedName(_)));
    }

    // --- validate_file_size ---

    #[test]
    fn validate_file_size_ok() {
        assert!(validate_file_size(1024 * 1024, 10).is_ok());
    }

    #[test]
    fn validate_file_size_exact_boundary() {
        // Exactly at the limit must be accepted
        let max_mb = 10u32;
        let exact = max_mb as usize * 1024 * 1024;
        assert!(validate_file_size(exact, max_mb).is_ok());
    }

    #[test]
    fn validate_file_size_too_large() {
        let max_mb = 10u32;
        let over = max_mb as usize * 1024 * 1024 + 1;
        let err = validate_file_size(over, max_mb).unwrap_err();
        assert!(matches!(err, SecurityError::FileTooLarge { .. }));
    }

    #[test]
    fn validate_file_size_zero_ok() {
        assert!(validate_file_size(0, 1).is_ok());
    }

    // --- checked_stride_offset ---

    #[test]
    fn checked_stride_offset_ok() {
        let result = checked_stride_offset(3, 4, 20).expect("should succeed");
        assert_eq!(result, 12);
    }

    #[test]
    fn checked_stride_offset_zero_index() {
        let result = checked_stride_offset(0, 8, 100).expect("should succeed");
        assert_eq!(result, 0);
    }

    #[test]
    fn checked_stride_offset_out_of_bounds() {
        // 5 * 4 = 20, total = 16 → out of bounds
        let err = checked_stride_offset(5, 4, 16).unwrap_err();
        assert!(matches!(err, SecurityError::OutOfBounds { .. }));
    }

    #[test]
    fn checked_stride_offset_overflow() {
        let err = checked_stride_offset(usize::MAX, 2, 100).unwrap_err();
        assert_eq!(err, SecurityError::OverflowError);
    }

    #[test]
    fn checked_stride_offset_exact_boundary_ok() {
        // index=4, stride=4, total=20 → 16 < 20 → ok
        let result = checked_stride_offset(4, 4, 20).expect("should succeed");
        assert_eq!(result, 16);
    }

    // --- is_safe_content_type ---

    #[test]
    fn is_safe_content_type_glb() {
        assert!(is_safe_content_type(
            b"glTF\x02\x00\x00\x00\x00\x00\x00\x00"
        ));
    }

    #[test]
    fn is_safe_content_type_ply_lf() {
        assert!(is_safe_content_type(b"ply\nformat ascii 1.0\n"));
    }

    #[test]
    fn is_safe_content_type_ply_cr() {
        assert!(is_safe_content_type(b"ply\rformat ascii 1.0\r"));
    }

    #[test]
    fn is_safe_content_type_fbx() {
        assert!(is_safe_content_type(
            b"Kaydara FBX Binary  \x00more_data_here"
        ));
    }

    #[test]
    fn is_safe_content_type_stl_ascii() {
        assert!(is_safe_content_type(b"solid MyModel\nfacet normal 0 0 1\n"));
    }

    #[test]
    fn is_safe_content_type_stl_ascii_uppercase() {
        assert!(is_safe_content_type(b"SOLID mymodel\n"));
    }

    #[test]
    fn is_safe_content_type_obj_vertex() {
        assert!(is_safe_content_type(b"v 0.0 0.0 0.0\nv 1.0 0.0 0.0\n"));
    }

    #[test]
    fn is_safe_content_type_obj_comment() {
        assert!(is_safe_content_type(
            b"# Exported by Blender\nmtllib mat.mtl\n"
        ));
    }

    #[test]
    fn is_safe_content_type_unknown_magic() {
        assert!(!is_safe_content_type(b"\x00\x01\x02\x03"));
    }

    #[test]
    fn is_safe_content_type_empty() {
        assert!(!is_safe_content_type(b""));
    }

    #[test]
    fn is_safe_content_type_random_text() {
        assert!(!is_safe_content_type(b"Hello, world!"));
    }

    // --- SecurityError Display ---

    #[test]
    fn security_error_display_path_traversal() {
        let msg = format!("{}", SecurityError::PathTraversal);
        assert!(msg.contains("path traversal"));
    }

    #[test]
    fn security_error_display_file_too_large() {
        let msg = format!(
            "{}",
            SecurityError::FileTooLarge {
                size_bytes: 200,
                max_bytes: 100
            }
        );
        assert!(msg.contains("200"));
        assert!(msg.contains("100"));
    }
}