Skip to main content

libsignal_service/push_service/
cdn.rs

1use std::{
2    collections::HashMap,
3    io::{self, Read, SeekFrom},
4};
5
6use futures::TryStreamExt;
7use reqwest::{
8    header::{CONTENT_LENGTH, CONTENT_RANGE, CONTENT_TYPE, RANGE},
9    multipart::Part,
10    Method, StatusCode,
11};
12use serde::Deserialize;
13use tracing::{debug, trace};
14use url::Url;
15
16use super::response::error_mapper;
17use crate::{
18    configuration::Endpoint, prelude::AttachmentIdentifier,
19    proto::AttachmentPointer, push_service::HttpAuthOverride,
20};
21
22use super::{response::SignalServiceResponse, PushService, ServiceError};
23
24#[derive(Debug, serde::Deserialize, Default)]
25#[serde(rename_all = "camelCase")]
26pub struct AttachmentV2UploadAttributes {
27    key: String,
28    credential: String,
29    acl: String,
30    algorithm: String,
31    date: String,
32    policy: String,
33    signature: String,
34}
35
36#[derive(Debug, Deserialize)]
37#[serde(rename_all = "camelCase")]
38pub struct AttachmentUploadForm {
39    pub cdn: u32,
40    pub key: String,
41    pub headers: HashMap<String, String>,
42    pub signed_upload_location: Url,
43}
44
45#[derive(Debug, Deserialize)]
46#[serde(rename_all = "camelCase")]
47pub struct AttachmentDigest {
48    pub digest: Vec<u8>,
49    pub incremental_digest: Option<Vec<u8>>,
50    pub incremental_mac_chunk_size: u64,
51}
52
53#[derive(Debug)]
54pub struct ResumeInfo {
55    pub content_range: Option<String>,
56    pub content_start: u64,
57}
58
59pub struct AttachmentDownload<R> {
60    pub stream: R,
61    pub content_length: Option<u64>,
62}
63
64// Signal-Server: controllers/AttachmentControllerV4.java:102
65// (GET /v4/attachments/form/upload)
66error_mapper! {
67    get_attachment_upload_form_errors:
68        // 413: attachment too large, AttachmentControllerV4.java:111
69        PAYLOAD_TOO_LARGE => AttachmentTooLarge,
70}
71
72impl PushService {
73    pub async fn get_attachment(
74        &mut self,
75        ptr: &AttachmentPointer,
76    ) -> Result<
77        AttachmentDownload<impl futures::io::AsyncRead + Send + Unpin>,
78        ServiceError,
79    > {
80        let path = match ptr.attachment_identifier.as_ref() {
81            Some(AttachmentIdentifier::CdnId(id)) => {
82                format!("attachments/{}", id)
83            },
84            Some(AttachmentIdentifier::CdnKey(key)) => {
85                format!("attachments/{}", key)
86            },
87            None => {
88                return Err(ServiceError::InvalidFrame {
89                    reason: "no attachment identifier in pointer",
90                });
91            },
92        };
93        self.get_from_cdn(ptr.cdn_number(), &path).await
94    }
95
96    #[tracing::instrument(skip(self))]
97    pub(crate) async fn get_from_cdn(
98        &mut self,
99        cdn_id: u32,
100        path: &str,
101    ) -> Result<
102        AttachmentDownload<impl futures::io::AsyncRead + Send + Unpin>,
103        ServiceError,
104    > {
105        let response = self
106            .request(
107                Method::GET,
108                Endpoint::cdn(cdn_id, path),
109                HttpAuthOverride::Unidentified, // CDN requests are always without authentication
110            )?
111            .send()
112            .await?
113            .error_for_status()?;
114        let content_length = match response.headers().get(CONTENT_LENGTH) {
115            Some(value) => {
116                match value.to_str().ok().and_then(|value| value.parse().ok()) {
117                    Some(value) => Some(value),
118                    None => {
119                        tracing::warn!("invalid Content-Length header");
120                        None
121                    },
122                }
123            },
124            None => None,
125        };
126        let response_stream = response
127            .bytes_stream()
128            .map_err(io::Error::other)
129            .into_async_read();
130
131        Ok(AttachmentDownload {
132            stream: response_stream,
133            content_length,
134        })
135    }
136
137    /// `upload_length` is the size of the encrypted upload; the server rejects 0
138    /// and caps uploads with the returned form at that size.
139    pub(crate) async fn get_attachment_v4_upload_attributes(
140        &mut self,
141        upload_length: u64,
142    ) -> Result<AttachmentUploadForm, ServiceError> {
143        self.request(
144            Method::GET,
145            Endpoint::service(format!(
146                "/v4/attachments/form/upload?uploadLength={upload_length}"
147            )),
148            HttpAuthOverride::NoOverride,
149        )?
150        .send()
151        .await?
152        .service_error_for_status_with(get_attachment_upload_form_errors)
153        .await?
154        .json()
155        .await
156        .map_err(Into::into)
157    }
158
159    #[tracing::instrument(skip(self), level=tracing::Level::TRACE)]
160    pub(crate) async fn get_attachment_resumable_upload_url(
161        &mut self,
162        attachment_upload_form: &AttachmentUploadForm,
163    ) -> Result<Url, ServiceError> {
164        let mut request = self.request(
165            Method::POST,
166            Endpoint::Absolute(
167                attachment_upload_form.signed_upload_location.clone(),
168            ),
169            HttpAuthOverride::Unidentified,
170        )?;
171
172        for (key, value) in &attachment_upload_form.headers {
173            request = request.header(key, value);
174        }
175        request = request.header(CONTENT_LENGTH, "0");
176
177        if attachment_upload_form.cdn == 2 {
178            request = request.header(CONTENT_TYPE, "application/octet-stream");
179        } else if attachment_upload_form.cdn == 3 {
180            request = request
181                .header("Upload-Defer-Length", "1")
182                .header("Tus-Resumable", "1.0.0");
183        } else {
184            return Err(ServiceError::UnknownCdnVersion(
185                attachment_upload_form.cdn,
186            ));
187        };
188
189        Ok(request
190            .send()
191            .await?
192            .error_for_status()?
193            .headers()
194            .get("location")
195            .ok_or(ServiceError::InvalidFrame {
196                reason: "missing location header in HTTP response",
197            })?
198            .to_str()
199            .map_err(|_| ServiceError::InvalidFrame {
200                reason: "invalid location header bytes in HTTP response",
201            })?
202            .parse()?)
203    }
204
205    #[tracing::instrument(skip(self))]
206    async fn get_attachment_resume_info_cdn2(
207        &mut self,
208        resumable_url: &Url,
209        content_length: u64,
210    ) -> Result<ResumeInfo, ServiceError> {
211        let response = self
212            .request(
213                Method::PUT,
214                Endpoint::cdn_url(2, resumable_url),
215                HttpAuthOverride::Unidentified,
216            )?
217            .header(CONTENT_RANGE, format!("bytes */{content_length}"))
218            .send()
219            .await?
220            .error_for_status()?;
221
222        let status = response.status();
223
224        if status.is_success() {
225            Ok(ResumeInfo {
226                content_range: None,
227                content_start: content_length,
228            })
229        } else if status == StatusCode::PERMANENT_REDIRECT {
230            let offset =
231                match response.headers().get(RANGE) {
232                    Some(range) => range
233                        .to_str()
234                        .map_err(|_| ServiceError::InvalidFrame {
235                            reason: "invalid format for Range HTTP header",
236                        })?
237                        .split('-')
238                        .nth(1)
239                        .ok_or(ServiceError::InvalidFrame {
240                            reason:
241                                "invalid value format for Range HTTP header",
242                        })?
243                        .parse::<u64>()
244                        .map_err(|_| ServiceError::InvalidFrame {
245                            reason:
246                                "invalid number format for Range HTTP header",
247                        })?
248                        + 1,
249                    None => 0,
250                };
251
252            Ok(ResumeInfo {
253                content_range: Some(format!(
254                    "bytes {}-{}/{}",
255                    offset,
256                    content_length - 1,
257                    content_length
258                )),
259                content_start: offset,
260            })
261        } else {
262            Err(ServiceError::InvalidFrame {
263                reason: "failed to get resumable upload data from CDN2",
264            })
265        }
266    }
267
268    #[tracing::instrument(skip(self))]
269    async fn get_attachment_resume_info_cdn3(
270        &mut self,
271        resumable_url: &Url,
272        headers: &HashMap<String, String>,
273    ) -> Result<ResumeInfo, ServiceError> {
274        let mut request = self
275            .request(
276                Method::HEAD,
277                Endpoint::cdn_url(3, resumable_url),
278                HttpAuthOverride::Unidentified,
279            )?
280            .header("Tus-Resumable", "1.0.0");
281
282        for (key, value) in headers {
283            request = request.header(key, value);
284        }
285
286        let response = request.send().await?.error_for_status()?;
287
288        let upload_offset = response
289            .headers()
290            .get("upload-offset")
291            .ok_or(ServiceError::InvalidFrame {
292                reason: "no Upload-Offset header in response",
293            })?
294            .to_str()
295            .map_err(|_| ServiceError::InvalidFrame {
296                reason: "invalid upload-offset header bytes in HTTP response",
297            })?
298            .parse()
299            .map_err(|_| ServiceError::InvalidFrame {
300                reason: "invalid integer value for Upload-Offset header",
301            })?;
302
303        Ok(ResumeInfo {
304            content_range: None,
305            content_start: upload_offset,
306        })
307    }
308
309    /// Upload attachment
310    ///
311    /// Returns attachment ID and the attachment digest
312    #[tracing::instrument(skip(self, headers, content))]
313    pub(crate) async fn upload_attachment_v4(
314        &mut self,
315        cdn_id: u32,
316        resumable_url: &Url,
317        content_length: u64,
318        headers: HashMap<String, String>,
319        content: impl std::io::Read + std::io::Seek + Send,
320    ) -> Result<AttachmentDigest, ServiceError> {
321        if cdn_id == 2 {
322            self.upload_to_cdn2(resumable_url, content_length, content)
323                .await
324        } else {
325            self.upload_to_cdn3(
326                resumable_url,
327                &headers,
328                content_length,
329                content,
330            )
331            .await
332        }
333    }
334
335    #[tracing::instrument(skip(self, upload_attributes, reader))]
336    pub async fn upload_to_cdn0(
337        &mut self,
338        path: &str,
339        upload_attributes: AttachmentV2UploadAttributes,
340        filename: String,
341        mut reader: impl Read + Send,
342    ) -> Result<(), ServiceError> {
343        let mut buf = Vec::new();
344        reader
345            .read_to_end(&mut buf)
346            .expect("infallible Read instance");
347
348        // Amazon S3 expects multipart fields in a very specific order
349        // DO NOT CHANGE THIS (or do it, but feel the wrath of the gods)
350        let form = reqwest::multipart::Form::new()
351            .text("acl", upload_attributes.acl)
352            .text("key", upload_attributes.key)
353            .text("policy", upload_attributes.policy)
354            .text("Content-Type", "application/octet-stream")
355            .text("x-amz-algorithm", upload_attributes.algorithm)
356            .text("x-amz-credential", upload_attributes.credential)
357            .text("x-amz-date", upload_attributes.date)
358            .text("x-amz-signature", upload_attributes.signature)
359            .part(
360                "file",
361                Part::stream(buf)
362                    .mime_str("application/octet-stream")?
363                    .file_name(filename),
364            );
365
366        let response = self
367            .request(
368                Method::POST,
369                Endpoint::cdn(0, path),
370                HttpAuthOverride::NoOverride,
371            )?
372            .multipart(form)
373            .send()
374            .await?
375            .error_for_status()?;
376
377        debug!("HyperPushService::PUT response: {:?}", response);
378
379        Ok(())
380    }
381
382    #[tracing::instrument(skip(self, content))]
383    async fn upload_to_cdn2(
384        &mut self,
385        resumable_url: &Url,
386        content_length: u64,
387        mut content: impl std::io::Read + std::io::Seek + Send,
388    ) -> Result<AttachmentDigest, ServiceError> {
389        let resume_info = self
390            .get_attachment_resume_info_cdn2(resumable_url, content_length)
391            .await?;
392
393        let mut digester =
394            crate::digeststream::DigestingReader::new(&mut content);
395
396        let mut buf = Vec::new();
397        digester.read_to_end(&mut buf)?;
398
399        trace!("digested content");
400
401        let mut request = self.request(
402            Method::PUT,
403            Endpoint::cdn_url(2, resumable_url),
404            HttpAuthOverride::Unidentified,
405        )?;
406
407        if let Some(content_range) = resume_info.content_range {
408            request = request.header(CONTENT_RANGE, content_range);
409        }
410
411        request.body(buf).send().await?.error_for_status()?;
412
413        Ok(AttachmentDigest {
414            digest: digester.finalize(),
415            incremental_digest: None,
416            incremental_mac_chunk_size: 0,
417        })
418    }
419
420    #[tracing::instrument(skip(self, content))]
421    async fn upload_to_cdn3(
422        &mut self,
423        resumable_url: &Url,
424        headers: &HashMap<String, String>,
425        content_length: u64,
426        mut content: impl std::io::Read + std::io::Seek + Send,
427    ) -> Result<AttachmentDigest, ServiceError> {
428        let resume_info = self
429            .get_attachment_resume_info_cdn3(resumable_url, headers)
430            .await?;
431
432        trace!(?resume_info, "got resume info");
433
434        if resume_info.content_start == content_length {
435            let mut digester =
436                crate::digeststream::DigestingReader::new(&mut content);
437            let mut buf = Vec::new();
438            digester.read_to_end(&mut buf)?;
439            return Ok(AttachmentDigest {
440                digest: digester.finalize(),
441                incremental_digest: None,
442                incremental_mac_chunk_size: 0,
443            });
444        }
445
446        let mut digester =
447            crate::digeststream::DigestingReader::new(&mut content);
448        digester.seek(SeekFrom::Start(resume_info.content_start))?;
449
450        let mut buf = Vec::new();
451        digester.read_to_end(&mut buf)?;
452
453        trace!("digested content");
454
455        let mut request = self.request(
456            Method::PATCH,
457            Endpoint::cdn(3, resumable_url.path()),
458            HttpAuthOverride::Unidentified,
459        )?;
460
461        for (key, value) in headers {
462            request = request.header(key, value);
463        }
464
465        request
466            .header("Tus-Resumable", "1.0.0")
467            .header("Upload-Offset", resume_info.content_start)
468            .header("Upload-Length", buf.len())
469            .header(CONTENT_TYPE, "application/offset+octet-stream")
470            .body(buf)
471            .send()
472            .await?
473            .error_for_status()?;
474
475        trace!("attachment uploaded");
476
477        Ok(AttachmentDigest {
478            digest: digester.finalize(),
479            incremental_digest: None,
480            incremental_mac_chunk_size: 0,
481        })
482    }
483}