Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions src/llm-coding-tools-core/src/path/allowed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

use super::PathResolver;
use crate::error::{ToolError, ToolResult};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::sync::Arc;

/// Path resolver that restricts access to allowed directories.
///
Expand Down Expand Up @@ -32,7 +33,7 @@ use std::path::PathBuf;
#[derive(Debug, Clone)]
pub struct AllowedPathResolver {
/// Canonicalized allowed base directories.
allowed_paths: Vec<PathBuf>,
allowed_paths: Arc<[PathBuf]>,
}

impl AllowedPathResolver {
Expand All @@ -41,14 +42,15 @@ impl AllowedPathResolver {
/// Each directory is canonicalized during construction to ensure
/// consistent path comparison. Returns an error if any directory
/// doesn't exist or can't be canonicalized.
pub fn new(allowed_paths: Vec<PathBuf>) -> ToolResult<Self> {
let canonicalized: Result<Vec<_>, _> = allowed_paths
pub fn new(allowed_paths: impl IntoIterator<Item = impl AsRef<Path>>) -> ToolResult<Self> {
let canonicalized: Result<Arc<[PathBuf]>, _> = allowed_paths
.into_iter()
.map(|p| {
p.canonicalize().map_err(|e| {
let path = p.as_ref();
path.canonicalize().map_err(|e| {
ToolError::InvalidPath(format!(
"failed to canonicalize allowed path '{}': {}",
p.display(),
path.display(),
e
))
})
Expand All @@ -69,8 +71,13 @@ impl AllowedPathResolver {
///
/// Caller must ensure paths are actually canonical. Using non-canonical
/// paths may allow path traversal attacks.
pub fn from_canonical(allowed_paths: Vec<PathBuf>) -> Self {
Self { allowed_paths }
pub fn from_canonical(allowed_paths: impl IntoIterator<Item = impl AsRef<Path>>) -> Self {
Self {
allowed_paths: allowed_paths
.into_iter()
.map(|p| p.as_ref().to_path_buf())
.collect(),
}
}

/// Returns the allowed base directories.
Expand All @@ -84,7 +91,7 @@ impl PathResolver for AllowedPathResolver {
let input_path = PathBuf::from(path);

// Try each allowed base directory in order
for base in &self.allowed_paths {
for base in self.allowed_paths.iter() {
let candidate = base.join(&input_path);

// Try to canonicalize for existing paths
Expand Down
8 changes: 2 additions & 6 deletions src/llm-coding-tools-rig/src/allowed/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rig::completion::ToolDefinition;
use rig::tool::Tool;
use schemars::{schema_for, JsonSchema};
use serde::Deserialize;
use std::path::{Path, PathBuf};
use std::path::Path;

/// Arguments for file editing.
#[derive(Debug, Clone, Deserialize, JsonSchema)]
Expand All @@ -33,12 +33,8 @@ pub struct EditTool {
impl EditTool {
/// Creates a new edit tool restricted to the given directories.
pub fn new(allowed_paths: impl IntoIterator<Item = impl AsRef<Path>>) -> ToolResult<Self> {
let paths: Vec<PathBuf> = allowed_paths
.into_iter()
.map(|p| p.as_ref().to_path_buf())
.collect();
Ok(Self {
resolver: AllowedPathResolver::new(paths)?,
resolver: AllowedPathResolver::new(allowed_paths)?,
})
}

Expand Down
8 changes: 2 additions & 6 deletions src/llm-coding-tools-rig/src/allowed/glob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rig::completion::ToolDefinition;
use rig::tool::Tool;
use schemars::{schema_for, JsonSchema};
use serde::Deserialize;
use std::path::{Path, PathBuf};
use std::path::Path;

/// Arguments for the glob tool.
#[derive(Debug, Deserialize, JsonSchema)]
Expand All @@ -27,12 +27,8 @@ pub struct GlobTool {
impl GlobTool {
/// Creates a new glob tool restricted to the given directories.
pub fn new(allowed_paths: impl IntoIterator<Item = impl AsRef<Path>>) -> ToolResult<Self> {
let paths: Vec<PathBuf> = allowed_paths
.into_iter()
.map(|p| p.as_ref().to_path_buf())
.collect();
Ok(Self {
resolver: AllowedPathResolver::new(paths)?,
resolver: AllowedPathResolver::new(allowed_paths)?,
})
}

Expand Down
8 changes: 2 additions & 6 deletions src/llm-coding-tools-rig/src/allowed/grep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rig::tool::Tool;
use schemars::{schema_for, JsonSchema};
use serde::Deserialize;
use std::fmt::Write;
use std::path::{Path, PathBuf};
use std::path::Path;

const DEFAULT_LIMIT: usize = 100;
const MAX_LIMIT: usize = 2000;
Expand Down Expand Up @@ -42,12 +42,8 @@ pub struct GrepTool<const LINE_NUMBERS: bool = true> {
impl<const LINE_NUMBERS: bool> GrepTool<LINE_NUMBERS> {
/// Creates a new grep tool restricted to the given directories.
pub fn new(allowed_paths: impl IntoIterator<Item = impl AsRef<Path>>) -> ToolResult<Self> {
let paths: Vec<PathBuf> = allowed_paths
.into_iter()
.map(|p| p.as_ref().to_path_buf())
.collect();
Ok(Self {
resolver: AllowedPathResolver::new(paths)?,
resolver: AllowedPathResolver::new(allowed_paths)?,
})
}

Expand Down
8 changes: 2 additions & 6 deletions src/llm-coding-tools-rig/src/allowed/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rig::completion::ToolDefinition;
use rig::tool::Tool;
use schemars::{schema_for, JsonSchema};
use serde::Deserialize;
use std::path::{Path, PathBuf};
use std::path::Path;

const DEFAULT_OFFSET: usize = 1;
const DEFAULT_LIMIT: usize = 2000;
Expand Down Expand Up @@ -44,12 +44,8 @@ pub struct ReadTool<const LINE_NUMBERS: bool = true> {
impl<const LINE_NUMBERS: bool> ReadTool<LINE_NUMBERS> {
/// Creates a new read tool restricted to the given directories.
pub fn new(allowed_paths: impl IntoIterator<Item = impl AsRef<Path>>) -> ToolResult<Self> {
let paths: Vec<PathBuf> = allowed_paths
.into_iter()
.map(|p| p.as_ref().to_path_buf())
.collect();
Ok(Self {
resolver: AllowedPathResolver::new(paths)?,
resolver: AllowedPathResolver::new(allowed_paths)?,
})
}

Expand Down
8 changes: 2 additions & 6 deletions src/llm-coding-tools-rig/src/allowed/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rig::completion::ToolDefinition;
use rig::tool::Tool;
use schemars::{schema_for, JsonSchema};
use serde::Deserialize;
use std::path::{Path, PathBuf};
use std::path::Path;

/// Arguments for the write tool.
#[derive(Debug, Clone, Deserialize, JsonSchema)]
Expand All @@ -27,12 +27,8 @@ pub struct WriteTool {
impl WriteTool {
/// Creates a new write tool restricted to the given directories.
pub fn new(allowed_paths: impl IntoIterator<Item = impl AsRef<Path>>) -> ToolResult<Self> {
let paths: Vec<PathBuf> = allowed_paths
.into_iter()
.map(|p| p.as_ref().to_path_buf())
.collect();
Ok(Self {
resolver: AllowedPathResolver::new(paths)?,
resolver: AllowedPathResolver::new(allowed_paths)?,
})
}

Expand Down