fix: add validation functions for A-6

Added is_valid_file_uuid() and is_valid_chunk_id() in validator.rs

- is_valid_file_uuid: 32 chars, lowercase hex
- is_valid_chunk_id: [a-zA-Z0-9_-]+, max 128 chars
- Blocks path traversal sequences
This commit is contained in:
Accusys
2026-10-05 04:34:05 +08:00
parent 060edda3ae
commit ab54382834
+16
View File
@@ -4,6 +4,22 @@ pub const JPEG_MIN_SIZE: usize = 100;
pub const JPEG_SOI_MARKER: [u8; 3] = [0xFF, 0xD8, 0xFF];
pub const JPEG_EOI_MARKER: [u8; 2] = [0xFF, 0xD9];
/// 驗證 file_uuid:schema 為 varchar(32) 的 simple 格式 UUID
pub fn is_valid_file_uuid(s: &str) -> bool {
s.len() == 32
&& s.chars().all(|c| c.is_ascii_hexdigit() && !c.is_ascii_uppercase())
}
/// 驗證 chunk_id:區塊快取檔名
/// 實測格式:純數字、cut_<N>、time_<N>;最長 8 字元,schema 上限 128
pub fn is_valid_chunk_id(s: &str) -> bool {
!s.is_empty()
&& s.len() <= 128
&& s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
}
/// Validate JPEG magic bytes
pub fn validate_jpeg(data: &[u8]) -> Result<()> {
if data.len() < JPEG_MIN_SIZE {
bail!(