cmn-substrate 0.3.0

CMN protocol core — Ed25519 signatures, BLAKE3 tree hashing, JSON schema validation, URI parsing, and JCS canonicalization. Zero I/O, WASM-compatible.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! CMN protocol HTTP client — fetch domains, spores, synapse APIs.
//! Requires the `client` feature. WASM-compatible.

mod domain;
mod synapse;

pub use domain::{
    fetch_cmn_entry, fetch_mycelium, fetch_mycelium_manifest, fetch_spore_manifest, fetch_taste,
    merge_mycelium_spores,
};
pub use synapse::{
    fetch_lineage, fetch_synapse_cmn, fetch_synapse_mycelium_by_hash, fetch_synapse_spore,
    fetch_synapse_taste, fetch_taste_reports, post_synapse_pulse, search, BondNode, BondsQuery,
    BondsResponse, BondsResult, BondsTrace, SearchResponse, SearchResponseQuery, SearchResult,
    SearchResultItem, SynapseCmnQuery, SynapseCmnResponse, SynapseCmnResult,
    SynapseMyceliumByHashQuery, SynapseMyceliumByHashResponse, SynapseMyceliumByHashResult,
    SynapseSporeQuery, SynapseSporeResponse, SynapseSporeResult, SynapseTasteQuery,
    SynapseTasteResponse, SynapseTasteResult,
};

use anyhow::{anyhow, Result};
use serde::de::{DeserializeOwned, Error as DeError, MapAccess, SeqAccess, Visitor};
use serde::Deserialize;
use std::collections::HashSet;
use std::fmt;

/// Default response body limit for JSON/text client reads (64 MiB).
pub const DEFAULT_FETCH_MAX_BYTES: usize = 64 * 1024 * 1024;

/// Options for controlling fetch behaviour.
///
/// Use `Default::default()` (or `FetchOptions::new()`) for the default 64 MiB
/// response limit with no extra headers, or build with `with_max_bytes` /
/// `with_bearer_token` / `headers`. Use `FetchOptions::unlimited()` only
/// when the caller has an explicit external size bound.
#[derive(Debug, Clone)]
pub struct FetchOptions {
    /// Maximum response body bytes. `None` means no limit (uses reqwest `.json()`).
    pub max_bytes: Option<usize>,
    /// Extra headers to attach to the request.
    pub headers: Option<reqwest::header::HeaderMap>,
}

impl Default for FetchOptions {
    fn default() -> Self {
        Self {
            max_bytes: Some(DEFAULT_FETCH_MAX_BYTES),
            headers: None,
        }
    }
}

impl FetchOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn unlimited() -> Self {
        Self {
            max_bytes: None,
            ..Default::default()
        }
    }

    pub fn with_max_bytes(max_bytes: usize) -> Self {
        Self {
            max_bytes: Some(max_bytes),
            ..Default::default()
        }
    }

    /// Convenience: set a Bearer token (adds Authorization header).
    pub fn with_bearer_token(token: &str) -> Self {
        let mut headers = reqwest::header::HeaderMap::new();
        if let Ok(val) = format!("Bearer {token}").parse() {
            headers.insert(reqwest::header::AUTHORIZATION, val);
        }
        Self {
            headers: Some(headers),
            ..Default::default()
        }
    }

    /// Merge additional headers into this FetchOptions.
    pub fn headers(mut self, extra: reqwest::header::HeaderMap) -> Self {
        match &mut self.headers {
            Some(existing) => existing.extend(extra),
            None => self.headers = Some(extra),
        }
        self
    }

    /// Set max_bytes on an existing FetchOptions (builder style).
    pub fn max_bytes(mut self, max: usize) -> Self {
        self.max_bytes = Some(max);
        self
    }
}

/// Apply FetchOptions headers to a request builder.
pub(crate) fn apply_headers(
    mut req: reqwest::RequestBuilder,
    opts: &FetchOptions,
) -> reqwest::RequestBuilder {
    if let Some(ref headers) = opts.headers {
        for (key, value) in headers {
            req = req.header(key, value);
        }
    }
    req
}

