mirror of
https://github.com/chatmail/core.git
synced 2026-05-08 09:26:29 +03:00
api: do not allow to set configured_* configs via public API
This commit is contained in:
@@ -4,7 +4,7 @@ use std::env;
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use anyhow::{ensure, Context as _, Result};
|
use anyhow::{bail, ensure, Context as _, Result};
|
||||||
use strum::{EnumProperty, IntoEnumIterator};
|
use strum::{EnumProperty, IntoEnumIterator};
|
||||||
use strum_macros::{AsRefStr, Display, EnumIter, EnumProperty, EnumString};
|
use strum_macros::{AsRefStr, Display, EnumIter, EnumProperty, EnumString};
|
||||||
|
|
||||||
@@ -487,7 +487,12 @@ impl Context {
|
|||||||
self.sql.set_raw_config(key.as_ref(), value).await?;
|
self.sql.set_raw_config(key.as_ref(), value).await?;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
self.sql.set_raw_config(key.as_ref(), value).await?;
|
let key = key.as_ref();
|
||||||
|
if key.starts_with("configured_") {
|
||||||
|
bail!("not allowed to set {key} parameter directly");
|
||||||
|
} else {
|
||||||
|
self.sql.set_raw_config(key, value).await?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -556,7 +561,8 @@ impl Context {
|
|||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
self.set_config(Config::ConfiguredAddr, Some(primary_new))
|
self.sql
|
||||||
|
.set_raw_config(Config::ConfiguredAddr.as_ref(), Some(primary_new))
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
if let Some(old_addr) = old_addr {
|
if let Some(old_addr) = old_addr {
|
||||||
@@ -698,6 +704,18 @@ mod tests {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Test that it is not allowed to set configured parameters directly via public interface.
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
|
async fn test_set_configured() -> Result<()> {
|
||||||
|
let t = TestContext::new().await;
|
||||||
|
|
||||||
|
assert!(t
|
||||||
|
.set_config(Config::ConfiguredAddr, Some("alice@example.org"))
|
||||||
|
.await
|
||||||
|
.is_err());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
async fn test_self_addrs() -> Result<()> {
|
async fn test_self_addrs() -> Result<()> {
|
||||||
let alice = TestContext::new_alice().await;
|
let alice = TestContext::new_alice().await;
|
||||||
|
|||||||
@@ -472,7 +472,11 @@ async fn configure(ctx: &Context, param: &mut LoginParam) -> Result<()> {
|
|||||||
|
|
||||||
// the trailing underscore is correct
|
// the trailing underscore is correct
|
||||||
param.save_as_configured_params(ctx).await?;
|
param.save_as_configured_params(ctx).await?;
|
||||||
ctx.set_config(Config::ConfiguredTimestamp, Some(&time().to_string()))
|
ctx.sql
|
||||||
|
.set_raw_config(
|
||||||
|
Config::ConfiguredTimestamp.as_ref(),
|
||||||
|
Some(&time().to_string()),
|
||||||
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
progress!(ctx, 920);
|
progress!(ctx, 920);
|
||||||
|
|||||||
26
src/imap.rs
26
src/imap.rs
@@ -1754,16 +1754,21 @@ impl Imap {
|
|||||||
.context("failed to configure mvbox")?;
|
.context("failed to configure mvbox")?;
|
||||||
|
|
||||||
context
|
context
|
||||||
.set_config(Config::ConfiguredInboxFolder, Some("INBOX"))
|
.sql
|
||||||
|
.set_raw_config(Config::ConfiguredInboxFolder.as_ref(), Some("INBOX"))
|
||||||
.await?;
|
.await?;
|
||||||
if let Some(mvbox_folder) = mvbox_folder {
|
if let Some(mvbox_folder) = mvbox_folder {
|
||||||
info!(context, "Setting MVBOX FOLDER TO {}", &mvbox_folder);
|
info!(context, "Setting MVBOX FOLDER TO {}", &mvbox_folder);
|
||||||
context
|
context
|
||||||
.set_config(Config::ConfiguredMvboxFolder, Some(mvbox_folder))
|
.sql
|
||||||
|
.set_raw_config(Config::ConfiguredMvboxFolder.as_ref(), Some(mvbox_folder))
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
for (config, name) in folder_configs {
|
for (config, name) in folder_configs {
|
||||||
context.set_config(config, Some(&name)).await?;
|
context
|
||||||
|
.sql
|
||||||
|
.set_raw_config(config.as_ref(), Some(&name))
|
||||||
|
.await?;
|
||||||
}
|
}
|
||||||
context
|
context
|
||||||
.sql
|
.sql
|
||||||
@@ -2652,15 +2657,12 @@ mod tests {
|
|||||||
println!("Testing: For folder {folder}, mvbox_move {mvbox_move}, chat_msg {chat_msg}, accepted {accepted_chat}, outgoing {outgoing}, setupmessage {setupmessage}");
|
println!("Testing: For folder {folder}, mvbox_move {mvbox_move}, chat_msg {chat_msg}, accepted {accepted_chat}, outgoing {outgoing}, setupmessage {setupmessage}");
|
||||||
|
|
||||||
let t = TestContext::new_alice().await;
|
let t = TestContext::new_alice().await;
|
||||||
t.ctx
|
t.set_raw_config(Config::ConfiguredMvboxFolder, Some("DeltaChat"))
|
||||||
.set_config(Config::ConfiguredMvboxFolder, Some("DeltaChat"))
|
.await;
|
||||||
.await?;
|
t.set_raw_config(Config::ConfiguredSentboxFolder, Some("Sent"))
|
||||||
t.ctx
|
.await;
|
||||||
.set_config(Config::ConfiguredSentboxFolder, Some("Sent"))
|
t.set_raw_config(Config::MvboxMove, Some(if mvbox_move { "1" } else { "0" }))
|
||||||
.await?;
|
.await;
|
||||||
t.ctx
|
|
||||||
.set_config(Config::MvboxMove, Some(if mvbox_move { "1" } else { "0" }))
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
if accepted_chat {
|
if accepted_chat {
|
||||||
let contact_id = Contact::create(&t.ctx, "", "bob@example.net").await?;
|
let contact_id = Contact::create(&t.ctx, "", "bob@example.net").await?;
|
||||||
|
|||||||
@@ -89,7 +89,8 @@ impl Imap {
|
|||||||
Config::ConfiguredTrashFolder,
|
Config::ConfiguredTrashFolder,
|
||||||
] {
|
] {
|
||||||
context
|
context
|
||||||
.set_config(conf, folder_configs.get(&conf).map(|s| s.as_str()))
|
.sql
|
||||||
|
.set_raw_config(conf.as_ref(), folder_configs.get(&conf).map(|s| s.as_str()))
|
||||||
.await?;
|
.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
15
src/key.rs
15
src/key.rs
@@ -531,9 +531,8 @@ i8pcjGO+IZffvyZJVRWfVooBJmWWbPB1pueo3tx8w3+fcuzpxz+RLFKaPyqXO+dD
|
|||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
async fn test_load_self_generate_public() {
|
async fn test_load_self_generate_public() {
|
||||||
let t = TestContext::new().await;
|
let t = TestContext::new().await;
|
||||||
t.set_config(Config::ConfiguredAddr, Some("alice@example.org"))
|
t.set_raw_config(Config::ConfiguredAddr, Some("alice@example.org"))
|
||||||
.await
|
.await;
|
||||||
.unwrap();
|
|
||||||
let key = SignedPublicKey::load_self(&t).await;
|
let key = SignedPublicKey::load_self(&t).await;
|
||||||
assert!(key.is_ok());
|
assert!(key.is_ok());
|
||||||
}
|
}
|
||||||
@@ -541,9 +540,8 @@ i8pcjGO+IZffvyZJVRWfVooBJmWWbPB1pueo3tx8w3+fcuzpxz+RLFKaPyqXO+dD
|
|||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
async fn test_load_self_generate_secret() {
|
async fn test_load_self_generate_secret() {
|
||||||
let t = TestContext::new().await;
|
let t = TestContext::new().await;
|
||||||
t.set_config(Config::ConfiguredAddr, Some("alice@example.org"))
|
t.set_raw_config(Config::ConfiguredAddr, Some("alice@example.org"))
|
||||||
.await
|
.await;
|
||||||
.unwrap();
|
|
||||||
let key = SignedSecretKey::load_self(&t).await;
|
let key = SignedSecretKey::load_self(&t).await;
|
||||||
assert!(key.is_ok());
|
assert!(key.is_ok());
|
||||||
}
|
}
|
||||||
@@ -553,9 +551,8 @@ i8pcjGO+IZffvyZJVRWfVooBJmWWbPB1pueo3tx8w3+fcuzpxz+RLFKaPyqXO+dD
|
|||||||
use std::thread;
|
use std::thread;
|
||||||
|
|
||||||
let t = TestContext::new().await;
|
let t = TestContext::new().await;
|
||||||
t.set_config(Config::ConfiguredAddr, Some("alice@example.org"))
|
t.set_raw_config(Config::ConfiguredAddr, Some("alice@example.org"))
|
||||||
.await
|
.await;
|
||||||
.unwrap();
|
|
||||||
let thr0 = {
|
let thr0 = {
|
||||||
let ctx = t.clone();
|
let ctx = t.clone();
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
|
|||||||
@@ -1948,19 +1948,16 @@ mod tests {
|
|||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
|
||||||
let d = test::TestContext::new().await;
|
let d = test::TestContext::new().await;
|
||||||
let ctx = &d.ctx;
|
d.set_raw_config(Config::ConfiguredAddr, Some("self@example.com"))
|
||||||
|
.await;
|
||||||
ctx.set_config(Config::ConfiguredAddr, Some("self@example.com"))
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let chat = d.create_chat_with_contact("", "dest@example.com").await;
|
let chat = d.create_chat_with_contact("", "dest@example.com").await;
|
||||||
|
|
||||||
let mut msg = Message::new(Viewtype::Text);
|
let mut msg = Message::new(Viewtype::Text);
|
||||||
|
|
||||||
let msg_id = chat::prepare_msg(ctx, chat.id, &mut msg).await.unwrap();
|
let msg_id = chat::prepare_msg(&d, chat.id, &mut msg).await.unwrap();
|
||||||
|
|
||||||
let _msg2 = Message::load_from_db(ctx, msg_id).await.unwrap();
|
let _msg2 = Message::load_from_db(&d, msg_id).await.unwrap();
|
||||||
assert_eq!(_msg2.get_filemime(), None);
|
assert_eq!(_msg2.get_filemime(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2088,11 +2085,8 @@ mod tests {
|
|||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
|
||||||
let d = test::TestContext::new().await;
|
let d = test::TestContext::new().await;
|
||||||
let ctx = &d.ctx;
|
d.set_raw_config(Config::ConfiguredAddr, Some("self@example.com"))
|
||||||
|
.await;
|
||||||
ctx.set_config(Config::ConfiguredAddr, Some("self@example.com"))
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let chat = d.create_chat_with_contact("", "dest@example.com").await;
|
let chat = d.create_chat_with_contact("", "dest@example.com").await;
|
||||||
|
|
||||||
@@ -2101,18 +2095,18 @@ mod tests {
|
|||||||
|
|
||||||
// Prepare message for sending, so it gets a Message-Id.
|
// Prepare message for sending, so it gets a Message-Id.
|
||||||
assert!(msg.rfc724_mid.is_empty());
|
assert!(msg.rfc724_mid.is_empty());
|
||||||
let msg_id = chat::prepare_msg(ctx, chat.id, &mut msg).await.unwrap();
|
let msg_id = chat::prepare_msg(&d, chat.id, &mut msg).await.unwrap();
|
||||||
let msg = Message::load_from_db(ctx, msg_id).await.unwrap();
|
let msg = Message::load_from_db(&d, msg_id).await.unwrap();
|
||||||
assert!(!msg.rfc724_mid.is_empty());
|
assert!(!msg.rfc724_mid.is_empty());
|
||||||
|
|
||||||
let mut msg2 = Message::new(Viewtype::Text);
|
let mut msg2 = Message::new(Viewtype::Text);
|
||||||
msg2.set_quote(ctx, Some(&msg))
|
msg2.set_quote(&d, Some(&msg))
|
||||||
.await
|
.await
|
||||||
.expect("can't set quote");
|
.expect("can't set quote");
|
||||||
assert_eq!(msg2.quoted_text().unwrap(), msg.get_text());
|
assert_eq!(msg2.quoted_text().unwrap(), msg.get_text());
|
||||||
|
|
||||||
let quoted_msg = msg2
|
let quoted_msg = msg2
|
||||||
.quoted_message(ctx)
|
.quoted_message(&d)
|
||||||
.await
|
.await
|
||||||
.expect("error while retrieving quoted message")
|
.expect("error while retrieving quoted message")
|
||||||
.expect("quoted message not found");
|
.expect("quoted message not found");
|
||||||
|
|||||||
@@ -381,20 +381,20 @@ impl TestContext {
|
|||||||
self.event_senders.write().await.push(sink)
|
self.event_senders.write().await.push(sink)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set private configuration value for testing purposes.
|
||||||
|
pub async fn set_raw_config(&self, key: Config, value: Option<&str>) {
|
||||||
|
self.sql.set_raw_config(key.as_ref(), value).await.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
/// Configure as a given email address.
|
/// Configure as a given email address.
|
||||||
///
|
///
|
||||||
/// The context will be configured but the key will not be pre-generated so if a key is
|
/// The context will be configured but the key will not be pre-generated so if a key is
|
||||||
/// used the fingerprint will be different every time.
|
/// used the fingerprint will be different every time.
|
||||||
pub async fn configure_addr(&self, addr: &str) {
|
pub async fn configure_addr(&self, addr: &str) {
|
||||||
self.ctx.set_config(Config::Addr, Some(addr)).await.unwrap();
|
self.ctx.set_config(Config::Addr, Some(addr)).await.unwrap();
|
||||||
self.ctx
|
self.set_raw_config(Config::ConfiguredAddr, Some(addr))
|
||||||
.set_config(Config::ConfiguredAddr, Some(addr))
|
.await;
|
||||||
.await
|
self.set_raw_config(Config::Configured, Some("1")).await;
|
||||||
.unwrap();
|
|
||||||
self.ctx
|
|
||||||
.set_config(Config::Configured, Some("1"))
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
if let Some(name) = addr.split('@').next() {
|
if let Some(name) = addr.split('@').next() {
|
||||||
self.set_name(name);
|
self.set_name(name);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user