im
Clone
https://git.tahinli.com/Tahinli/im.git
0 watching0 forksRustAGPL-3.0542.8 KiBb9f0726Tahinli27 days ago34 commits
| 1 | //! Email addresses: normalisation and the shape check. |
| 2 | //! |
| 3 | //! An address has one stored form: trimmed and lowercased. The same string is the `email` and the |
| 4 | //! `email_folded` of a user, so lookups are case-insensitive by construction. The shape check |
| 5 | //! accepts ASCII only. Look-alike characters from other scripts would otherwise let a second |
| 6 | //! account look exactly like the first one. |
| 7 | //! |
| 8 | //! The check is a sanity check on the text, not proof that the address exists or receives mail. |
| 9 | //! |
| 10 | //! This module also checks display names ([`parse_display_name`]). A name is shown in the topbar, |
| 11 | //! in mail and in other apps, so control characters and bidirectional overrides are refused: they |
| 12 | //! can make one name read as another. |
| 13 | |
| 14 | use Deserialize; |
| 15 | |
| 16 | use crate; |
| 17 | |
| 18 | /// Longest display name, in characters. The schema has the same limit. |
| 19 | pub const MAX_NAME_CHARS: usize = 100; |
| 20 | |
| 21 | /// Longest address accepted, in bytes (RFC 5321 path limit). |
| 22 | pub const MAX_EMAIL_LEN: usize = 254; |
| 23 | |
| 24 | /// Longest local part (the part before `@`), in bytes (RFC 5321). |
| 25 | pub const MAX_LOCAL_LEN: usize = 64; |
| 26 | |
| 27 | /// Longest domain label, in bytes (RFC 1035). |
| 28 | const MAX_LABEL_LEN: usize = 63; |
| 29 | |
| 30 | /// The stored form of an address: surrounding ASCII whitespace removed, then ASCII lowercased. |
| 31 | /// |
| 32 | /// Only ASCII is touched. Unicode trimming or case folding would turn look-alikes (a no-break space, |
| 33 | /// the Kelvin sign) into an existing address; left alone, they fail the ASCII shape check instead. |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | /// Normalises `input` and checks its shape. Returns the stored form. |
| 39 | /// |
| 40 | /// Rules: at most 254 bytes; exactly one `@`; a local part of 1 to 64 characters from the RFC 5322 |
| 41 | /// `atext` set (no leading, trailing or doubled dot); a domain of dot-separated labels, at least |
| 42 | /// one dot, each label 1 to 63 letters, digits or hyphens without a leading or trailing hyphen. |
| 43 | /// Anything else, including whitespace and control characters, is refused. |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | /// Checks a display name and returns its stored form. |
| 51 | /// |
| 52 | /// Rules: surrounding whitespace is removed; what is left has 1 to [`MAX_NAME_CHARS`] characters; |
| 53 | /// no character is a control character (this includes NUL, tab and line breaks); and no character |
| 54 | /// is a bidirectional override or isolate (U+202A to U+202E, U+2066 to U+2069). Those last ones |
| 55 | /// let a name display differently from the order its characters are stored in. |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | /// Removes the control characters from a single-line field: every C0 control (U+0000 to U+001F, so NUL, |
| 72 | /// tab and line breaks too) and DEL (U+007F). Applied where form values are read, so a typed control |
| 73 | /// character is never used or echoed back into a page. |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | /// Removes the control characters from a multi-line field, keeping line feed and tab. A carriage return |
| 79 | /// is removed, so a browser's CRLF becomes LF. |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | /// Serde adapter for a single-line form field: see [`strip_line_controls`]. |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | /// Serde adapter for a multi-line form field: see [`strip_text_controls`]. |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | /// The bidirectional embedding, override and isolate controls that [`parse_display_name`] refuses. |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | /// The shape rules of [`parse_email`], for text that is already normalised. |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | /// RFC 5322 `atext` restricted to ASCII. The dot is handled by the caller. |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 |