fix: Refactor and block media downloads larger than max_request_size
This commit is contained in:
parent
c97111e3ca
commit
7f165e5bbe
1 changed files with 43 additions and 26 deletions
|
|
@ -10,6 +10,8 @@ use std::time::SystemTime;
|
||||||
use conduwuit::{Err, Result, debug, err, utils::response::LimitReadExt};
|
use conduwuit::{Err, Result, debug, err, utils::response::LimitReadExt};
|
||||||
use conduwuit_core::implement;
|
use conduwuit_core::implement;
|
||||||
use ipaddress::IPAddress;
|
use ipaddress::IPAddress;
|
||||||
|
#[cfg(feature = "url_preview")]
|
||||||
|
use ruma::OwnedMxcUri;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
|
|
@ -181,22 +183,12 @@ pub async fn download_video(
|
||||||
url: &str,
|
url: &str,
|
||||||
preview_data: Option<UrlPreviewData>,
|
preview_data: Option<UrlPreviewData>,
|
||||||
) -> Result<UrlPreviewData> {
|
) -> Result<UrlPreviewData> {
|
||||||
use conduwuit::utils::random_string;
|
|
||||||
use ruma::Mxc;
|
|
||||||
|
|
||||||
let mut preview_data = preview_data.unwrap_or_default();
|
let mut preview_data = preview_data.unwrap_or_default();
|
||||||
|
|
||||||
if self.services.globals.url_preview_allow_audio_video() {
|
if self.services.globals.url_preview_allow_audio_video() {
|
||||||
let video = self.services.client.url_preview.get(url).send().await?;
|
let (url, size) = self.download_media(url).await?;
|
||||||
let video = video.bytes().await?;
|
preview_data.video = Some(url.to_string());
|
||||||
let mxc = Mxc {
|
preview_data.video_size = Some(size);
|
||||||
server_name: self.services.globals.server_name(),
|
|
||||||
media_id: &random_string(super::MXC_LENGTH),
|
|
||||||
};
|
|
||||||
|
|
||||||
self.create(&mxc, None, None, None, &video).await?;
|
|
||||||
|
|
||||||
preview_data.video = Some(mxc.to_string());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(preview_data)
|
Ok(preview_data)
|
||||||
|
|
@ -209,27 +201,46 @@ pub async fn download_audio(
|
||||||
url: &str,
|
url: &str,
|
||||||
preview_data: Option<UrlPreviewData>,
|
preview_data: Option<UrlPreviewData>,
|
||||||
) -> Result<UrlPreviewData> {
|
) -> Result<UrlPreviewData> {
|
||||||
use conduwuit::utils::random_string;
|
|
||||||
use ruma::Mxc;
|
|
||||||
|
|
||||||
let mut preview_data = preview_data.unwrap_or_default();
|
let mut preview_data = preview_data.unwrap_or_default();
|
||||||
|
|
||||||
if self.services.globals.url_preview_allow_audio_video() {
|
if self.services.globals.url_preview_allow_audio_video() {
|
||||||
let audio = self.services.client.url_preview.get(url).send().await?;
|
let (url, size) = self.download_media(url).await?;
|
||||||
let audio = audio.bytes().await?;
|
preview_data.audio = Some(url.to_string());
|
||||||
let mxc = Mxc {
|
preview_data.audio_size = Some(size);
|
||||||
server_name: self.services.globals.server_name(),
|
|
||||||
media_id: &random_string(super::MXC_LENGTH),
|
|
||||||
};
|
|
||||||
|
|
||||||
self.create(&mxc, None, None, None, &audio).await?;
|
|
||||||
|
|
||||||
preview_data.video = Some(mxc.to_string());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(preview_data)
|
Ok(preview_data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "url_preview")]
|
||||||
|
#[implement(Service)]
|
||||||
|
pub async fn download_media(&self, url: &str) -> Result<(OwnedMxcUri, usize)> {
|
||||||
|
use conduwuit::utils::random_string;
|
||||||
|
use ruma::Mxc;
|
||||||
|
|
||||||
|
let max_request_size = self.services.server.config.max_request_size.try_into()?;
|
||||||
|
|
||||||
|
let media = self.services.client.url_preview.get(url).send().await?;
|
||||||
|
if media
|
||||||
|
.content_length()
|
||||||
|
.is_none_or(|len| len > max_request_size)
|
||||||
|
{
|
||||||
|
return Err!(Request(TooLarge(
|
||||||
|
"Content length not given or greater than max_request_size"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
let media = media.bytes().await?;
|
||||||
|
let mxc = Mxc {
|
||||||
|
server_name: self.services.globals.server_name(),
|
||||||
|
media_id: &random_string(super::MXC_LENGTH),
|
||||||
|
};
|
||||||
|
|
||||||
|
self.create(&mxc, None, None, None, &media).await?;
|
||||||
|
|
||||||
|
return Ok((OwnedMxcUri::from(mxc.to_string()), media.len()));
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "url_preview"))]
|
#[cfg(not(feature = "url_preview"))]
|
||||||
#[implement(Service)]
|
#[implement(Service)]
|
||||||
pub async fn download_image(
|
pub async fn download_image(
|
||||||
|
|
@ -260,6 +271,12 @@ pub async fn download_audio(
|
||||||
Err!(FeatureDisabled("url_preview"))
|
Err!(FeatureDisabled("url_preview"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "url_preview"))]
|
||||||
|
#[implement(Service)]
|
||||||
|
pub async fn download_media(&self, _url: &str) -> Result<UrlPreviewData> {
|
||||||
|
Err!(FeatureDisabled("url_preview"))
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(feature = "url_preview")]
|
#[cfg(feature = "url_preview")]
|
||||||
#[implement(Service)]
|
#[implement(Service)]
|
||||||
async fn download_html(&self, url: &str) -> Result<UrlPreviewData> {
|
async fn download_html(&self, url: &str) -> Result<UrlPreviewData> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue