diff --git a/.github/workflows/python-native-qualification.yml b/.github/workflows/python-native-qualification.yml index fd94a36e..ac60e9d2 100644 --- a/.github/workflows/python-native-qualification.yml +++ b/.github/workflows/python-native-qualification.yml @@ -161,7 +161,7 @@ jobs: "$RUNNER_TEMP/runtime-venv/bin/python" -m pip install pytest==8.4.2 cd "$RUNNER_TEMP" "$RUNNER_TEMP/runtime-venv/bin/python" -m pytest -q "$source_dir/bindings/python/tests" - "$RUNNER_TEMP/runtime-venv/bin/python" -c "import eggsearch; from importlib.metadata import distribution; assert len(eggsearch.tool_names()) == 10; assert distribution('eggsearch').version == '0.4.3'" + "$RUNNER_TEMP/runtime-venv/bin/python" -c "import eggsearch; from importlib.metadata import distribution; assert len(eggsearch.tool_names()) == 10; assert distribution('eggsearch').version == '0.4.4'" sha256sum "$GITHUB_WORKSPACE"/dist/* "$GITHUB_WORKSPACE"/extracted-wheel/* | tee "$GITHUB_WORKSPACE/dist/SHA256SUMS" cat "$GITHUB_WORKSPACE/dist/SHA256SUMS" >> "$GITHUB_STEP_SUMMARY" - uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 diff --git a/CHANGELOG.md b/CHANGELOG.md index 796dc6f3..d2c30c10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [0.4.4] - 2026-10-19 + +### Added + +- CodeGG-managed stdio requests can carry authenticated per-call local + workspace scope for `repo_search`, `repo_fetch`, and `repo_map`. Each + canonical root and indexing configuration gets an isolated bounded + in-memory backend/inventory view. Invalid host metadata fails closed and + never falls back to startup-configured roots; standalone requests without + host metadata retain existing behavior. + +### Fixed + +- Local `repo_fetch` reads through the bounded descriptor-relative opener + after path validation instead of reopening the canonical pathname. Local + `repo_map` enrichment uses the same bounded safe reader for manifests, + source files, and test files, and skips symlinked entries even when local + search is configured to follow symlinks. + ## [0.4.3] - 2026-10-13 ### Fixed diff --git a/Cargo.lock b/Cargo.lock index 9c30515c..41e548d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1231,7 +1231,7 @@ dependencies = [ [[package]] name = "eggsearch" -version = "0.4.3" +version = "0.4.4" dependencies = [ "anyhow", "axum", diff --git a/Cargo.toml b/Cargo.toml index 5dfe8274..aea2082b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "eggsearch" -version = "0.4.3" +version = "0.4.4" edition = "2021" rust-version = "1.89" authors = ["eggstack"] diff --git a/architecture/local-workspace.md b/architecture/local-workspace.md index afcd5168..37c300dc 100644 --- a/architecture/local-workspace.md +++ b/architecture/local-workspace.md @@ -7,6 +7,18 @@ ## Overview +CodeGG-managed Eggsearch 0.4.4+ can receive an authenticated private request +scope over stdio. For each repository tool call, CodeGG supplies one +authorized workspace root or an explicit `root: null` local-disabled scope. +Eggsearch authenticates that metadata with the per-process +`CODEGG_EGGSEARCH_HOST_SCOPE_TOKEN`; missing or invalid authentication is an +error and never falls back to configured roots. The selected backend and +inventory cache are isolated by canonical root and full indexing configuration +in a bounded eight-entry in-memory LRU. Standalone calls with no host metadata +continue to use `[local].roots`. The metadata is not part of a model-facing +tool schema. This private extension is for a trusted locally spawned CodeGG +child only; it does not establish authority for HTTP or remote MCP clients. + The local workspace backend searches operator-configured filesystem roots without cloning or indexing services. Each call ensures a cached inventory, selects a bounded deterministic candidate set, reads files through @@ -215,6 +227,14 @@ capped `top_symbols`, heuristic `test_relationships`, CI/Dockerfile/Makefile `build_configs`, and `structure_truncated` on any cap breach. Caps come from the `[local]` budgets; the scan never executes workspace code. +Local `repo_fetch` validates the requested path, then reads it with +`safe_read_file()` relative to the selected workspace root; it never reopens +the canonicalized pathname. Local `repo_map` structure enrichment uses the +same bounded opener for manifest, source, and test contents. Its structural +walk skips symlink entries even when local search permits following symlinks, +so map enrichment does not enumerate or read a linked tree outside the +workspace. + --- ## Cache Identity and Local Gating diff --git a/bindings/python/Cargo.lock b/bindings/python/Cargo.lock index 5c053a0d..fc205d20 100644 --- a/bindings/python/Cargo.lock +++ b/bindings/python/Cargo.lock @@ -581,7 +581,7 @@ dependencies = [ [[package]] name = "eggsearch" -version = "0.4.3" +version = "0.4.4" dependencies = [ "anyhow", "axum", @@ -620,7 +620,7 @@ dependencies = [ [[package]] name = "eggsearch-python" -version = "0.4.3" +version = "0.4.4" dependencies = [ "eggsearch", "futures", diff --git a/bindings/python/Cargo.toml b/bindings/python/Cargo.toml index f4dce7c2..b73eed4c 100644 --- a/bindings/python/Cargo.toml +++ b/bindings/python/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "eggsearch-python" -version = "0.4.3" +version = "0.4.4" edition = "2021" rust-version = "1.89" publish = false @@ -13,7 +13,7 @@ name = "_native" crate-type = ["cdylib"] [dependencies] -eggsearch = { path = "../..", version = "=0.4.3" } +eggsearch = { path = "../..", version = "=0.4.4" } futures = "0.3" pyo3 = { version = "=0.29.3", features = ["abi3-py311", "extension-module", "macros"] } pyo3-async-runtimes = { version = "=0.29.0", features = ["tokio-runtime"] } diff --git a/bindings/python/pyproject.toml b/bindings/python/pyproject.toml index 8f412716..ab370051 100644 --- a/bindings/python/pyproject.toml +++ b/bindings/python/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "maturin" [project] name = "eggsearch" -version = "0.4.3" +version = "0.4.4" description = "Native asyncio bindings for the Eggsearch evidence engine" requires-python = ">=3.11,<3.15" license = "MIT" diff --git a/docs/codegg-integration.md b/docs/codegg-integration.md index 8a99b5c1..5ed7d053 100644 --- a/docs/codegg-integration.md +++ b/docs/codegg-integration.md @@ -1081,6 +1081,17 @@ Step 4: Bundle with trust markers from both local and remote ## Trust Boundary Rules +CodeGG 0.4.4 and newer use a private stdio extension for local repository +retrieval. CodeGG starts one child with a fresh per-process +`CODEGG_EGGSEARCH_HOST_SCOPE_TOKEN`, then places an authenticated workspace +scope in MCP request metadata for each `repo_search`, `repo_fetch`, and +`repo_map` call. A null root means local retrieval is disabled for that call. +Eggsearch rejects malformed or unauthenticated scope metadata and does not +fall back to its startup roots. Do not expose the scope key through a model +tool schema or reuse this extension over an untrusted HTTP/MCP connection. +Standalone Eggsearch requests that carry no host scope keep their configured +local-root behavior. + ### Trust Levels | Level | Meaning | Source | diff --git a/src/mcp/server.rs b/src/mcp/server.rs index 31a07c21..fb38a3b9 100644 --- a/src/mcp/server.rs +++ b/src/mcp/server.rs @@ -8,15 +8,16 @@ use rmcp::model::{ CallToolResult, Implementation, InitializeResult, ListToolsResult, PaginatedRequestParams, ServerCapabilities, ServerInfo, }; -use rmcp::{tool, tool_handler, tool_router, ErrorData as McpError, ServerHandler}; +use rmcp::service::RequestContext; +use rmcp::{tool, tool_handler, tool_router, ErrorData as McpError, RoleServer, ServerHandler}; use crate::mcp::state::ServerState; use crate::mcp::tools::{ map_tool_result, run_batch_fetch, run_build_evidence_bundle, run_provider_status_async, - run_repo_fetch, run_repo_map, run_repo_search, run_research_search, run_security_search, - run_web_fetch, run_web_search, BatchFetchArgs, EvidenceBundleArgs, ProviderStatusArgs, - RepoFetchArgs, RepoMapArgs, RepoSearchArgs, ResearchSearchArgs, SecuritySearchArgs, - WebFetchArgs, WebSearchArgs, + run_repo_fetch_with_host_scope, run_repo_map_with_host_scope, run_repo_search_with_host_scope, + run_research_search, run_security_search, run_web_fetch, run_web_search, BatchFetchArgs, + EvidenceBundleArgs, ProviderStatusArgs, RepoFetchArgs, RepoMapArgs, RepoSearchArgs, + ResearchSearchArgs, SecuritySearchArgs, WebFetchArgs, WebSearchArgs, }; #[derive(Clone)] @@ -95,10 +96,12 @@ impl EggsearchServer { )] async fn repo_search( &self, + context: RequestContext, Parameters(args): Parameters, ) -> Result { let state = self.state.clone(); - map_tool_result(run_repo_search(state, args).await) + let host_scope = context.meta.0 .0.get("codegg/host-local-scope").cloned(); + map_tool_result(run_repo_search_with_host_scope(state, args, host_scope).await) } #[tool( @@ -121,10 +124,12 @@ impl EggsearchServer { )] async fn repo_fetch( &self, + context: RequestContext, Parameters(args): Parameters, ) -> Result { let state = self.state.clone(); - map_tool_result(run_repo_fetch(state, args).await) + let host_scope = context.meta.0 .0.get("codegg/host-local-scope").cloned(); + map_tool_result(run_repo_fetch_with_host_scope(state, args, host_scope).await) } #[tool( @@ -173,10 +178,12 @@ impl EggsearchServer { )] async fn repo_map( &self, + context: RequestContext, Parameters(args): Parameters, ) -> Result { let state = self.state.clone(); - map_tool_result(run_repo_map(state, args).await) + let host_scope = context.meta.0 .0.get("codegg/host-local-scope").cloned(); + map_tool_result(run_repo_map_with_host_scope(state, args, host_scope).await) } #[tool( diff --git a/src/mcp/state.rs b/src/mcp/state.rs index 11746b1b..6a35967c 100644 --- a/src/mcp/state.rs +++ b/src/mcp/state.rs @@ -1,8 +1,11 @@ //! Server state: shared state passed to every tool call. +use std::num::NonZeroUsize; +use std::path::PathBuf; use std::sync::{Arc, Mutex}; use std::time::{Duration, Instant}; +use serde::Deserialize; use tracing; use crate::core::config::AppConfig; @@ -30,6 +33,120 @@ pub struct LocalInventoryCache { pub fetched_at: Instant, } +/// Host-injected scope carried only on a CodeGG-managed local MCP process. +/// The request field is hidden from the generated tool schema; this token +/// authenticates the private transport instead of treating that omission as +/// an authorization boundary. +#[derive(Clone, Deserialize, schemars::JsonSchema)] +pub struct HostLocalScope { + pub root: Option, + pub token: String, +} + +impl std::fmt::Debug for HostLocalScope { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("HostLocalScope") + .field("root", &self.root) + .field("token", &"") + .finish() + } +} + +const HOST_LOCAL_SCOPE_TOKEN_ENV: &str = "CODEGG_EGGSEARCH_HOST_SCOPE_TOKEN"; +const HOST_LOCAL_BACKEND_CACHE_CAPACITY: usize = 8; + +/// Request-local view over a local backend and its root-specific inventory +/// cache. Standalone requests reuse the startup-configured view; authenticated +/// host-scoped requests use an isolated bounded-cache entry. +#[derive(Clone)] +pub struct LocalWorkspaceView { + backend: Option>, + inventory_cache: Arc>>, +} + +impl LocalWorkspaceView { + pub fn backend(&self) -> Option<&LocalWorkspaceBackend> { + self.backend.as_deref() + } + + pub fn is_enabled(&self) -> bool { + self.backend() + .is_some_and(LocalWorkspaceBackend::is_enabled) + } + + pub async fn inventory(&self) -> Vec { + { + if let Ok(cache) = self.inventory_cache.lock() { + if let Some(snapshot) = cache.as_ref() { + if snapshot.fetched_at.elapsed() < LOCAL_INVENTORY_CACHE_TTL { + return snapshot.inventory.clone(); + } + } + } + } + + let backend = match self.backend.as_deref() { + Some(backend) if backend.is_enabled() => backend, + _ => return Vec::new(), + }; + let roots = backend.roots(); + let mut local_config = backend.config().clone(); + local_config.enabled = true; + let roots_for_walk: Vec = roots.iter().map(|(_, path)| path.clone()).collect(); + let inventory = match tokio::task::spawn_blocking(move || { + let mut config = local_config; + config.roots = roots_for_walk; + discover_local_repos(&config, 2) + }) + .await + { + Ok(inventory) => inventory, + Err(error) => { + tracing::error!(%error, "local repository inventory task failed"); + return Vec::new(); + } + }; + if let Ok(mut cache) = self.inventory_cache.lock() { + *cache = Some(LocalInventoryCache { + inventory: inventory.clone(), + fetched_at: Instant::now(), + }); + } + inventory + } +} + +#[derive(Default)] +struct HostScopedLocalBackendCache { + entries: Option>>, +} + +impl HostScopedLocalBackendCache { + fn get_or_insert( + &mut self, + key: String, + config: crate::core::local::LocalConfig, + ) -> Result, String> { + let entries = self.entries.get_or_insert_with(|| { + lru::LruCache::new( + NonZeroUsize::new(HOST_LOCAL_BACKEND_CACHE_CAPACITY) + .expect("non-zero local backend cache capacity"), + ) + }); + if let Some(view) = entries.get(&key) { + return Ok(view.clone()); + } + let backend = LocalWorkspaceBackend::new(config) + .map_err(|error| format!("host-scoped local workspace is unavailable: {error}"))?; + let view = Arc::new(LocalWorkspaceView { + backend: Some(Arc::new(backend)), + inventory_cache: Arc::new(Mutex::new(None)), + }); + entries.put(key, view.clone()); + Ok(view) + } +} + /// Shared state for the MCP server. Cheap to clone (all fields are Arc). #[derive(Clone)] pub struct ServerState { @@ -54,6 +171,9 @@ pub struct ServerState { /// call. Public so tests can construct `ServerState` instances /// with custom local-backend configurations. pub local_inventory_cache: Arc>>, + /// Bounded cache of host-authorized one-root local backend views. This + /// contains indexes only; host authorization is checked for every call. + host_scoped_local_backends: Arc>, /// Browser profile manager. `None` when browser feature is not /// compiled in or when persistent profiles are disabled. #[cfg(feature = "browser")] @@ -316,6 +436,9 @@ impl ServerState { kev_client, local_backend, local_inventory_cache: Arc::new(Mutex::new(None)), + host_scoped_local_backends: Arc::new( + Mutex::new(HostScopedLocalBackendCache::default()), + ), #[cfg(feature = "browser")] profile_manager, #[cfg(feature = "browser")] @@ -382,6 +505,9 @@ impl ServerState { kev_client, local_backend: None, local_inventory_cache: Arc::new(Mutex::new(None)), + host_scoped_local_backends: Arc::new( + Mutex::new(HostScopedLocalBackendCache::default()), + ), #[cfg(feature = "browser")] profile_manager: None, #[cfg(feature = "browser")] @@ -423,49 +549,64 @@ impl ServerState { /// task to keep the async runtime responsive; the cache keeps the /// cost bounded across repeated tool calls. pub async fn local_inventory(&self) -> Vec { - { - if let Ok(cache) = self.local_inventory_cache.lock() { - if let Some(snapshot) = cache.as_ref() { - if snapshot.fetched_at.elapsed() < LOCAL_INVENTORY_CACHE_TTL { - return snapshot.inventory.clone(); - } - } - } + match self.local_workspace_view(None) { + Ok(view) => view.inventory().await, + Err(_) => Vec::new(), } + } - let backend = match self.local_backend.as_deref() { - Some(b) if b.is_enabled() => b, - _ => return Vec::new(), + /// Resolve an authenticated request-local local-workspace view. `None` + /// preserves standalone Eggsearch's startup-configured roots. A present + /// but invalid host scope fails closed and never falls back to those roots. + pub fn local_workspace_view( + &self, + host_scope: Option<&serde_json::Value>, + ) -> Result, String> { + let Some(host_scope_value) = host_scope else { + return Ok(Arc::new(LocalWorkspaceView { + backend: self.local_backend.clone(), + inventory_cache: self.local_inventory_cache.clone(), + })); }; - let roots = backend.roots(); - let mut local_config = backend.config().clone(); - local_config.enabled = true; - let roots_for_walk: Vec = - roots.iter().map(|(_, p)| p.clone()).collect(); + let host_scope: HostLocalScope = serde_json::from_value(host_scope_value.clone()) + .map_err(|_| "host-scoped local retrieval metadata is malformed".to_string())?; - let inventory = match tokio::task::spawn_blocking(move || { - let mut cfg = local_config; - cfg.roots = roots_for_walk; - discover_local_repos(&cfg, 2) - }) - .await - { - Ok(inventory) => inventory, - Err(error) => { - tracing::error!(%error, "local repository inventory task failed"); - return Vec::new(); - } + let expected = std::env::var(HOST_LOCAL_SCOPE_TOKEN_ENV).map_err(|_| { + "host-scoped local retrieval is unavailable on this Eggsearch process".to_string() + })?; + if !constant_time_eq(expected.as_bytes(), host_scope.token.as_bytes()) { + return Err("host-scoped local retrieval request authentication failed".to_string()); + } + let Some(root) = host_scope.root.as_ref() else { + return Ok(Arc::new(LocalWorkspaceView { + backend: None, + inventory_cache: Arc::new(Mutex::new(None)), + })); }; - - if let Ok(mut cache) = self.local_inventory_cache.lock() { - *cache = Some(LocalInventoryCache { - inventory: inventory.clone(), - fetched_at: Instant::now(), - }); + if !self.config.local.enabled { + return Ok(Arc::new(LocalWorkspaceView { + backend: None, + inventory_cache: Arc::new(Mutex::new(None)), + })); } - - inventory + let canonical_root = root + .canonicalize() + .map_err(|error| format!("host-scoped local workspace root is unavailable: {error}"))?; + if !canonical_root.is_dir() { + return Err("host-scoped local workspace root is not a directory".to_string()); + } + let mut config = self.config.local.clone(); + config.enabled = true; + config.roots = vec![canonical_root]; + let fingerprint = serde_json::to_vec(&config) + .map_err(|error| format!("cannot fingerprint local workspace config: {error}"))?; + use sha2::{Digest, Sha256}; + let key = format!("{:x}", Sha256::digest(fingerprint)); + self.host_scoped_local_backends + .lock() + .map_err(|_| "host-scoped local backend cache is unavailable".to_string())? + .get_or_insert(key, config) } /// Invalidate the cached local inventory. Useful for tests and for @@ -477,6 +618,18 @@ impl ServerState { } } +fn constant_time_eq(left: &[u8], right: &[u8]) -> bool { + let max_len = left.len().max(right.len()); + let mut difference = left.len() ^ right.len(); + for index in 0..max_len { + difference |= usize::from( + left.get(index).copied().unwrap_or_default() + ^ right.get(index).copied().unwrap_or_default(), + ); + } + difference == 0 +} + #[cfg(test)] mod tests { use super::*; @@ -505,4 +658,71 @@ mod tests { .iter() .any(|id| id == "brave_api")); } + + #[test] + fn host_scopes_authenticate_and_cache_distinct_roots() { + let first = tempfile::tempdir().expect("first root"); + let second = tempfile::tempdir().expect("second root"); + let token = format!("test-{}", std::process::id()); + let previous_token = std::env::var_os(HOST_LOCAL_SCOPE_TOKEN_ENV); + std::env::set_var(HOST_LOCAL_SCOPE_TOKEN_ENV, &token); + let mut config = AppConfig::default(); + config.local.enabled = true; + config.local.roots = vec![first.path().to_path_buf()]; + let state = ServerState::build(config).expect("state builds"); + + let scope = + |root: &std::path::Path, token: &str| serde_json::json!({"root": root, "token": token}); + let first_view = state + .local_workspace_view(Some(&scope(first.path(), &token))) + .expect("authorized first root"); + let second_view = state + .local_workspace_view(Some(&scope(second.path(), &token))) + .expect("authorized second root"); + let first_warm = state + .local_workspace_view(Some(&scope(first.path(), &token))) + .expect("warm authorized first root"); + assert_eq!(first_view.backend().unwrap().roots()[0].1, first.path()); + assert_eq!(second_view.backend().unwrap().roots()[0].1, second.path()); + assert!(!Arc::ptr_eq(&first_view, &second_view)); + assert!(Arc::ptr_eq(&first_view, &first_warm)); + + let disabled = state + .local_workspace_view(Some(&serde_json::json!({"root": null, "token": token}))) + .expect("authenticated explicit disabled scope"); + assert!( + !disabled.is_enabled(), + "null scope must not fall back to configured roots" + ); + + let forged = state.local_workspace_view(Some(&scope(second.path(), "forged"))); + assert!(forged.is_err()); + let malformed = + state.local_workspace_view(Some(&serde_json::json!({"root": second.path()}))); + assert!(malformed.is_err()); + std::fs::remove_dir_all(second.path()).expect("remove host-scoped workspace"); + assert!( + state + .local_workspace_view(Some(&scope(second.path(), &token))) + .is_err(), + "removed workspace must fail rather than use a cached or static root" + ); + std::env::remove_var(HOST_LOCAL_SCOPE_TOKEN_ENV); + assert!( + state + .local_workspace_view(Some(&scope(first.path(), &token))) + .is_err(), + "missing process token must reject host metadata" + ); + if let Some(previous_token) = previous_token { + std::env::set_var(HOST_LOCAL_SCOPE_TOKEN_ENV, previous_token); + } + } + + #[test] + fn host_scope_token_comparison_checks_length_and_contents() { + assert!(constant_time_eq(b"same-token", b"same-token")); + assert!(!constant_time_eq(b"same-token", b"same-token-x")); + assert!(!constant_time_eq(b"same-token", b"other-token")); + } } diff --git a/src/mcp/tools/mod.rs b/src/mcp/tools/mod.rs index 5edb0d8d..06d10c25 100644 --- a/src/mcp/tools/mod.rs +++ b/src/mcp/tools/mod.rs @@ -21,9 +21,9 @@ pub use batch_fetch::{run_batch_fetch, BatchFetchArgs}; pub use common::{map_tool_result, RepairHint, ToolError, ToolErrorCode, ToolErrorExecution}; pub use evidence_bundle::{run_build_evidence_bundle, EvidenceBundleArgs}; pub use provider_status::{run_provider_status, run_provider_status_async, ProviderStatusArgs}; -pub use repo_fetch::{run_repo_fetch, RepoFetchArgs}; -pub use repo_map::{run_repo_map, RepoMapArgs}; -pub use repo_search::{run_repo_search, RepoSearchArgs}; +pub use repo_fetch::{run_repo_fetch, run_repo_fetch_with_host_scope, RepoFetchArgs}; +pub use repo_map::{run_repo_map, run_repo_map_with_host_scope, RepoMapArgs}; +pub use repo_search::{run_repo_search, run_repo_search_with_host_scope, RepoSearchArgs}; pub use research_search::{run_research_search, ResearchSearchArgs}; pub use security_search::{run_security_search, SecuritySearchArgs}; pub use web_fetch::{run_web_fetch, WebFetchArgs}; diff --git a/src/mcp/tools/repo_fetch.rs b/src/mcp/tools/repo_fetch.rs index 90f975f9..1cd9b427 100644 --- a/src/mcp/tools/repo_fetch.rs +++ b/src/mcp/tools/repo_fetch.rs @@ -92,6 +92,14 @@ pub struct RepoFetchArgs { pub async fn run_repo_fetch( state: Arc, args: RepoFetchArgs, +) -> Result { + run_repo_fetch_with_host_scope(state, args, None).await +} + +pub async fn run_repo_fetch_with_host_scope( + state: Arc, + args: RepoFetchArgs, + host_local_scope: Option, ) -> Result { use crate::core::code_evidence::infer_source_role; use crate::core::code_metadata::CodeHost; @@ -103,20 +111,29 @@ pub async fn run_repo_fetch( RepoFetchRequest, RepoFetchResponse, RepoLocator, }; + let local_view = state + .local_workspace_view(host_local_scope.as_ref()) + .map_err(ToolError::Validation)?; + // --- workspace:// local file fetch (bypasses fetch policy) --- if let Some(ref h) = args.host { if h.to_lowercase() == "workspace" { let detail = crate::mcp::projection::ResponseDetail::from_opt(args.response_detail); - let value = run_workspace_fetch(state, args).await?; + let value = run_workspace_fetch(state, args, local_view).await?; return Ok(crate::mcp::projection::project("repo_fetch", value, detail)); } } // --- prefer_local: resolve to local workspace when enabled --- if args.prefer_local.unwrap_or(false) { - if let Some(backend) = state.local_backend.as_deref() { + if host_local_scope.is_some() && !local_view.is_enabled() { + return Err(ToolError::Validation( + "local repository fetch was requested, but the authorized host scope has no local workspace".to_string(), + )); + } + if let Some(backend) = local_view.backend() { if backend.is_enabled() { - let inventory = state.local_inventory().await; + let inventory = local_view.inventory().await; let parsed_host = parse_code_host_arg(args.host.as_deref())?; let matched = crate::meta::local_inventory::match_local_repo( &inventory, @@ -150,9 +167,14 @@ pub async fn run_repo_fetch( prefer_local: None, response_detail: None, }; - let value = run_workspace_fetch(state, ws_args).await?; + let value = run_workspace_fetch(state, ws_args, local_view).await?; return Ok(crate::mcp::projection::project("repo_fetch", value, detail)); } + if host_local_scope.is_some() { + return Err(ToolError::Validation( + "local repository fetch was requested, but no matching repository exists in the authorized workspace".to_string(), + )); + } } } } @@ -598,6 +620,7 @@ pub async fn run_repo_fetch( async fn run_workspace_fetch( state: Arc, args: RepoFetchArgs, + local_view: Arc, ) -> Result { use crate::core::code_evidence::infer_source_role; use crate::core::local::validate_local_fetch_path; @@ -606,7 +629,7 @@ async fn run_workspace_fetch( }; use crate::core::sanitize::TrustMarkers; - let backend = state.local_backend.as_ref().ok_or_else(|| { + let backend = local_view.backend().ok_or_else(|| { ToolError::Validation("local workspace search is not enabled".to_string()) })?; @@ -669,14 +692,27 @@ async fn run_workspace_fetch( })?; // Use centralized path validation for traversal, binary, symlink, and containment checks - let canonical = validate_local_fetch_path(root_path, &relative_path, backend.config()) + validate_local_fetch_path(root_path, &relative_path, backend.config()) .map_err(|e| ToolError::Validation(e.to_string()))?; - // Read file content (off the runtime thread) - let content = tokio::task::spawn_blocking(move || std::fs::read_to_string(&canonical)) - .await - .map_err(|e| ToolError::internal(format!("failed to join read task: {e}")))? - .map_err(|e| ToolError::internal(format!("failed to read file: {e}")))?; + let root_path = root_path.clone(); + let relative_path_for_read = relative_path.clone(); + let local_config = backend.config().clone(); + let max_file_bytes = local_config.max_file_bytes; + let content = tokio::task::spawn_blocking(move || { + let bytes = crate::meta::safe_open::safe_read_file( + &root_path, + &relative_path_for_read, + &local_config, + max_file_bytes, + ) + .map_err(|error| std::io::Error::other(error.to_string()))?; + String::from_utf8(bytes) + .map_err(|error| std::io::Error::new(std::io::ErrorKind::InvalidData, error)) + }) + .await + .map_err(|e| ToolError::internal(format!("failed to join read task: {e}")))? + .map_err(|e| ToolError::internal(format!("failed to read file: {e}")))?; let all_lines: Vec = content.lines().map(String::from).collect(); let total_lines = if all_lines.is_empty() { diff --git a/src/mcp/tools/repo_map.rs b/src/mcp/tools/repo_map.rs index e5d7733f..adcaf1bf 100644 --- a/src/mcp/tools/repo_map.rs +++ b/src/mcp/tools/repo_map.rs @@ -94,15 +94,27 @@ fn build_forge_tree_config( pub async fn run_repo_map( state: Arc, args: RepoMapArgs, +) -> Result { + run_repo_map_with_host_scope(state, args, None).await +} + +pub async fn run_repo_map_with_host_scope( + state: Arc, + args: RepoMapArgs, + host_local_scope: Option, ) -> Result { use crate::core::code_metadata::CodeHost; use crate::core::repo_map::RepoMapRequest; + let local_view = state + .local_workspace_view(host_local_scope.as_ref()) + .map_err(ToolError::Validation)?; + // Permit a local-only path when the local backend is enabled, even in // off mode, so air-gapped operators can inspect a configured local // checkout without enabling live metasearch. - let local_only_path = matches!(live_allowed(state.config.search.mode), Policy::Deny) - && state.local_backend.is_some(); + let local_only_path = + matches!(live_allowed(state.config.search.mode), Policy::Deny) && local_view.is_enabled(); if matches!(live_allowed(state.config.search.mode), Policy::Deny) && !local_only_path { return Err(ToolError::Validation(live_search_denied_message( @@ -234,9 +246,9 @@ pub async fn run_repo_map( // Discover local checkout for the requested repo let mut local_checkout_root: Option = None; - if let Some(backend) = state.local_backend.as_deref() { + if let Some(backend) = local_view.backend() { if backend.is_enabled() { - let inventory = state.local_inventory().await; + let inventory = local_view.inventory().await; let matched = crate::meta::local_inventory::match_local_repo( &inventory, req.host.as_ref(), @@ -296,7 +308,10 @@ pub async fn run_repo_map( crate::meta::repo_mapper::populate_structure_from_local_checkout( &mut response, root, - &state.config.local, + local_view + .backend() + .map(|backend| backend.config()) + .unwrap_or(&state.config.local), ); } diff --git a/src/mcp/tools/repo_search.rs b/src/mcp/tools/repo_search.rs index d96ec23b..4e96823e 100644 --- a/src/mcp/tools/repo_search.rs +++ b/src/mcp/tools/repo_search.rs @@ -120,16 +120,24 @@ pub struct RepoSearchArgs { pub async fn run_repo_search( state: Arc, args: RepoSearchArgs, +) -> Result { + run_repo_search_with_host_scope(state, args, None).await +} + +pub async fn run_repo_search_with_host_scope( + state: Arc, + args: RepoSearchArgs, + host_local_scope: Option, ) -> Result { use crate::core::repo_search::RepoSearchRequest; + let local_view = state + .local_workspace_view(host_local_scope.as_ref()) + .map_err(ToolError::Validation)?; + let remote_denied = matches!(live_allowed(state.config.search.mode), Policy::Deny); - let local_only_path = remote_denied - && state - .local_backend - .as_ref() - .is_some_and(|backend| backend.is_enabled()) - && args.include_local != Some(false); + let local_only_path = + remote_denied && local_view.is_enabled() && args.include_local != Some(false); if remote_denied && !local_only_path { return Err(ToolError::Validation(live_search_denied_message( @@ -324,14 +332,19 @@ pub async fn run_repo_search( req.providers = effective_providers; } - let local_inventory = state.local_inventory().await; + if remote_denied && !local_view.is_enabled() { + return Err(ToolError::Validation( + "local workspace retrieval is required by the active local-only policy, but no authorized local scope is available".to_string(), + )); + } + let local_inventory = local_view.inventory().await; let mut response = state .adapter .repo_search_with_remote_execution( &req, effective_max, state.config.search.max_results_cap, - state.local_backend.as_deref(), + local_view.backend(), Some(&local_inventory), if local_only_path { crate::meta::adapter::RepoRemoteExecution::Denied diff --git a/src/meta/repo_mapper.rs b/src/meta/repo_mapper.rs index 5b0d609d..cbd2cd36 100644 --- a/src/meta/repo_mapper.rs +++ b/src/meta/repo_mapper.rs @@ -534,11 +534,23 @@ fn manifest_ecosystem(file_name: &str) -> Option<(&'static str, &'static str)> { } } -fn parse_package_name(root: &Path, relative: &str, ecosystem: &str) -> Option { - let bytes = std::fs::read(root.join(relative)).ok()?; - if bytes.len() > STRUCTURE_MANIFEST_READ_CAP { - return None; - } +fn read_structure_file( + root: &Path, + relative: &str, + config: &LocalConfig, + cap: usize, +) -> Option> { + crate::meta::safe_open::safe_read_file(root, relative, config, cap.min(config.max_file_bytes)) + .ok() +} + +fn parse_package_name( + root: &Path, + relative: &str, + ecosystem: &str, + config: &LocalConfig, +) -> Option { + let bytes = read_structure_file(root, relative, config, STRUCTURE_MANIFEST_READ_CAP)?; let text = String::from_utf8_lossy(&bytes); match ecosystem { "rust" => { @@ -644,14 +656,15 @@ pub fn build_local_structure(root: &Path, config: &LocalConfig) -> LocalStructur continue; } let path = entry.path(); - if !config.follow_symlinks { - if let Ok(meta) = std::fs::symlink_metadata(&path) { - if meta.file_type().is_symlink() { - continue; - } - } + let metadata = match std::fs::symlink_metadata(&path) { + Ok(metadata) => metadata, + Err(_) => continue, + }; + let file_type = metadata.file_type(); + if file_type.is_symlink() { + continue; } - if path.is_dir() { + if file_type.is_dir() { if depth < STRUCTURE_SCAN_DEPTH { stack.push((path, depth + 1)); } else { @@ -659,7 +672,7 @@ pub fn build_local_structure(root: &Path, config: &LocalConfig) -> LocalStructur } continue; } - if !path.is_file() { + if !file_type.is_file() { continue; } let relative = match path.strip_prefix(root) { @@ -669,7 +682,7 @@ pub fn build_local_structure(root: &Path, config: &LocalConfig) -> LocalStructur if is_binary_extension(&name) { continue; } - let size = std::fs::metadata(&path).map(|m| m.len()).unwrap_or(0); + let size = metadata.len(); if size > config.max_file_bytes as u64 { continue; } @@ -701,7 +714,7 @@ pub fn build_local_structure(root: &Path, config: &LocalConfig) -> LocalStructur let file_name = file.relative.rsplit('/').next().unwrap_or(&file.relative); if manifest_ecosystem(file_name).is_some() { let (ecosystem, _) = manifest_ecosystem(file_name).unwrap_or(("other", "other")); - let name = parse_package_name(root, &file.relative, ecosystem); + let name = parse_package_name(root, &file.relative, ecosystem, config); packages.push(RepoPackageSummary { path: file.relative.clone(), ecosystem: ecosystem.to_string(), @@ -789,9 +802,10 @@ pub fn build_local_structure(root: &Path, config: &LocalConfig) -> LocalStructur continue; } parsed += 1; - let bytes = match std::fs::read(root.join(&file.relative)) { - Ok(b) => b, - Err(_) => continue, + let bytes = match read_structure_file(root, &file.relative, config, config.max_parse_bytes) + { + Some(bytes) => bytes, + None => continue, }; if bytes.len() > config.max_parse_bytes { truncated = true; @@ -897,13 +911,13 @@ pub fn build_local_structure(root: &Path, config: &LocalConfig) -> LocalStructur .collect(); let mut test_texts: HashMap = HashMap::new(); for test_path in &test_files { - if let Ok(bytes) = std::fs::read(root.join(test_path)) { - if bytes.len() <= STRUCTURE_MANIFEST_READ_CAP { - test_texts.insert( - test_path.clone(), - String::from_utf8_lossy(&bytes).to_string(), - ); - } + if let Some(bytes) = + read_structure_file(root, test_path, config, STRUCTURE_MANIFEST_READ_CAP) + { + test_texts.insert( + test_path.clone(), + String::from_utf8_lossy(&bytes).to_string(), + ); } } let symbol_names: HashMap> = @@ -1235,4 +1249,59 @@ mod tests { let parsed: RepoMapSearchSubquery = serde_json::from_str(&json).unwrap(); assert_eq!(parsed, sub); } + + #[cfg(unix)] + #[test] + fn local_structure_skips_symlinked_files_and_directories() { + let root = tempfile::tempdir().unwrap(); + let outside = tempfile::tempdir().unwrap(); + std::fs::create_dir_all(outside.path().join("src")).unwrap(); + std::fs::write( + outside.path().join("Cargo.toml"), + "[package]\nname = \"outside-secret\"\nversion = \"0.1.0\"\n", + ) + .unwrap(); + std::fs::write( + outside.path().join("src/lib.rs"), + "pub fn outside_secret_symbol() {}\n", + ) + .unwrap(); + std::os::unix::fs::symlink( + outside.path().join("Cargo.toml"), + root.path().join("Cargo.toml"), + ) + .unwrap(); + std::os::unix::fs::symlink(outside.path().join("src"), root.path().join("src")).unwrap(); + + let config = LocalConfig { + enabled: true, + follow_symlinks: true, + ..LocalConfig::default() + }; + let structure = build_local_structure(root.path(), &config); + + assert!(structure.packages.is_empty()); + assert!(structure.top_symbols.is_empty()); + assert!(structure.test_relationships.is_empty()); + } + + #[cfg(unix)] + #[test] + fn structure_file_reader_rejects_symlink_escape_even_when_following_is_enabled() { + let root = tempfile::tempdir().unwrap(); + let outside = tempfile::tempdir().unwrap(); + std::fs::write(outside.path().join("secret.rs"), "secret contents").unwrap(); + std::os::unix::fs::symlink( + outside.path().join("secret.rs"), + root.path().join("link.rs"), + ) + .unwrap(); + + let config = LocalConfig { + enabled: true, + follow_symlinks: true, + ..LocalConfig::default() + }; + assert!(read_structure_file(root.path(), "link.rs", &config, 1024).is_none()); + } } diff --git a/tests/security_workflow.rs b/tests/security_workflow.rs index 02ff5e2c..6f54a2d5 100644 --- a/tests/security_workflow.rs +++ b/tests/security_workflow.rs @@ -801,30 +801,15 @@ mod security_context_safety { }; let backend = LocalWorkspaceBackend::new(cfg.local.clone()).expect("backend"); - let state = Arc::new(ServerState { - config: Arc::new(cfg), - adapter: Arc::new(MetadataSearchAdapter::from_engines( + let mut state = ServerState::with_adapter( + cfg, + Arc::new(MetadataSearchAdapter::from_engines( mock_engines(vec![]), Duration::from_secs(5), )), - fetch_client: None, - origin_controller: None, - fetch_cache: None, - kev_client: Arc::new(eggsearch::meta::engines::kev::KevClient::new( - eggfetch_core::Client::new(), - )), - local_backend: Some(Arc::new(backend)), - local_inventory_cache: Arc::new(std::sync::Mutex::new(None)), - #[cfg(feature = "browser")] - profile_manager: None, - #[cfg(feature = "browser")] - browser_lifecycle: None, - #[cfg(feature = "browser")] - browser_global_semaphore: None, - #[cfg(feature = "browser")] - browser_discovery_state: - eggsearch::fetch::browser::types::BrowserDiscoveryState::NotFound, - }); + ); + state.local_backend = Some(Arc::new(backend)); + let state = Arc::new(state); let first = state.local_inventory().await; let second = state.local_inventory().await; @@ -866,30 +851,15 @@ mod security_context_safety { }; let backend = LocalWorkspaceBackend::new(cfg.local.clone()).expect("backend"); - let state = Arc::new(ServerState { - config: Arc::new(cfg), - adapter: Arc::new(MetadataSearchAdapter::from_engines( + let mut state = ServerState::with_adapter( + cfg, + Arc::new(MetadataSearchAdapter::from_engines( mock_engines(vec![]), Duration::from_secs(5), )), - fetch_client: None, - origin_controller: None, - fetch_cache: None, - kev_client: Arc::new(eggsearch::meta::engines::kev::KevClient::new( - eggfetch_core::Client::new(), - )), - local_backend: Some(Arc::new(backend)), - local_inventory_cache: Arc::new(std::sync::Mutex::new(None)), - #[cfg(feature = "browser")] - profile_manager: None, - #[cfg(feature = "browser")] - browser_lifecycle: None, - #[cfg(feature = "browser")] - browser_global_semaphore: None, - #[cfg(feature = "browser")] - browser_discovery_state: - eggsearch::fetch::browser::types::BrowserDiscoveryState::NotFound, - }); + ); + state.local_backend = Some(Arc::new(backend)); + let state = Arc::new(state); let inventory = state.local_inventory().await; let names: Vec<&str> = inventory.iter().map(|r| r.root_name.as_str()).collect(); assert!(