fix(content): bound remote resource usage
This commit is contained in:
1 parent
bf2957e75b
commit
d488954548
7 files changed
+82
-7
No files matched your search
@@ -1,7 +1,6 @@
|
|||||||
// curseforge modpack archives: manifest.json references provider file ids,
|
// curseforge modpack archives: manifest.json references provider file ids,
|
||||||
// while the overrides directory contains configs and other bundled files.
|
// while the overrides directory contains configs and other bundled files.
|
||||||
|
|
||||||
use std::io::Read;
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
@@ -236,9 +235,8 @@ fn extract_overrides(
|
|||||||
if let Some(parent) = destination.parent() {
|
if let Some(parent) = destination.parent() {
|
||||||
std::fs::create_dir_all(parent)?;
|
std::fs::create_dir_all(parent)?;
|
||||||
}
|
}
|
||||||
let mut bytes = Vec::new();
|
let mut destination = std::fs::File::create(destination)?;
|
||||||
entry.read_to_end(&mut bytes)?;
|
std::io::copy(&mut entry, &mut destination)?;
|
||||||
std::fs::write(destination, bytes)?;
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -105,6 +105,30 @@ impl HttpClient {
|
|||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_bytes_limited(&self, url: &str, limit: usize) -> Result<Vec<u8>, NetError> {
|
||||||
|
get_with_retry(self, url, move |mut response| async move {
|
||||||
|
if response
|
||||||
|
.content_length()
|
||||||
|
.is_some_and(|length| length > limit as u64)
|
||||||
|
{
|
||||||
|
return Err(NetError::Parse(format!(
|
||||||
|
"Response exceeds the {limit}-byte limit"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
let mut bytes = Vec::new();
|
||||||
|
while let Some(chunk) = response.chunk().await? {
|
||||||
|
if bytes.len().saturating_add(chunk.len()) > limit {
|
||||||
|
return Err(NetError::Parse(format!(
|
||||||
|
"Response exceeds the {limit}-byte limit"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
bytes.extend_from_slice(&chunk);
|
||||||
|
}
|
||||||
|
Ok(bytes)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn post_json<B, T>(&self, url: &str, body: &B) -> Result<T, NetError>
|
pub async fn post_json<B, T>(&self, url: &str, body: &B) -> Result<T, NetError>
|
||||||
where
|
where
|
||||||
B: Serialize + ?Sized,
|
B: Serialize + ?Sized,
|
||||||
|
|||||||
+4
-1
@@ -1006,7 +1006,10 @@ impl App {
|
|||||||
.await
|
.await
|
||||||
.map_err(|error| error.to_string())?;
|
.map_err(|error| error.to_string())?;
|
||||||
let bytes = client
|
let bytes = client
|
||||||
.get_bytes(&url)
|
.get_bytes_limited(
|
||||||
|
&url,
|
||||||
|
crate::tui::widgets::markdown::MAX_PROJECT_IMAGE_BYTES,
|
||||||
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(|error| error.to_string())?;
|
.map_err(|error| error.to_string())?;
|
||||||
tokio::task::spawn_blocking(move || {
|
tokio::task::spawn_blocking(move || {
|
||||||
|
|||||||
@@ -555,6 +555,14 @@ fn gif_images_decode_their_first_frame() {
|
|||||||
assert_eq!(image.height(), 1);
|
assert_eq!(image.height(), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn oversized_images_are_rejected_before_decoding() {
|
||||||
|
let image = image::DynamicImage::new_rgba8(super::MAX_PROJECT_IMAGE_DIMENSION + 1, 1);
|
||||||
|
let mut bytes = std::io::Cursor::new(Vec::new());
|
||||||
|
image.write_to(&mut bytes, image::ImageFormat::Png).unwrap();
|
||||||
|
assert!(super::decode_image(bytes.get_ref()).is_err());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn image_rows_align_fitted_images_to_the_bottom() {
|
fn image_rows_align_fitted_images_to_the_bottom() {
|
||||||
let picker = ratatui_image::picker::Picker::halfblocks();
|
let picker = ratatui_image::picker::Picker::halfblocks();
|
||||||
|
|||||||
@@ -30,6 +30,9 @@ use crate::instance::content::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
const MAX_DOCUMENT_WIDTH: u16 = 110;
|
const MAX_DOCUMENT_WIDTH: u16 = 110;
|
||||||
|
pub(crate) const MAX_PROJECT_IMAGE_BYTES: usize = 16 * 1024 * 1024;
|
||||||
|
const MAX_PROJECT_IMAGE_DIMENSION: u32 = 8192;
|
||||||
|
const MAX_PROJECT_IMAGE_ALLOCATION: u64 = 64 * 1024 * 1024;
|
||||||
|
|
||||||
struct TextBlock {
|
struct TextBlock {
|
||||||
source: String,
|
source: String,
|
||||||
@@ -225,13 +228,31 @@ pub fn image_urls(title: &str, body: &str) -> Vec<String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn decode_image(bytes: &[u8]) -> Result<DynamicImage, String> {
|
pub fn decode_image(bytes: &[u8]) -> Result<DynamicImage, String> {
|
||||||
if let Ok(image) = image::load_from_memory(bytes) {
|
let mut reader = image::ImageReader::new(std::io::Cursor::new(bytes))
|
||||||
|
.with_guessed_format()
|
||||||
|
.map_err(|error| error.to_string())?;
|
||||||
|
let mut limits = image::Limits::default();
|
||||||
|
limits.max_image_width = Some(MAX_PROJECT_IMAGE_DIMENSION);
|
||||||
|
limits.max_image_height = Some(MAX_PROJECT_IMAGE_DIMENSION);
|
||||||
|
limits.max_alloc = Some(MAX_PROJECT_IMAGE_ALLOCATION);
|
||||||
|
reader.limits(limits);
|
||||||
|
if let Ok(image) = reader.decode() {
|
||||||
return Ok(image);
|
return Ok(image);
|
||||||
}
|
}
|
||||||
let mut options = resvg::usvg::Options::default();
|
let mut options = resvg::usvg::Options::default();
|
||||||
options.fontdb_mut().load_system_fonts();
|
options.fontdb_mut().load_system_fonts();
|
||||||
let tree = resvg::usvg::Tree::from_data(bytes, &options).map_err(|error| error.to_string())?;
|
let tree = resvg::usvg::Tree::from_data(bytes, &options).map_err(|error| error.to_string())?;
|
||||||
let size = tree.size().to_int_size();
|
let size = tree.size().to_int_size();
|
||||||
|
let allocation = u64::from(size.width())
|
||||||
|
.checked_mul(u64::from(size.height()))
|
||||||
|
.and_then(|pixels| pixels.checked_mul(4))
|
||||||
|
.ok_or_else(|| "SVG image dimensions are too large".to_owned())?;
|
||||||
|
if size.width() > MAX_PROJECT_IMAGE_DIMENSION
|
||||||
|
|| size.height() > MAX_PROJECT_IMAGE_DIMENSION
|
||||||
|
|| allocation > MAX_PROJECT_IMAGE_ALLOCATION
|
||||||
|
{
|
||||||
|
return Err("SVG image dimensions are too large".to_owned());
|
||||||
|
}
|
||||||
let mut pixmap = resvg::tiny_skia::Pixmap::new(size.width(), size.height())
|
let mut pixmap = resvg::tiny_skia::Pixmap::new(size.width(), size.height())
|
||||||
.ok_or_else(|| "SVG image dimensions are too large".to_owned())?;
|
.ok_or_else(|| "SVG image dimensions are too large".to_owned())?;
|
||||||
resvg::render(
|
resvg::render(
|
||||||
|
|||||||
@@ -842,7 +842,10 @@ fn spawn_project_page(request: crate::tui::widgets::content::discovery::ProjectP
|
|||||||
.await
|
.await
|
||||||
.map_err(|error| error.to_string())?;
|
.map_err(|error| error.to_string())?;
|
||||||
let bytes = client
|
let bytes = client
|
||||||
.get_bytes(&url)
|
.get_bytes_limited(
|
||||||
|
&url,
|
||||||
|
crate::tui::widgets::markdown::MAX_PROJECT_IMAGE_BYTES,
|
||||||
|
)
|
||||||
.await
|
.await
|
||||||
.map_err(|error| error.to_string())?;
|
.map_err(|error| error.to_string())?;
|
||||||
tokio::task::spawn_blocking(move || {
|
tokio::task::spawn_blocking(move || {
|
||||||
|
|||||||
@@ -103,6 +103,24 @@ async fn get_json_gives_up_after_max_retries() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn get_bytes_limited_rejects_oversized_responses() {
|
||||||
|
let server = MockServer::start().await;
|
||||||
|
Mock::given(method("GET"))
|
||||||
|
.and(path("/large.bin"))
|
||||||
|
.respond_with(ResponseTemplate::new(200).set_body_bytes(vec![0; 9]))
|
||||||
|
.expect(1)
|
||||||
|
.mount(&server)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
let url = format!("{}/large.bin", server.uri());
|
||||||
|
let error = client_without_timeout()
|
||||||
|
.get_bytes_limited(&url, 8)
|
||||||
|
.await
|
||||||
|
.unwrap_err();
|
||||||
|
assert!(error.to_string().contains("8-byte limit"));
|
||||||
|
}
|
||||||
|
|
||||||
// ---------- download_file ----------
|
// ---------- download_file ----------
|
||||||
|
|
||||||
#[tokio::test(start_paused = true)]
|
#[tokio::test(start_paused = true)]
|
||||||
|
|||||||
Reference in new issue
Block a user