rigspark_runtime/catalog_update/
transport.rs1use super::{CatalogStatus, CatalogStore, CatalogUpdateError, MAX_ARTIFACT_BYTES};
2use std::time::Duration;
3
4pub const CATALOG_URL: &str =
5 "https://github.com/rigspark/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 follow_redirect(attempt.previous().len(), attempt.url()) {
38 attempt.follow()
39 } else {
40 attempt.error("catalog redirect is not permitted")
41 }
42 }))
43 .build()
44 .map_err(|_| CatalogUpdateError::Download)?;
45 let response = client
46 .get(CATALOG_URL)
47 .header(reqwest::header::USER_AGENT, "rigspark-catalog")
48 .send()
49 .await
50 .map_err(|_| CatalogUpdateError::Download)?;
51 read_catalog(response, MAX_ARTIFACT_BYTES).await
52 }
53}
54
55fn follow_redirect(previous: usize, url: &url::Url) -> bool {
57 previous < 3 && allowed(url)
58}
59
60async fn read_catalog(
62 mut response: reqwest::Response,
63 limit: usize,
64) -> Result<Vec<u8>, CatalogUpdateError> {
65 if !response.status().is_success() {
66 return Err(CatalogUpdateError::Download);
67 }
68 if response
69 .content_length()
70 .is_some_and(|length| length > limit as u64)
71 {
72 return Err(CatalogUpdateError::TooLarge);
73 }
74 let mut bytes = Vec::new();
75 while let Some(chunk) = response
76 .chunk()
77 .await
78 .map_err(|_| CatalogUpdateError::Download)?
79 {
80 if chunk.len() > limit - bytes.len() {
81 return Err(CatalogUpdateError::TooLarge);
82 }
83 bytes.extend_from_slice(&chunk);
84 }
85 Ok(bytes)
86}
87
88impl CatalogStore {
89 pub async fn update(
90 &self,
91 transport: &dyn CatalogTransport,
92 ) -> Result<CatalogStatus, CatalogUpdateError> {
93 self.key.ok_or(CatalogUpdateError::NotConfigured)?;
94 self.install(&transport.download().await?)
95 }
96}
97
98#[cfg(test)]
99mod tests {
100 use super::*;
101
102 #[test]
103 fn redirects_stay_on_official_https_hosts() {
104 assert!(allowed(&url::Url::parse(CATALOG_URL).unwrap()));
105 assert!(allowed(
106 &url::Url::parse("https://release-assets.githubusercontent.com/path?token=value")
107 .unwrap()
108 ));
109 for raw in [
110 "http://github.com/path",
111 "https://github.com:8443/path",
112 "https://github.com.evil.test/path",
113 "https://user@github.com/path",
114 "https://127.0.0.1/path",
115 ] {
116 assert!(!allowed(&url::Url::parse(raw).unwrap()));
117 }
118 }
119
120 #[test]
121 fn at_most_three_redirects_are_followed() {
122 let url = url::Url::parse(CATALOG_URL).unwrap();
123 assert!(follow_redirect(0, &url));
124 assert!(follow_redirect(2, &url));
125 assert!(!follow_redirect(3, &url));
126 assert!(!follow_redirect(
127 0,
128 &url::Url::parse("https://evil.test/c.json").unwrap()
129 ));
130 }
131
132 fn sized(status: u16, body: &'static [u8]) -> reqwest::Response {
134 let response = http::Response::builder().status(status).body(body).unwrap();
135 reqwest::Response::from(response)
136 }
137
138 fn streamed(body: &'static [u8]) -> reqwest::Response {
140 let chunks = body
141 .chunks(2)
142 .map(|chunk| Ok::<_, std::io::Error>(bytes::Bytes::from_static(chunk)));
143 let body =
144 reqwest::Body::wrap_stream(futures_util::stream::iter(chunks.collect::<Vec<_>>()));
145 reqwest::Response::from(http::Response::builder().status(200).body(body).unwrap())
146 }
147
148 #[tokio::test]
149 async fn catalog_bodies_are_bounded_and_need_success() {
150 assert_eq!(
151 read_catalog(sized(200, b"12345"), 5).await.unwrap(),
152 b"12345"
153 );
154 assert_eq!(read_catalog(streamed(b"12345"), 5).await.unwrap(), b"12345");
155 assert_eq!(read_catalog(sized(200, b"123"), 5).await.unwrap(), b"123");
157 for status in [302, 404, 500] {
158 assert!(matches!(
159 read_catalog(sized(status, b"12345"), 5).await,
160 Err(CatalogUpdateError::Download)
161 ));
162 }
163 assert!(matches!(
165 read_catalog(sized(200, b"123456"), 5).await,
166 Err(CatalogUpdateError::TooLarge)
167 ));
168 assert!(matches!(
170 read_catalog(streamed(b"123456"), 5).await,
171 Err(CatalogUpdateError::TooLarge)
172 ));
173 }
174}