Base: codex-main originale (prima del fork aKode)
This commit is contained in:
20
codex-rs/core-plugin-common/src/identity.rs
Normal file
20
codex-rs/core-plugin-common/src/identity.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
//! Source-neutral identity shared by plugin catalogs and runtime attribution.
|
||||
|
||||
/// Stable key used to merge discoveries of the same logical plugin.
|
||||
/// Identity is independent of whether the plugin is supplied by the cloud or an executor.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
pub enum PluginIdentity {
|
||||
/// Stable ID assigned by plugin-service and shared by its materialized installations.
|
||||
Remote { remote_plugin_id: String },
|
||||
/// Local config key (normally `name@marketplace`), or a legacy selected-root identity.
|
||||
Local { plugin_id: String },
|
||||
}
|
||||
|
||||
impl PluginIdentity {
|
||||
pub fn as_str(&self) -> &str {
|
||||
match self {
|
||||
Self::Remote { remote_plugin_id } => remote_plugin_id,
|
||||
Self::Local { plugin_id } => plugin_id,
|
||||
}
|
||||
}
|
||||
}
|
||||
88
codex-rs/core-plugin-common/src/installed.rs
Normal file
88
codex-rs/core-plugin-common/src/installed.rs
Normal file
@@ -0,0 +1,88 @@
|
||||
//! Read-only installed-version selection and persisted installation identity.
|
||||
|
||||
use codex_utils_absolute_path::AbsolutePathBuf;
|
||||
use semver::Version;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
use std::cmp::Ordering;
|
||||
use std::fs;
|
||||
|
||||
pub const DEFAULT_PLUGIN_VERSION: &str = "local";
|
||||
pub const PLUGINS_CACHE_DIR: &str = "plugins/cache";
|
||||
pub const REMOTE_PLUGIN_INSTALL_METADATA_FILE: &str = ".codex-remote-plugin-install.json";
|
||||
pub const REMOTE_PLUGIN_INSTALL_METADATA_SCHEMA_VERSION: u8 = 1;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct RemotePluginInstallMetadata {
|
||||
pub schema_version: u8,
|
||||
pub remote_plugin_id: String,
|
||||
}
|
||||
|
||||
pub fn validate_plugin_version_segment(plugin_version: &str) -> Result<(), String> {
|
||||
if plugin_version.is_empty() {
|
||||
return Err("invalid plugin version: must not be empty".to_string());
|
||||
}
|
||||
if matches!(plugin_version, "." | "..") {
|
||||
return Err("invalid plugin version: path traversal is not allowed".to_string());
|
||||
}
|
||||
if !plugin_version
|
||||
.chars()
|
||||
.all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '-' | '_' | '.' | '+'))
|
||||
{
|
||||
return Err(
|
||||
"invalid plugin version: only ASCII letters, digits, `.`, `+`, `_`, and `-` are allowed"
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn compare_plugin_versions(left: &str, right: &str) -> Ordering {
|
||||
match (Version::parse(left), Version::parse(right)) {
|
||||
(Ok(left), Ok(right)) => left.cmp(&right),
|
||||
_ => left.cmp(right),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn active_plugin_version(plugin_base_root: &AbsolutePathBuf) -> Option<String> {
|
||||
let mut discovered_versions = fs::read_dir(plugin_base_root.as_path())
|
||||
.ok()?
|
||||
.filter_map(Result::ok)
|
||||
.filter_map(|entry| {
|
||||
entry.file_type().ok().filter(std::fs::FileType::is_dir)?;
|
||||
entry.file_name().into_string().ok()
|
||||
})
|
||||
.filter(|version| validate_plugin_version_segment(version).is_ok())
|
||||
.collect::<Vec<_>>();
|
||||
discovered_versions.sort_unstable_by(|left, right| compare_plugin_versions(left, right));
|
||||
if discovered_versions.is_empty() {
|
||||
None
|
||||
} else if discovered_versions
|
||||
.iter()
|
||||
.any(|version| version == DEFAULT_PLUGIN_VERSION)
|
||||
{
|
||||
Some(DEFAULT_PLUGIN_VERSION.to_string())
|
||||
} else {
|
||||
discovered_versions.pop()
|
||||
}
|
||||
}
|
||||
|
||||
/// Validates persisted installation metadata and returns its normalized remote identity.
|
||||
pub fn parse_remote_plugin_id(contents: &str) -> Result<String, String> {
|
||||
let metadata: RemotePluginInstallMetadata = serde_json::from_str(contents)
|
||||
.map_err(|err| format!("failed to parse remote plugin install metadata: {err}"))?;
|
||||
if metadata.schema_version != REMOTE_PLUGIN_INSTALL_METADATA_SCHEMA_VERSION {
|
||||
return Err(format!(
|
||||
"unsupported remote plugin install metadata schema version: {}",
|
||||
metadata.schema_version
|
||||
));
|
||||
}
|
||||
let remote_plugin_id = metadata.remote_plugin_id.trim();
|
||||
if remote_plugin_id.is_empty() {
|
||||
return Err(
|
||||
"invalid remote plugin install metadata: remote plugin id must not be blank"
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
Ok(remote_plugin_id.to_string())
|
||||
}
|
||||
7
codex-rs/core-plugin-common/src/lib.rs
Normal file
7
codex-rs/core-plugin-common/src/lib.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
//! Shared plugin identities and read-only installed-package rules.
|
||||
|
||||
mod identity;
|
||||
pub mod installed;
|
||||
pub mod plugin_id;
|
||||
|
||||
pub use identity::PluginIdentity;
|
||||
83
codex-rs/core-plugin-common/src/plugin_id.rs
Normal file
83
codex-rs/core-plugin-common/src/plugin_id.rs
Normal file
@@ -0,0 +1,83 @@
|
||||
//! Stable plugin identifier parsing and validation shared with the plugin cache.
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum PluginIdError {
|
||||
#[error("{0}")]
|
||||
Invalid(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct PluginId {
|
||||
pub plugin_name: String,
|
||||
pub marketplace_name: String,
|
||||
}
|
||||
|
||||
impl PluginId {
|
||||
pub fn new(plugin_name: String, marketplace_name: String) -> Result<Self, PluginIdError> {
|
||||
validate_plugin_segment(&plugin_name, "plugin name").map_err(PluginIdError::Invalid)?;
|
||||
validate_plugin_segment(&marketplace_name, "marketplace name")
|
||||
.map_err(PluginIdError::Invalid)?;
|
||||
Ok(Self {
|
||||
plugin_name,
|
||||
marketplace_name,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn parse(plugin_key: &str) -> Result<Self, PluginIdError> {
|
||||
let Some((plugin_name, marketplace_name)) = plugin_key.rsplit_once('@') else {
|
||||
return Err(PluginIdError::Invalid(format!(
|
||||
"invalid plugin key `{plugin_key}`; expected <plugin>@<marketplace>"
|
||||
)));
|
||||
};
|
||||
if plugin_name.is_empty() || marketplace_name.is_empty() {
|
||||
return Err(PluginIdError::Invalid(format!(
|
||||
"invalid plugin key `{plugin_key}`; expected <plugin>@<marketplace>"
|
||||
)));
|
||||
}
|
||||
|
||||
Self::new(plugin_name.to_string(), marketplace_name.to_string()).map_err(|err| match err {
|
||||
PluginIdError::Invalid(message) => {
|
||||
PluginIdError::Invalid(format!("{message} in `{plugin_key}`"))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn as_key(&self) -> String {
|
||||
format!("{}@{}", self.plugin_name, self.marketplace_name)
|
||||
}
|
||||
}
|
||||
|
||||
/// Validates a single path segment used in plugin IDs and cache layout.
|
||||
pub fn validate_plugin_segment(segment: &str, kind: &str) -> Result<(), String> {
|
||||
if segment.is_empty() {
|
||||
return Err(format!("invalid {kind}: must not be empty"));
|
||||
}
|
||||
let allow_dots = kind == "plugin name";
|
||||
if allow_dots && matches!(segment, "." | "..") {
|
||||
return Err(format!("invalid {kind}: path traversal is not allowed"));
|
||||
}
|
||||
if allow_dots && (segment.starts_with('.') || segment.ends_with('.') || segment.contains(".."))
|
||||
{
|
||||
return Err(format!(
|
||||
"invalid {kind}: dots must separate non-empty name segments"
|
||||
));
|
||||
}
|
||||
if !segment
|
||||
.chars()
|
||||
.all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '-' | '_') || allow_dots && ch == '.')
|
||||
{
|
||||
let allowed_characters = if allow_dots {
|
||||
"ASCII letters, digits, `.`, `_`, and `-`"
|
||||
} else {
|
||||
"ASCII letters, digits, `_`, and `-`"
|
||||
};
|
||||
return Err(format!(
|
||||
"invalid {kind}: only {allowed_characters} are allowed"
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "plugin_id_tests.rs"]
|
||||
mod tests;
|
||||
34
codex-rs/core-plugin-common/src/plugin_id_tests.rs
Normal file
34
codex-rs/core-plugin-common/src/plugin_id_tests.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
use super::PluginId;
|
||||
|
||||
#[test]
|
||||
fn accepts_dotted_plugin_names() {
|
||||
let plugin_id =
|
||||
PluginId::new("acme.tools".to_string(), "marketplace".to_string()).expect("plugin id");
|
||||
|
||||
assert_eq!(plugin_id.as_key(), "acme.tools@marketplace");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn marketplace_names_preserve_legacy_character_set() {
|
||||
PluginId::new("acme".to_string(), "marketplace_name-1".to_string()).expect("marketplace name");
|
||||
|
||||
let err = PluginId::new("acme".to_string(), "market.place".to_string())
|
||||
.expect_err("dotted marketplace name");
|
||||
assert_eq!(
|
||||
err.to_string(),
|
||||
"invalid marketplace name: only ASCII letters, digits, `_`, and `-` are allowed"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_dot_path_segments() {
|
||||
assert!(PluginId::new(".".to_string(), "marketplace".to_string()).is_err());
|
||||
assert!(PluginId::new("plugin".to_string(), "..".to_string()).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_dots_that_can_alias_path_segments() {
|
||||
for plugin_name in [".plugin", "plugin.", "plugin..name"] {
|
||||
assert!(PluginId::new(plugin_name.to_string(), "marketplace".to_string()).is_err());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user