diff --git a/crates/iz-core/src/lib.rs b/crates/iz-core/src/lib.rs index d466c9b..9580ed8 100644 --- a/crates/iz-core/src/lib.rs +++ b/crates/iz-core/src/lib.rs @@ -21,6 +21,7 @@ pub mod slides; pub mod sniff; pub mod staging; pub mod store; +pub mod text; pub mod time; pub mod token; pub mod zones; diff --git a/crates/iz-core/src/store/attachments.rs b/crates/iz-core/src/store/attachments.rs index dc1edf2..91b0d9d 100644 --- a/crates/iz-core/src/store/attachments.rs +++ b/crates/iz-core/src/store/attachments.rs @@ -169,7 +169,10 @@ pub fn clean_name(raw: &str) -> Result { let last = raw.rsplit(['/', '\\']).next().unwrap_or(""); // Stripped before the checks: a name that is only invisible characters is empty, and an extension hidden by // a bidi override is the extension the name really has. - let stripped: String = last.chars().filter(|c| !is_invisible_or_bidi(*c)).collect(); + let stripped: String = last + .chars() + .filter(|c| !crate::text::is_invisible_or_bidi(*c)) + .collect(); let name = stripped.trim(); if name.is_empty() || name == "." || name == ".." { return Err(Error::Invalid("name_empty".into())); @@ -183,24 +186,6 @@ pub fn clean_name(raw: &str) -> Result { Ok(name.to_owned()) } -/// Whether `c` is an invisible formatting character or a bidi control, which changes how a name is shown: -/// U+202E makes `invoice\u{202E}fdp.exe` read as `invoice exe.pdf`. Upload names drop these characters. -/// The set is the one of `in`'s display-name rules (`in-core` `names.rs`); copied, not shared. U+200D stays: -/// emoji sequences need it, and it cannot reorder text. -fn is_invisible_or_bidi(c: char) -> bool { - matches!( - c, - '\u{200B}'..='\u{200C}' - | '\u{200E}'..='\u{200F}' - | '\u{202A}'..='\u{202E}' - | '\u{2060}'..='\u{2064}' - | '\u{2066}'..='\u{2069}' - | '\u{FEFF}' - | '\u{2028}' - | '\u{2029}' - ) -} - /// The extension of a clean name, lowercase, without the dot. `None` when there is none or it is not a /// plain alphanumeric word. pub fn extension_of(name: &str) -> Option { diff --git a/crates/iz-core/src/store/board.rs b/crates/iz-core/src/store/board.rs index 226594b..abdaabb 100644 --- a/crates/iz-core/src/store/board.rs +++ b/crates/iz-core/src/store/board.rs @@ -139,24 +139,24 @@ pub fn valid_prefix(prefix: &str) -> bool { /// Trims and checks a column name. pub fn clean_column_name(name: &str) -> Result { - let name = name.trim(); + let name = crate::text::single_line(name); if name.is_empty() || name.chars().count() > COLUMN_NAME_MAX { return Err(Error::Invalid(format!( "column name must be 1 to {COLUMN_NAME_MAX} characters" ))); } - Ok(name.to_owned()) + Ok(name) } /// Trims and checks a tag name. pub fn clean_tag_name(name: &str) -> Result { - let name = name.trim(); + let name = crate::text::single_line(name); if name.is_empty() || name.chars().count() > TAG_NAME_MAX { return Err(Error::Invalid(format!( "project name must be 1 to {TAG_NAME_MAX} characters" ))); } - Ok(name.to_owned()) + Ok(name) } /// Live columns of a board in display order. A query helper shared with the task module. diff --git a/crates/iz-core/src/store/comments.rs b/crates/iz-core/src/store/comments.rs index 5d0b4b2..0d4ad8a 100644 --- a/crates/iz-core/src/store/comments.rs +++ b/crates/iz-core/src/store/comments.rs @@ -48,13 +48,13 @@ fn map_comment(row: &turso::Row) -> Result { } fn clean_body(body: &str) -> Result { - let body = body.trim(); + let body = crate::text::multi_line(body); if body.is_empty() || body.chars().count() > COMMENT_MAX { return Err(Error::Invalid(format!( "comment must be 1 to {COMMENT_MAX} characters" ))); } - Ok(body.to_owned()) + Ok(body) } /// The member id behind an actor that has already passed `authorize`. diff --git a/crates/iz-core/src/store/rules.rs b/crates/iz-core/src/store/rules.rs index 6685626..f35bc2e 100644 --- a/crates/iz-core/src/store/rules.rs +++ b/crates/iz-core/src/store/rules.rs @@ -86,7 +86,7 @@ fn map_rule(row: &turso::Row) -> Result { /// Checks the input and returns the cleaned copy. Every refusal is a message for the admin. async fn checked(conn: &Connection, board_id: &str, input: &RuleInput) -> Result { - let name = input.name.trim().to_owned(); + let name = crate::text::single_line(&input.name); if name.is_empty() || name.chars().count() > RULE_NAME_MAX { return Err(Error::Invalid(format!( "the name must be 1 to {RULE_NAME_MAX} characters" diff --git a/crates/iz-core/src/store/tasks.rs b/crates/iz-core/src/store/tasks.rs index 78ccae8..d599bdf 100644 --- a/crates/iz-core/src/store/tasks.rs +++ b/crates/iz-core/src/store/tasks.rs @@ -312,13 +312,13 @@ fn map_assignee(row: &turso::Row) -> Result<(String, Assignee)> { } fn clean_title(title: &str) -> Result { - let title = title.trim(); + let title = crate::text::single_line(title); if title.is_empty() || title.chars().count() > TITLE_MAX { return Err(Error::Invalid(format!( "title must be 1 to {TITLE_MAX} characters" ))); } - Ok(title.to_owned()) + Ok(title) } fn clean_description(description: &str) -> Result { @@ -327,7 +327,7 @@ fn clean_description(description: &str) -> Result { "description must be at most {DESCRIPTION_MAX} characters" ))); } - Ok(description.to_owned()) + Ok(crate::text::multi_line(description)) } /// Removes repeats while keeping the first occurrence of each id. diff --git a/crates/iz-core/src/text.rs b/crates/iz-core/src/text.rs new file mode 100644 index 0000000..01d0613 --- /dev/null +++ b/crates/iz-core/src/text.rs @@ -0,0 +1,52 @@ +//! Cleaning of the text people type: titles, names and comments. Control characters are dropped, so a NUL +//! or a CR/LF never reaches a page, a mail subject or a header. Bidi controls and invisible formatting +//! characters are dropped too, because they change how text is shown: U+202E makes `invoice\u{202E}fdp.exe` +//! read as `invoice exe.pdf`. + +/// Whether `c` is an invisible formatting character or a bidi control. The set is the one of `in`'s +/// display-name rules (`in-core` `names.rs`); copied, not shared. U+200D stays: emoji sequences need it, +/// and it cannot reorder text. +pub fn is_invisible_or_bidi(c: char) -> bool { + matches!( + c, + '\u{200B}'..='\u{200C}' + | '\u{200E}'..='\u{200F}' + | '\u{202A}'..='\u{202E}' + | '\u{2060}'..='\u{2064}' + | '\u{2066}'..='\u{2069}' + | '\u{FEFF}' + | '\u{2028}' + | '\u{2029}' + ) +} + +/// One line of text: every control character, bidi control and invisible formatting character removed, +/// then trimmed. +pub fn single_line(raw: &str) -> String { + raw.chars() + .filter(|&c| !c.is_control() && !is_invisible_or_bidi(c)) + .collect::() + .trim() + .to_owned() +} + +/// Several lines of text: like [`single_line`], but line feeds and tabs stay. A CR is dropped, so CRLF +/// line ends become LF. +pub fn multi_line(raw: &str) -> String { + raw.chars() + .filter(|&c| (c == '\n' || c == '\t' || !c.is_control()) && !is_invisible_or_bidi(c)) + .collect::() + .trim() + .to_owned() +} + +#[cfg(test)] +mod tests { + use super::{multi_line, single_line}; + + #[test] + fn controls_and_bidi_are_dropped_and_lines_kept_only_where_asked() { + assert_eq!(single_line(" a\u{0}b\r\nBcc: x\u{202E} "), "abBcc: x"); + assert_eq!(multi_line("one\r\ntwo\t\u{0}\u{202E}"), "one\ntwo"); + } +}