im
Clone
https://git.tahinli.com/Tahinli/im.git
0 watching0 forksRustAGPL-3.0542.8 KiBb9f0726Tahinli27 days ago34 commits
| 1 | //! In-memory sliding-window failure counters that slow down password guessing. |
| 2 | //! |
| 3 | //! Each key keeps the instants of its recent failures, and is limited while it has `limit` of them |
| 4 | //! inside [`WINDOW`]. An attempt first asks [`Throttle::reserve`], which counts it before the work |
| 5 | //! is done and answers with the wait when the key is already limited. A success gives the count back |
| 6 | //! with [`Throttle::refund`] (or drops the key with [`Throttle::clear`] where the old failures go too). |
| 7 | //! Check and count are one step under one lock, so parallel attempts cannot all pass together. |
| 8 | //! |
| 9 | //! Memory is bounded. Expired instants and empty keys are pruned every few hundred attempts. At the key |
| 10 | //! cap, expired keys go first; then the key whose latest failure is oldest is evicted, but never a key |
| 11 | //! that is limited right now. When every key is limited, a new key is refused: fail closed. |
| 12 | //! |
| 13 | //! Email keys are capped too. An address that is too long or not plausible shares one fixed key, so a |
| 14 | //! flood of junk addresses cannot grow the map or the key size. |
| 15 | //! |
| 16 | //! Each [`Throttle`] has one window for all its keys. The failure counters use [`WINDOW`]; the per-minute |
| 17 | //! and per-hour counters of the OAuth endpoints and the account pages are separate instances, built with |
| 18 | //! [`Throttle::per_minute`] and [`Throttle::email_changes`]. |
| 19 | //! |
| 20 | //! The counters live in one process. A restart clears them, and two processes would each count |
| 21 | //! their own. `im` runs as one process, so this is the whole picture. |
| 22 | |
| 23 | use ; |
| 24 | use IpAddr; |
| 25 | use ; |
| 26 | use ; |
| 27 | |
| 28 | use parse_email; |
| 29 | |
| 30 | /// Length of the failure window. |
| 31 | pub const WINDOW: Duration = from_secs; |
| 32 | |
| 33 | /// Failed sign-ins allowed per normalised email address inside [`WINDOW`]. Also counts wrong |
| 34 | /// passwords on the security page, so guessing there locks sign-in too. |
| 35 | pub const LOGIN_USER_LIMIT: usize = 10; |
| 36 | |
| 37 | /// Failed sign-ins allowed per client address inside [`WINDOW`]. |
| 38 | pub const LOGIN_IP_LIMIT: usize = 30; |
| 39 | |
| 40 | /// Failed second-factor checks allowed per user inside [`WINDOW`]. |
| 41 | pub const TWO_FACTOR_USER_LIMIT: usize = 10; |
| 42 | |
| 43 | /// Failed second-factor checks allowed per user inside [`TWO_FACTOR_DAY_WINDOW`]. It works beside |
| 44 | /// [`TWO_FACTOR_USER_LIMIT`], so a guesser who pauses between windows still runs out. |
| 45 | pub const TWO_FACTOR_DAY_LIMIT: usize = 30; |
| 46 | |
| 47 | /// Length of the second-factor day window: 24 hours. Kept on its own [`Throttle`] (see |
| 48 | /// [`Throttle::two_factor_days`]), because a throttle has one window for all its keys. |
| 49 | pub const TWO_FACTOR_DAY_WINDOW: Duration = from_secs; |
| 50 | |
| 51 | /// Reset-link requests allowed per normalised email address inside [`WINDOW`]. Every request counts, known |
| 52 | /// address or not, so the limit leaks nothing about the account. |
| 53 | pub const FORGOT_EMAIL_LIMIT: usize = 3; |
| 54 | |
| 55 | /// Reset-link requests allowed per client address inside [`WINDOW`]. |
| 56 | pub const FORGOT_IP_LIMIT: usize = 20; |
| 57 | |
| 58 | /// Profile photo uploads allowed per account inside [`PHOTO_UPLOAD_WINDOW`]. Every upload counts, whether |
| 59 | /// or not its picture is accepted, because each one costs a decode. |
| 60 | pub const PHOTO_UPLOAD_LIMIT: usize = 10; |
| 61 | |
| 62 | /// Length of the photo upload window: one hour. Kept on its own [`Throttle`] (see |
| 63 | /// [`Throttle::photo_uploads`]), because a throttle has one window for all its keys. |
| 64 | pub const PHOTO_UPLOAD_WINDOW: Duration = from_secs; |
| 65 | |
| 66 | /// Length of the per-minute window of the OAuth endpoints and the audit of failed sign-ins. |
| 67 | pub const MINUTE: Duration = from_secs; |
| 68 | |
| 69 | /// Length of the window of email-change requests: one hour. |
| 70 | pub const EMAIL_CHANGE_WINDOW: Duration = from_secs; |
| 71 | |
| 72 | /// Email-change requests allowed per account inside [`EMAIL_CHANGE_WINDOW`]. Each one mails two addresses. |
| 73 | pub const EMAIL_CHANGE_LIMIT: usize = 3; |
| 74 | |
| 75 | /// Code issuances allowed per browser session inside [`MINUTE`] at `/authorize`. A refused one is answered |
| 76 | /// with the `temporarily_unavailable` redirect. |
| 77 | pub const AUTHORIZE_SESSION_LIMIT: usize = 60; |
| 78 | |
| 79 | /// Requests allowed per authenticated client inside [`MINUTE`] at `/token`, over all its grants together. |
| 80 | /// A client with many users shares this ceiling, so it is set well above one person's use. |
| 81 | pub const TOKEN_CLIENT_LIMIT: usize = 1200; |
| 82 | |
| 83 | /// Requests allowed inside [`MINUTE`] for one refresh-token family, or for one authorization code, at |
| 84 | /// `/token`. One person uses one chain or one code, so this is what limits a flood on a single one. |
| 85 | pub const TOKEN_CHAIN_LIMIT: usize = 30; |
| 86 | |
| 87 | /// `LoginFailed` audit rows written per typed address inside [`MINUTE`], whether or not an account has that |
| 88 | /// address, so the work done for a failure does not show which. Failures beyond it are still refused and |
| 89 | /// counted; only their audit rows are skipped. |
| 90 | pub const LOGIN_AUDIT_LIMIT: usize = 1; |
| 91 | |
| 92 | /// Maximum number of distinct keys tracked at once. A flood of random keys cannot grow memory past |
| 93 | /// this. |
| 94 | const MAX_KEYS: usize = 100_000; |
| 95 | |
| 96 | /// A full prune runs every this many attempts. |
| 97 | const PRUNE_EVERY: u32 = 256; |
| 98 | |
| 99 | /// The part of an email key used for an address that is not plausible. Starts with `<`, which no valid |
| 100 | /// address contains, so it never collides with a real address. |
| 101 | const INVALID_ADDRESS: &str = "<invalid>"; |
| 102 | |
| 103 | /// The most bytes of a raw, unparseable address that go into its key. Longer input is cut here, so the |
| 104 | /// key stays bounded. |
| 105 | const MAX_RAW_ADDRESS_BYTES: usize = 320; |
| 106 | |
| 107 | /// The failure counters of the process. Shared by every request through the app state. |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | /// The failures of one key, and the limit they were counted against. |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 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 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | /// Removes the failures that have left the window. |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | /// The wait until `stamps` drops below `limit` failures: the failure at this index is the last one to |
| 305 | /// leave the window before the count does. |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | /// The part of an email key for `email`: the address itself when it is plausible, otherwise the marker |
| 316 | /// and the raw text, trimmed, lowercased and cut to [`MAX_RAW_ADDRESS_BYTES`]. Each junk address gets its |
| 317 | /// own key, so one junk address cannot lock out a real account whose stored address happens to be |
| 318 | /// unparseable under the current rules. |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | /// Throttle key for a normalised email address. |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | /// Throttle key for the reset-link requests of one normalised address. |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | /// Throttle key for the photo uploads of one user, by user id. |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | /// Throttle key for the second factor of one user, by user id. |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | /// Throttle key for the email-change requests of one user, by user id. |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | /// Throttle key for the code issuances of one browser session, by session id. |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | /// Throttle key for the token requests of one client, by client id. |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | /// Throttle key for the refresh requests of one token family (one chain) of a client. |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | /// Throttle key for the code exchanges of one authorization code of a client, by the code's hash. |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | /// Throttle key for the `LoginFailed` audit rows of one normalised address. Its own key, not the sign-in |
| 380 | /// counter of the address, so the audit rows do not use up the sign-in attempts. |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | /// Throttle key for an address under a named scope, such as `login-ip`. |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 |