in
Clone
https://git.tahinli.com/Tahinli/in.git
0 watching0 forksRustAGPL-3.0528.7 KiB9215377Tahinli27 days ago37 commits
| 1 | //! Failure throttling in memory: a sliding window of failures per key, with a bounded number of keys. |
| 2 | //! |
| 3 | //! An attempt first asks [`Throttle::reserve`], which counts it as a failure and answers `429` with |
| 4 | //! `Retry-After` when the key has reached its limit. A success gives the count back with |
| 5 | //! [`Throttle::refund`]. Check and count are one step, so parallel attempts cannot pass together. |
| 6 | //! Nothing is persisted, so a restart forgets the failures; that is accepted for a family instance and documented in the backlog. |
| 7 | //! |
| 8 | //! The key count is capped. When a new key would exceed the cap, the windows that have expired are |
| 9 | //! dropped first, and then the key whose last failure is oldest. So an attacker who spreads guesses over |
| 10 | //! many keys can only evict other keys, never grow the memory. |
| 11 | |
| 12 | use HashMap; |
| 13 | use ; |
| 14 | use ; |
| 15 | |
| 16 | /// Length of the sliding window for share-link passwords: 15 minutes. |
| 17 | pub const PASSWORD_WINDOW: Duration = from_secs; |
| 18 | |
| 19 | /// Failed password attempts allowed against one link in a window, from all visitors together. This is a |
| 20 | /// ceiling: it stops guesses spread over many addresses. The 101st is refused with `429`. |
| 21 | pub const LINK_PASSWORD_LIMIT: usize = 100; |
| 22 | |
| 23 | /// Failed password attempts allowed from one client address against one link in a window. The eleventh is |
| 24 | /// refused with `429`. Other visitors of the same link have their own count. |
| 25 | pub const VISITOR_PASSWORD_LIMIT: usize = 10; |
| 26 | |
| 27 | /// Most keys held at once. Each key is a short string and a short list of instants. |
| 28 | pub const MAX_KEYS: usize = 10_000; |
| 29 | |
| 30 | /// Failures per key within a sliding window, with a cap on the number of keys. |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | /// The two throttles of share-link passwords: one per link (by token hash), and one per link and client |
| 122 | /// address (the key is the token hash, a colon, and the address). |
| 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 |