From 7bfefcaa13102c52345cb185bfe1d525b9e3b39f Mon Sep 17 00:00:00 2001 From: louib Date: Mon, 26 Aug 2024 18:52:00 -0400 Subject: [PATCH] chore: check for compilation warnings in CI (#236) --- .github/workflows/ci.yml | 6 ++++++ Cargo.toml | 3 +++ src/compression.rs | 9 ++++++++- src/config.rs | 12 +++++++++++- src/crypt/ciphers.rs | 24 +++++++++++++++++++++++- src/format/kdbx4/mod.rs | 7 ++++++- src/format/mod.rs | 6 +++++- src/hmac_block_stream.rs | 1 + src/lib.rs | 1 + src/variant_dictionary.rs | 16 +++++++++++++--- src/xml_db/mod.rs | 2 ++ 11 files changed, 79 insertions(+), 8 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e0843ed4..1fe47e74 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,6 +21,8 @@ jobs: doc: name: Build Doc runs-on: ubuntu-latest + env: + RUSTFLAGS: -D warnings steps: - uses: actions/checkout@v4 with: @@ -35,6 +37,8 @@ jobs: check: name: Check runs-on: ubuntu-latest + env: + RUSTFLAGS: -D warnings steps: - uses: actions/checkout@v4 - uses: taiki-e/install-action@v2 @@ -48,6 +52,8 @@ jobs: test: name: Test Suite runs-on: ubuntu-latest + env: + RUSTFLAGS: -D warnings steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable diff --git a/Cargo.toml b/Cargo.toml index 7f8ed8f3..e3b3c88d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -127,3 +127,6 @@ required-features = ["utilities", "save_kdbx4", "challenge_response"] [[bin]] name = "kp-yk-recover" required-features = ["utilities", "save_kdbx4", "challenge_response"] + +[lints.rust] +unexpected_cfgs = { level = "warn", check-cfg = ['cfg(tarpaulin_include)'] } diff --git a/src/compression.rs b/src/compression.rs index 7fe9033b..9ef4fa4c 100644 --- a/src/compression.rs +++ b/src/compression.rs @@ -1,9 +1,14 @@ use flate2::read::GzDecoder; +#[cfg(feature = "save_kdbx4")] use flate2::write::GzEncoder; +#[cfg(feature = "save_kdbx4")] use flate2::Compression as Flate2Compression; -use std::io::{Read, Write}; +use std::io::Read; +#[cfg(feature = "save_kdbx4")] +use std::io::Write; pub trait Compression { + #[cfg(feature = "save_kdbx4")] fn compress(&self, in_buffer: &[u8]) -> Result, std::io::Error>; fn decompress(&self, in_buffer: &[u8]) -> Result, std::io::Error>; } @@ -11,6 +16,7 @@ pub trait Compression { pub struct NoCompression; impl Compression for NoCompression { + #[cfg(feature = "save_kdbx4")] fn compress(&self, in_buffer: &[u8]) -> Result, std::io::Error> { Ok(in_buffer.to_vec()) } @@ -22,6 +28,7 @@ impl Compression for NoCompression { pub struct GZipCompression; impl Compression for GZipCompression { + #[cfg(feature = "save_kdbx4")] fn compress(&self, in_buffer: &[u8]) -> Result, std::io::Error> { let mut res = Vec::new(); let mut encoder = GzEncoder::new(&mut res, Flate2Compression::default()); diff --git a/src/config.rs b/src/config.rs index 25e404ee..e64fd6e9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -5,10 +5,12 @@ use std::convert::TryFrom; pub use crate::format::DatabaseVersion; +#[cfg(feature = "save_kdbx4")] +use crate::crypt::ciphers::Cipher; use crate::{ compression, crypt::{ - ciphers::{self, Cipher}, + ciphers::{self}, kdf, }, error::{ @@ -89,6 +91,7 @@ impl OuterCipherConfig { } } + #[cfg(feature = "save_kdbx4")] pub(crate) fn get_iv_size(&self) -> usize { match self { OuterCipherConfig::AES256 => ciphers::AES256Cipher::iv_size(), @@ -97,6 +100,7 @@ impl OuterCipherConfig { } } + #[cfg(feature = "save_kdbx4")] pub(crate) fn dump(&self) -> [u8; 16] { match self { OuterCipherConfig::AES256 => CIPHERSUITE_AES256, @@ -139,6 +143,7 @@ impl InnerCipherConfig { } } + #[cfg(feature = "save_kdbx4")] pub(crate) fn dump(&self) -> u32 { match self { InnerCipherConfig::Plain => PLAIN, @@ -147,6 +152,7 @@ impl InnerCipherConfig { } } + #[cfg(feature = "save_kdbx4")] pub(crate) fn get_key_size(&self) -> usize { match self { InnerCipherConfig::Plain => ciphers::PlainCipher::key_size(), @@ -216,6 +222,7 @@ fn serialize_argon2_version( } impl KdfConfig { + #[cfg(feature = "save_kdbx4")] fn seed_size(&self) -> usize { match self { KdfConfig::Aes { .. } => 32, @@ -226,6 +233,7 @@ impl KdfConfig { /// For writing out a database, generate a new KDF seed from the config and return the KDF /// and the generated seed + #[cfg(feature = "save_kdbx4")] pub(crate) fn get_kdf_and_seed(&self) -> Result<(Box, Vec), getrandom::Error> { let mut kdf_seed = vec![0; self.seed_size()]; getrandom::getrandom(&mut kdf_seed)?; @@ -271,6 +279,7 @@ impl KdfConfig { } } + #[cfg(feature = "save_kdbx4")] pub(crate) fn to_variant_dictionary(&self, seed: &[u8]) -> VariantDictionary { let mut vd = VariantDictionary::new(); @@ -394,6 +403,7 @@ impl CompressionConfig { } } + #[cfg(feature = "save_kdbx4")] pub(crate) fn dump(&self) -> [u8; 4] { match self { CompressionConfig::None => [0, 0, 0, 0], diff --git a/src/crypt/ciphers.rs b/src/crypt/ciphers.rs index d69eb9bf..0683f517 100644 --- a/src/crypt/ciphers.rs +++ b/src/crypt/ciphers.rs @@ -1,5 +1,7 @@ use aes::Aes256; -use cipher::{block_padding::Pkcs7, generic_array::GenericArray, BlockDecryptMut, BlockEncryptMut}; +#[cfg(feature = "save_kdbx4")] +use cipher::BlockEncryptMut; +use cipher::{block_padding::Pkcs7, generic_array::GenericArray, BlockDecryptMut}; use salsa20::{ cipher::{KeyIvInit, StreamCipher}, Salsa20, @@ -8,20 +10,24 @@ use salsa20::{ use crate::crypt::CryptographyError; pub(crate) trait Cipher { + #[cfg(feature = "save_kdbx4")] fn encrypt(&mut self, plaintext: &[u8]) -> Result, CryptographyError>; fn decrypt(&mut self, ciphertext: &[u8]) -> Result, CryptographyError>; + #[cfg(feature = "save_kdbx4")] /// The number of bytes expected by the cipher as an initialization vector. fn iv_size() -> usize where Self: Sized; + #[cfg(feature = "save_kdbx4")] /// The number of bytes expected by the cipher as a key. fn key_size() -> usize where Self: Sized; } +#[cfg(feature = "save_kdbx4")] type Aes256CbcEncryptor = cbc::Encryptor; type Aes256CbcDecryptor = cbc::Decryptor; pub(crate) struct AES256Cipher { @@ -39,6 +45,7 @@ impl AES256Cipher { } impl Cipher for AES256Cipher { + #[cfg(feature = "save_kdbx4")] fn encrypt(&mut self, plaintext: &[u8]) -> Result, CryptographyError> { let cipher = Aes256CbcEncryptor::new_from_slices(&self.key, &self.iv)?; @@ -60,15 +67,18 @@ impl Cipher for AES256Cipher { Ok(out) } + #[cfg(feature = "save_kdbx4")] fn iv_size() -> usize { 16 } + #[cfg(feature = "save_kdbx4")] fn key_size() -> usize { 32 } } +#[cfg(feature = "save_kdbx4")] type TwofishCbcEncryptor = cbc::Encryptor; type TwofishCbcDecryptor = cbc::Decryptor; pub(crate) struct TwofishCipher { @@ -86,6 +96,7 @@ impl TwofishCipher { } impl Cipher for TwofishCipher { + #[cfg(feature = "save_kdbx4")] fn encrypt(&mut self, plaintext: &[u8]) -> Result, CryptographyError> { let cipher = TwofishCbcEncryptor::new_from_slices(&self.key, &self.iv)?; @@ -102,10 +113,12 @@ impl Cipher for TwofishCipher { Ok(buf) } + #[cfg(feature = "save_kdbx4")] fn iv_size() -> usize { 16 } + #[cfg(feature = "save_kdbx4")] fn key_size() -> usize { 32 } @@ -127,6 +140,7 @@ impl Salsa20Cipher { } impl Cipher for Salsa20Cipher { + #[cfg(feature = "save_kdbx4")] fn encrypt(&mut self, plaintext: &[u8]) -> Result, CryptographyError> { let mut buffer = Vec::from(plaintext); self.cipher.apply_keystream(&mut buffer); @@ -138,11 +152,13 @@ impl Cipher for Salsa20Cipher { Ok(buffer) } + #[cfg(feature = "save_kdbx4")] fn iv_size() -> usize { // or 16 32 } + #[cfg(feature = "save_kdbx4")] fn key_size() -> usize { 32 } @@ -174,6 +190,7 @@ impl ChaCha20Cipher { } impl Cipher for ChaCha20Cipher { + #[cfg(feature = "save_kdbx4")] fn encrypt(&mut self, plaintext: &[u8]) -> Result, CryptographyError> { let mut buffer = Vec::from(plaintext); self.cipher.apply_keystream(&mut buffer); @@ -185,10 +202,12 @@ impl Cipher for ChaCha20Cipher { Ok(buffer) } + #[cfg(feature = "save_kdbx4")] fn iv_size() -> usize { 12 } + #[cfg(feature = "save_kdbx4")] fn key_size() -> usize { 32 } @@ -201,6 +220,7 @@ impl PlainCipher { } } impl Cipher for PlainCipher { + #[cfg(feature = "save_kdbx4")] fn encrypt(&mut self, plaintext: &[u8]) -> Result, CryptographyError> { Ok(Vec::from(plaintext)) } @@ -208,10 +228,12 @@ impl Cipher for PlainCipher { Ok(Vec::from(ciphertext)) } + #[cfg(feature = "save_kdbx4")] fn iv_size() -> usize { 1 } + #[cfg(feature = "save_kdbx4")] fn key_size() -> usize { 1 } diff --git a/src/format/kdbx4/mod.rs b/src/format/kdbx4/mod.rs index d8681c0d..48daad11 100644 --- a/src/format/kdbx4/mod.rs +++ b/src/format/kdbx4/mod.rs @@ -1,3 +1,4 @@ +#[cfg(feature = "save_kdbx4")] mod dump; mod parse; @@ -6,9 +7,11 @@ use crate::{ format::DatabaseVersion, }; +#[cfg(feature = "save_kdbx4")] pub(crate) use crate::format::kdbx4::dump::dump_kdbx4; pub(crate) use crate::format::kdbx4::parse::{decrypt_kdbx4, parse_kdbx4}; +#[cfg(feature = "save_kdbx4")] /// Size for a master seed in bytes pub const HEADER_MASTER_SEED_SIZE: usize = 32; @@ -51,14 +54,16 @@ struct KDBX4InnerHeader { inner_random_stream_key: Vec, } +#[cfg(feature = "save_kdbx4")] #[cfg(test)] mod kdbx4_tests { use super::*; + use crate::format::kdbx4::dump::dump_kdbx4; use crate::{ config::{CompressionConfig, DatabaseConfig, InnerCipherConfig, KdfConfig, OuterCipherConfig}, db::{Database, Entry, Group, HeaderAttachment, NodeRef, Value}, - format::{kdbx4::dump::dump_kdbx4, KDBX4_CURRENT_MINOR_VERSION}, + format::KDBX4_CURRENT_MINOR_VERSION, key::DatabaseKey, }; diff --git a/src/format/mod.rs b/src/format/mod.rs index 3e6e6e99..6fefb981 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -2,9 +2,12 @@ pub(crate) mod kdb; pub(crate) mod kdbx3; pub(crate) mod kdbx4; +#[cfg(feature = "save_kdbx4")] use std::io::Write; -use byteorder::{ByteOrder, LittleEndian, WriteBytesExt}; +#[cfg(feature = "save_kdbx4")] +use byteorder::WriteBytesExt; +use byteorder::{ByteOrder, LittleEndian}; use crate::error::DatabaseIntegrityError; @@ -70,6 +73,7 @@ impl DatabaseVersion { Ok(response) } + #[cfg(feature = "save_kdbx4")] fn dump(&self, writer: &mut dyn Write) -> Result<(), std::io::Error> { if let DatabaseVersion::KDB4(minor_version) = self { writer.write(&crate::format::KDBX_IDENTIFIER)?; diff --git a/src/hmac_block_stream.rs b/src/hmac_block_stream.rs index 2b1c660a..57a134fa 100644 --- a/src/hmac_block_stream.rs +++ b/src/hmac_block_stream.rs @@ -49,6 +49,7 @@ pub(crate) fn read_hmac_block_stream( Ok(out) } +#[cfg(feature = "save_kdbx4")] /// Write a raw buffer as a HMAC block stream pub(crate) fn write_hmac_block_stream( data: &[u8], diff --git a/src/lib.rs b/src/lib.rs index a999aef8..9c19700f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ pub mod db; pub mod error; pub(crate) mod format; pub(crate) mod hmac_block_stream; +#[cfg(feature = "save_kdbx4")] mod io; mod key; pub(crate) mod variant_dictionary; diff --git a/src/variant_dictionary.rs b/src/variant_dictionary.rs index dccf8990..45406892 100644 --- a/src/variant_dictionary.rs +++ b/src/variant_dictionary.rs @@ -1,7 +1,13 @@ -use byteorder::{ByteOrder, LittleEndian, WriteBytesExt}; -use std::{collections::HashMap, io::Write}; +#[cfg(feature = "save_kdbx4")] +use byteorder::WriteBytesExt; +use byteorder::{ByteOrder, LittleEndian}; +use std::collections::HashMap; +#[cfg(feature = "save_kdbx4")] +use std::io::Write; -use crate::{error::VariantDictionaryError, io::WriteLengthTaggedExt}; +use crate::error::VariantDictionaryError; +#[cfg(feature = "save_kdbx4")] +use crate::io::WriteLengthTaggedExt; pub const VARIANT_DICTIONARY_VERSION: u16 = 0x100; pub const VARIANT_DICTIONARY_END: u8 = 0x0; @@ -20,6 +26,7 @@ pub(crate) struct VariantDictionary { } impl VariantDictionary { + #[cfg(feature = "save_kdbx4")] pub(crate) fn new() -> Self { Self { data: HashMap::new() } } @@ -78,6 +85,7 @@ impl VariantDictionary { Ok(VariantDictionary { data }) } + #[cfg(feature = "save_kdbx4")] pub(crate) fn dump(&self, writer: &mut dyn Write) -> Result<(), std::io::Error> { writer.write_u16::(VARIANT_DICTIONARY_VERSION)?; @@ -144,6 +152,7 @@ impl VariantDictionary { .ok_or_else(|| VariantDictionaryError::Mistyped { key: key.to_owned() }) } + #[cfg(feature = "save_kdbx4")] pub(crate) fn set(&mut self, key: &str, value: T) where T: Into, @@ -303,6 +312,7 @@ mod variant_dictionary_tests { } #[test] + #[cfg(feature = "save_kdbx4")] fn variant_dictionary() { let mut vd = VariantDictionary::new(); diff --git a/src/xml_db/mod.rs b/src/xml_db/mod.rs index 509037e0..f019f7f8 100644 --- a/src/xml_db/mod.rs +++ b/src/xml_db/mod.rs @@ -1,3 +1,4 @@ +#[cfg(feature = "save_kdbx4")] pub mod dump; pub mod parse; @@ -7,6 +8,7 @@ pub fn get_epoch_baseline() -> chrono::NaiveDateTime { chrono::NaiveDateTime::parse_from_str("0001-01-01T00:00:00", "%Y-%m-%dT%H:%M:%S").unwrap() } +#[cfg(feature = "save_kdbx4")] #[cfg(test)] mod tests { use chrono::NaiveDateTime;