ic-platform 0.33.7

Controlled Candid and IC runtime dependency boundary
Documentation
//! Stream the selected certified range list without allocating a list of principals.
use super::{Error, Principal};
use serde::{
    Deserialize, Deserializer,
    de::{self, DeserializeSeed, SeqAccess, Visitor},
};
use std::{cell::Cell, fmt};

pub(super) fn authorize(bytes: &[u8], canister: Principal, maximum: usize) -> Result<(), Error> {
    let exceeded = Cell::new(false);
    let mut decoder = serde_cbor::Deserializer::from_slice(bytes);
    let matched = Ranges {
        canister,
        maximum,
        exceeded: &exceeded,
    }
    .deserialize(&mut decoder)
    .map_err(|_| {
        if exceeded.get() {
            Error::RangeLimit
        } else {
            Error::InvalidRanges
        }
    })?;
    decoder.end().map_err(|_| Error::InvalidRanges)?;
    if matched {
        Ok(())
    } else {
        Err(Error::NotAuthorized)
    }
}

struct Ranges<'a> {
    canister: Principal,
    maximum: usize,
    exceeded: &'a Cell<bool>,
}
impl<'de> DeserializeSeed<'de> for Ranges<'_> {
    type Value = bool;
    fn deserialize<D: Deserializer<'de>>(self, d: D) -> Result<bool, D::Error> {
        d.deserialize_seq(self)
    }
}
impl<'de> Visitor<'de> for Ranges<'_> {
    type Value = bool;
    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("bounded canister-range pairs")
    }
    fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<bool, A::Error> {
        if seq.size_hint().is_some_and(|n| n > self.maximum) {
            self.exceeded.set(true);
            return Err(de::Error::custom("canister-range limit"));
        }
        let mut count = 0;
        let mut matched = false;
        while let Some((PrincipalBytes(start), PrincipalBytes(end))) =
            seq.next_element_seed(Pair {
                allowed: count < self.maximum,
                exceeded: self.exceeded,
            })?
        {
            if start > end {
                return Err(de::Error::custom("reversed canister range"));
            }
            matched |= start <= self.canister && self.canister <= end;
            count += 1;
        }
        Ok(matched)
    }
}

struct Pair<'a> {
    allowed: bool,
    exceeded: &'a Cell<bool>,
}
impl<'de> DeserializeSeed<'de> for Pair<'_> {
    type Value = (PrincipalBytes, PrincipalBytes);
    fn deserialize<D: Deserializer<'de>>(self, d: D) -> Result<Self::Value, D::Error> {
        if !self.allowed {
            self.exceeded.set(true);
            return Err(de::Error::custom("canister-range limit"));
        }
        // Tuple deserialization enforces exactly two elements, including indefinite arrays.
        <(PrincipalBytes, PrincipalBytes)>::deserialize(d)
    }
}

struct PrincipalBytes(Principal);
impl<'de> Deserialize<'de> for PrincipalBytes {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        d.deserialize_any(PrincipalVisitor)
    }
}
struct PrincipalVisitor;
impl Visitor<'_> for PrincipalVisitor {
    type Value = PrincipalBytes;
    fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("principal byte string of at most 29 bytes")
    }
    fn visit_bytes<E: de::Error>(self, bytes: &[u8]) -> Result<Self::Value, E> {
        Principal::try_from_slice(bytes)
            .map(PrincipalBytes)
            .map_err(E::custom)
    }
    fn visit_byte_buf<E: de::Error>(self, bytes: Vec<u8>) -> Result<Self::Value, E> {
        self.visit_bytes(&bytes)
    }
}