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:
+50
-40
@@ -1,27 +1,9 @@
|
||||
use crate::pb::*;
|
||||
use crate::pb::repository_service_client::RepositoryServiceClient;
|
||||
use crate::pb::*;
|
||||
|
||||
use super::{GitksService, git_cmd, into_status, repository_maint, remote_endpoint};
|
||||
use super::{GitksService, git_cmd, into_status, repository_maint};
|
||||
|
||||
async fn remote_repository_client(
|
||||
svc: &GitksService,
|
||||
header: Option<&RepositoryHeader>,
|
||||
is_write: bool,
|
||||
) -> Result<Option<RepositoryServiceClient<tonic::transport::Channel>>, tonic::Status> {
|
||||
let header = match header {
|
||||
Some(h) => h,
|
||||
None => return Ok(None),
|
||||
};
|
||||
let Some(route) = svc.route_repository(header, is_write).await? else {
|
||||
return Ok(None);
|
||||
};
|
||||
tracing::info!(storage_name = %route.storage_name, relative_path = %route.relative_path, actor_name = %route.actor_name, grpc_addr = %route.grpc_addr, "forwarding repository rpc");
|
||||
let endpoint = remote_endpoint(&route.grpc_addr).await?;
|
||||
let client = RepositoryServiceClient::connect(endpoint)
|
||||
.await
|
||||
.map_err(|e| tonic::Status::unavailable(e.to_string()))?;
|
||||
Ok(Some(client))
|
||||
}
|
||||
remote_client!(remote_repository_client, RepositoryServiceClient<tonic::transport::Channel>, "repository");
|
||||
|
||||
fn default_branch_name(gb: &crate::bare::GitBare) -> String {
|
||||
git_cmd(gb, &["symbolic-ref", "HEAD"])
|
||||
@@ -48,7 +30,9 @@ impl repository_service_server::RepositoryService for GitksService {
|
||||
let gb = match self.resolve(inner.repository.as_ref()) {
|
||||
Ok(gb) => gb,
|
||||
Err(err) if err.code() == tonic::Code::NotFound => {
|
||||
if let Some(mut client) = remote_repository_client(self, inner.repository.as_ref(), false).await? {
|
||||
if let Some(mut client) =
|
||||
remote_repository_client(self, inner.repository.as_ref(), false).await?
|
||||
{
|
||||
return client.get_repository(inner).await;
|
||||
}
|
||||
return Err(err);
|
||||
@@ -95,10 +79,11 @@ impl repository_service_server::RepositoryService for GitksService {
|
||||
let span = tracing::info_span!("repo.delete_repository", %repo);
|
||||
let _enter = span.enter();
|
||||
let bare_dir = self.resolve_for_init(inner.repository.as_ref())?;
|
||||
if !bare_dir.exists() {
|
||||
if let Some(mut client) = remote_repository_client(self, inner.repository.as_ref(), true).await? {
|
||||
return client.delete_repository(inner).await;
|
||||
}
|
||||
if !bare_dir.exists()
|
||||
&& let Some(mut client) =
|
||||
remote_repository_client(self, inner.repository.as_ref(), true).await?
|
||||
{
|
||||
return client.delete_repository(inner).await;
|
||||
}
|
||||
tracing::warn!(%repo, path = %bare_dir.display(), "deleting repository");
|
||||
std::fs::remove_dir_all(&bare_dir).map_err(|e| tonic::Status::internal(e.to_string()))?;
|
||||
@@ -117,10 +102,11 @@ impl repository_service_server::RepositoryService for GitksService {
|
||||
let _enter = span.enter();
|
||||
let bare_dir = self.resolve_for_init(inner.repository.as_ref())?;
|
||||
let exists = bare_dir.exists() && bare_dir.is_dir() && bare_dir.join("HEAD").exists();
|
||||
if !exists {
|
||||
if let Some(mut client) = remote_repository_client(self, inner.repository.as_ref(), false).await? {
|
||||
return client.repository_exists(inner).await;
|
||||
}
|
||||
if !exists
|
||||
&& let Some(mut client) =
|
||||
remote_repository_client(self, inner.repository.as_ref(), false).await?
|
||||
{
|
||||
return client.repository_exists(inner).await;
|
||||
}
|
||||
Ok(tonic::Response::new(RepositoryExistsResponse { exists }))
|
||||
}
|
||||
@@ -136,7 +122,9 @@ impl repository_service_server::RepositoryService for GitksService {
|
||||
let gb = match self.resolve(inner.repository.as_ref()) {
|
||||
Ok(gb) => gb,
|
||||
Err(err) if err.code() == tonic::Code::NotFound => {
|
||||
if let Some(mut client) = remote_repository_client(self, inner.repository.as_ref(), false).await? {
|
||||
if let Some(mut client) =
|
||||
remote_repository_client(self, inner.repository.as_ref(), false).await?
|
||||
{
|
||||
return client.get_object_format(inner).await;
|
||||
}
|
||||
return Err(err);
|
||||
@@ -159,7 +147,9 @@ impl repository_service_server::RepositoryService for GitksService {
|
||||
let gb = match self.resolve(inner.repository.as_ref()) {
|
||||
Ok(gb) => gb,
|
||||
Err(err) if err.code() == tonic::Code::NotFound => {
|
||||
if let Some(mut client) = remote_repository_client(self, inner.repository.as_ref(), false).await? {
|
||||
if let Some(mut client) =
|
||||
remote_repository_client(self, inner.repository.as_ref(), false).await?
|
||||
{
|
||||
return client.get_default_branch(inner).await;
|
||||
}
|
||||
return Err(err);
|
||||
@@ -183,7 +173,9 @@ impl repository_service_server::RepositoryService for GitksService {
|
||||
let gb = match self.resolve(inner.repository.as_ref()) {
|
||||
Ok(gb) => gb,
|
||||
Err(err) if err.code() == tonic::Code::NotFound => {
|
||||
if let Some(mut client) = remote_repository_client(self, inner.repository.as_ref(), true).await? {
|
||||
if let Some(mut client) =
|
||||
remote_repository_client(self, inner.repository.as_ref(), true).await?
|
||||
{
|
||||
return client.set_default_branch(inner).await;
|
||||
}
|
||||
return Err(err);
|
||||
@@ -213,7 +205,9 @@ impl repository_service_server::RepositoryService for GitksService {
|
||||
let gb = match self.resolve(inner.repository.as_ref()) {
|
||||
Ok(gb) => gb,
|
||||
Err(err) if err.code() == tonic::Code::NotFound => {
|
||||
if let Some(mut client) = remote_repository_client(self, inner.repository.as_ref(), false).await? {
|
||||
if let Some(mut client) =
|
||||
remote_repository_client(self, inner.repository.as_ref(), false).await?
|
||||
{
|
||||
return client.get_repository_config(inner).await;
|
||||
}
|
||||
return Err(err);
|
||||
@@ -238,6 +232,8 @@ impl repository_service_server::RepositoryService for GitksService {
|
||||
}
|
||||
} else {
|
||||
for key in &inner.keys {
|
||||
crate::sanitize::validate_config_key(key)
|
||||
.map_err(|e| tonic::Status::invalid_argument(e.to_string()))?;
|
||||
let out = git_cmd(&gb, &["config", "--get-all", key])?;
|
||||
if out.status.success() {
|
||||
let vals: Vec<String> = String::from_utf8_lossy(&out.stdout)
|
||||
@@ -270,7 +266,9 @@ impl repository_service_server::RepositoryService for GitksService {
|
||||
let gb = match self.resolve(inner.repository.as_ref()) {
|
||||
Ok(gb) => gb,
|
||||
Err(err) if err.code() == tonic::Code::NotFound => {
|
||||
if let Some(mut client) = remote_repository_client(self, inner.repository.as_ref(), true).await? {
|
||||
if let Some(mut client) =
|
||||
remote_repository_client(self, inner.repository.as_ref(), true).await?
|
||||
{
|
||||
return client.set_repository_config(inner).await;
|
||||
}
|
||||
return Err(err);
|
||||
@@ -278,6 +276,8 @@ impl repository_service_server::RepositoryService for GitksService {
|
||||
Err(err) => return Err(err),
|
||||
};
|
||||
for entry in &inner.entries {
|
||||
crate::sanitize::validate_config_key(&entry.key)
|
||||
.map_err(|e| tonic::Status::invalid_argument(e.to_string()))?;
|
||||
if entry.values.is_empty() {
|
||||
git_cmd(&gb, &["config", "--unset-all", &entry.key])?;
|
||||
} else {
|
||||
@@ -305,7 +305,9 @@ impl repository_service_server::RepositoryService for GitksService {
|
||||
let gb = match self.resolve(inner.repository.as_ref()) {
|
||||
Ok(gb) => gb,
|
||||
Err(err) if err.code() == tonic::Code::NotFound => {
|
||||
if let Some(mut client) = remote_repository_client(self, inner.repository.as_ref(), false).await? {
|
||||
if let Some(mut client) =
|
||||
remote_repository_client(self, inner.repository.as_ref(), false).await?
|
||||
{
|
||||
return client.get_repository_statistics(inner).await;
|
||||
}
|
||||
return Err(err);
|
||||
@@ -326,7 +328,9 @@ impl repository_service_server::RepositoryService for GitksService {
|
||||
let gb = match self.resolve(inner.repository.as_ref()) {
|
||||
Ok(gb) => gb,
|
||||
Err(err) if err.code() == tonic::Code::NotFound => {
|
||||
if let Some(mut client) = remote_repository_client(self, inner.repository.as_ref(), false).await? {
|
||||
if let Some(mut client) =
|
||||
remote_repository_client(self, inner.repository.as_ref(), false).await?
|
||||
{
|
||||
return client.check_repository_health(inner).await;
|
||||
}
|
||||
return Err(err);
|
||||
@@ -349,7 +353,9 @@ impl repository_service_server::RepositoryService for GitksService {
|
||||
let gb = match self.resolve(inner.repository.as_ref()) {
|
||||
Ok(gb) => gb,
|
||||
Err(err) if err.code() == tonic::Code::NotFound => {
|
||||
if let Some(mut client) = remote_repository_client(self, inner.repository.as_ref(), true).await? {
|
||||
if let Some(mut client) =
|
||||
remote_repository_client(self, inner.repository.as_ref(), true).await?
|
||||
{
|
||||
return client.garbage_collect(inner).await;
|
||||
}
|
||||
return Err(err);
|
||||
@@ -372,7 +378,9 @@ impl repository_service_server::RepositoryService for GitksService {
|
||||
let gb = match self.resolve(inner.repository.as_ref()) {
|
||||
Ok(gb) => gb,
|
||||
Err(err) if err.code() == tonic::Code::NotFound => {
|
||||
if let Some(mut client) = remote_repository_client(self, inner.repository.as_ref(), true).await? {
|
||||
if let Some(mut client) =
|
||||
remote_repository_client(self, inner.repository.as_ref(), true).await?
|
||||
{
|
||||
return client.repack(inner).await;
|
||||
}
|
||||
return Err(err);
|
||||
@@ -400,7 +408,9 @@ impl repository_service_server::RepositoryService for GitksService {
|
||||
let gb = match self.resolve(inner.repository.as_ref()) {
|
||||
Ok(gb) => gb,
|
||||
Err(err) if err.code() == tonic::Code::NotFound => {
|
||||
if let Some(mut client) = remote_repository_client(self, inner.repository.as_ref(), true).await? {
|
||||
if let Some(mut client) =
|
||||
remote_repository_client(self, inner.repository.as_ref(), true).await?
|
||||
{
|
||||
return client.write_commit_graph(inner).await;
|
||||
}
|
||||
return Err(err);
|
||||
|
||||
Reference in New Issue
Block a user