Move validators to seperate thread, other fixes (#253)

* Move validators to seperate thread, other fixes

* Update rust version in Dockerfile

* Fix notifs not working

* Fix pack validator not enforcing files
This commit is contained in:
Geometrically
2021-11-13 15:46:08 -07:00
committed by GitHub
parent 13187de97d
commit 7f791d4919
19 changed files with 257 additions and 196 deletions

View File

@@ -28,7 +28,7 @@ impl super::Validator for FabricValidator {
fn validate(
&self,
archive: &mut ZipArchive<Cursor<&[u8]>>,
archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
) -> Result<ValidationResult, ValidationError> {
archive.by_name("fabric.mod.json").map_err(|_| {
ValidationError::InvalidInputError("No fabric.mod.json present for Fabric file.".into())
@@ -39,7 +39,7 @@ impl super::Validator for FabricValidator {
.any(|name| name.ends_with("refmap.json") || name.ends_with(".class"))
{
return Ok(ValidationResult::Warning(
"Fabric mod file is a source file!".into(),
"Fabric mod file is a source file!",
));
}

View File

@@ -28,7 +28,7 @@ impl super::Validator for ForgeValidator {
fn validate(
&self,
archive: &mut ZipArchive<Cursor<&[u8]>>,
archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
) -> Result<ValidationResult, ValidationError> {
archive.by_name("META-INF/mods.toml").map_err(|_| {
ValidationError::InvalidInputError("No mods.toml present for Forge file.".into())
@@ -36,7 +36,7 @@ impl super::Validator for ForgeValidator {
if !archive.file_names().any(|name| name.ends_with(".class")) {
return Ok(ValidationResult::Warning(
"Forge mod file is a source file!".into(),
"Forge mod file is a source file!",
));
}
@@ -71,7 +71,7 @@ impl super::Validator for LegacyForgeValidator {
fn validate(
&self,
archive: &mut ZipArchive<Cursor<&[u8]>>,
archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
) -> Result<ValidationResult, ValidationError> {
archive.by_name("mcmod.info").map_err(|_| {
ValidationError::InvalidInputError("No mcmod.info present for Forge file.".into())
@@ -79,7 +79,7 @@ impl super::Validator for LegacyForgeValidator {
if !archive.file_names().any(|name| name.ends_with(".class")) {
return Ok(ValidationResult::Warning(
"Forge mod file is a source file!".into(),
"Forge mod file is a source file!",
));
}

View File

@@ -21,6 +21,8 @@ pub enum ValidationError {
SerdeError(#[from] serde_json::Error),
#[error("Invalid Input: {0}")]
InvalidInputError(std::borrow::Cow<'static, str>),
#[error("Error while managing threads")]
BlockingError,
}
#[derive(Eq, PartialEq)]
@@ -45,7 +47,7 @@ pub trait Validator: Sync {
fn get_supported_game_versions(&self) -> SupportedGameVersions;
fn validate(
&self,
archive: &mut ZipArchive<Cursor<&[u8]>>,
archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
) -> Result<ValidationResult, ValidationError>;
}
@@ -57,48 +59,52 @@ static VALIDATORS: [&dyn Validator; 4] = [
];
/// The return value is whether this file should be marked as primary or not, based on the analysis of the file
pub fn validate_file(
data: &[u8],
file_extension: &str,
project_type: &str,
pub async fn validate_file(
data: bytes::Bytes,
file_extension: String,
project_type: String,
loaders: Vec<Loader>,
game_versions: Vec<GameVersion>,
all_game_versions: &[crate::database::models::categories::GameVersion],
all_game_versions: Vec<crate::database::models::categories::GameVersion>,
) -> Result<ValidationResult, ValidationError> {
let reader = std::io::Cursor::new(data);
let mut zip = zip::ZipArchive::new(reader)?;
Ok(actix_web::web::block(move || {
let reader = std::io::Cursor::new(data);
let mut zip = zip::ZipArchive::new(reader)?;
let mut visited = false;
for validator in &VALIDATORS {
if validator.get_project_types().contains(&project_type)
&& loaders
.iter()
.any(|x| validator.get_supported_loaders().contains(&&*x.0))
&& game_version_supported(
&game_versions,
all_game_versions,
validator.get_supported_game_versions(),
)
{
if validator.get_file_extensions().contains(&file_extension) {
return validator.validate(&mut zip);
} else {
visited = true;
let mut visited = false;
for validator in &VALIDATORS {
if validator.get_project_types().contains(&&*project_type)
&& loaders
.iter()
.any(|x| validator.get_supported_loaders().contains(&&*x.0))
&& game_version_supported(
&game_versions,
&all_game_versions,
validator.get_supported_game_versions(),
)
{
if validator.get_file_extensions().contains(&&*file_extension) {
return validator.validate(&mut zip);
} else {
visited = true;
}
}
}
}
if visited {
Err(ValidationError::InvalidInputError(
format!(
"File extension {} is invalid for input file",
file_extension
)
.into(),
))
} else {
Ok(ValidationResult::Pass)
}
if visited {
Err(ValidationError::InvalidInputError(
format!(
"File extension {} is invalid for input file",
file_extension
)
.into(),
))
} else {
Ok(ValidationResult::Pass)
}
})
.await
.map_err(|_| ValidationError::BlockingError)?)
}
fn game_version_supported(

View File

@@ -1,19 +1,60 @@
use crate::models::projects::SideType;
use crate::validate::{SupportedGameVersions, ValidationError, ValidationResult};
use serde::{Deserialize, Serialize};
use std::io::{Cursor, Read};
use zip::ZipArchive;
use validator::Validate;
use crate::util::validate::validation_errors_to_string;
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Validate)]
#[serde(rename_all = "camelCase")]
pub struct PackFormat<'a> {
pub game: &'a str,
pub format_version: i32,
#[validate(length(min = 3, max = 512))]
pub version_id: &'a str,
#[validate(length(min = 3, max = 512))]
pub name: &'a str,
#[validate(length(max = 2048))]
pub summary: Option<&'a str>,
#[validate]
pub files: Vec<PackFile<'a>>,
pub dependencies: std::collections::HashMap<PackDependency, &'a str>,
}
#[derive(Serialize, Deserialize, Validate)]
pub struct PackFile<'a> {
pub path: &'a str,
pub hashes: std::collections::HashMap<FileHash, &'a str>,
pub env: std::collections::HashMap<EnvType, SideType>,
#[validate(custom(function = "validate_download_url"))]
pub downloads: Vec<&'a str>,
}
fn validate_download_url(values: &Vec<&str>) -> Result<(), validator::ValidationError> {
for value in values {
if !validator::validate_url(*value) {
return Err(validator::ValidationError::new("invalid URL"));
}
}
Ok(())
}
#[derive(Serialize, Deserialize, Eq, PartialEq, Hash)]
#[serde(rename_all = "camelCase")]
pub enum FileHash {
Sha1,
Sha512,
}
#[derive(Serialize, Deserialize, Eq, PartialEq, Hash)]
#[serde(rename_all = "camelCase")]
pub enum EnvType {
Client,
Server,
}
#[derive(Serialize, Deserialize, Clone, Hash, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum PackDependency {
@@ -60,7 +101,7 @@ impl super::Validator for PackValidator {
fn validate(
&self,
archive: &mut ZipArchive<Cursor<&[u8]>>,
archive: &mut ZipArchive<Cursor<bytes::Bytes>>,
) -> Result<ValidationResult, ValidationError> {
let mut file = archive
.by_name("index.json")
@@ -71,6 +112,10 @@ impl super::Validator for PackValidator {
let pack: PackFormat = serde_json::from_str(&contents)?;
pack
.validate()
.map_err(|err| ValidationError::InvalidInputError(validation_errors_to_string(err, None).into()))?;
if pack.game != "minecraft" {
return Err(ValidationError::InvalidInputError(
format!("Game {0} does not exist!", pack.game).into(),