Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins
TypeScript ai-agentcoding-agentdesktop-appelectronglobali18nlocal-firstmcppipi-agentpi-desktoppluginsreactrusttypescript
refactor(plugins): isolate OAuth manifest validation
vastsa96c7a89a4ca2
diff --git a/crates/host-core/src/plugins.rs b/crates/host-core/src/plugins.rsindex 3cab6aaa7..8fecad444 100644--- a/crates/host-core/src/plugins.rs+++ b/crates/host-core/src/plugins.rs@@ -24,6 +24,7 @@ pub mod marketplace;mod model;mod permissions;pub(crate) mod progress;+mod provider_validation;mod providers;mod registry;mod renderer;@@ -68,6 +69,7 @@ pub(crate) use marketplace::{latest_market_version,};pub(crate) use permissions::{derive_capabilities, derive_settings, permission_diff, sanitize_id};+pub(crate) use provider_validation::validate_declared_provider_oauth;pub(crate) use providers::{declared_providers, is_known_api_style, is_known_auth_kind, owned_provider_ids,plugin_provider_row_id, reconcile_all, reconcile_plugin, remove_plugin_providers,diff --git a/crates/host-core/src/plugins/provider_validation.rs b/crates/host-core/src/plugins/provider_validation.rsnew file mode 100644index 000000000..f2f9b90ac--- /dev/null+++ b/crates/host-core/src/plugins/provider_validation.rs@@ -0,0 +1,50 @@+use super::{anyhow, bail, validation::require_permission, PluginManifest, Result, Value};++pub(crate) fn validate_declared_provider_oauth(+ id: &str,+ provider: &serde_json::Map<String, Value>,+ auth_kind: &str,+ manifest: &PluginManifest,+) -> Result<()> {+ if auth_kind == "oauth" {+ require_permission(manifest, "provider.oauth", "OAuth providers")?;+ if provider.get("baseUrl").and_then(Value::as_str).is_none() {+ bail!("PLUGIN_INVALID: provider {id} requires baseUrl for OAuth");+ }+ }++ let Some(oauth) = provider.get("oauth") else {+ return Ok(());+ };+ if auth_kind != "oauth" {+ bail!("PLUGIN_INVALID: provider {id} oauth metadata requires authKind oauth");+ }+ let oauth = oauth+ .as_object()+ .ok_or_else(|| anyhow!("PLUGIN_INVALID: provider {id} oauth must be an object"))?;+ if let Some(field) = oauth+ .keys()+ .find(|key| key.as_str() != "loginLabel" && key.as_str() != "isSubscription")+ {+ bail!("PLUGIN_INVALID: provider {id} oauth has unsupported field {field}");+ }+ if let Some(login_label) = oauth.get("loginLabel") {+ if login_label+ .as_str()+ .map(str::trim)+ .filter(|value| !value.is_empty() && value.encode_utf16().count() <= 128)+ .is_none()+ {+ bail!(+ "PLUGIN_INVALID: provider {id} oauth.loginLabel must be a non-empty string of at most 128 characters"+ );+ }+ }+ if oauth+ .get("isSubscription")+ .is_some_and(|value| !value.is_boolean())+ {+ bail!("PLUGIN_INVALID: provider {id} oauth.isSubscription must be a boolean");+ }+ Ok(())+}diff --git a/crates/host-core/src/plugins/providers/tests.rs b/crates/host-core/src/plugins/providers/tests.rsindex 0ff625476..629cd29a6 100644--- a/crates/host-core/src/plugins/providers/tests.rs+++ b/crates/host-core/src/plugins/providers/tests.rs@@ -553,6 +553,54 @@ fn the_declaration_shape_is_validated() {),);assert!(PluginManager::read_manifest(&root).is_ok());++ let oauth_permissions = json!(["provider.register", "provider.oauth"]);+ let invalid_oauth_providers = [+ (+ json!([{ "id": "demo", "name": "Demo", "authKind": "oauth",+ "models": [{ "id": "m" }] }]),+ "requires baseUrl for OAuth",+ ),+ (+ json!([{ "id": "demo", "name": "Demo", "authKind": "api_key",+ "baseUrl": "https://api.example.com/v1",+ "oauth": { "loginLabel": "Continue" },+ "models": [{ "id": "m" }] }]),+ "requires authKind oauth",+ ),+ (+ json!([{ "id": "demo", "name": "Demo", "authKind": "oauth",+ "baseUrl": "https://api.example.com/v1", "oauth": "invalid",+ "models": [{ "id": "m" }] }]),+ "oauth must be an object",+ ),+ (+ json!([{ "id": "demo", "name": "Demo", "authKind": "oauth",+ "baseUrl": "https://api.example.com/v1",+ "oauth": { "unexpected": true }, "models": [{ "id": "m" }] }]),+ "oauth has unsupported field unexpected",+ ),+ (+ json!([{ "id": "demo", "name": "Demo", "authKind": "oauth",+ "baseUrl": "https://api.example.com/v1", "oauth": { "loginLabel": " " },+ "models": [{ "id": "m" }] }]),+ "oauth.loginLabel must be a non-empty string",+ ),+ (+ json!([{ "id": "demo", "name": "Demo", "authKind": "oauth",+ "baseUrl": "https://api.example.com/v1",+ "oauth": { "isSubscription": "yes" }, "models": [{ "id": "m" }] }]),+ "oauth.isSubscription must be a boolean",+ ),+ ];+ for (providers, expected_error) in invalid_oauth_providers {+ write_plugin(+ &root,+ declaration_manifest(providers, oauth_permissions.clone()),+ );+ let error = read_manifest_err(&root);+ assert!(error.contains(expected_error), "{error}");+ }}/// A row id already in use by something other than this plugin cannot arisediff --git a/crates/host-core/src/plugins/validation.rs b/crates/host-core/src/plugins/validation.rsindex d95520c58..d39f38466 100644--- a/crates/host-core/src/plugins/validation.rs+++ b/crates/host-core/src/plugins/validation.rs@@ -264,44 +264,7 @@ pub(crate) fn validate_contributions(root: &Path, manifest: &PluginManifest) ->if !is_known_auth_kind(auth_kind) {bail!("PLUGIN_INVALID: provider {id} has unsupported authKind {auth_kind}");}- if auth_kind == "oauth" {- require_permission(manifest, "provider.oauth", "OAuth providers")?;- if obj.get("baseUrl").and_then(Value::as_str).is_none() {- bail!("PLUGIN_INVALID: provider {id} requires baseUrl for OAuth");- }- }- if let Some(oauth) = obj.get("oauth") {- if auth_kind != "oauth" {- bail!("PLUGIN_INVALID: provider {id} oauth metadata requires authKind oauth");- }- let oauth = oauth.as_object().ok_or_else(|| {- anyhow!("PLUGIN_INVALID: provider {id} oauth must be an object")- })?;- if let Some(field) = oauth- .keys()- .find(|key| key.as_str() != "loginLabel" && key.as_str() != "isSubscription")- {- bail!("PLUGIN_INVALID: provider {id} oauth has unsupported field {field}");- }- if let Some(login_label) = oauth.get("loginLabel") {- if login_label- .as_str()- .map(str::trim)- .filter(|value| !value.is_empty() && value.encode_utf16().count() <= 128)- .is_none()- {- bail!(- "PLUGIN_INVALID: provider {id} oauth.loginLabel must be a non-empty string of at most 128 characters"- );- }- }- if oauth- .get("isSubscription")- .is_some_and(|value| !value.is_boolean())- {- bail!("PLUGIN_INVALID: provider {id} oauth.isSubscription must be a boolean");- }- }+ validate_declared_provider_oauth(id, obj, auth_kind, manifest)?;if let Some(base_url) = obj.get("baseUrl").and_then(Value::as_str) {// The runtime reaches this endpoint, so a declaration may only// name an absolute http(s) URL.@@ -739,7 +702,11 @@ fn array_of<'a>(value: &'a Value, field: &str) -> Result<&'a [Value]> {.ok_or_else(|| anyhow!("PLUGIN_INVALID: {field} must be an array"))}-fn require_permission(manifest: &PluginManifest, permission: &str, what: &str) -> Result<()> {+pub(super) fn require_permission(+ manifest: &PluginManifest,+ permission: &str,+ what: &str,+) -> Result<()> {if manifest.permissions.iter().any(|p| p == permission) {return Ok(());}