/// Read a response body with a strict byte limit, then deserialize as JSON.
///
/// If `max_bytes` is `None`, falls back to reqwest's `.json()`.
pub async fn json_from_response<T: serde::de::DeserializeOwned>(
    response: reqwest::Response,
    source: &str,
    max_bytes: Option<usize>,
) -> Result<T> {
    let bytes = match max_bytes {
        None => response
            .bytes()
            .await
            .map_err(|e| anyhow!("Failed to read JSON body from {source}: {e}"))?
            .to_vec(),
        Some(limit) => read_body_limited(response, source, limit).await?,
    };
    json_from_slice_reject_duplicates(&bytes, source)
}

/// Deserialize JSON while rejecting duplicate object keys.
///
/// `serde_json` keeps the last duplicate key by default. For signed protocol
/// objects that creates parser-differential risk, so client fetch boundaries
/// fail closed before typed deserialization.
pub fn json_from_slice_reject_duplicates<T: DeserializeOwned>(
    bytes: &[u8],
    source: &str,
) -> Result<T> {
    reject_duplicate_json_keys(bytes, source)?;
    serde_json::from_slice(bytes).map_err(|e| anyhow!("Failed to parse JSON from {source}: {e}"))
}

fn reject_duplicate_json_keys(bytes: &[u8], source: &str) -> Result<()> {
    let mut de = serde_json::Deserializer::from_slice(bytes);
    NoDuplicateKeys::deserialize(&mut de)
        .map_err(|e| anyhow!("Duplicate-key-safe JSON parse failed from {source}: {e}"))?;
    de.end()
        .map_err(|e| anyhow!("Failed to parse JSON from {source}: {e}"))
}

struct NoDuplicateKeys;

impl<'de> Deserialize<'de> for NoDuplicateKeys {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_any(NoDuplicateVisitor)
    }
}

struct NoDuplicateVisitor;

impl<'de> Visitor<'de> for NoDuplicateVisitor {
    type Value = NoDuplicateKeys;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("any JSON value without duplicate object keys")
    }

    fn visit_bool<E>(self, _v: bool) -> std::result::Result<Self::Value, E> {
        Ok(NoDuplicateKeys)
    }

    fn visit_i64<E>(self, _v: i64) -> std::result::Result<Self::Value, E> {
        Ok(NoDuplicateKeys)
    }

    fn visit_u64<E>(self, _v: u64) -> std::result::Result<Self::Value, E> {
        Ok(NoDuplicateKeys)
    }

    fn visit_f64<E>(self, _v: f64) -> std::result::Result<Self::Value, E> {
        Ok(NoDuplicateKeys)
    }

    fn visit_str<E>(self, _v: &str) -> std::result::Result<Self::Value, E> {
        Ok(NoDuplicateKeys)
    }

    fn visit_string<E>(self, _v: String) -> std::result::Result<Self::Value, E> {
        Ok(NoDuplicateKeys)
    }

    fn visit_none<E>(self) -> std::result::Result<Self::Value, E> {
        Ok(NoDuplicateKeys)
    }

    fn visit_unit<E>(self) -> std::result::Result<Self::Value, E> {
        Ok(NoDuplicateKeys)
    }

    fn visit_some<D>(self, deserializer: D) -> std::result::Result<Self::Value, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        NoDuplicateKeys::deserialize(deserializer)
    }

    fn visit_seq<A>(self, mut seq: A) -> std::result::Result<Self::Value, A::Error>
    where
        A: SeqAccess<'de>,
    {
        while seq.next_element::<NoDuplicateKeys>()?.is_some() {}
        Ok(NoDuplicateKeys)
    }

    fn visit_map<A>(self, mut map: A) -> std::result::Result<Self::Value, A::Error>
    where
        A: MapAccess<'de>,
    {
        let mut keys = HashSet::new();
        while let Some(key) = map.next_key::<String>()? {
            if !keys.insert(key.clone()) {
                return Err(A::Error::custom(format!(
                    "duplicate JSON object key '{key}'"
                )));
            }
            map.next_value::<NoDuplicateKeys>()?;
        }
        Ok(NoDuplicateKeys)
    }
}

