Remove newlines from group names, chat names and the displayname (#1845)

This commit is contained in:
Hocuri
2020-08-20 09:05:08 +02:00
committed by GitHub
parent 3faf968b7c
commit b9ca7b8ace
4 changed files with 29 additions and 6 deletions

View File

@@ -1952,7 +1952,8 @@ pub async fn create_group_chat(
verified: VerifiedStatus, verified: VerifiedStatus,
chat_name: impl AsRef<str>, chat_name: impl AsRef<str>,
) -> Result<ChatId, Error> { ) -> Result<ChatId, Error> {
ensure!(!chat_name.as_ref().is_empty(), "Invalid chat name"); let chat_name = improve_single_line_input(chat_name);
ensure!(!chat_name.is_empty(), "Invalid chat name");
let draft_txt = context let draft_txt = context
.stock_string_repl_str(StockMessage::NewGroupDraft, &chat_name) .stock_string_repl_str(StockMessage::NewGroupDraft, &chat_name)
@@ -1967,7 +1968,7 @@ pub async fn create_group_chat(
} else { } else {
Chattype::Group Chattype::Group
}, },
chat_name.as_ref().to_string(), chat_name,
grpid, grpid,
time(), time(),
], ],
@@ -2432,17 +2433,18 @@ pub async fn set_chat_name(
chat_id: ChatId, chat_id: ChatId,
new_name: impl AsRef<str>, new_name: impl AsRef<str>,
) -> Result<(), Error> { ) -> Result<(), Error> {
let new_name = improve_single_line_input(new_name);
/* the function only sets the names of group chats; normal chats get their names from the contacts */ /* the function only sets the names of group chats; normal chats get their names from the contacts */
let mut success = false; let mut success = false;
ensure!(!new_name.as_ref().is_empty(), "Invalid name"); ensure!(!new_name.is_empty(), "Invalid name");
ensure!(!chat_id.is_special(), "Invalid chat ID"); ensure!(!chat_id.is_special(), "Invalid chat ID");
let chat = Chat::load_from_db(context, chat_id).await?; let chat = Chat::load_from_db(context, chat_id).await?;
let mut msg = Message::default(); let mut msg = Message::default();
if real_group_exists(context, chat_id).await { if real_group_exists(context, chat_id).await {
if chat.name == new_name.as_ref() { if chat.name == new_name {
success = true; success = true;
} else if !is_contact_in_chat(context, chat_id, DC_CONTACT_ID_SELF).await { } else if !is_contact_in_chat(context, chat_id, DC_CONTACT_ID_SELF).await {
emit_event!( emit_event!(
@@ -2455,7 +2457,7 @@ pub async fn set_chat_name(
.sql .sql
.execute( .execute(
"UPDATE chats SET name=? WHERE id=?;", "UPDATE chats SET name=? WHERE id=?;",
paramsv![new_name.as_ref().to_string(), chat_id], paramsv![new_name.to_string(), chat_id],
) )
.await .await
.is_ok() .is_ok()
@@ -2467,7 +2469,7 @@ pub async fn set_chat_name(
.stock_system_msg( .stock_system_msg(
StockMessage::MsgGrpName, StockMessage::MsgGrpName,
&chat.name, &chat.name,
new_name.as_ref(), &new_name,
DC_CONTACT_ID_SELF, DC_CONTACT_ID_SELF,
) )
.await, .await,

View File

@@ -232,6 +232,10 @@ impl Context {
}); });
ret ret
} }
Config::Displayname => {
let value = value.map(improve_single_line_input);
self.sql.set_raw_config(self, key, value.as_deref()).await
}
_ => self.sql.set_raw_config(self, key, value).await, _ => self.sql.set_raw_config(self, key, value).await,
} }
} }

View File

@@ -235,6 +235,7 @@ impl Contact {
name: impl AsRef<str>, name: impl AsRef<str>,
addr: impl AsRef<str>, addr: impl AsRef<str>,
) -> Result<u32> { ) -> Result<u32> {
let name = improve_single_line_input(name);
ensure!( ensure!(
!addr.as_ref().is_empty(), !addr.as_ref().is_empty(),
"Cannot create contact with empty address" "Cannot create contact with empty address"

View File

@@ -612,6 +612,16 @@ pub(crate) fn listflags_has(listflags: u32, bitindex: usize) -> bool {
(listflags & bitindex) == bitindex (listflags & bitindex) == bitindex
} }
/// Makes sure that a user input that is not supposed to contain newlines does not contain newlines.
pub(crate) fn improve_single_line_input(input: impl AsRef<str>) -> String {
input
.as_ref()
.replace("\n", " ")
.replace("\r", " ")
.trim()
.to_string()
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
@@ -960,4 +970,10 @@ mod tests {
assert_eq!(w, 100); assert_eq!(w, 100);
assert_eq!(h, 50); assert_eq!(h, 50);
} }
#[test]
fn test_improve_single_line_input() {
assert_eq!(improve_single_line_input("Hi\naiae "), "Hi aiae");
assert_eq!(improve_single_line_input("\r\nahte\n\r"), "ahte");
}
} }