From 0bd4dc400b7456a67c154ef60759dd466bba07bb Mon Sep 17 00:00:00 2001 From: Rob Wong Date: Wed, 18 Oct 2023 21:06:44 -0600 Subject: [PATCH] Feat: generate checkfile from proto --- validation/src/lib.rs | 81 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/validation/src/lib.rs b/validation/src/lib.rs index 2efbc96..ac87c8a 100644 --- a/validation/src/lib.rs +++ b/validation/src/lib.rs @@ -952,3 +952,84 @@ pub fn generate_checkfile(module: &modsurfer_module::Module) -> Result Result { + let mut validation = Validation::default(); + + let namespaces: Vec<&str> = module + .imports + .iter() + .fold(std::collections::HashSet::new(), |mut acc, import| { + acc.insert(import.module_name.as_str()); + acc + }) + .into_iter() + .collect(); + + // allow_wasi + if namespaces.contains(&"wasi_snapshot_preview1") { + validation.validate.allow_wasi = Some(true); + } + + // imports (add all to include + namespace) + let mut imports = Imports::default(); + let mut include_imports = vec![]; + let mod_imports = modsurfer_convert::from_api::imports(module.imports.clone()); + mod_imports.iter().for_each(|imp| { + include_imports.push(ImportItem::Item { + namespace: Some(imp.module_name.clone()), + name: imp.func.name.clone(), + params: Some(imp.func.ty.params.clone()), + results: Some(imp.func.ty.results.clone()), + }); + }); + imports.include = Some(include_imports); + + // imports.namespace (add all to imports) + let mut namespace = Namespace::default(); + namespace.include = Some( + namespaces + .iter() + .map(|name| NamespaceItem::Name(name.to_string())) + .collect::>(), + ); + if !namespaces.is_empty() { + imports.namespace = Some(namespace); + } + + // exports (add all exports) + let mut exports = Exports::default(); + let mut include_exports = vec![]; + let mod_exports = modsurfer_convert::from_api::exports(module.exports.clone()); + mod_exports.iter().for_each(|exp| { + include_exports.push(FunctionItem::Item { + name: exp.func.name.clone(), + params: Some(exp.func.ty.params.clone()), + results: Some(exp.func.ty.results.clone()), + hash: module.function_hashes.get(&exp.func.name).cloned(), + }); + }); + let export_count = include_exports.len(); + exports.include = Some(include_exports); + + // exports.max (match number of exports) + exports.max = Some(export_count as u32); + + // size.max (use size from module) + let mut size = Size::default(); + size.max = Some(human_bytes(module.size as f64)); + + // complexity.max_risk (use complexity) + let mut complexity = Complexity::default(); + complexity.max_risk = Some(RiskLevel::from(module.complexity.unwrap_or_default())); + + validation.validate.url = None; + validation.validate.imports = Some(imports); + validation.validate.exports = Some(exports); + validation.validate.size = Some(size); + validation.validate.complexity = Some(complexity); + + Ok(validation) +}