fix(content): complete provider hardening
This commit is contained in:
1 parent
695ba5a758
commit
062a9234ee
7 files changed
+65
-36
No files matched your search
@@ -25,9 +25,12 @@ fn offline_uuid_has_valid_v3_shape() {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn offline_uuid_deterministic() {
|
||||
assert_eq!(offline_uuid("Steve"), offline_uuid("Steve"));
|
||||
assert_eq!(offline_uuid("Alex"), offline_uuid("Alex"));
|
||||
fn offline_uuid_is_pinned_for_known_names() {
|
||||
assert_eq!(
|
||||
offline_uuid("Steve"),
|
||||
"7e0a3689-ed7c-347c-87cc-3689ed7c47cc"
|
||||
);
|
||||
assert_eq!(offline_uuid("Alex"), "a13d42a3-454f-3e39-a392-42a3454fe392");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -181,7 +181,9 @@ impl ContentProvider for ModrinthProvider {
|
||||
}
|
||||
|
||||
async fn icon(&self, url: &str) -> Result<Vec<u8>, crate::net::NetError> {
|
||||
self.client.get_bytes(url).await
|
||||
self.client
|
||||
.get_bytes_limited(url, crate::net::MAX_PROVIDER_ASSET_BYTES)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn download_version(
|
||||
@@ -338,7 +340,9 @@ impl ContentProvider for CurseForgeProvider {
|
||||
}
|
||||
|
||||
async fn icon(&self, url: &str) -> Result<Vec<u8>, crate::net::NetError> {
|
||||
self.client.get_bytes(url).await
|
||||
self.client
|
||||
.get_bytes_limited(url, crate::net::MAX_PROVIDER_ASSET_BYTES)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn download_version(
|
||||
|
||||
@@ -15,6 +15,8 @@ use serde::de::DeserializeOwned;
|
||||
use std::path::Path;
|
||||
use thiserror::Error;
|
||||
|
||||
pub const MAX_PROVIDER_ASSET_BYTES: usize = 16 * 1024 * 1024;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum NetError {
|
||||
#[error("HTTP request failed: {0}")]
|
||||
|
||||
+8
-6
@@ -1176,12 +1176,14 @@ impl App {
|
||||
) {
|
||||
widgets::content::discovery::spawn_provider_search(
|
||||
query,
|
||||
widgets::content::discovery::DiscoveryTarget::Content {
|
||||
instance,
|
||||
kind,
|
||||
manifest,
|
||||
minecraft_dir,
|
||||
},
|
||||
widgets::content::discovery::DiscoveryTarget::Content(Box::new(
|
||||
widgets::content::discovery::ContentDiscoveryTarget {
|
||||
instance,
|
||||
kind,
|
||||
manifest,
|
||||
minecraft_dir,
|
||||
},
|
||||
)),
|
||||
meta_dir,
|
||||
request,
|
||||
);
|
||||
|
||||
@@ -80,13 +80,15 @@ pub struct DiscoveryRequest {
|
||||
pub known_projects: std::collections::HashMap<String, (String, String)>,
|
||||
}
|
||||
|
||||
pub(crate) struct ContentDiscoveryTarget {
|
||||
pub instance: InstanceConfig,
|
||||
pub kind: ContentKind,
|
||||
pub manifest: Option<crate::instance::ContentManifest>,
|
||||
pub minecraft_dir: PathBuf,
|
||||
}
|
||||
|
||||
pub(crate) enum DiscoveryTarget {
|
||||
Content {
|
||||
instance: InstanceConfig,
|
||||
kind: ContentKind,
|
||||
manifest: Option<crate::instance::ContentManifest>,
|
||||
minecraft_dir: PathBuf,
|
||||
},
|
||||
Content(Box<ContentDiscoveryTarget>),
|
||||
Modpacks,
|
||||
}
|
||||
|
||||
@@ -100,8 +102,10 @@ async fn search_provider(
|
||||
) -> Option<Result<DiscoveryResults, crate::net::NetError>> {
|
||||
let provider = provider.filter(|_| enabled)?;
|
||||
Some(match target {
|
||||
DiscoveryTarget::Content { instance, kind, .. } => {
|
||||
provider.search(*kind, query, instance, offset, limit).await
|
||||
DiscoveryTarget::Content(content) => {
|
||||
provider
|
||||
.search(content.kind, query, &content.instance, offset, limit)
|
||||
.await
|
||||
}
|
||||
DiscoveryTarget::Modpacks => provider.search_modpacks(query, offset, limit).await,
|
||||
})
|
||||
@@ -182,20 +186,16 @@ pub(crate) fn spawn_provider_search(
|
||||
returned.insert(stem.clone());
|
||||
let project_id = project.id.clone();
|
||||
let installed_path = match &target {
|
||||
DiscoveryTarget::Content {
|
||||
manifest,
|
||||
minecraft_dir,
|
||||
..
|
||||
} => merged
|
||||
DiscoveryTarget::Content(content) => merged
|
||||
.sources
|
||||
.iter()
|
||||
.filter(|(source_stem, _)| source_stem == &stem)
|
||||
.find_map(|(_, source)| {
|
||||
manifest.as_ref().and_then(|manifest| {
|
||||
content.manifest.as_ref().and_then(|manifest| {
|
||||
manifest.resolved_project_path(
|
||||
&source.provider,
|
||||
&source.project_id,
|
||||
minecraft_dir,
|
||||
&content.minecraft_dir,
|
||||
)
|
||||
})
|
||||
}),
|
||||
@@ -227,10 +227,7 @@ pub(crate) fn spawn_provider_search(
|
||||
return;
|
||||
};
|
||||
match client
|
||||
.get_bytes_limited(
|
||||
&url,
|
||||
crate::tui::widgets::markdown::MAX_PROJECT_IMAGE_BYTES,
|
||||
)
|
||||
.get_bytes_limited(&url, crate::net::MAX_PROVIDER_ASSET_BYTES)
|
||||
.await
|
||||
{
|
||||
Ok(bytes) if !bytes.is_empty() => {
|
||||
@@ -332,10 +329,7 @@ pub(crate) fn spawn_project_page(request: ProjectPageRequest) {
|
||||
.await
|
||||
.map_err(|error| error.to_string())?;
|
||||
let bytes = client
|
||||
.get_bytes_limited(
|
||||
&url,
|
||||
crate::tui::widgets::markdown::MAX_PROJECT_IMAGE_BYTES,
|
||||
)
|
||||
.get_bytes_limited(&url, crate::net::MAX_PROVIDER_ASSET_BYTES)
|
||||
.await
|
||||
.map_err(|error| error.to_string())?;
|
||||
tokio::task::spawn_blocking(move || {
|
||||
|
||||
@@ -30,7 +30,6 @@ use crate::instance::content::{
|
||||
};
|
||||
|
||||
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;
|
||||
|
||||
|
||||
+26
-1
@@ -154,7 +154,10 @@ async fn modrinth_discovery_returns_unique_compatible_mods() {
|
||||
.iter()
|
||||
.find_map(|project| project.icon_url.as_deref())
|
||||
.expect("expected at least one project icon");
|
||||
let icon = client.get_bytes(icon_url).await.unwrap();
|
||||
let icon = client
|
||||
.get_bytes_limited(icon_url, net::MAX_PROVIDER_ASSET_BYTES)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(image::load_from_memory(&icon).is_ok());
|
||||
|
||||
let mut seen = result
|
||||
@@ -183,3 +186,25 @@ async fn modrinth_discovery_returns_unique_compatible_mods() {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "hits live CurseForge API"]
|
||||
async fn curseforge_discovery_returns_compatible_mods() {
|
||||
let api_key = net::curseforge::api_key().expect("build has no CurseForge API key");
|
||||
let result = net::curseforge::search_discovery(
|
||||
&HttpClient::new(),
|
||||
api_key,
|
||||
ContentKind::Mod,
|
||||
"sodium",
|
||||
"1.21.1",
|
||||
ModLoader::Fabric,
|
||||
0,
|
||||
20,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert!(!result.projects.is_empty());
|
||||
assert!(result.total_hits >= result.projects.len());
|
||||
assert!(result.projects.iter().all(|project| !project.id.is_empty()));
|
||||
}
|
||||
Reference in new issue
Block a user