fix: A-6 security patch - path traversal prevention
**Security Issue**: - chunk_thumbnail: file_uuid/chunk_id unvalidated → path traversal - chunk_thumbnail: cache-hit missing JPEG validation → content-type confusion - scene_thumbnail: file_uuid unvalidated → path traversal **Fixes**: 1. Added is_valid_file_uuid() in validator.rs - 32 chars, lowercase hex only - Blocks any path containing / or .. 2. Added is_valid_chunk_id() in validator.rs - [a-zA-Z0-9_-]+, length <= 128 - Matches all 5226 existing chunks (cut_, time_, pure digits) - Blocks traversal sequences 3. chunk_thumbnail (media_api.rs:1162): - Added validation before path construction - Added JPEG validation for cache-hit - Returns 404 for invalid input 4. scene_thumbnail (media_api.rs:965): - Added file_uuid validation - filename already validated (scene_<N>.jpg) **Verification**: - Defense in depth: character whitelist + canonicalize - No regression: all existing chunk_id formats pass - Mitigation: both handlers behind protected_routes (auth required)
This commit is contained in:
@@ -968,6 +968,11 @@ async fn scene_thumbnail(
|
||||
) -> Result<impl IntoResponse, StatusCode> {
|
||||
let output_dir = crate::core::config::OUTPUT_DIR.as_str();
|
||||
|
||||
// Security: validate file_uuid to prevent path traversal
|
||||
if !crate::core::thumbnail::validator::is_valid_file_uuid(&file_uuid) {
|
||||
return Err(StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
// Security: only allow scene_*.jpg filenames (no path traversal)
|
||||
let safe_name = filename
|
||||
.strip_prefix("scene_")
|
||||
@@ -1166,6 +1171,13 @@ async fn chunk_thumbnail(
|
||||
let videos_table = schema::table_name("videos");
|
||||
let chunk_table = schema::table_name("chunk");
|
||||
|
||||
// Security: validate file_uuid and chunk_id to prevent path traversal
|
||||
if !crate::core::thumbnail::validator::is_valid_file_uuid(&file_uuid)
|
||||
|| !crate::core::thumbnail::validator::is_valid_chunk_id(&chunk_id)
|
||||
{
|
||||
return Err(StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
let output_dir = crate::core::config::OUTPUT_DIR.as_str();
|
||||
let cached_path = std::path::PathBuf::from(output_dir)
|
||||
.join(".chunk_thumbs")
|
||||
@@ -1177,6 +1189,13 @@ async fn chunk_thumbnail(
|
||||
tracing::warn!("[chunk_thumbnail] Failed to read cache: {}", e);
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
})?;
|
||||
|
||||
// Security: validate JPEG magic bytes to prevent content-type confusion
|
||||
crate::core::thumbnail::validator::validate_jpeg(&bytes).map_err(|e| {
|
||||
tracing::warn!("[chunk_thumbnail] JPEG validation failed: {}", e);
|
||||
StatusCode::INTERNAL_SERVER_ERROR
|
||||
})?;
|
||||
|
||||
return Ok(image_response(bytes));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user