Skip to main content

ic_backup/model/ic_request/
mod.rs

1//! Closed host-ingress IC request codec; no transport, authority or effect admission.
2
3mod method;
4pub use method::{IcManagementMethodRecord, IcRequestEffect};
5
6use crate::model::{artifacts::ArtifactChecksumRecord, attempt_journal::OperationBindingRecord};
7use candid::Principal;
8use ic_management_canister_types::{
9    CanisterIdRecord, LoadCanisterSnapshotArgs, TakeCanisterSnapshotArgs,
10};
11use serde::{Deserialize, Deserializer, Serialize, de};
12use std::fmt;
13use thiserror::Error;
14
15/// Maximum raw snapshot identifier bytes, distinct from any backend's rendered token.
16pub const MAX_IC_SNAPSHOT_ID_BYTES: usize = 256;
17/// Maximum derived Candid argument bytes; checked before a request is admitted.
18pub const MAX_IC_ARGUMENT_BYTES: usize = 4096;
19/// Bound callers must use for encoded JSON input/output when retaining this record.
20pub const MAX_IC_REQUEST_RECORD_BYTES: u64 = 8192;
21
22/// Passive method/target/snapshot input; network, caller and permissions stay with their owners.
23#[derive(Clone, Debug)]
24pub struct IcManagementRequest {
25    /// One of the closed supported management methods.
26    pub method: IcManagementMethodRecord,
27    /// Exact effective canister principal; normalized on admission.
28    pub target: String,
29    /// Raw nonempty snapshot bytes required only for load; otherwise exactly None.
30    pub snapshot_id: Option<Vec<u8>>,
31}
32
33/// Immutable v1 request declaration with model-derived exact Candid bytes.
34///
35/// All supported methods use replicated update ingress, including observations.
36/// Network/caller selection, signing, freshness, budgets, custody and lifecycle
37/// safety are not granted by this record or its digest.
38#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
39#[serde(try_from = "RequestFields")]
40pub struct IcManagementRequestRecord {
41    version: u16,
42    method: IcManagementMethodRecord,
43    target: String,
44    snapshot_id: Option<Vec<u8>>,
45    #[serde(skip)]
46    target_bytes: Vec<u8>,
47    #[serde(skip)]
48    arguments: Vec<u8>,
49}
50#[derive(Deserialize)]
51#[serde(deny_unknown_fields)]
52struct RequestFields {
53    version: u16,
54    method: IcManagementMethodRecord,
55    target: String,
56    #[serde(deserialize_with = "required_snapshot")]
57    snapshot_id: Option<Vec<u8>>,
58}
59impl TryFrom<RequestFields> for IcManagementRequestRecord {
60    type Error = IcRequestError;
61    fn try_from(fields: RequestFields) -> Result<Self, Self::Error> {
62        if fields.version != 1 {
63            return Err(IcRequestError::UnsupportedVersion(fields.version));
64        }
65        Self::new(IcManagementRequest {
66            method: fields.method,
67            target: fields.target,
68            snapshot_id: fields.snapshot_id,
69        })
70    }
71}
72impl IcManagementRequestRecord {
73    /// Validate the closed request and encode exact host-ingress arguments.
74    ///
75    /// Capture retains code and creates a new snapshot; sender canister version
76    /// stays absent for host ingress. Load names exact raw bytes, with no identity remap.
77    ///
78    /// # Errors
79    /// Rejects malformed principals, missing/unexpected/oversized snapshot IDs,
80    /// Candid encoding failure and excessive derived argument bytes.
81    pub fn new(request: IcManagementRequest) -> Result<Self, IcRequestError> {
82        let target = crate::model::principal::canonical_text(&request.target)
83            .ok_or(IcRequestError::InvalidTarget)?;
84        let principal = Principal::from_text(&target).map_err(|_| IcRequestError::InvalidTarget)?;
85        match (request.method, request.snapshot_id.as_deref()) {
86            (IcManagementMethodRecord::LoadCanisterSnapshot, None) => {
87                return Err(IcRequestError::SnapshotRequired);
88            }
89            (IcManagementMethodRecord::LoadCanisterSnapshot, Some(bytes)) => {
90                if bytes.is_empty() || bytes.len() > MAX_IC_SNAPSHOT_ID_BYTES {
91                    return Err(IcRequestError::InvalidSnapshotId);
92                }
93            }
94            (_, Some(_)) => return Err(IcRequestError::UnexpectedSnapshot),
95            (_, None) => {}
96        }
97        let arguments = match request.method {
98            IcManagementMethodRecord::TakeCanisterSnapshot => {
99                candid::encode_one(TakeCanisterSnapshotArgs {
100                    canister_id: principal,
101                    replace_snapshot: None,
102                    uninstall_code: Some(false),
103                    sender_canister_version: None,
104                })
105            }
106            IcManagementMethodRecord::LoadCanisterSnapshot => {
107                candid::encode_one(LoadCanisterSnapshotArgs {
108                    canister_id: principal,
109                    snapshot_id: request
110                        .snapshot_id
111                        .clone()
112                        .ok_or(IcRequestError::SnapshotRequired)?,
113                    sender_canister_version: None,
114                })
115            }
116            IcManagementMethodRecord::CanisterStatus
117            | IcManagementMethodRecord::ListCanisterSnapshots
118            | IcManagementMethodRecord::StartCanister
119            | IcManagementMethodRecord::StopCanister => candid::encode_one(CanisterIdRecord {
120                canister_id: principal,
121            }),
122        }
123        .map_err(|error| IcRequestError::Encoding(error.to_string()))?;
124        if arguments.len() > MAX_IC_ARGUMENT_BYTES {
125            return Err(IcRequestError::ArgumentsTooLarge);
126        }
127        Ok(Self {
128            version: 1,
129            method: request.method,
130            target,
131            snapshot_id: request.snapshot_id,
132            target_bytes: principal.as_slice().to_vec(),
133            arguments,
134        })
135    }
136    /// Read the exact closed method; its effect class does not grant spending authority.
137    #[must_use]
138    pub const fn method(&self) -> IcManagementMethodRecord {
139        self.method
140    }
141    /// Read the canonical effective routing target, also encoded inside the arguments.
142    #[must_use]
143    pub fn target(&self) -> &str {
144        &self.target
145    }
146    /// Read exact raw load snapshot bytes, without backend token interpretation.
147    #[must_use]
148    pub fn snapshot_id(&self) -> Option<&[u8]> {
149        self.snapshot_id.as_deref()
150    }
151    /// Read derived exact Candid bytes for a future qualified update-ingress transport.
152    #[must_use]
153    pub fn arguments(&self) -> &[u8] {
154        &self.arguments
155    }
156    /// Read the fixed management receiver; the effective routing target is separate.
157    #[must_use]
158    pub const fn receiver(&self) -> &'static str {
159        "aaaaa-aa"
160    }
161    /// Hash fixed receiver, effective target, update mode, exact method and Candid bytes.
162    ///
163    /// Network/caller/intent/release/budgets stay outside this nonrecursive payload
164    /// digest and are bound by the existing operation plan/attempt authority.
165    #[must_use]
166    pub fn digest(&self) -> ArtifactChecksumRecord {
167        management_request_digest(&self.target_bytes, self.method.name(), &self.arguments)
168    }
169    /// Check exact payload target/digest and mutation class against original declared binding.
170    ///
171    /// This verifies bytes only; it does not authenticate the binding or admit dispatch.
172    /// # Errors
173    /// Rejects observation methods and changed target or request digest.
174    pub fn validate_mutation_binding(
175        &self,
176        binding: &OperationBindingRecord,
177    ) -> Result<(), IcRequestError> {
178        self.require_effect(IcRequestEffect::Mutation)?;
179        self.validate_identity(binding, binding.request())
180    }
181    /// Check an observation payload against its exact target and reserved observation digest.
182    ///
183    /// The original binding still names the mutation. The observation digest is
184    /// separately retained by the existing journal's observation reservation.
185    /// # Errors
186    /// Rejects mutation methods, changed target or a different observation digest.
187    pub fn validate_observation_binding(
188        &self,
189        binding: &OperationBindingRecord,
190        request: &ArtifactChecksumRecord,
191    ) -> Result<(), IcRequestError> {
192        self.require_effect(IcRequestEffect::Observation)?;
193        self.validate_identity(binding, request.hash())
194    }
195    fn require_effect(&self, expected: IcRequestEffect) -> Result<(), IcRequestError> {
196        if self.method.effect() != expected {
197            return Err(IcRequestError::EffectMismatch { expected });
198        }
199        Ok(())
200    }
201    fn validate_identity(
202        &self,
203        binding: &OperationBindingRecord,
204        expected: &str,
205    ) -> Result<(), IcRequestError> {
206        if self.target != binding.target() {
207            return Err(IcRequestError::TargetMismatch);
208        }
209        if self.digest().hash() != expected {
210            return Err(IcRequestError::DigestMismatch);
211        }
212        Ok(())
213    }
214}
215
216// Callers own canonical principal bytes, a fixed ASCII method and bounded arguments.
217pub(super) fn management_request_digest(
218    target_bytes: &[u8],
219    method: &str,
220    arguments: &[u8],
221) -> ArtifactChecksumRecord {
222    let mut bytes = b"ic-backup/ic-management-request/v1\0".to_vec();
223    bytes.push(0); // Management receiver principal has zero raw bytes.
224    bytes.push(target_bytes.len().to_le_bytes()[0]); // Principal <=29 raw bytes.
225    bytes.extend_from_slice(target_bytes);
226    bytes.push(1); // Replicated update ingress.
227    bytes.push(method.len().to_le_bytes()[0]); // Fixed names fit one byte.
228    bytes.extend_from_slice(method.as_bytes());
229    append_argument_length(&mut bytes, arguments.len());
230    bytes.extend_from_slice(arguments);
231    ArtifactChecksumRecord::from_bytes(&bytes)
232}
233
234#[expect(
235    clippy::cast_possible_truncation,
236    reason = "admitted argument byte length is at most 4096"
237)]
238fn append_argument_length(bytes: &mut Vec<u8>, length: usize) {
239    bytes.extend_from_slice(&(length as u32).to_be_bytes());
240}
241
242fn required_snapshot<'de, D: Deserializer<'de>>(
243    deserializer: D,
244) -> Result<Option<Vec<u8>>, D::Error> {
245    Ok(Option::<SnapshotBytes>::deserialize(deserializer)?.map(|bytes| bytes.0))
246}
247struct SnapshotBytes(Vec<u8>);
248impl<'de> Deserialize<'de> for SnapshotBytes {
249    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
250        struct BytesVisitor;
251        impl<'de> de::Visitor<'de> for BytesVisitor {
252            type Value = SnapshotBytes;
253            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
254                f.write_str("at most 256 exact snapshot bytes")
255            }
256            fn visit_seq<A: de::SeqAccess<'de>>(
257                self,
258                mut sequence: A,
259            ) -> Result<Self::Value, A::Error> {
260                let mut bytes = Vec::new();
261                while bytes.len() < MAX_IC_SNAPSHOT_ID_BYTES {
262                    match sequence.next_element::<u8>()? {
263                        Some(byte) => bytes.push(byte),
264                        None => return Ok(SnapshotBytes(bytes)),
265                    }
266                }
267                if sequence.next_element::<de::IgnoredAny>()?.is_some() {
268                    return Err(de::Error::custom(IcRequestError::InvalidSnapshotId));
269                }
270                Ok(SnapshotBytes(bytes))
271            }
272        }
273        deserializer.deserialize_seq(BytesVisitor)
274    }
275}
276
277/// Typed closed IC request, exact byte binding or codec admission failure.
278#[derive(Debug, Error, Eq, PartialEq)]
279pub enum IcRequestError {
280    /// Other product generations are not maintained.
281    #[error("unsupported IC request version {0}")]
282    UnsupportedVersion(u16),
283    /// Target text is not a bounded canonicalizable principal.
284    #[error("invalid IC request target principal")]
285    InvalidTarget,
286    /// Load requires exact raw snapshot bytes.
287    #[error("load snapshot request requires snapshot_id")]
288    SnapshotRequired,
289    /// A method that has no snapshot argument received one.
290    #[error("snapshot_id is not admitted for this IC request method")]
291    UnexpectedSnapshot,
292    /// Snapshot bytes are empty or exceed their finite bound.
293    #[error("snapshot_id must contain 1..={MAX_IC_SNAPSHOT_ID_BYTES} raw bytes")]
294    InvalidSnapshotId,
295    /// The pinned upstream Candid encoder rejected the typed arguments.
296    #[error("IC request Candid encoding failed: {0}")]
297    Encoding(String),
298    /// Derived argument size exceeds the codec bound.
299    #[error("IC request arguments exceed {MAX_IC_ARGUMENT_BYTES} bytes")]
300    ArgumentsTooLarge,
301    /// A mutation and observation request cannot substitute for each other.
302    #[error("IC request must have effect class {expected:?}")]
303    EffectMismatch {
304        /// Required semantic class, independent of replicated call mode.
305        expected: IcRequestEffect,
306    },
307    /// The payload/routing target differs from the original operation target.
308    #[error("IC request target differs from original binding")]
309    TargetMismatch,
310    /// Exact method/argument digest differs from its original reservation.
311    #[error("IC request digest differs from original binding")]
312    DigestMismatch,
313}
314
315#[cfg(test)]
316mod tests;