Files
imeai-aKode/akode-rs/akode-mcp/src/rmcp_client_credential_policy_tests.rs
andrea.difonzo a005236bcb aKode: integrazione della memoria di imeAI (plugin memoria)
- nuovo crate ext/memoria: aggiunge le istruzioni del plugin (instructions.md)
  come developer policy solo se il plugin memoria è abilitato e il suo server
  MCP è pronto; nessuna modifica alle istruzioni di base, [features] memories
  resta spento
- pollutes_memory configurabile per server MCP (predefinito true)
- project_header per i server HTTP: progetto ricavato dalla cartella della
  sessione (origin come host/owner/repo, altrimenti la cartella), connessioni
  separate per progetto
- token Bearer dei server MCP anche dal portachiavi di aKode, solo sotto
  mcp:<server> (mai le chiavi dei provider dei modelli)
- tool namespace (MCP, multi-agente) appiattiti in <namespace>__<tool> per i
  server compatibili OpenAI che accettano solo function tool (llama.cpp,
  ik_llama.cpp, vLLM li scartavano); capacità del provider tool_namespaces
- prove: parsing e propagazione, intestazione del progetto, iniezione solo se
  attivo e connesso, appiattimento e mappatura delle chiamate, chiavi dei
  provider mai consegnate ai server MCP

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 17:55:21 +01:00

107 lines
3.9 KiB
Rust

//! Credential resolution must retain executor provenance when host fallback is considered.
use super::resolve_bearer_token;
use crate::server::McpCredentialPolicy;
use pretty_assertions::assert_eq;
#[test]
fn executor_credentials_reject_host_fallback_for_arbitrary_environment_names() {
for env_var in ["PATH", "MCP_UNPROTECTED_TEST_TOKEN"] {
let error = resolve_bearer_token("docs", Some(env_var), McpCredentialPolicy::ExecutorOnly)
.expect_err("executor credential names must never resolve on the host");
assert_eq!(
error.to_string(),
"MCP server 'docs' requires executor-side environment credential resolution; update the executor to a version that supports it (host fallback is disabled)",
);
}
}
#[test]
fn servers_without_environment_credentials_do_not_require_host_fallback() {
for policy in [
McpCredentialPolicy::ExecutorOnly,
McpCredentialPolicy::HostFallbackAllowed,
] {
assert_eq!(
resolve_bearer_token("docs", /*bearer_token_env_var*/ None, policy)
.expect("a server without an environment credential needs no resolution"),
None,
);
}
}
#[test]
fn configured_credentials_retain_host_environment_resolution() {
// PATH is nonsecret and read without changing the process environment.
let expected = match std::env::var("PATH") {
Ok(value) if value.is_empty() => {
Err("Environment variable PATH for MCP server 'docs' is empty".to_string())
}
Ok(value) => Ok(Some(value)),
Err(std::env::VarError::NotPresent) => {
Err("Environment variable PATH for MCP server 'docs' is not set".to_string())
}
Err(std::env::VarError::NotUnicode(_)) => Err(
"Environment variable PATH for MCP server 'docs' contains invalid Unicode".to_string(),
),
};
assert_eq!(
resolve_bearer_token(
"docs",
Some("PATH"),
McpCredentialPolicy::HostFallbackAllowed
)
.map_err(|error| error.to_string()),
expected,
);
}
/// In-memory key store for the fallback test (no key material in `Debug`).
struct MemoryKeyStore(std::collections::HashMap<String, String>);
impl std::fmt::Debug for MemoryKeyStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("MemoryKeyStore")
}
}
impl akode_model_provider_info::ProviderApiKeyStore for MemoryKeyStore {
fn load(&self, provider_id: &str) -> Result<Option<String>, String> {
Ok(self.0.get(provider_id).cloned())
}
fn save(&self, _: &str, _: &str) -> Result<akode_model_provider_info::ProviderApiKeyLocation, String> {
Err("read-only".to_string())
}
fn delete(&self, _: &str) -> Result<bool, String> {
Ok(false)
}
fn locate(&self, _: &str) -> Result<Option<akode_model_provider_info::ProviderApiKeyLocation>, String> {
Ok(None)
}
}
#[test]
fn key_store_fallback_never_hands_out_model_provider_keys() {
let store = MemoryKeyStore(
[
("openrouter".to_string(), "provider-secret".to_string()),
("mcp:memoria".to_string(), "memoria-token".to_string()),
]
.into_iter()
.collect(),
);
akode_model_provider_info::set_provider_api_key_store(std::sync::Arc::new(store));
let unset = "AKODE_TEST_SURELY_UNSET_MCP_TOKEN_VAR";
// A server that reuses a provider's name (e.g. from a plugin) must not get that provider's key.
let error = resolve_bearer_token("openrouter", Some(unset), McpCredentialPolicy::HostFallbackAllowed)
.expect_err("provider keys are not MCP tokens");
assert!(!error.to_string().contains("provider-secret"));
assert_eq!(
resolve_bearer_token("memoria", Some(unset), McpCredentialPolicy::HostFallbackAllowed)
.expect("token stored under mcp:memoria"),
Some("memoria-token".to_string()),
);
}