use std::collections::HashSet;
use std::fmt;
use serde::{Deserialize, Serialize};
use super::Descriptor;
#[cfg(feature = "schema")]
use super::descriptor::PLAIN_TEXT_PATTERN;
use super::descriptor::plain_text;
pub const MAX_MANIFEST_BYTES: usize = 1024 * 1024;
pub const MAX_PLUGIN_ARTIFACTS: usize = 64;
pub const MAX_PLUGIN_RESOURCES: usize = 64;
pub const MAX_PLUGIN_ARTIFACT_BYTES: u64 = 256 * 1024 * 1024;
pub const MAX_PLUGIN_RESOURCE_BYTES: u64 = 2 * 1024 * 1024 * 1024;
pub const MAX_PLUGIN_RESOURCES_TOTAL_BYTES: u64 = 4 * 1024 * 1024 * 1024;
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct PluginManifest {
pub id: String,
pub version: String,
#[cfg_attr(feature = "schema", schemars(extend("const" = alpha2_plugin_abi::ABI)))]
pub abi: String,
pub kind: PluginKind,
#[cfg_attr(feature = "schema", schemars(length(min = 1)))]
pub artifacts: Vec<PluginArtifact>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub resources: Vec<PluginResource>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub about: Option<PluginAbout>,
pub descriptor: Descriptor,
}
pub const MAX_ABOUT_NAME_CHARS: usize = 80;
pub const MAX_ABOUT_SUMMARY_CHARS: usize = 400;
pub const MAX_ABOUT_AUTHOR_CHARS: usize = 80;
pub const MAX_ABOUT_HOMEPAGE_CHARS: usize = 2048;
pub const MAX_ABOUT_LICENSE_CHARS: usize = 256;
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct PluginAbout {
#[cfg_attr(
feature = "schema",
schemars(length(min = 1, max = MAX_ABOUT_NAME_CHARS), regex(pattern = PLAIN_TEXT_PATTERN))
)]
pub name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(
feature = "schema",
schemars(length(max = MAX_ABOUT_SUMMARY_CHARS), regex(pattern = PLAIN_TEXT_PATTERN))
)]
pub summary: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(
feature = "schema",
schemars(length(max = MAX_ABOUT_AUTHOR_CHARS), regex(pattern = PLAIN_TEXT_PATTERN))
)]
pub author: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(
feature = "schema",
schemars(length(max = MAX_ABOUT_HOMEPAGE_CHARS), regex(pattern = HOMEPAGE_PATTERN))
)]
pub homepage: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(
feature = "schema",
schemars(length(max = MAX_ABOUT_LICENSE_CHARS), regex(pattern = LICENSE_PATTERN))
)]
pub license: Option<String>,
}
#[cfg(feature = "schema")]
const HOMEPAGE_PATTERN: &str = "^https://[!-~]+$";
#[cfg(feature = "schema")]
const LICENSE_PATTERN: &str = r"^[A-Za-z0-9.+:() -]+$";
impl PluginAbout {
fn validate(&self) -> Result<(), String> {
let text = |field: &str, value: &str, min: usize, max: usize| {
if plain_text(value, min, max) {
Ok(())
} else {
Err(format!(
"about.{field} is not {min} to {max} characters without control characters"
))
}
};
text("name", &self.name, 1, MAX_ABOUT_NAME_CHARS)?;
if let Some(summary) = &self.summary {
text("summary", summary, 0, MAX_ABOUT_SUMMARY_CHARS)?;
}
if let Some(author) = &self.author {
text("author", author, 0, MAX_ABOUT_AUTHOR_CHARS)?;
}
if let Some(homepage) = &self.homepage {
let rest = homepage.strip_prefix("https://").unwrap_or_default();
if homepage.len() > MAX_ABOUT_HOMEPAGE_CHARS
|| rest.is_empty()
|| !rest.bytes().all(|byte| matches!(byte, b'!'..=b'~'))
{
return Err(format!(
"about.homepage is not an https: URL of printable ASCII, at most \
{MAX_ABOUT_HOMEPAGE_CHARS} characters"
));
}
}
if let Some(license) = &self.license
&& (license.len() > MAX_ABOUT_LICENSE_CHARS || !spdx_expression(license))
{
return Err(format!(
"about.license {license:?} is not an SPDX license expression of at most \
{MAX_ABOUT_LICENSE_CHARS} characters"
));
}
Ok(())
}
}
fn spdx_expression(text: &str) -> bool {
let spaced = text.replace('(', " ( ").replace(')', " ) ");
let tokens = spaced.split_ascii_whitespace().collect::<Vec<_>>();
let mut at = 0;
spdx_or(&tokens, &mut at, 0) && at == tokens.len()
}
const SPDX_MAX_NESTING: usize = 32;
fn spdx_id(token: &str) -> bool {
!matches!(token, "AND" | "OR" | "WITH" | "(" | ")")
&& !token.is_empty()
&& token
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'.' | b'-' | b':'))
}
fn spdx_or(tokens: &[&str], at: &mut usize, depth: usize) -> bool {
spdx_and(tokens, at, depth)
&& loop {
if tokens.get(*at) != Some(&"OR") {
break true;
}
*at += 1;
if !spdx_and(tokens, at, depth) {
break false;
}
}
}
fn spdx_and(tokens: &[&str], at: &mut usize, depth: usize) -> bool {
spdx_term(tokens, at, depth)
&& loop {
if tokens.get(*at) != Some(&"AND") {
break true;
}
*at += 1;
if !spdx_term(tokens, at, depth) {
break false;
}
}
}
fn spdx_term(tokens: &[&str], at: &mut usize, depth: usize) -> bool {
let Some(&token) = tokens.get(*at) else {
return false;
};
*at += 1;
if token == "(" {
if depth >= SPDX_MAX_NESTING
|| !spdx_or(tokens, at, depth + 1)
|| tokens.get(*at) != Some(&")")
{
return false;
}
*at += 1;
return true;
}
if !spdx_id(token.strip_suffix('+').unwrap_or(token)) {
return false;
}
if tokens.get(*at) == Some(&"WITH") {
*at += 1;
let exception = tokens.get(*at).is_some_and(|token| spdx_id(token));
*at += 1;
return exception;
}
true
}
#[derive(Copy, Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum PluginKind {
Extractor,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct PluginArtifact {
pub target: String,
#[serde(rename = "type")]
pub kind: ArtifactKind,
pub url: String,
#[cfg_attr(feature = "schema", schemars(regex(pattern = r"^[0-9a-f]{64}$")))]
pub sha256: String,
#[cfg_attr(feature = "schema", schemars(range(max = MAX_PLUGIN_ARTIFACT_BYTES)))]
pub bytes: u64,
}
#[derive(Copy, Clone, Debug, Deserialize, Eq, PartialEq, Hash, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum ArtifactKind {
Wasm,
Cdylib,
Staticlib,
}
pub const MAX_PLUGIN_SLOTS: usize = 16;
pub const MAX_SLOT_ACCEPT: usize = 16;
pub const MAX_SLOT_ACCEPT_BYTES: usize = 64;
pub const MAX_SLOT_NAME_BYTES: usize = 64;
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(try_from = "ResourceEntry", into = "ResourceEntry")]
pub struct PluginResource {
pub name: String,
pub source: ResourceSource,
}
#[derive(Clone, Debug, PartialEq)]
pub enum ResourceSource {
Pinned(PinnedResource),
Supplied(SuppliedSlot),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PinnedResource {
pub url: String,
pub sha256: String,
pub bytes: u64,
}
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct SuppliedSlot {
pub shape: SlotShape,
#[serde(default = "required_default", skip_serializing_if = "is_true")]
#[cfg_attr(feature = "schema", schemars(extend("default" = true)))]
pub required: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "schema", schemars(range(min = 1)))]
pub max_bytes: Option<u64>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[cfg_attr(
feature = "schema",
schemars(length(max = MAX_SLOT_ACCEPT), inner(length(min = 1, max = MAX_SLOT_ACCEPT_BYTES)))
)]
pub accept: Vec<String>,
}
const fn required_default() -> bool {
true
}
#[expect(
clippy::trivially_copy_pass_by_ref,
reason = "serde passes a reference"
)]
const fn is_true(value: &bool) -> bool {
*value
}
#[derive(Copy, Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(rename_all = "snake_case")]
pub enum SlotShape {
File,
Directory,
}
impl PluginResource {
#[must_use]
pub const fn pinned(&self) -> Option<&PinnedResource> {
match &self.source {
ResourceSource::Pinned(pinned) => Some(pinned),
ResourceSource::Supplied(_) => None,
}
}
#[must_use]
pub const fn slot(&self) -> Option<&SuppliedSlot> {
match &self.source {
ResourceSource::Pinned(_) => None,
ResourceSource::Supplied(slot) => Some(slot),
}
}
}
#[derive(Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
struct ResourceEntry {
name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
url: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
sha256: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
bytes: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
supplied: Option<SuppliedSlot>,
}
impl TryFrom<ResourceEntry> for PluginResource {
type Error = String;
fn try_from(entry: ResourceEntry) -> Result<Self, String> {
let ResourceEntry {
name,
url,
sha256,
bytes,
supplied,
} = entry;
let source = match (url, sha256, bytes, supplied) {
(Some(url), Some(sha256), Some(bytes), None) => {
ResourceSource::Pinned(PinnedResource { url, sha256, bytes })
}
(None, None, None, Some(slot)) => ResourceSource::Supplied(slot),
(None, None, None, None) => {
return Err(format!(
"resource {name:?} has neither url, sha256 and bytes nor supplied"
));
}
(_, _, _, Some(_)) => {
return Err(format!(
"resource {name:?} has supplied and a pin; a resource is one or the other"
));
}
_ => {
return Err(format!(
"resource {name:?} needs all of url, sha256 and bytes"
));
}
};
Ok(Self { name, source })
}
}
impl From<PluginResource> for ResourceEntry {
fn from(resource: PluginResource) -> Self {
let mut entry = Self {
name: resource.name,
url: None,
sha256: None,
bytes: None,
supplied: None,
};
match resource.source {
ResourceSource::Pinned(PinnedResource { url, sha256, bytes }) => {
entry.url = Some(url);
entry.sha256 = Some(sha256);
entry.bytes = Some(bytes);
}
ResourceSource::Supplied(slot) => entry.supplied = Some(slot),
}
entry
}
}
#[cfg(feature = "schema")]
mod resource_schema {
use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
use super::{MAX_PLUGIN_RESOURCE_BYTES, SuppliedSlot};
#[derive(JsonSchema)]
#[schemars(deny_unknown_fields, rename = "PinnedResource")]
#[expect(dead_code, reason = "a shape for the schema alone")]
struct PinnedResourceEntry {
#[schemars(length(min = 1))]
name: String,
url: String,
#[schemars(regex(pattern = r"^[0-9a-f]{64}$"))]
sha256: String,
#[schemars(range(max = MAX_PLUGIN_RESOURCE_BYTES))]
bytes: u64,
}
#[derive(JsonSchema)]
#[schemars(deny_unknown_fields, rename = "SuppliedResource")]
#[expect(dead_code, reason = "a shape for the schema alone")]
struct SuppliedResourceEntry {
#[schemars(regex(pattern = r"^[A-Za-z0-9_-][A-Za-z0-9._-]{0,63}$"))]
name: String,
supplied: SuppliedSlot,
}
impl JsonSchema for super::PluginResource {
fn schema_name() -> std::borrow::Cow<'static, str> {
"PluginResource".into()
}
fn json_schema(generator: &mut SchemaGenerator) -> Schema {
json_schema!({
"description": "A file a plugin reads: pinned by the manifest, or a slot the \
caller fills at load time.",
"oneOf": [
generator.subschema_for::<PinnedResourceEntry>(),
generator.subschema_for::<SuppliedResourceEntry>(),
]
})
}
}
}
#[must_use]
pub fn is_slot_name(name: &str) -> bool {
let allowed = |byte: u8| byte.is_ascii_alphanumeric() || matches!(byte, b'_' | b'-');
name.len() <= MAX_SLOT_NAME_BYTES
&& name.bytes().next().is_some_and(allowed)
&& name.bytes().all(|byte| allowed(byte) || byte == b'.')
}
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ManifestError {
UnsupportedAbi(String),
Invalid(String),
}
impl fmt::Display for ManifestError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnsupportedAbi(abi) => write!(
formatter,
"the plugin needs ABI {abi}; this build implements {}",
alpha2_plugin_abi::ABI
),
Self::Invalid(why) => write!(formatter, "invalid plugin manifest: {why}"),
}
}
}
impl std::error::Error for ManifestError {}
impl PluginManifest {
pub fn from_json(text: &str) -> Result<Self, ManifestError> {
if text.len() > MAX_MANIFEST_BYTES {
return Err(ManifestError::Invalid(format!(
"longer than {MAX_MANIFEST_BYTES} bytes"
)));
}
let value: serde_json::Value = serde_json::from_str(text)
.map_err(|error| ManifestError::Invalid(error.to_string()))?;
match value.get("abi").and_then(serde_json::Value::as_str) {
Some(abi) if abi != alpha2_plugin_abi::ABI => {
return Err(ManifestError::UnsupportedAbi(abi.to_owned()));
}
_ => {}
}
let manifest: Self = serde_json::from_value(value)
.map_err(|error| ManifestError::Invalid(error.to_string()))?;
manifest.validate().map_err(ManifestError::Invalid)?;
Ok(manifest)
}
fn validate(&self) -> Result<(), String> {
if self.id != self.descriptor.id {
return Err(format!(
"id {} differs from the descriptor's {}",
self.id, self.descriptor.id
));
}
if !semver(&self.version) {
return Err(format!(
"version {} is not a semantic version",
self.version
));
}
super::session::validate_descriptor(&self.descriptor).map_err(|error| error.to_string())?;
if self.artifacts.is_empty() || self.artifacts.len() > MAX_PLUGIN_ARTIFACTS {
return Err(format!("needs 1 to {MAX_PLUGIN_ARTIFACTS} artifacts"));
}
if self.resources.len() > MAX_PLUGIN_RESOURCES {
return Err(format!("lists more than {MAX_PLUGIN_RESOURCES} resources"));
}
let mut targets = HashSet::new();
for artifact in &self.artifacts {
if artifact.target.is_empty() || !targets.insert((&artifact.target, artifact.kind)) {
return Err(format!(
"artifact target {:?} is empty or listed twice for one type",
artifact.target
));
}
if (artifact.target == WASM_TARGET) != (artifact.kind == ArtifactKind::Wasm) {
return Err(format!(
"artifact {}: only the wasm32 target is a wasm module",
artifact.url
));
}
check_file(&artifact.url, &artifact.sha256)?;
if artifact.bytes > MAX_PLUGIN_ARTIFACT_BYTES {
return Err(format!(
"artifact {} is longer than {MAX_PLUGIN_ARTIFACT_BYTES} bytes",
artifact.url
));
}
}
self.validate_resources()?;
self.about.as_ref().map_or(Ok(()), PluginAbout::validate)
}
fn validate_resources(&self) -> Result<(), String> {
let mut names = HashSet::new();
let mut directories = Vec::new();
let (mut slots, mut total) = (0_usize, 0_u64);
for resource in &self.resources {
if resource.name.is_empty() || !names.insert(&resource.name) {
return Err(format!(
"resource name {:?} is empty or repeated",
resource.name
));
}
match &resource.source {
ResourceSource::Pinned(pinned) => {
check_file(&pinned.url, &pinned.sha256)?;
if pinned.bytes > MAX_PLUGIN_RESOURCE_BYTES {
return Err(format!(
"resource {} is longer than {MAX_PLUGIN_RESOURCE_BYTES} bytes",
resource.name
));
}
total = total.saturating_add(pinned.bytes);
}
ResourceSource::Supplied(slot) => {
slots += 1;
check_slot(&resource.name, slot)?;
if slot.shape == SlotShape::Directory {
directories.push(format!("{}/", resource.name));
}
}
}
}
if slots > MAX_PLUGIN_SLOTS {
return Err(format!(
"declares more than {MAX_PLUGIN_SLOTS} resource slots"
));
}
if let Some(resource) = self.resources.iter().find(|resource| {
resource.pinned().is_some()
&& directories
.iter()
.any(|prefix| resource.name.starts_with(prefix.as_str()))
}) {
return Err(format!(
"pinned resource {} lies under a directory slot's names",
resource.name
));
}
if total > MAX_PLUGIN_RESOURCES_TOTAL_BYTES {
return Err(format!(
"the pinned resources are longer than {MAX_PLUGIN_RESOURCES_TOTAL_BYTES} bytes \
together"
));
}
Ok(())
}
#[must_use]
pub fn artifact(&self, target: &str, kind: ArtifactKind) -> Option<&PluginArtifact> {
self.artifacts
.iter()
.find(|artifact| artifact.target == target && artifact.kind == kind)
}
}
pub const WASM_TARGET: &str = "wasm32";
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Reference<'a> {
Relative(&'a str),
Https(&'a str),
}
fn scheme(url: &str) -> Option<&str> {
let (scheme, _) = url.split_once(':')?;
let valid = scheme.starts_with(|c: char| c.is_ascii_alphabetic())
&& scheme
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.'));
valid.then_some(scheme)
}
pub fn reference(url: &str) -> Result<Reference<'_>, String> {
let refused = |why: &str| Err(format!("{url}: {why}"));
if url.is_empty() {
return Err("a URL is empty".to_owned());
}
match scheme(url) {
Some("https") => return Ok(Reference::Https(url)),
Some(scheme) if scheme.len() == 1 => {
return refused("a drive-prefixed path is not under the manifest");
}
Some(_) => return refused("only https: URLs and paths under the manifest are allowed"),
None => {}
}
if url.contains(['\\', '\0']) {
return refused("a path takes neither backslashes nor NUL");
}
if url.starts_with('/') {
return refused("an absolute path is not under the manifest");
}
let path = url.split(['?', '#']).next().unwrap_or_default();
let mut depth = 0_usize;
for segment in path.split('/') {
match segment {
"" | "." => {}
".." => {
depth = depth
.checked_sub(1)
.ok_or_else(|| format!("{url}: the path climbs above the manifest"))?;
}
_ => depth += 1,
}
}
Ok(Reference::Relative(url))
}
fn check_slot(name: &str, slot: &SuppliedSlot) -> Result<(), String> {
if !is_slot_name(name) {
return Err(format!(
"slot name {name:?} is not 1 to {MAX_SLOT_NAME_BYTES} of A-Z, a-z, 0-9, '.', '_' and \
'-', not starting with '.'"
));
}
if slot.max_bytes == Some(0) {
return Err(format!("slot {name}: max_bytes is 0"));
}
if slot.accept.len() > MAX_SLOT_ACCEPT {
return Err(format!(
"slot {name}: accept lists more than {MAX_SLOT_ACCEPT} suffixes"
));
}
if let Some(suffix) = slot
.accept
.iter()
.find(|suffix| suffix.is_empty() || suffix.len() > MAX_SLOT_ACCEPT_BYTES)
{
return Err(format!(
"slot {name}: accept suffix {suffix:?} is empty or longer than \
{MAX_SLOT_ACCEPT_BYTES} bytes"
));
}
Ok(())
}
fn check_file(url: &str, sha256: &str) -> Result<(), String> {
reference(url)?;
if sha256.len() != 64
|| !sha256
.bytes()
.all(|byte| matches!(byte, b'0'..=b'9' | b'a'..=b'f'))
{
return Err(format!(
"{url}: sha256 is not 64 lowercase hexadecimal digits"
));
}
Ok(())
}
fn semver(version: &str) -> bool {
let (rest, build) = version
.split_once('+')
.map_or((version, None), |(a, b)| (a, Some(b)));
let (core, pre) = rest
.split_once('-')
.map_or((rest, None), |(a, b)| (a, Some(b)));
let identifiers = |text: &str| {
text.split('.').all(|part| {
!part.is_empty()
&& part
.bytes()
.all(|byte| byte.is_ascii_alphanumeric() || byte == b'-')
})
};
let numbers = core.split('.').collect::<Vec<_>>();
numbers.len() == 3
&& numbers.iter().all(|part| {
!part.is_empty()
&& part.bytes().all(|byte| byte.is_ascii_digit())
&& (part.len() == 1 || !part.starts_with('0'))
})
&& pre.is_none_or(identifiers)
&& build.is_none_or(identifiers)
}
#[cfg(feature = "schema")]
#[must_use]
pub fn plugin_manifest_json_schema() -> serde_json::Value {
let mut schema = schemars::generate::SchemaSettings::draft2020_12()
.for_deserialize()
.into_generator()
.into_root_schema_for::<PluginManifest>();
schema.insert(
"$id".to_owned(),
serde_json::Value::String(PLUGIN_MANIFEST_SCHEMA_ID.to_owned()),
);
schema.insert(
"title".to_owned(),
serde_json::Value::String(format!(
"alpha2 plugin manifest, ABI {}",
alpha2_plugin_abi::ABI
)),
);
let mut value = schema.to_value();
alpha2_model::finish_json_schema(&mut value);
value
}
pub const PLUGIN_MANIFEST_SCHEMA_ID: &str = "https://alpha2.sh/schema/plugin-manifest.v1.json";
#[cfg(feature = "schema")]
#[must_use]
pub fn plugin_manifest_shape_json() -> serde_json::Value {
let mut value = plugin_manifest_json_schema();
alpha2_model::strip_json_schema_prose(&mut value);
value
}