From 95ca2ba3b88b72e76c9c5a53f9b8963c89e934c9 Mon Sep 17 00:00:00 2001 From: Philipp Eder Date: Thu, 15 Feb 2024 15:45:11 +0100 Subject: [PATCH] refactor(infisto): remove bincode As bincode is very unreliable while serializing and deserializing infisto switched to rmp-serde as a default. This removes bincode completely. --- rust/Cargo.lock | 38 --------- rust/infisto/Cargo.toml | 1 - rust/infisto/src/base.rs | 8 +- rust/infisto/src/bincode.rs | 112 -------------------------- rust/infisto/src/lib.rs | 1 - rust/models/Cargo.toml | 6 +- rust/models/src/credential.rs | 3 - rust/models/src/host_info.rs | 1 - rust/models/src/lib.rs | 5 +- rust/models/src/parameter.rs | 1 - rust/models/src/port.rs | 3 - rust/models/src/scan.rs | 1 - rust/models/src/scanner_preference.rs | 1 - rust/models/src/status.rs | 2 - rust/models/src/target.rs | 2 - rust/models/src/vt.rs | 1 - rust/openvasd/Cargo.toml | 1 - rust/openvasd/src/storage/file.rs | 9 --- rust/storage/Cargo.toml | 4 +- rust/storage/src/item.rs | 5 -- rust/storage/src/types.rs | 1 - 21 files changed, 5 insertions(+), 201 deletions(-) delete mode 100644 rust/infisto/src/bincode.rs diff --git a/rust/Cargo.lock b/rust/Cargo.lock index a7854f5dc..c338753a0 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -198,34 +198,6 @@ version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8c3c1a368f70d6cf7302d78f8f7093da241fb8e8807c05cc9e51a125895a6d5b" -[[package]] -name = "bincode" -version = "1.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" -dependencies = [ - "serde", -] - -[[package]] -name = "bincode" -version = "2.0.0-rc.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f11ea1a0346b94ef188834a65c068a03aec181c94896d481d7a0a40d85b0ce95" -dependencies = [ - "bincode_derive", - "serde", -] - -[[package]] -name = "bincode_derive" -version = "2.0.0-rc.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7e30759b3b99a1b802a7a3aa21c85c3ded5c28e1c83170d82d70f08bbf7f3e4c" -dependencies = [ - "virtue", -] - [[package]] name = "bit-set" version = "0.5.3" @@ -1320,7 +1292,6 @@ dependencies = [ name = "infisto" version = "0.1.0" dependencies = [ - "bincode 2.0.0-rc.3", "chacha20", "criterion", "pbkdf2", @@ -1633,7 +1604,6 @@ dependencies = [ name = "models" version = "0.1.0" dependencies = [ - "bincode 2.0.0-rc.3", "serde", "serde_json", ] @@ -1970,7 +1940,6 @@ version = "0.1.0" dependencies = [ "async-trait", "base64", - "bincode 1.3.3", "chacha20", "clap", "feed", @@ -2983,7 +2952,6 @@ checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" name = "storage" version = "0.1.0" dependencies = [ - "bincode 2.0.0-rc.3", "models", "serde", "time", @@ -3562,12 +3530,6 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" -[[package]] -name = "virtue" -version = "0.0.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9dcc60c0624df774c82a0ef104151231d37da4962957d691c011c852b2473314" - [[package]] name = "walkdir" version = "2.4.0" diff --git a/rust/infisto/Cargo.toml b/rust/infisto/Cargo.toml index 41cedca2d..abc9da308 100644 --- a/rust/infisto/Cargo.toml +++ b/rust/infisto/Cargo.toml @@ -8,7 +8,6 @@ edition.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -bincode = { version = "2.0.0-rc.3", features = ["serde"] } serde = { version = "1", features = ["derive"] } rand = "0" chacha20 = "0" diff --git a/rust/infisto/src/base.rs b/rust/infisto/src/base.rs index 3fc349a57..5ba84625a 100644 --- a/rust/infisto/src/base.rs +++ b/rust/infisto/src/base.rs @@ -165,9 +165,7 @@ impl IndexedFileStorer { fn store_index(&self, index: &[Index], id: &str) -> Result<(), Error> { let fn_name = format!("{}.idx", id); - let config = bincode::config::standard(); - let to_store = - bincode::serde::encode_to_vec(index, config).map_err(|_| Error::Serialize)?; + let to_store = rmp_serde::to_vec(index).map_err(|_e| Error::Serialize)?; let path = Path::new(&self.base).join(fn_name); let mut file = std::fs::OpenOptions::new() @@ -219,9 +217,7 @@ impl IndexedFileStorer { .map_err(|e| e.kind()) .map_err(Error::Read)?; - let config = bincode::config::standard(); - let (index, _) = - bincode::serde::decode_from_slice(&buffer, config).map_err(|_| Error::Serialize)?; + let index = rmp_serde::from_slice(&buffer).map_err(|_e| Error::Serialize)?; Ok(index) } diff --git a/rust/infisto/src/bincode.rs b/rust/infisto/src/bincode.rs deleted file mode 100644 index c49ac49ad..000000000 --- a/rust/infisto/src/bincode.rs +++ /dev/null @@ -1,112 +0,0 @@ -//! Contains helper for serializing and deserializing structs. -use crate::base; -use bincode::{Decode, Encode}; - -#[derive(Debug)] -/// Serializes and deserializes data -pub enum Serialization { - /// Wrapper for Deserialized T - Deserialized(T), - /// Wrapper for Serialized T - Serialized(Vec), -} - -impl Serialization -where - T: Encode, -{ - /// Serializes given data to Vec - pub fn serialize(t: T) -> Result { - let config = bincode::config::standard(); - - match bincode::encode_to_vec(&t, config) { - Ok(v) => Ok(Serialization::Serialized(v)), - Err(_) => Err(base::Error::Serialize), - } - } - - /// Deserializes given Serialization to T - pub fn deserialize(self) -> Result { - match self { - Serialization::Deserialized(s) => Ok(s), - Serialization::Serialized(_) => Err(base::Error::Serialize), - } - } -} - -impl TryFrom> for Serialization -where - T: Decode, -{ - type Error = base::Error; - - fn try_from(value: Vec) -> Result { - let config = bincode::config::standard(); - match bincode::decode_from_slice(&value, config) { - Ok((t, _)) => Ok(Serialization::Deserialized(t)), - Err(_) => Err(base::Error::Serialize), - } - } -} - -impl AsRef<[u8]> for Serialization { - fn as_ref(&self) -> &[u8] { - match self { - Serialization::Deserialized(_) => &[0u8], - Serialization::Serialized(v) => v.as_ref(), - } - } -} - -impl From> for Vec { - fn from(s: Serialization) -> Self { - match s { - Serialization::Deserialized(_) => vec![0u8], - Serialization::Serialized(v) => v, - } - } -} - -#[cfg(test)] -mod test { - - use crate::base::{CachedIndexFileStorer, IndexedByteStorage, Range}; - - const BASE: &str = "/tmp/openvasd/unittest"; - #[derive(bincode::Decode, bincode::Encode, Clone, Debug, PartialEq)] - struct Test { - a: u32, - b: u32, - } - - #[test] - fn serialization() { - let t = Test { a: 1, b: 2 }; - let s = super::Serialization::serialize(t).unwrap(); - let v = Vec::::from(s); - let s = super::Serialization::::try_from(v).unwrap(); - let t = match s { - super::Serialization::Deserialized(t) => t, - _ => panic!("Serialization::try_from failed"), - }; - assert_eq!(t, Test { a: 1, b: 2 }); - } - - #[test] - fn create_on_append() { - let key = "create_serde_on_append"; - let test = Test { a: 1, b: 2 }; - let serialized = super::Serialization::serialize(&test).unwrap(); - let mut store = CachedIndexFileStorer::init(BASE).unwrap(); - store.append(key, serialized).unwrap(); - let results: Vec> = store.by_range(key, Range::All).unwrap(); - assert_eq!(results.len(), 1); - let test2 = match results.first().unwrap() { - super::Serialization::Deserialized(t) => t.clone(), - _ => panic!("Serialization::try_from failed"), - }; - - assert_eq!(test, test2); - store.remove(key).unwrap(); - } -} diff --git a/rust/infisto/src/lib.rs b/rust/infisto/src/lib.rs index 780e86081..de891a798 100644 --- a/rust/infisto/src/lib.rs +++ b/rust/infisto/src/lib.rs @@ -2,6 +2,5 @@ #![warn(missing_docs)] pub mod base; -pub mod bincode; pub mod crypto; pub mod serde; diff --git a/rust/models/Cargo.toml b/rust/models/Cargo.toml index ca8d8908e..68605dd41 100644 --- a/rust/models/Cargo.toml +++ b/rust/models/Cargo.toml @@ -8,14 +8,10 @@ license = "GPL-2.0-or-later" [dependencies] serde = {version = "1", features = ["derive"], optional = true} -bincode = {version = "2.0.0-rc.3", optional = true } [features] -default = ["serde_support", "bincode_support"] +default = ["serde_support"] serde_support = ["serde"] -bincode_support = ["bincode"] [dev-dependencies] serde_json = "1" -# required for credentials -bincode = "2.0.0-rc.3" diff --git a/rust/models/src/credential.rs b/rust/models/src/credential.rs index 03a7f06e2..3be5eb8f4 100644 --- a/rust/models/src/credential.rs +++ b/rust/models/src/credential.rs @@ -8,7 +8,6 @@ feature = "serde_support", derive(serde::Serialize, serde::Deserialize) )] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] pub struct Credential { /// Service to use for accessing a host pub service: Service, @@ -61,7 +60,6 @@ impl Default for Credential { feature = "serde_support", derive(serde::Serialize, serde::Deserialize) )] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] pub enum Service { #[cfg_attr(feature = "serde_support", serde(rename = "ssh"))] /// SSH, supports [UP](CredentialType::UP) and [USK](CredentialType::USK) as credential types @@ -93,7 +91,6 @@ impl AsRef for Service { feature = "serde_support", derive(serde::Serialize, serde::Deserialize) )] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] /// Enum representing the type of credentials. pub enum CredentialType { #[cfg_attr(feature = "serde_support", serde(rename = "up"))] diff --git a/rust/models/src/host_info.rs b/rust/models/src/host_info.rs index 6e502d44c..f507bfc4c 100644 --- a/rust/models/src/host_info.rs +++ b/rust/models/src/host_info.rs @@ -8,7 +8,6 @@ feature = "serde_support", derive(serde::Serialize, serde::Deserialize) )] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] pub struct HostInfo { /// Number of all hosts, that are contained in a target pub all: u32, diff --git a/rust/models/src/lib.rs b/rust/models/src/lib.rs index 0bb3a6444..b76430102 100644 --- a/rust/models/src/lib.rs +++ b/rust/models/src/lib.rs @@ -168,9 +168,6 @@ mod tests { } "#; // tests that it doesn't panic when parsing the json - let s: Scan = serde_json::from_str(json_str).unwrap(); - - let _b = bincode::encode_to_vec(s, bincode::config::standard()).unwrap(); - //let _: Target = bincode::deserialize(&b).unwrap(); + let _: Scan = serde_json::from_str(json_str).unwrap(); } } diff --git a/rust/models/src/parameter.rs b/rust/models/src/parameter.rs index a675bdb04..0cd77e271 100644 --- a/rust/models/src/parameter.rs +++ b/rust/models/src/parameter.rs @@ -7,7 +7,6 @@ feature = "serde_support", derive(serde::Serialize, serde::Deserialize) )] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] /// Represents a parameter for a VTS configuration. pub struct Parameter { /// The ID of the parameter. diff --git a/rust/models/src/port.rs b/rust/models/src/port.rs index 086cb94a3..6864528ac 100644 --- a/rust/models/src/port.rs +++ b/rust/models/src/port.rs @@ -9,7 +9,6 @@ use std::fmt::Display; feature = "serde_support", derive(serde::Serialize, serde::Deserialize) )] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] pub struct Port { #[cfg_attr( feature = "serde_support", @@ -27,7 +26,6 @@ pub struct Port { feature = "serde_support", derive(serde::Serialize, serde::Deserialize) )] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] pub struct PortRange { /// The required start port. /// @@ -59,7 +57,6 @@ impl Display for PortRange { feature = "serde_support", derive(serde::Serialize, serde::Deserialize) )] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] #[cfg_attr(feature = "serde_support", serde(rename_all = "lowercase"))] pub enum Protocol { UDP, diff --git a/rust/models/src/scan.rs b/rust/models/src/scan.rs index aac965aa6..3146c0a22 100644 --- a/rust/models/src/scan.rs +++ b/rust/models/src/scan.rs @@ -11,7 +11,6 @@ use super::{scanner_preference::ScannerPreference, target::Target, vt::VT}; derive(serde::Serialize, serde::Deserialize), serde(deny_unknown_fields) )] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] pub struct Scan { #[cfg_attr(feature = "serde_support", serde(default))] /// Unique ID of a scan diff --git a/rust/models/src/scanner_preference.rs b/rust/models/src/scanner_preference.rs index b00eebd5d..5e1a461b2 100644 --- a/rust/models/src/scanner_preference.rs +++ b/rust/models/src/scanner_preference.rs @@ -8,7 +8,6 @@ feature = "serde_support", derive(serde::Serialize, serde::Deserialize) )] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] pub struct ScannerPreference { /// The ID of the scanner preference. pub id: String, diff --git a/rust/models/src/status.rs b/rust/models/src/status.rs index 2d7a11851..868a730d9 100644 --- a/rust/models/src/status.rs +++ b/rust/models/src/status.rs @@ -12,7 +12,6 @@ use super::host_info::HostInfo; feature = "serde_support", derive(serde::Serialize, serde::Deserialize) )] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] pub struct Status { /// Timestamp for the start of a scan pub start_time: Option, @@ -41,7 +40,6 @@ impl Status { derive(serde::Serialize, serde::Deserialize) )] #[cfg_attr(feature = "serde_support", serde(rename_all = "snake_case"))] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] pub enum Phase { /// A scan has been stored but not started yet #[default] diff --git a/rust/models/src/target.rs b/rust/models/src/target.rs index c255c3dce..76626f02d 100644 --- a/rust/models/src/target.rs +++ b/rust/models/src/target.rs @@ -10,7 +10,6 @@ use super::{credential::Credential, port::Port}; feature = "serde_support", derive(serde::Serialize, serde::Deserialize) )] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] pub struct Target { /// List of hosts to scan pub hosts: Vec, @@ -40,7 +39,6 @@ pub struct Target { feature = "serde_support", derive(serde::Serialize, serde::Deserialize) )] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] #[cfg_attr(feature = "serde_support", serde(rename_all = "snake_case"))] pub enum AliveTestMethods { Icmp, diff --git a/rust/models/src/vt.rs b/rust/models/src/vt.rs index 7000d9f19..78581045a 100644 --- a/rust/models/src/vt.rs +++ b/rust/models/src/vt.rs @@ -10,7 +10,6 @@ use super::parameter::Parameter; feature = "serde_support", derive(serde::Serialize, serde::Deserialize) )] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] pub struct VT { /// The ID of the VT to execute pub oid: String, diff --git a/rust/openvasd/Cargo.toml b/rust/openvasd/Cargo.toml index c03adc674..eacf83f00 100644 --- a/rust/openvasd/Cargo.toml +++ b/rust/openvasd/Cargo.toml @@ -21,7 +21,6 @@ tracing = "0.1.37" tracing-subscriber = { version = "0.3.17", features = ["env-filter"] } serde_json = "1.0.96" serde = { version = "1.0.163", features = ["derive"] } -bincode = "1" uuid = { version = "1", features = ["v4", "fast-rng", "serde"] } rustls = {version = "0.22"} tokio-rustls = "0.25" diff --git a/rust/openvasd/src/storage/file.rs b/rust/openvasd/src/storage/file.rs index 6e3f1c7fa..4f47eb2f4 100644 --- a/rust/openvasd/src/storage/file.rs +++ b/rust/openvasd/src/storage/file.rs @@ -515,15 +515,6 @@ mod tests { use super::*; - #[test] - fn serialize() { - let scan = models::Status::default(); - - let serialized = bincode::serialize(&scan).unwrap(); - let deserialized = bincode::deserialize(&serialized).unwrap(); - assert_eq!(scan, deserialized); - } - #[tokio::test] async fn credentials() { let jraw = r#" diff --git a/rust/storage/Cargo.toml b/rust/storage/Cargo.toml index 85d2dbf94..2b80eca12 100644 --- a/rust/storage/Cargo.toml +++ b/rust/storage/Cargo.toml @@ -9,11 +9,9 @@ license = "GPL-2.0-or-later" [dependencies] time = {version = "0", features = ["parsing"]} serde = { version = "1.0", features = ["derive"], optional = true } -bincode = {version = "2.0.0-rc.3", optional = true } tracing = "0.1.37" models = { path = "../models" } [features] -default = ["serde_support", "bincode_support"] +default = ["serde_support"] serde_support = ["serde"] -bincode_support = ["bincode"] diff --git a/rust/storage/src/item.rs b/rust/storage/src/item.rs index 2f0bf07fa..35a6c931a 100644 --- a/rust/storage/src/item.rs +++ b/rust/storage/src/item.rs @@ -48,7 +48,6 @@ use crate::{ derive(serde::Serialize, serde::Deserialize), serde(rename_all = "snake_case") )] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] pub enum ACT { /// Defines a initializer Init, @@ -106,7 +105,6 @@ macro_rules! make_str_lookup_enum { derive(serde::Serialize, serde::Deserialize), serde(rename_all = "snake_case") )] - #[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] pub enum $enum_name { $( #[doc = concat!(stringify!($matcher))] @@ -296,7 +294,6 @@ Category will be used to identify the type of the NASL plugin."### => derive(serde::Serialize, serde::Deserialize), serde(rename_all = "snake_case") )] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] pub struct NvtPreference { /// Preference ID pub id: Option, @@ -315,7 +312,6 @@ pub struct NvtPreference { derive(serde::Serialize, serde::Deserialize), serde(rename_all = "snake_case") )] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] pub struct NvtRef { /// Reference type ("cve", "bid", ...) pub class: String, @@ -425,7 +421,6 @@ impl TagValue { } #[derive(Clone, Debug, Default, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] #[cfg_attr( feature = "serde_support", derive(serde::Serialize, serde::Deserialize), diff --git a/rust/storage/src/types.rs b/rust/storage/src/types.rs index 26700ae73..fa03db5b4 100644 --- a/rust/storage/src/types.rs +++ b/rust/storage/src/types.rs @@ -12,7 +12,6 @@ use std::collections::HashMap; derive(serde::Serialize, serde::Deserialize), serde(untagged) )] -#[cfg_attr(feature = "bincode_support", derive(bincode::Encode, bincode::Decode))] /// Allowed type definitions pub enum Primitive { /// String value