fix(config): protect custom provider files (#11438)
Signed-off-by: Jasper Hugo <jasper@spiral.xyz>
This commit is contained in:
@@ -7,6 +7,7 @@ use crate::providers::huggingface_auth;
|
||||
use crate::providers::inventory::declarative_inventory_identity;
|
||||
use crate::providers::ollama_def::OllamaProviderDef;
|
||||
use crate::providers::openai_def::OpenAiProviderDef;
|
||||
use crate::providers::private_file::write_private_file;
|
||||
use anyhow::Result;
|
||||
use once_cell::sync::Lazy;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -128,6 +129,13 @@ pub(crate) fn custom_provider_file_path(id: &str) -> Result<PathBuf> {
|
||||
Ok(custom_providers_dir().join(format!("{}.json", id)))
|
||||
}
|
||||
|
||||
fn persist_custom_provider(provider: &DeclarativeProviderConfig) -> Result<()> {
|
||||
let json_content = serde_json::to_string_pretty(provider)?;
|
||||
let file_path = custom_provider_file_path(&provider.name)?;
|
||||
write_private_file(&file_path, &json_content)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn generate_api_key_name(id: &str) -> String {
|
||||
format!("{}_API_KEY", id.to_uppercase())
|
||||
}
|
||||
@@ -234,12 +242,7 @@ pub fn create_custom_provider(
|
||||
setup: None,
|
||||
};
|
||||
|
||||
let custom_providers_dir = custom_providers_dir();
|
||||
std::fs::create_dir_all(&custom_providers_dir)?;
|
||||
|
||||
let json_content = serde_json::to_string_pretty(&provider_config)?;
|
||||
let file_path = custom_providers_dir.join(format!("{}.json", id));
|
||||
std::fs::write(file_path, json_content)?;
|
||||
persist_custom_provider(&provider_config)?;
|
||||
|
||||
Ok(provider_config)
|
||||
}
|
||||
@@ -354,9 +357,7 @@ pub fn update_custom_provider(params: UpdateCustomProviderParams) -> Result<()>
|
||||
setup: existing_config.setup,
|
||||
};
|
||||
|
||||
let file_path = custom_provider_file_path(&updated_config.name)?;
|
||||
let json_content = serde_json::to_string_pretty(&updated_config)?;
|
||||
std::fs::write(file_path, json_content)?;
|
||||
persist_custom_provider(&updated_config)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -300,6 +300,23 @@ fn acp_catalog_and_custom_provider_methods_use_core_provider_store() {
|
||||
custom_provider_path.exists(),
|
||||
"custom provider should be saved in Goose's declarative provider store"
|
||||
);
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
assert_eq!(
|
||||
std::fs::metadata(&custom_provider_path)
|
||||
.unwrap()
|
||||
.permissions()
|
||||
.mode()
|
||||
& 0o777,
|
||||
0o600
|
||||
);
|
||||
std::fs::set_permissions(
|
||||
&custom_provider_path,
|
||||
std::fs::Permissions::from_mode(0o644),
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
let saved_provider: DeclarativeProviderConfig =
|
||||
serde_json::from_str(&std::fs::read_to_string(&custom_provider_path).unwrap())
|
||||
.expect("saved provider should be core-compatible declarative config");
|
||||
@@ -395,6 +412,18 @@ fn acp_catalog_and_custom_provider_methods_use_core_provider_store() {
|
||||
)
|
||||
.await
|
||||
.expect("custom provider update should succeed");
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
assert_eq!(
|
||||
std::fs::metadata(&custom_provider_path)
|
||||
.unwrap()
|
||||
.permissions()
|
||||
.mode()
|
||||
& 0o777,
|
||||
0o600
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
updated.get("status"),
|
||||
Some(&serde_json::json!({
|
||||
|
||||
Reference in New Issue
Block a user