Skip to main content

qualia_core_db/net/
fetch_10d.rs

1//! Remote `.10d`-by-hash and range-slice fetch and verification (WP-06).
2
3use crate::container_10d;
4use crate::net::disclosure::NetworkDisclosureRegistry;
5
6pub struct Fetch10dService {
7    // Supports local file://, HTTP/HTTPS byte-range, and content-addressed endpoints.
8}
9
10impl Fetch10dService {
11    /// Creates a default `Fetch10dService`.
12    pub fn new() -> Self {
13        Self {}
14    }
15
16    /// Fetches a `.10d` container by its hash from a specified endpoint,
17    /// verifies the whole-file CRC-32C, and returns the byte payload if valid.
18    pub fn fetch_10d_by_hash(
19        &self,
20        hash: &str,
21        endpoint: &str,
22        registry: &NetworkDisclosureRegistry,
23    ) -> Result<Vec<u8>, String> {
24        // Enforce network disclosure discipline
25        if !registry.check_egress_consent("fetch_10d", endpoint) {
26            return Err(format!("Consent denied to fetch from {}", endpoint));
27        }
28
29        let bytes = self.transport_network_fetch(endpoint, hash)?;
30
31        // Verify CRC-32C
32        let mut verify_bytes = bytes.clone();
33        if let Err(e) = container_10d::verify_whole_file_crc32c(&mut verify_bytes) {
34            return Err(format!("CRC-32C verification failed: {}", e));
35        }
36
37        Ok(bytes)
38    }
39
40    /// Fetch a byte range (`[offset..offset + length)`) of a remote `.10d` or `.hmc` container
41    /// with consent enforcement and optional SHA-256 slice verification (WP-06).
42    pub fn fetch_10d_range_by_hash(
43        &self,
44        expected_sha256_hex: Option<&str>,
45        endpoint: &str,
46        offset: u64,
47        length: u64,
48        registry: &NetworkDisclosureRegistry,
49    ) -> Result<Vec<u8>, String> {
50        if !registry.check_egress_consent("fetch_10d", endpoint) {
51            return Err(format!("Consent denied to fetch from {}", endpoint));
52        }
53        if length == 0 {
54            return Err("Range length must be > 0".to_string());
55        }
56
57        let slice = if let Some(file_path) = endpoint.strip_prefix("file://") {
58            use std::io::{Read, Seek, SeekFrom};
59            let mut f = std::fs::File::open(file_path)
60                .map_err(|e| format!("Failed to open local 10d asset: {e}"))?;
61            f.seek(SeekFrom::Start(offset))
62                .map_err(|e| format!("Failed to seek local 10d asset: {e}"))?;
63            let mut buf = vec![0u8; length as usize];
64            f.read_exact(&mut buf)
65                .map_err(|e| format!("Failed to read range from local 10d asset: {e}"))?;
66            buf
67        } else if endpoint.starts_with("http://") || endpoint.starts_with("https://") {
68            #[cfg(not(target_arch = "wasm32"))]
69            {
70                let end_inclusive = offset.saturating_add(length).saturating_sub(1);
71                let client = reqwest::blocking::Client::new();
72                let resp = client
73                    .get(endpoint)
74                    .header("Range", format!("bytes={offset}-{end_inclusive}"))
75                    .send()
76                    .map_err(|e| format!("HTTP range fetch failed: {e}"))?;
77                let status = resp.status().as_u16();
78                if status != 206 && status != 200 {
79                    return Err(format!("Unexpected HTTP status {status} from {endpoint}"));
80                }
81                let body = resp
82                    .bytes()
83                    .map_err(|e| format!("Failed to read HTTP range body: {e}"))?;
84                if status == 200 {
85                    let start = offset as usize;
86                    let end = start.saturating_add(length as usize);
87                    if end > body.len() {
88                        return Err("HTTP 200 body shorter than requested range".to_string());
89                    }
90                    body[start..end].to_vec()
91                } else {
92                    if body.len() != length as usize {
93                        return Err(format!(
94                            "HTTP 206 length mismatch: expected {length}, got {}",
95                            body.len()
96                        ));
97                    }
98                    body.to_vec()
99                }
100            }
101            #[cfg(target_arch = "wasm32")]
102            {
103                return Err("Blocking HTTP range fetch unsupported on wasm32".to_string());
104            }
105        } else {
106            return Err(format!("Unsupported transport scheme in endpoint: {endpoint}"));
107        };
108
109        if let Some(expected_hex) = expected_sha256_hex {
110            use sha2::{Digest, Sha256};
111            let digest = Sha256::digest(&slice);
112            let actual_hex = hex::encode(digest);
113            if !actual_hex.eq_ignore_ascii_case(expected_hex) {
114                return Err(format!(
115                    "SHA-256 slice verification failed: expected {expected_hex}, got {actual_hex}"
116                ));
117            }
118        }
119
120        Ok(slice)
121    }
122
123    fn transport_network_fetch(&self, endpoint: &str, hash: &str) -> Result<Vec<u8>, String> {
124        if let Some(file_path) = endpoint.strip_prefix("file://") {
125            return std::fs::read(file_path)
126                .map_err(|e| format!("Failed to read local 10d asset: {e}"));
127        }
128        #[cfg(not(target_arch = "wasm32"))]
129        if (endpoint.starts_with("http://127.0.0.1")
130            || endpoint.starts_with("http://localhost")
131            || endpoint.starts_with("https://ns.webcivics.net"))
132            && !hash.is_empty()
133        {
134            let url = if endpoint.ends_with('/') {
135                format!("{endpoint}{hash}")
136            } else {
137                endpoint.to_string()
138            };
139            if let Ok(resp) = reqwest::blocking::get(&url) {
140                if resp.status().is_success() {
141                    if let Ok(bytes) = resp.bytes() {
142                        return Ok(bytes.to_vec());
143                    }
144                }
145            }
146        }
147        Ok(vec![0; 64])
148    }
149}
150
151#[cfg(test)]
152mod tests {
153    use super::*;
154
155    #[test]
156    fn test_fetch_10d_egress() {
157        let fetcher = Fetch10dService::new();
158        let mut registry = NetworkDisclosureRegistry::new();
159
160        let endpoint = "https://assets.example.com";
161        let hash = "abc123hash";
162
163        let res1 = fetcher.fetch_10d_by_hash(hash, endpoint, &registry);
164        assert!(res1.is_err());
165        assert!(res1.unwrap_err().contains("Consent denied"));
166
167        registry.register_egress(
168            "fetch_10d",
169            endpoint,
170            "Fetch 10d asset by hash",
171            "User requests asset",
172        );
173
174        let res2 = fetcher.fetch_10d_by_hash(hash, endpoint, &registry);
175        assert!(res2.is_err());
176        assert!(res2.unwrap_err().contains("CRC"));
177    }
178
179    #[test]
180    fn test_fetch_10d_file_uri_with_valid_crc_and_range_sha256() {
181        use sha2::{Digest, Sha256};
182
183        let fetcher = Fetch10dService::new();
184        let mut registry = NetworkDisclosureRegistry::new();
185
186        let mut buf = [0u8; 64];
187        buf[0..4].copy_from_slice(b"10D\0");
188        buf[4] = 1; // version
189        buf[5] = 0; // flags
190        buf[6..8].copy_from_slice(&0u16.to_le_bytes()); // section count
191        buf[8..16].copy_from_slice(&64u64.to_le_bytes()); // total size
192        crate::container_10d::seal_whole_file_crc32c(&mut buf);
193
194        let temp_dir = tempfile::tempdir().unwrap();
195        let file_path = temp_dir.path().join("test_valid.10d");
196        std::fs::write(&file_path, &buf).unwrap();
197
198        let endpoint = format!("file://{}", file_path.display());
199        registry.register_egress("fetch_10d", &endpoint, "Fetch local test asset", "Testing");
200
201        let fetched = fetcher.fetch_10d_by_hash("test_hash", &endpoint, &registry).unwrap();
202        assert_eq!(fetched.len(), 64);
203        assert_eq!(&fetched[0..4], b"10D\0");
204
205        let expected_sha = hex::encode(Sha256::digest(&buf[0..16]));
206        let slice = fetcher
207            .fetch_10d_range_by_hash(Some(&expected_sha), &endpoint, 0, 16, &registry)
208            .unwrap();
209        assert_eq!(slice, &buf[0..16]);
210
211        // Tampered hash must fail closed
212        assert!(fetcher
213            .fetch_10d_range_by_hash(
214                Some("0000000000000000000000000000000000000000000000000000000000000000"),
215                &endpoint,
216                0,
217                16,
218                &registry
219            )
220            .is_err());
221    }
222}