Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

extension_cli: Add basic validation for extension manifest (#59189)

Finn Evers51a3ac29deb2
diff --git a/Cargo.lock b/Cargo.lock
index a51b6ab34cb..a899ba16f65 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -6157,6 +6157,7 @@ dependencies = [
"fs",
"futures 0.3.32",
"gpui_platform",
+ "http_client",
"language",
"log",
"reqwest_client",
diff --git a/crates/extension_cli/Cargo.toml b/crates/extension_cli/Cargo.toml
index 1f6c1493d36..3a41e8b802f 100644
--- a/crates/extension_cli/Cargo.toml
+++ b/crates/extension_cli/Cargo.toml
@@ -21,6 +21,7 @@ extension.workspace = true
futures.workspace = true
fs.workspace = true
gpui_platform.workspace = true
+http_client.workspace = true
language.workspace = true
log.workspace = true
reqwest_client.workspace = true
diff --git a/crates/extension_cli/src/main.rs b/crates/extension_cli/src/main.rs
index 921990d284c..bf941a946c0 100644
--- a/crates/extension_cli/src/main.rs
+++ b/crates/extension_cli/src/main.rs
@@ -14,6 +14,7 @@ use extension::build_debug_adapter_schema_path;
use extension::extension_builder::CompilationConcurrency;
use extension::extension_builder::{CompileExtensionOptions, ExtensionBuilder};
use extension::{ExtensionManifest, ExtensionSnippets};
+use http_client::Url;
use language::LanguageConfig;
use reqwest_client::ReqwestClient;
use settings_content::SemanticTokenRules;
@@ -92,6 +93,7 @@ async fn main() -> Result<()> {
.await
.context("failed to compile extension")?;
+ validate_extension_manifest(&manifest)?;
let extension_provides = manifest.provides();
validate_extension_features(&extension_provides)?;
@@ -411,6 +413,82 @@ fn validate_extension_features(
Ok(())
}
+#[derive(Debug, PartialEq, Eq, thiserror::Error)]
+enum ExtensionManifestValidationError {
+ #[error("extension manifest must specify a name")]
+ MissingName,
+ #[error("extension manifest must specify a description")]
+ MissingDescription,
+ #[error("extension manifest description must be more expressive than the name")]
+ DescriptionNotLongerThanName,
+ #[error("extension manifest must specify at least one author")]
+ MissingAuthors,
+ #[error("extension manifest must specify a repository")]
+ MissingRepository,
+ #[error("extension manifest repository is not a valid URL: {0}")]
+ InvalidRepository(String),
+ #[error(
+ "extension manifest must not provide language model providers, \
+ as these are currently unsupported"
+ )]
+ LanguageModelProvidersUnsupported,
+}
+
+fn validate_extension_manifest(
+ manifest: &ExtensionManifest,
+) -> Result<(), ExtensionManifestValidationError> {
+ if manifest.name.trim().is_empty() {
+ return Err(ExtensionManifestValidationError::MissingName);
+ }
+
+ let description_is_empty = manifest
+ .description
+ .as_ref()
+ .is_none_or(|description| description.trim().is_empty());
+ if description_is_empty {
+ return Err(ExtensionManifestValidationError::MissingDescription);
+ }
+
+ let description_is_longer_than_name =
+ manifest.description.as_ref().is_some_and(|description| {
+ description.trim().chars().count() > manifest.name.trim().chars().count()
+ });
+ if !description_is_longer_than_name {
+ return Err(ExtensionManifestValidationError::DescriptionNotLongerThanName);
+ }
+
+ if manifest
+ .authors
+ .iter()
+ .all(|author| author.trim().is_empty())
+ {
+ return Err(ExtensionManifestValidationError::MissingAuthors);
+ }
+
+ let repository = manifest
+ .repository
+ .as_ref()
+ .map(|repository| repository.trim())
+ .filter(|repository| !repository.is_empty())
+ .ok_or(ExtensionManifestValidationError::MissingRepository)?;
+
+ let repository_url = repository
+ .parse::<Url>()
+ .map_err(|_| ExtensionManifestValidationError::InvalidRepository(repository.to_string()))?;
+
+ if repository_url.host_str().is_none() {
+ return Err(ExtensionManifestValidationError::InvalidRepository(
+ repository.to_string(),
+ ));
+ };
+
+ if !manifest.language_model_providers.is_empty() {
+ return Err(ExtensionManifestValidationError::LanguageModelProvidersUnsupported);
+ }
+
+ Ok(())
+}
+
fn test_grammars(
manifest: &ExtensionManifest,
extension_path: &Path,
@@ -606,10 +684,187 @@ async fn test_debug_adapter_schemas(
#[cfg(test)]
mod tests {
+ use std::collections::BTreeMap;
+
use cloud_api_types::ExtensionProvides;
+ use extension::{LanguageModelProviderManifestEntry, SchemaVersion};
use super::*;
+ fn valid_manifest() -> ExtensionManifest {
+ ExtensionManifest {
+ id: "test".into(),
+ name: "Test Extension".to_string(),
+ version: "1.0.0".into(),
+ schema_version: SchemaVersion::ZERO,
+ description: Some("A test extension".to_string()),
+ repository: Some("https://github.com/zed-industries/zed".to_string()),
+ authors: vec!["Zed".to_string()],
+ lib: Default::default(),
+ themes: Vec::new(),
+ icon_themes: Vec::new(),
+ languages: Vec::new(),
+ grammars: BTreeMap::default(),
+ language_servers: BTreeMap::default(),
+ context_servers: BTreeMap::default(),
+ slash_commands: BTreeMap::default(),
+ snippets: None,
+ capabilities: Vec::new(),
+ debug_adapters: BTreeMap::default(),
+ debug_locators: BTreeMap::default(),
+ language_model_providers: BTreeMap::default(),
+ }
+ }
+
+ #[test]
+ fn test_validate_manifest_valid() {
+ assert!(validate_extension_manifest(&valid_manifest()).is_ok());
+ }
+
+ #[test]
+ fn test_validate_manifest_missing_name() {
+ let manifest = ExtensionManifest {
+ name: " ".to_string(),
+ ..valid_manifest()
+ };
+ assert_eq!(
+ validate_extension_manifest(&manifest),
+ Err(ExtensionManifestValidationError::MissingName),
+ );
+ }
+
+ #[test]
+ fn test_validate_manifest_missing_description() {
+ let manifest = ExtensionManifest {
+ description: None,
+ ..valid_manifest()
+ };
+ assert_eq!(
+ validate_extension_manifest(&manifest),
+ Err(ExtensionManifestValidationError::MissingDescription),
+ );
+ }
+
+ #[test]
+ fn test_validate_manifest_empty_description() {
+ let manifest = ExtensionManifest {
+ description: Some(" ".to_string()),
+ ..valid_manifest()
+ };
+ assert_eq!(
+ validate_extension_manifest(&manifest),
+ Err(ExtensionManifestValidationError::MissingDescription),
+ );
+ }
+
+ #[test]
+ fn test_validate_manifest_description_equal_to_name() {
+ let manifest = ExtensionManifest {
+ description: Some("Test Extension".to_string()),
+ ..valid_manifest()
+ };
+ assert_eq!(
+ validate_extension_manifest(&manifest),
+ Err(ExtensionManifestValidationError::DescriptionNotLongerThanName),
+ );
+ }
+
+ #[test]
+ fn test_validate_manifest_description_shorter_than_name() {
+ let manifest = ExtensionManifest {
+ description: Some("Test".to_string()),
+ ..valid_manifest()
+ };
+ assert_eq!(
+ validate_extension_manifest(&manifest),
+ Err(ExtensionManifestValidationError::DescriptionNotLongerThanName),
+ );
+ }
+
+ #[test]
+ fn test_validate_manifest_missing_authors() {
+ let manifest = ExtensionManifest {
+ authors: Vec::new(),
+ ..valid_manifest()
+ };
+ assert_eq!(
+ validate_extension_manifest(&manifest),
+ Err(ExtensionManifestValidationError::MissingAuthors),
+ );
+ }
+
+ #[test]
+ fn test_validate_manifest_blank_authors() {
+ let manifest = ExtensionManifest {
+ authors: vec![" ".to_string()],
+ ..valid_manifest()
+ };
+ assert_eq!(
+ validate_extension_manifest(&manifest),
+ Err(ExtensionManifestValidationError::MissingAuthors),
+ );
+ }
+
+ #[test]
+ fn test_validate_manifest_missing_repository() {
+ let manifest = ExtensionManifest {
+ repository: None,
+ ..valid_manifest()
+ };
+ assert_eq!(
+ validate_extension_manifest(&manifest),
+ Err(ExtensionManifestValidationError::MissingRepository),
+ );
+ }
+
+ #[test]
+ fn test_validate_manifest_invalid_repository() {
+ let manifest = ExtensionManifest {
+ repository: Some("not-a-valid-url".to_string()),
+ ..valid_manifest()
+ };
+ assert_eq!(
+ validate_extension_manifest(&manifest),
+ Err(ExtensionManifestValidationError::InvalidRepository(
+ "not-a-valid-url".to_string()
+ )),
+ );
+ }
+
+ #[test]
+ fn test_validate_manifest_repository_without_host() {
+ let manifest = ExtensionManifest {
+ repository: Some("file:///some/local/path".to_string()),
+ ..valid_manifest()
+ };
+ assert_eq!(
+ validate_extension_manifest(&manifest),
+ Err(ExtensionManifestValidationError::InvalidRepository(
+ "file:///some/local/path".to_string()
+ )),
+ );
+ }
+
+ #[test]
+ fn test_validate_manifest_language_model_providers_unsupported() {
+ let mut language_model_providers = BTreeMap::new();
+ language_model_providers.insert(
+ "provider".into(),
+ LanguageModelProviderManifestEntry {
+ name: "Provider".to_string(),
+ icon: None,
+ },
+ );
+ let manifest = ExtensionManifest {
+ language_model_providers,
+ ..valid_manifest()
+ };
+ assert_eq!(
+ validate_extension_manifest(&manifest),
+ Err(ExtensionManifestValidationError::LanguageModelProvidersUnsupported),
+ );
+ }
+
#[test]
fn test_validate_empty_features() {
let provides = BTreeSet::new();