use serde::{Deserialize, Serialize};
pub const MAX_EXTENSION_METHODS: usize = 64;
pub const MAX_EXTENSION_CAPABILITIES: usize = 64;
pub const MAX_EXTENSION_CAPABILITY_LEN: usize = 512;
pub const MAX_EXTENSION_METHOD_LEN: usize = 512;
pub const MAX_EXTENSION_NAME_LEN: usize = 256;
pub const MAX_EXTENSION_VERSION_LEN: usize = 64;
pub const MAX_EXTENSION_ID_LEN: usize = 256;
#[derive(Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct ExtensionDescriptor {
pub id: String,
pub name: String,
pub version: String,
#[serde(default)]
pub capabilities: Vec<String>,
#[serde(default)]
pub methods: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub signature: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub public_key: Option<String>,
}
impl std::fmt::Debug for ExtensionDescriptor {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ExtensionDescriptor")
.field("id", &self.id)
.field("name", &self.name)
.field("version", &self.version)
.field("capabilities", &self.capabilities)
.field("methods", &self.methods)
.field("signature", &self.signature.as_ref().map(|_| "[REDACTED]"))
.field(
"public_key",
&self.public_key.as_ref().map(|_| "[REDACTED]"),
)
.finish()
}
}
impl ExtensionDescriptor {
pub fn validate(&self) -> Result<(), ExtensionError> {
if self.id.is_empty() {
return Err(ExtensionError::Validation(
"extension id must not be empty".to_string(),
));
}
if self.id.len() > MAX_EXTENSION_ID_LEN {
return Err(ExtensionError::Validation(format!(
"extension id length {} exceeds max {}",
self.id.len(),
MAX_EXTENSION_ID_LEN
)));
}
if self.name.is_empty() {
return Err(ExtensionError::Validation(
"extension name must not be empty".to_string(),
));
}
if self.name.len() > MAX_EXTENSION_NAME_LEN {
return Err(ExtensionError::Validation(format!(
"extension name length {} exceeds max {}",
self.name.len(),
MAX_EXTENSION_NAME_LEN
)));
}
if crate::core::has_dangerous_chars(&self.name) {
return Err(ExtensionError::Validation(
"extension name contains control or format characters".to_string(),
));
}
if self.version.is_empty() {
return Err(ExtensionError::Validation(
"extension version must not be empty".to_string(),
));
}
if self.version.len() > MAX_EXTENSION_VERSION_LEN {
return Err(ExtensionError::Validation(format!(
"extension version length {} exceeds max {}",
self.version.len(),
MAX_EXTENSION_VERSION_LEN
)));
}
if crate::core::has_dangerous_chars(&self.version) {
return Err(ExtensionError::Validation(
"extension version contains control or format characters".to_string(),
));
}
if crate::core::has_dangerous_chars(&self.id) {
return Err(ExtensionError::Validation(
"extension id contains control or format characters".to_string(),
));
}
if self.methods.len() > MAX_EXTENSION_METHODS {
return Err(ExtensionError::Validation(format!(
"too many methods: {} (max {})",
self.methods.len(),
MAX_EXTENSION_METHODS
)));
}
for (i, method) in self.methods.iter().enumerate() {
if method.is_empty() {
return Err(ExtensionError::Validation(format!(
"method[{i}] must not be empty"
)));
}
if method.len() > MAX_EXTENSION_METHOD_LEN {
return Err(ExtensionError::Validation(format!(
"method[{}] length {} exceeds max {}",
i,
method.len(),
MAX_EXTENSION_METHOD_LEN
)));
}
if method
.chars()
.any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
{
return Err(ExtensionError::Validation(format!(
"method[{i}] contains control or format characters"
)));
}
}
if self.capabilities.len() > MAX_EXTENSION_CAPABILITIES {
return Err(ExtensionError::Validation(format!(
"too many capabilities: {} (max {})",
self.capabilities.len(),
MAX_EXTENSION_CAPABILITIES
)));
}
for (i, cap) in self.capabilities.iter().enumerate() {
if cap.is_empty() {
return Err(ExtensionError::Validation(format!(
"capability[{i}] must not be empty"
)));
}
if cap.len() > MAX_EXTENSION_CAPABILITY_LEN {
return Err(ExtensionError::Validation(format!(
"capability[{}] length {} exceeds max {}",
i,
cap.len(),
MAX_EXTENSION_CAPABILITY_LEN
)));
}
if cap
.chars()
.any(|c| c.is_control() || crate::core::is_unicode_format_char(c))
{
return Err(ExtensionError::Validation(format!(
"capability[{i}] contains control or format characters"
)));
}
}
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct ExtensionResourceLimits {
#[serde(default = "default_max_concurrent")]
pub max_concurrent_requests: usize,
#[serde(default = "default_max_rps")]
pub max_requests_per_sec: u32,
}
fn default_max_concurrent() -> usize {
10
}
fn default_max_rps() -> u32 {
100
}
impl Default for ExtensionResourceLimits {
fn default() -> Self {
Self {
max_concurrent_requests: default_max_concurrent(),
max_requests_per_sec: default_max_rps(),
}
}
}
impl ExtensionResourceLimits {
const MAX_CONCURRENT: usize = 10_000;
const MAX_RPS: u32 = 100_000;
pub fn validate(&self) -> Result<(), String> {
if self.max_concurrent_requests == 0 {
return Err("max_concurrent_requests must be > 0".to_string());
}
if self.max_concurrent_requests > Self::MAX_CONCURRENT {
return Err(format!(
"max_concurrent_requests {} exceeds max {}",
self.max_concurrent_requests,
Self::MAX_CONCURRENT
));
}
if self.max_requests_per_sec == 0 {
return Err("max_requests_per_sec must be > 0".to_string());
}
if self.max_requests_per_sec > Self::MAX_RPS {
return Err(format!(
"max_requests_per_sec {} exceeds max {}",
self.max_requests_per_sec,
Self::MAX_RPS
));
}
Ok(())
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ExtensionNegotiationResult {
pub accepted: Vec<String>,
pub rejected: Vec<(String, String)>,
}
impl ExtensionNegotiationResult {
const MAX_ACCEPTED: usize = 256;
const MAX_REJECTED: usize = 256;
pub fn validate(&self) -> Result<(), String> {
if self.accepted.len() > Self::MAX_ACCEPTED {
return Err(format!(
"ExtensionNegotiationResult accepted count {} exceeds max {}",
self.accepted.len(),
Self::MAX_ACCEPTED,
));
}
if self.rejected.len() > Self::MAX_REJECTED {
return Err(format!(
"ExtensionNegotiationResult rejected count {} exceeds max {}",
self.rejected.len(),
Self::MAX_REJECTED,
));
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExtensionError {
Validation(String),
NotFound(String),
AlreadyRegistered(String),
MethodNotFound(String),
Blocked(String),
HandlerFailed(String),
}
impl std::fmt::Display for ExtensionError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ExtensionError::Validation(msg) => write!(f, "Extension validation: {msg}"),
ExtensionError::NotFound(msg) => write!(f, "Extension not found: {msg}"),
ExtensionError::AlreadyRegistered(msg) => {
write!(f, "Extension already registered: {msg}")
}
ExtensionError::MethodNotFound(msg) => {
write!(f, "Extension method not found: {msg}")
}
ExtensionError::Blocked(msg) => write!(f, "Extension blocked: {msg}"),
ExtensionError::HandlerFailed(msg) => write!(f, "Extension handler failed: {msg}"),
}
}
}
impl std::error::Error for ExtensionError {}