From f279730b0f398bfbaf29a0d12a383f043a8f4797 Mon Sep 17 00:00:00 2001 From: link2xt Date: Thu, 5 Oct 2023 14:58:22 +0000 Subject: [PATCH] feat: validate boolean values passed to set_config They may only be set to "0" and "1". Validation prevents accidentally setting the value to "true", "True" etc. --- src/config.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/config.rs b/src/config.rs index e99b6ed24..da21f91d9 100644 --- a/src/config.rs +++ b/src/config.rs @@ -465,6 +465,27 @@ impl Context { .set_raw_config(key.as_ref(), value.as_deref()) .await?; } + Config::Socks5Enabled + | Config::BccSelf + | Config::E2eeEnabled + | Config::MdnsEnabled + | Config::SentboxWatch + | Config::MvboxMove + | Config::OnlyFetchMvbox + | Config::FetchExistingMsgs + | Config::DeleteToTrash + | Config::SaveMimeHeaders + | Config::Configured + | Config::Bot + | Config::NotifyAboutWrongPw + | Config::SendSyncMsgs + | Config::SignUnencrypted => { + ensure!( + matches!(value, None | Some("0") | Some("1")), + "Boolean value must be either 0 or 1" + ); + self.sql.set_raw_config(key.as_ref(), value).await?; + } _ => { self.sql.set_raw_config(key.as_ref(), value).await?; } @@ -614,6 +635,18 @@ mod tests { ); } + /// Tests that "bot" config can only be set to "0" or "1". + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] + async fn test_set_config_bot() { + let t = TestContext::new().await; + + assert!(t.set_config(Config::Bot, None).await.is_ok()); + assert!(t.set_config(Config::Bot, Some("0")).await.is_ok()); + assert!(t.set_config(Config::Bot, Some("1")).await.is_ok()); + assert!(t.set_config(Config::Bot, Some("2")).await.is_err()); + assert!(t.set_config(Config::Bot, Some("Foobar")).await.is_err()); + } + #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn test_media_quality_config_option() { let t = TestContext::new().await;