/// Read a response body as UTF-8 text with an optional byte limit.
///
/// If `max_bytes` is `None`, falls back to reqwest's `.text()`.
pub async fn text_from_response(
    response: reqwest::Response,
    source: &str,
    max_bytes: Option<usize>,
) -> Result<String> {
    match max_bytes {
        None => response
            .text()
            .await
            .map_err(|e| anyhow!("Failed to read text from {source}: {e}")),
        Some(limit) => {
            let bytes = read_body_limited(response, source, limit).await?;
            String::from_utf8(bytes)
                .map_err(|e| anyhow!("Response was not UTF-8 from {source}: {e}"))
        }
    }
}

/// Stream a response body into a writer with a strict byte limit.
///
/// Calls `on_progress(downloaded_bytes, total_bytes)` before reading starts and
/// after each chunk is written. Returns the total bytes written.
pub async fn download_response_to_writer<W, F>(
    response: reqwest::Response,
    source: &str,
    max_bytes: u64,
    mut writer: W,
    mut on_progress: F,
) -> Result<u64>
where
    W: std::io::Write,
    F: FnMut(u64, Option<u64>),
{
    let total_bytes = response.content_length();
    if let Some(cl) = total_bytes {
        if cl > max_bytes {
            return Err(anyhow!(
                "Remote payload too large from {source}: {cl} bytes exceeds limit {max_bytes}"
            ));
        }
    }

    on_progress(0, total_bytes);

    #[cfg(not(target_arch = "wasm32"))]
    {
        let mut response = response;
        let mut downloaded: u64 = 0;
        while let Some(chunk) = response
            .chunk()
            .await
            .map_err(|e| anyhow!("Failed reading response body from {source}: {e}"))?
        {
            downloaded = downloaded.saturating_add(chunk.len() as u64);
            if downloaded > max_bytes {
                return Err(anyhow!(
                    "Remote payload exceeded limit from {source}: >{max_bytes} bytes"
                ));
            }
            writer
                .write_all(&chunk)
                .map_err(|e| anyhow!("Failed writing response body for {source}: {e}"))?;
            on_progress(downloaded, total_bytes);
        }
        writer
            .flush()
            .map_err(|e| anyhow!("Failed flushing response body for {source}: {e}"))?;
        Ok(downloaded)
    }

    #[cfg(target_arch = "wasm32")]
    {
        let bytes = response
            .bytes()
            .await
            .map_err(|e| anyhow!("Failed reading response body from {source}: {e}"))?;
        if bytes.len() as u64 > max_bytes {
            return Err(anyhow!(
                "Remote payload exceeded limit from {source}: {} bytes exceeds limit {max_bytes}",
                bytes.len()
            ));
        }
        writer
            .write_all(&bytes)
            .map_err(|e| anyhow!("Failed writing response body for {source}: {e}"))?;
        writer
            .flush()
            .map_err(|e| anyhow!("Failed flushing response body for {source}: {e}"))?;
        on_progress(bytes.len() as u64, total_bytes);
        Ok(bytes.len() as u64)
    }
}

