Skip to main content

rigspark_runtime/catalog_update/
transport.rs

1use super::{CatalogStatus, CatalogStore, CatalogUpdateError, MAX_ARTIFACT_BYTES};
2use std::time::Duration;
3
4pub const CATALOG_URL: &str =
5    "https://github.com/shashankswe2020-ux/rigspark/releases/download/catalog-v1/catalog.json";
6
7#[async_trait::async_trait]
8pub trait CatalogTransport: Send + Sync {
9    async fn download(&self) -> Result<Vec<u8>, CatalogUpdateError>;
10}
11
12pub struct OfficialCatalogTransport;
13
14fn allowed(url: &url::Url) -> bool {
15    url.scheme() == "https"
16        && url.port_or_known_default() == Some(443)
17        && url.username().is_empty()
18        && url.password().is_none()
19        && matches!(
20            url.host_str(),
21            Some(
22                "github.com"
23                    | "release-assets.githubusercontent.com"
24                    | "objects.githubusercontent.com"
25            )
26        )
27}
28
29#[async_trait::async_trait]
30impl CatalogTransport for OfficialCatalogTransport {
31    async fn download(&self) -> Result<Vec<u8>, CatalogUpdateError> {
32        let client = reqwest::Client::builder()
33            .https_only(true)
34            .connect_timeout(Duration::from_secs(10))
35            .timeout(Duration::from_secs(60))
36            .redirect(reqwest::redirect::Policy::custom(|attempt| {
37                if attempt.previous().len() >= 3 || !allowed(attempt.url()) {
38                    attempt.error("catalog redirect is not permitted")
39                } else {
40                    attempt.follow()
41                }
42            }))
43            .build()
44            .map_err(|_| CatalogUpdateError::Download)?;
45        let mut response = client
46            .get(CATALOG_URL)
47            .header(reqwest::header::USER_AGENT, "rigspark-catalog")
48            .send()
49            .await
50            .map_err(|_| CatalogUpdateError::Download)?;
51        if !response.status().is_success() {
52            return Err(CatalogUpdateError::Download);
53        }
54        if response
55            .content_length()
56            .is_some_and(|length| length > MAX_ARTIFACT_BYTES as u64)
57        {
58            return Err(CatalogUpdateError::TooLarge);
59        }
60        let mut bytes = Vec::new();
61        while let Some(chunk) = response
62            .chunk()
63            .await
64            .map_err(|_| CatalogUpdateError::Download)?
65        {
66            if chunk.len() > MAX_ARTIFACT_BYTES - bytes.len() {
67                return Err(CatalogUpdateError::TooLarge);
68            }
69            bytes.extend_from_slice(&chunk);
70        }
71        Ok(bytes)
72    }
73}
74
75impl CatalogStore {
76    pub async fn update(
77        &self,
78        transport: &dyn CatalogTransport,
79    ) -> Result<CatalogStatus, CatalogUpdateError> {
80        self.key.ok_or(CatalogUpdateError::NotConfigured)?;
81        self.install(&transport.download().await?)
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    #[test]
90    fn redirects_stay_on_official_https_hosts() {
91        assert!(allowed(&url::Url::parse(CATALOG_URL).unwrap()));
92        assert!(allowed(
93            &url::Url::parse("https://release-assets.githubusercontent.com/path?token=value")
94                .unwrap()
95        ));
96        for raw in [
97            "http://github.com/path",
98            "https://github.com:8443/path",
99            "https://github.com.evil.test/path",
100            "https://user@github.com/path",
101            "https://127.0.0.1/path",
102        ] {
103            assert!(!allowed(&url::Url::parse(raw).unwrap()));
104        }
105    }
106}