🛡️ text: titles, names, rules and comments drop control, bidi and invisible characters
Browse files at this commitf40bc54a38090d4ba8dc4fe6d99c84479abf9174Parent: d4234627 changed files +67 -29
- crates/iz-core/src/lib.rs +1 -0
- crates/iz-core/src/store/attachments.rs +4 -19
- crates/iz-core/src/store/board.rs +4 -4
- crates/iz-core/src/store/comments.rs +2 -2
- crates/iz-core/src/store/rules.rs +1 -1
- crates/iz-core/src/store/tasks.rs +3 -3
- crates/iz-core/src/text.rs +52 -0
crates/iz-core/src/lib.rsmodified+1 -0
| @@ -21,6 +21,7 @@ pub mod slides; | |||
| 21 | pub mod sniff; | 21 | pub mod sniff; |
| 22 | pub mod staging; | 22 | pub mod staging; |
| 23 | pub mod store; | 23 | pub mod store; |
| 24 | +pub mod text; | ||
| 24 | pub mod time; | 25 | pub mod time; |
| 25 | pub mod token; | 26 | pub mod token; |
| 26 | pub mod zones; | 27 | pub mod zones; |
crates/iz-core/src/store/attachments.rsmodified+4 -19
| @@ -169,7 +169,10 @@ pub fn clean_name(raw: &str) -> Result<String> { | |||
| 169 | let last = raw.rsplit(['/', '\\']).next().unwrap_or(""); | 169 | let last = raw.rsplit(['/', '\\']).next().unwrap_or(""); |
| 170 | // Stripped before the checks: a name that is only invisible characters is empty, and an extension hidden by | 170 | // Stripped before the checks: a name that is only invisible characters is empty, and an extension hidden by |
| 171 | // a bidi override is the extension the name really has. | 171 | // a bidi override is the extension the name really has. |
| 172 | - let stripped: String = last.chars().filter(|c| !is_invisible_or_bidi(*c)).collect(); | 172 | + let stripped: String = last |
| 173 | + .chars() | ||
| 174 | + .filter(|c| !crate::text::is_invisible_or_bidi(*c)) | ||
| 175 | + .collect(); | ||
| 173 | let name = stripped.trim(); | 176 | let name = stripped.trim(); |
| 174 | if name.is_empty() || name == "." || name == ".." { | 177 | if name.is_empty() || name == "." || name == ".." { |
| 175 | return Err(Error::Invalid("name_empty".into())); | 178 | return Err(Error::Invalid("name_empty".into())); |
| @@ -183,24 +186,6 @@ pub fn clean_name(raw: &str) -> Result<String> { | |||
| 183 | Ok(name.to_owned()) | 186 | Ok(name.to_owned()) |
| 184 | } | 187 | } |
| 185 | 188 | ||
| 186 | -/// Whether `c` is an invisible formatting character or a bidi control, which changes how a name is shown: | ||
| 187 | -/// U+202E makes `invoice\u{202E}fdp.exe` read as `invoice exe.pdf`. Upload names drop these characters. | ||
| 188 | -/// The set is the one of `in`'s display-name rules (`in-core` `names.rs`); copied, not shared. U+200D stays: | ||
| 189 | -/// emoji sequences need it, and it cannot reorder text. | ||
| 190 | -fn is_invisible_or_bidi(c: char) -> bool { | ||
| 191 | - matches!( | ||
| 192 | - c, | ||
| 193 | - '\u{200B}'..='\u{200C}' | ||
| 194 | - | '\u{200E}'..='\u{200F}' | ||
| 195 | - | '\u{202A}'..='\u{202E}' | ||
| 196 | - | '\u{2060}'..='\u{2064}' | ||
| 197 | - | '\u{2066}'..='\u{2069}' | ||
| 198 | - | '\u{FEFF}' | ||
| 199 | - | '\u{2028}' | ||
| 200 | - | '\u{2029}' | ||
| 201 | - ) | ||
| 202 | -} | ||
| 203 | - | ||
| 204 | /// The extension of a clean name, lowercase, without the dot. `None` when there is none or it is not a | 189 | /// The extension of a clean name, lowercase, without the dot. `None` when there is none or it is not a |
| 205 | /// plain alphanumeric word. | 190 | /// plain alphanumeric word. |
| 206 | pub fn extension_of(name: &str) -> Option<String> { | 191 | pub fn extension_of(name: &str) -> Option<String> { |
crates/iz-core/src/store/board.rsmodified+4 -4
| @@ -139,24 +139,24 @@ pub fn valid_prefix(prefix: &str) -> bool { | |||
| 139 | 139 | ||
| 140 | /// Trims and checks a column name. | 140 | /// Trims and checks a column name. |
| 141 | pub fn clean_column_name(name: &str) -> Result<String> { | 141 | pub fn clean_column_name(name: &str) -> Result<String> { |
| 142 | - let name = name.trim(); | 142 | + let name = crate::text::single_line(name); |
| 143 | if name.is_empty() || name.chars().count() > COLUMN_NAME_MAX { | 143 | if name.is_empty() || name.chars().count() > COLUMN_NAME_MAX { |
| 144 | return Err(Error::Invalid(format!( | 144 | return Err(Error::Invalid(format!( |
| 145 | "column name must be 1 to {COLUMN_NAME_MAX} characters" | 145 | "column name must be 1 to {COLUMN_NAME_MAX} characters" |
| 146 | ))); | 146 | ))); |
| 147 | } | 147 | } |
| 148 | - Ok(name.to_owned()) | 148 | + Ok(name) |
| 149 | } | 149 | } |
| 150 | 150 | ||
| 151 | /// Trims and checks a tag name. | 151 | /// Trims and checks a tag name. |
| 152 | pub fn clean_tag_name(name: &str) -> Result<String> { | 152 | pub fn clean_tag_name(name: &str) -> Result<String> { |
| 153 | - let name = name.trim(); | 153 | + let name = crate::text::single_line(name); |
| 154 | if name.is_empty() || name.chars().count() > TAG_NAME_MAX { | 154 | if name.is_empty() || name.chars().count() > TAG_NAME_MAX { |
| 155 | return Err(Error::Invalid(format!( | 155 | return Err(Error::Invalid(format!( |
| 156 | "project name must be 1 to {TAG_NAME_MAX} characters" | 156 | "project name must be 1 to {TAG_NAME_MAX} characters" |
| 157 | ))); | 157 | ))); |
| 158 | } | 158 | } |
| 159 | - Ok(name.to_owned()) | 159 | + Ok(name) |
| 160 | } | 160 | } |
| 161 | 161 | ||
| 162 | /// Live columns of a board in display order. A query helper shared with the task module. | 162 | /// Live columns of a board in display order. A query helper shared with the task module. |
crates/iz-core/src/store/comments.rsmodified+2 -2
| @@ -48,13 +48,13 @@ fn map_comment(row: &turso::Row) -> Result<Comment> { | |||
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | fn clean_body(body: &str) -> Result<String> { | 50 | fn clean_body(body: &str) -> Result<String> { |
| 51 | - let body = body.trim(); | 51 | + let body = crate::text::multi_line(body); |
| 52 | if body.is_empty() || body.chars().count() > COMMENT_MAX { | 52 | if body.is_empty() || body.chars().count() > COMMENT_MAX { |
| 53 | return Err(Error::Invalid(format!( | 53 | return Err(Error::Invalid(format!( |
| 54 | "comment must be 1 to {COMMENT_MAX} characters" | 54 | "comment must be 1 to {COMMENT_MAX} characters" |
| 55 | ))); | 55 | ))); |
| 56 | } | 56 | } |
| 57 | - Ok(body.to_owned()) | 57 | + Ok(body) |
| 58 | } | 58 | } |
| 59 | 59 | ||
| 60 | /// The member id behind an actor that has already passed `authorize`. | 60 | /// The member id behind an actor that has already passed `authorize`. |
crates/iz-core/src/store/rules.rsmodified+1 -1
| @@ -86,7 +86,7 @@ fn map_rule(row: &turso::Row) -> Result<MailRule> { | |||
| 86 | 86 | ||
| 87 | /// Checks the input and returns the cleaned copy. Every refusal is a message for the admin. | 87 | /// Checks the input and returns the cleaned copy. Every refusal is a message for the admin. |
| 88 | async fn checked(conn: &Connection, board_id: &str, input: &RuleInput) -> Result<RuleInput> { | 88 | async fn checked(conn: &Connection, board_id: &str, input: &RuleInput) -> Result<RuleInput> { |
| 89 | - let name = input.name.trim().to_owned(); | 89 | + let name = crate::text::single_line(&input.name); |
| 90 | if name.is_empty() || name.chars().count() > RULE_NAME_MAX { | 90 | if name.is_empty() || name.chars().count() > RULE_NAME_MAX { |
| 91 | return Err(Error::Invalid(format!( | 91 | return Err(Error::Invalid(format!( |
| 92 | "the name must be 1 to {RULE_NAME_MAX} characters" | 92 | "the name must be 1 to {RULE_NAME_MAX} characters" |
crates/iz-core/src/store/tasks.rsmodified+3 -3
| @@ -312,13 +312,13 @@ fn map_assignee(row: &turso::Row) -> Result<(String, Assignee)> { | |||
| 312 | } | 312 | } |
| 313 | 313 | ||
| 314 | fn clean_title(title: &str) -> Result<String> { | 314 | fn clean_title(title: &str) -> Result<String> { |
| 315 | - let title = title.trim(); | 315 | + let title = crate::text::single_line(title); |
| 316 | if title.is_empty() || title.chars().count() > TITLE_MAX { | 316 | if title.is_empty() || title.chars().count() > TITLE_MAX { |
| 317 | return Err(Error::Invalid(format!( | 317 | return Err(Error::Invalid(format!( |
| 318 | "title must be 1 to {TITLE_MAX} characters" | 318 | "title must be 1 to {TITLE_MAX} characters" |
| 319 | ))); | 319 | ))); |
| 320 | } | 320 | } |
| 321 | - Ok(title.to_owned()) | 321 | + Ok(title) |
| 322 | } | 322 | } |
| 323 | 323 | ||
| 324 | fn clean_description(description: &str) -> Result<String> { | 324 | fn clean_description(description: &str) -> Result<String> { |
| @@ -327,7 +327,7 @@ fn clean_description(description: &str) -> Result<String> { | |||
| 327 | "description must be at most {DESCRIPTION_MAX} characters" | 327 | "description must be at most {DESCRIPTION_MAX} characters" |
| 328 | ))); | 328 | ))); |
| 329 | } | 329 | } |
| 330 | - Ok(description.to_owned()) | 330 | + Ok(crate::text::multi_line(description)) |
| 331 | } | 331 | } |
| 332 | 332 | ||
| 333 | /// Removes repeats while keeping the first occurrence of each id. | 333 | /// Removes repeats while keeping the first occurrence of each id. |
crates/iz-core/src/text.rsadded+52 -0
| @@ -0,0 +1,52 @@ | |||
| 1 | +//! Cleaning of the text people type: titles, names and comments. Control characters are dropped, so a NUL | ||
| 2 | +//! or a CR/LF never reaches a page, a mail subject or a header. Bidi controls and invisible formatting | ||
| 3 | +//! characters are dropped too, because they change how text is shown: U+202E makes `invoice\u{202E}fdp.exe` | ||
| 4 | +//! read as `invoice exe.pdf`. | ||
| 5 | + | ||
| 6 | +/// Whether `c` is an invisible formatting character or a bidi control. The set is the one of `in`'s | ||
| 7 | +/// display-name rules (`in-core` `names.rs`); copied, not shared. U+200D stays: emoji sequences need it, | ||
| 8 | +/// and it cannot reorder text. | ||
| 9 | +pub fn is_invisible_or_bidi(c: char) -> bool { | ||
| 10 | + matches!( | ||
| 11 | + c, | ||
| 12 | + '\u{200B}'..='\u{200C}' | ||
| 13 | + | '\u{200E}'..='\u{200F}' | ||
| 14 | + | '\u{202A}'..='\u{202E}' | ||
| 15 | + | '\u{2060}'..='\u{2064}' | ||
| 16 | + | '\u{2066}'..='\u{2069}' | ||
| 17 | + | '\u{FEFF}' | ||
| 18 | + | '\u{2028}' | ||
| 19 | + | '\u{2029}' | ||
| 20 | + ) | ||
| 21 | +} | ||
| 22 | + | ||
| 23 | +/// One line of text: every control character, bidi control and invisible formatting character removed, | ||
| 24 | +/// then trimmed. | ||
| 25 | +pub fn single_line(raw: &str) -> String { | ||
| 26 | + raw.chars() | ||
| 27 | + .filter(|&c| !c.is_control() && !is_invisible_or_bidi(c)) | ||
| 28 | + .collect::<String>() | ||
| 29 | + .trim() | ||
| 30 | + .to_owned() | ||
| 31 | +} | ||
| 32 | + | ||
| 33 | +/// Several lines of text: like [`single_line`], but line feeds and tabs stay. A CR is dropped, so CRLF | ||
| 34 | +/// line ends become LF. | ||
| 35 | +pub fn multi_line(raw: &str) -> String { | ||
| 36 | + raw.chars() | ||
| 37 | + .filter(|&c| (c == '\n' || c == '\t' || !c.is_control()) && !is_invisible_or_bidi(c)) | ||
| 38 | + .collect::<String>() | ||
| 39 | + .trim() | ||
| 40 | + .to_owned() | ||
| 41 | +} | ||
| 42 | + | ||
| 43 | +#[cfg(test)] | ||
| 44 | +mod tests { | ||
| 45 | + use super::{multi_line, single_line}; | ||
| 46 | + | ||
| 47 | + #[test] | ||
| 48 | + fn controls_and_bidi_are_dropped_and_lines_kept_only_where_asked() { | ||
| 49 | + assert_eq!(single_line(" a\u{0}b\r\nBcc: x\u{202E} "), "abBcc: x"); | ||
| 50 | + assert_eq!(multi_line("one\r\ntwo\t\u{0}\u{202E}"), "one\ntwo"); | ||
| 51 | + } | ||
| 52 | +} | ||