/// Read a response body with a strict byte limit.
///
/// On native targets, reads chunk-by-chunk for early abort on oversized payloads.
/// On WASM, falls back to full-body read then size check (streaming not available).
async fn read_body_limited(
    response: reqwest::Response,
    source: &str,
    max_bytes: usize,
) -> Result<Vec<u8>> {
    if let Some(cl) = response.content_length() {
        if cl > max_bytes as u64 {
            return Err(anyhow!(
                "Remote payload too large from {source}: {cl} bytes exceeds limit {max_bytes}"
            ));
        }
    }

    #[cfg(not(target_arch = "wasm32"))]
    {
        let mut response = response;
        let mut out = Vec::new();
        while let Some(chunk) = response
            .chunk()
            .await
            .map_err(|e| anyhow!("Failed reading response body from {source}: {e}"))?
        {
            if out.len().saturating_add(chunk.len()) > max_bytes {
                return Err(anyhow!(
                    "Remote payload exceeded limit from {source}: >{max_bytes} bytes"
                ));
            }
            out.extend_from_slice(&chunk);
        }
        Ok(out)
    }

    #[cfg(target_arch = "wasm32")]
    {
        let bytes = response
            .bytes()
            .await
            .map_err(|e| anyhow!("Failed reading response body from {source}: {e}"))?;
        if bytes.len() > max_bytes {
            return Err(anyhow!(
                "Remote payload exceeded limit from {source}: {} bytes exceeds limit {max_bytes}",
                bytes.len()
            ));
        }
        Ok(bytes.to_vec())
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use serde_json::Value;

    #[test]
    fn json_from_slice_rejects_duplicate_keys() {
        let err = json_from_slice_reject_duplicates::<Value>(
            br#"{"capsule":{"key":"a","key":"b"}}"#,
            "test-json",
        )
        .unwrap_err();
        assert!(err.to_string().contains("duplicate JSON object key 'key'"));
    }

    #[test]
    fn json_from_slice_rejects_nested_duplicate_keys() {
        let err = json_from_slice_reject_duplicates::<Value>(
            br#"[{"a":1},{"nested":{"b":2,"b":3}}]"#,
            "test-json",
        )
        .unwrap_err();
        assert!(err.to_string().contains("duplicate JSON object key 'b'"));
    }

    #[test]
    fn json_from_slice_accepts_distinct_keys() {
        let value = json_from_slice_reject_duplicates::<Value>(
            br#"{"capsule":{"key":"a","history":[{"key":"old"}]}}"#,
            "test-json",
        )
        .unwrap();
        assert_eq!(value["capsule"]["key"], "a");
    }
}

/// DNS resolver that rejects private/loopback IP addresses (SSRF protection).
///
/// Prevents DNS rebinding attacks: an attacker registers a domain that initially
/// resolves to a public IP, then changes DNS to point at an internal network address.
///
/// Requires the `client-safe-dns` feature. Not available on WASM (browsers handle
/// DNS resolution and CORS policy provides equivalent protection).
#[cfg(feature = "client-safe-dns")]
pub mod safe_dns {
    use reqwest::dns::{Addrs, Name, Resolve, Resolving};
    use std::net::{SocketAddr, ToSocketAddrs};

    /// A DNS resolver that performs system DNS resolution and rejects private IPs.
    pub struct SafeResolver;

    impl SafeResolver {
        pub fn new() -> Self {
            Self
        }
    }

    impl Default for SafeResolver {
        fn default() -> Self {
            Self::new()
        }
    }

    impl Resolve for SafeResolver {
        fn resolve(&self, name: Name) -> Resolving {
            let host = name.as_str().to_string();
            Box::pin(async move {
                let addrs: Vec<SocketAddr> = tokio::task::spawn_blocking(move || {
                    (host.as_str(), 0u16)
                        .to_socket_addrs()
                        .map(|iter| iter.collect::<Vec<_>>())
                })
                .await
                .map_err(|e| -> Box<dyn std::error::Error + Send + Sync> { Box::new(e) })?
                .map_err(|e| -> Box<dyn std::error::Error + Send + Sync> { Box::new(e) })?;

                let filtered: Vec<SocketAddr> = addrs
                    .into_iter()
                    .filter(|addr| crate::uri::is_public_ip(addr.ip()))
                    .collect();

                if filtered.is_empty() {
                    return Err("DNS resolved to private/loopback address (blocked)".into());
                }

                let addrs: Addrs = Box::new(filtered.into_iter());
                Ok(addrs)
            })
        }
    }
}

/// Build a reqwest client with SSRF protections enabled.
///
/// Requires the `client-safe-dns` feature. On WASM, use `reqwest::Client::new()`
/// directly (browsers provide CORS protection).
#[cfg(feature = "client-safe-dns")]
pub fn http_client(timeout_secs: u64) -> Result<reqwest::Client> {
    reqwest::Client::builder()
        .timeout(std::time::Duration::from_secs(timeout_secs))
        .redirect(reqwest::redirect::Policy::none())
        .dns_resolver(std::sync::Arc::new(safe_dns::SafeResolver::new()))
        .build()
        .map_err(|e| anyhow!("Failed to create HTTP client: {e}"))
}