core_api/
plugin_contributions.rs1use serde::{Deserialize, Serialize};
3use sha2::{Digest, Sha256};
4use std::collections::BTreeMap;
5
6pub const NODE_CONTRIBUTIONS_TARGET: &str = "/node/contributions/read";
7pub const CONTRIBUTIONS_INDEX_FILE: &str = "contributions.json";
8pub const CONTRIBUTIONS_INDEX_MAX_BYTES: usize = 128 * 1024;
9pub const CONTRIBUTION_MAX_BYTES: usize = 1024 * 1024;
11pub const CONTRIBUTIONS_MAX_FILES: usize = 512;
12
13#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
14#[serde(rename_all = "camelCase", deny_unknown_fields)]
15pub struct ContributionFile {
16 pub size: u64,
17 pub sha256: String,
18}
19
20impl ContributionFile {
21 pub fn validate(&self, maximum: usize) -> Result<(), String> {
22 if self.size > maximum as u64 || !crate::plugin_preview::is_sha256(&self.sha256) {
23 return Err("Invalid Plugin contribution size or digest".into());
24 }
25 Ok(())
26 }
27
28 pub fn verify(&self, bytes: &[u8], maximum: usize) -> Result<(), String> {
29 self.validate(maximum)?;
30 if bytes.len() as u64 != self.size || format!("{:x}", Sha256::digest(bytes)) != self.sha256
31 {
32 return Err("Plugin contribution content differs from installed source".into());
33 }
34 Ok(())
35 }
36}
37
38#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
39#[serde(rename_all = "camelCase", deny_unknown_fields)]
40pub struct PluginContributionIndex {
41 pub format_version: u32,
42 pub plugin_id: String,
43 pub version: String,
44 pub files: BTreeMap<String, ContributionFile>,
46}
47
48impl PluginContributionIndex {
49 pub fn parse(bytes: &[u8], plugin_id: &str, version: &str) -> Result<Self, String> {
50 if bytes.len() > CONTRIBUTIONS_INDEX_MAX_BYTES {
51 return Err("Plugin contribution index exceeds size limit".into());
52 }
53 let index: Self = serde_json::from_slice(bytes).map_err(|e| e.to_string())?;
54 if index.format_version != 1
55 || index.plugin_id != plugin_id
56 || index.version != version
57 || index.files.is_empty()
58 || index.files.len() > CONTRIBUTIONS_MAX_FILES
59 {
60 return Err("Invalid Plugin contribution index identity or file count".into());
61 }
62 for (path, file) in &index.files {
63 if !is_contribution_path(path) {
64 return Err("Invalid public Plugin contribution path".into());
65 }
66 file.validate(CONTRIBUTION_MAX_BYTES)?;
67 }
68 Ok(index)
69 }
70}
71
72pub fn is_contribution_path(path: &str) -> bool {
73 if path.is_empty()
74 || path.len() > 512
75 || path.contains('\\')
76 || path.chars().any(char::is_control)
77 || path
78 .split('/')
79 .any(|part| part.is_empty() || part == "." || part == ".." || part.starts_with('.'))
80 {
81 return false;
82 }
83 path == "extension-package.json"
84 || path.starts_with("contracts/")
85 || path.starts_with("skills/")
86 || path.starts_with("ui/")
87 || path.starts_with("assets/")
88}
89
90#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
91#[serde(rename_all = "camelCase", deny_unknown_fields)]
92pub struct ReadNodeContribution {
93 pub source_manifest_sha256: String,
95 pub resource: Option<String>,
97}
98
99#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
100#[serde(rename_all = "camelCase", deny_unknown_fields)]
101pub struct NodeContributionContent {
102 pub source_manifest_sha256: String,
103 pub resource: Option<String>,
104 pub content_base64: String,
105}
106
107#[cfg(test)]
108mod tests {
109 use super::*;
110 use serde_json::json;
111
112 #[test]
113 fn source_digest_size_and_safe_public_paths_are_required() {
114 let bytes = b"public contract";
115 let file = ContributionFile {
116 size: bytes.len() as u64,
117 sha256: format!("{:x}", Sha256::digest(bytes)),
118 };
119 file.verify(bytes, CONTRIBUTION_MAX_BYTES).unwrap();
120 assert!(file
121 .verify(b"altered contract", CONTRIBUTION_MAX_BYTES)
122 .is_err());
123 assert!(file.verify(bytes, 1).is_err());
124 let mut raw = json!({"formatVersion":1,"pluginId":"example","version":"1.0.0","files":{"skills/native.md":file}});
125 assert!(PluginContributionIndex::parse(
126 &serde_json::to_vec(&raw).unwrap(),
127 "example",
128 "1.0.0"
129 )
130 .is_ok());
131 assert!(PluginContributionIndex::parse(
132 &serde_json::to_vec(&raw).unwrap(),
133 "other",
134 "1.0.0"
135 )
136 .is_err());
137 for path in [
138 "../identity.json",
139 "bin/example",
140 "skills/../../key",
141 "skills/.env",
142 "ui//main.js",
143 "/ui/main.js",
144 "ui\\main.js",
145 ] {
146 let reference = raw["files"]["skills/native.md"].clone();
147 raw["files"] = json!({path: reference});
148 assert!(
149 PluginContributionIndex::parse(
150 &serde_json::to_vec(&raw).unwrap(),
151 "example",
152 "1.0.0"
153 )
154 .is_err(),
155 "{path}"
156 );
157 }
158 }
159}