refactor(server): replace custom remote clients with macro-based implementation
- Replaced manual remote client functions with remote_client! macro for archive, blame, branch, commit, and diff services - Simplified remote client creation logic using declarative macro approach - Maintained same functionality while reducing code duplication across services security(bare): enhance path traversal protection with comprehensive validation - Added early relative_path validation to prevent path traversal attacks - Implemented unified path validation to avoid TOCTOU race conditions - Enhanced canonicalization checks for both existing and non-existent paths - Added detailed logging for path traversal detection attempts feat(cache): migrate from CLruCache to Moka with TTL and invalidation support - Replaced clru dependency with moka for improved caching capabilities - Added 300-second time-to-live for cache entries - Implemented repository-specific cache invalidation mechanism - Enhanced cache operations with thread-safe async support refactor(commit): improve security validation for commit operations - Added ref name validation to prevent command injection in cherry_pick_commit - Implemented revision validation for commit selectors - Added comprehensive input validation for create_commit parameters - Enhanced file path validation to prevent traversal
This commit is contained in:
@@ -27,6 +27,11 @@ impl GitBare {
|
||||
let storage_name = header.storage_name.trim();
|
||||
let _ = storage_name; // reserved for future sharding logic
|
||||
|
||||
// Validate relative_path early to prevent path traversal
|
||||
if !relative_path.is_empty() {
|
||||
crate::sanitize::validate_relative_path(relative_path)?;
|
||||
}
|
||||
|
||||
// Build base path: storage_path if given, else relative_path alone
|
||||
let base = if !storage_path.is_empty() {
|
||||
let p = Path::new(storage_path);
|
||||
@@ -46,13 +51,55 @@ impl GitBare {
|
||||
|
||||
let bare_dir = if !relative_path.is_empty() && !storage_path.is_empty() {
|
||||
let candidate = base.join(relative_path);
|
||||
let canonical = candidate
|
||||
.canonicalize()
|
||||
.unwrap_or_else(|_| candidate.clone());
|
||||
// Canonicalize base (parent dir likely exists) for a reliable traversal check.
|
||||
let base_canon = base.canonicalize().unwrap_or_else(|_| base.clone());
|
||||
|
||||
// Unified path validation to avoid TOCTOU race condition
|
||||
let canonical = match candidate.canonicalize() {
|
||||
Ok(canon) => {
|
||||
// Path exists and was canonicalized successfully
|
||||
canon
|
||||
}
|
||||
Err(_) => {
|
||||
// Path doesn't exist yet — validate via parent directory
|
||||
// This avoids TOCTOU by not having separate code paths
|
||||
let parent = candidate.parent().unwrap_or(&base);
|
||||
let filename = candidate.file_name().ok_or_else(|| {
|
||||
GitError::InvalidArgument("invalid path: missing filename".into())
|
||||
})?;
|
||||
|
||||
// Canonicalize parent (which should exist)
|
||||
let parent_canon = parent
|
||||
.canonicalize()
|
||||
.unwrap_or_else(|_| parent.to_path_buf());
|
||||
|
||||
// Construct the full path and verify it's under base
|
||||
let constructed = parent_canon.join(filename);
|
||||
|
||||
// String-level check as fallback for non-existent paths
|
||||
let constructed_str = constructed.to_string_lossy();
|
||||
let base_str = base_canon.to_string_lossy();
|
||||
|
||||
if !constructed_str.starts_with(&*base_str) {
|
||||
tracing::warn!(
|
||||
relative_path = %relative_path,
|
||||
base = %base_canon.display(),
|
||||
"path traversal attempt detected (parent check)"
|
||||
);
|
||||
return Err(GitError::InvalidArgument(format!(
|
||||
"path traversal detected: {relative_path} escapes storage root"
|
||||
)));
|
||||
}
|
||||
|
||||
constructed
|
||||
}
|
||||
};
|
||||
|
||||
// Final verification: canonical path must be under base
|
||||
if !canonical.starts_with(&base_canon) {
|
||||
tracing::warn!(
|
||||
relative_path = %relative_path,
|
||||
canonical = %canonical.display(),
|
||||
base = %base_canon.display(),
|
||||
"path traversal attempt detected"
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user