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:
zhenyi
2026-06-08 09:43:57 +08:00
parent 8c95eb230d
commit d243dce027
60 changed files with 1746 additions and 561 deletions
+33 -10
View File
@@ -8,6 +8,19 @@ use crate::pb::{
impl GitBare {
pub fn create_commit(&self, request: CreateCommitRequest) -> GitResult<CreateCommitResponse> {
// Validate branch name to prevent command injection
crate::sanitize::validate_ref_name(&request.branch)?;
// Validate start_revision if provided
if let Some(rev) = request.start_revision.as_ref() {
match rev.selector.as_ref() {
Some(object_selector::Selector::Revision(name)) => {
crate::sanitize::validate_revision(&name.revision)?;
}
Some(object_selector::Selector::Oid(_)) => {} // OID is always safe
None => {} // will use branch name, already validated
}
}
let repo = self.gix_repo()?;
let branch = request.branch.clone();
tracing::debug!(
@@ -21,15 +34,15 @@ impl GitBare {
Some(object_selector::Selector::Revision(name)) => name.revision,
None => request.branch.clone(),
};
let parent_id = repo
.rev_parse_single(start_rev.as_str())
.ok()
.map(|id| id.to_string());
let current_branch_tip = repo
.find_reference(format!("refs/heads/{}", request.branch).as_str())
.ok()
.and_then(|mut r| r.peel_to_id().ok())
.map(|id| id.to_string());
let parent_id = match repo.rev_parse_single(start_rev.as_str()) {
Ok(id) => Some(id.to_string()),
Err(_) => None, // branch/revision does not exist yet — will create initial commit
};
let current_branch_tip =
match repo.find_reference(format!("refs/heads/{}", request.branch).as_str()) {
Ok(mut r) => r.peel_to_id().ok().map(|id| id.to_string()),
Err(_) => None, // branch does not exist yet
};
let tree_id = if request.actions.is_empty() {
let Some(parent) = parent_id.as_ref() else {
@@ -91,9 +104,11 @@ impl GitBare {
parent_id: Option<&str>,
actions: &[crate::pb::CreateCommitAction],
) -> GitResult<String> {
// Use system temp directory instead of bare_dir to avoid clutter
// and ensure proper cleanup even if process crashes
let tmp_index = tempfile::Builder::new()
.prefix("gitks-index-")
.tempfile_in(&self.bare_dir)
.tempfile()
.map_err(GitError::Io)?;
let tmp_index_path = tmp_index.path().to_string_lossy().into_owned();
@@ -140,6 +155,14 @@ impl GitBare {
index_path: &str,
action: &crate::pb::CreateCommitAction,
) -> GitResult<()> {
// Validate file paths to prevent command injection / traversal
if !action.file_path.is_empty() {
crate::sanitize::validate_file_path(&action.file_path)?;
}
if !action.previous_path.is_empty() {
crate::sanitize::validate_file_path(&action.previous_path)?;
}
let action_type = create_commit_action::Action::try_from(action.action)
.unwrap_or(create_commit_action::Action::CreateCommitActionUnspecified);
match action_type {