|
| 1 | +""" |
| 2 | +Rate limiting in this implementation relies on a cache and uses non-atomic |
| 3 | +operations, making it vulnerable to race conditions. As a result, users may |
| 4 | +occasionally bypass the intended rate limit due to concurrent access. However, |
| 5 | +such race conditions are rare in practice. For example, if the limit is set to |
| 6 | +10 requests per minute and a large number of parallel processes attempt to test |
| 7 | +that limit, you may occasionally observe slight overruns—such as 11 or 12 |
| 8 | +requests slipping through. Nevertheless, exceeding the limit by a large margin |
| 9 | +is highly unlikely due to the low probability of many processes entering the |
| 10 | +critical non-atomic code section simultaneously. |
| 11 | +""" |
| 12 | + |
| 13 | +import hashlib |
| 14 | +import time |
| 15 | +from collections import namedtuple |
| 16 | +from dataclasses import dataclass |
| 17 | +from typing import Dict, List, Optional, Tuple, Union |
| 18 | + |
| 19 | +from django.core.cache import cache |
| 20 | +from django.core.exceptions import ImproperlyConfigured |
| 21 | +from django.http import HttpRequest, HttpResponse |
| 22 | +from django.shortcuts import render |
| 23 | + |
| 24 | +from allauth.core.exceptions import RateLimited |
| 25 | + |
| 26 | + |
| 27 | +Rate = namedtuple("Rate", "amount duration per") |
| 28 | + |
| 29 | + |
| 30 | +@dataclass |
| 31 | +class SingleRateLimitUsage: |
| 32 | + cache_key: str |
| 33 | + cache_duration: Union[float, int] |
| 34 | + timestamp: float |
| 35 | + |
| 36 | + def rollback(self) -> None: |
| 37 | + history = cache.get(self.cache_key, []) |
| 38 | + history = [ts for ts in history if ts != self.timestamp] |
| 39 | + cache.set(self.cache_key, history, self.cache_duration) |
| 40 | + |
| 41 | + |
| 42 | +@dataclass |
| 43 | +class RateLimitUsage: |
| 44 | + usage: List[SingleRateLimitUsage] |
| 45 | + |
| 46 | + def rollback(self) -> None: |
| 47 | + for usage in self.usage: |
| 48 | + usage.rollback() |
| 49 | + |
| 50 | + |
| 51 | +def parse_duration(duration) -> Union[int, float]: |
| 52 | + if len(duration) == 0: |
| 53 | + raise ValueError(duration) |
| 54 | + unit = duration[-1] |
| 55 | + value = duration[0:-1] |
| 56 | + unit_map = {"s": 1, "m": 60, "h": 3600, "d": 86400} |
| 57 | + if unit not in unit_map: |
| 58 | + raise ValueError("Invalid duration unit: %s" % unit) |
| 59 | + if len(value) == 0: |
| 60 | + value = 1 |
| 61 | + else: |
| 62 | + value = float(value) |
| 63 | + return value * unit_map[unit] |
| 64 | + |
| 65 | + |
| 66 | +def parse_rate(rate: str) -> Rate: |
| 67 | + parts = rate.split("/") |
| 68 | + if len(parts) == 2: |
| 69 | + amount, duration = parts |
| 70 | + per = "ip" |
| 71 | + elif len(parts) == 3: |
| 72 | + amount, duration, per = parts |
| 73 | + else: |
| 74 | + raise ValueError(rate) |
| 75 | + amount_v = int(amount) |
| 76 | + duration_v = parse_duration(duration) |
| 77 | + return Rate(amount_v, duration_v, per) |
| 78 | + |
| 79 | + |
| 80 | +def parse_rates(rates: Optional[str]) -> List[Rate]: |
| 81 | + ret = [] |
| 82 | + if rates: |
| 83 | + rates = rates.strip() |
| 84 | + if rates: |
| 85 | + parts = rates.split(",") |
| 86 | + for part in parts: |
| 87 | + ret.append(parse_rate(part.strip())) |
| 88 | + return ret |
| 89 | + |
| 90 | + |
| 91 | +def get_cache_key(request, *, action: str, rate: Rate, key=None, user=None): |
| 92 | + from allauth.account.adapter import get_adapter |
| 93 | + |
| 94 | + source: Tuple[str, ...] |
| 95 | + if rate.per == "ip": |
| 96 | + source = ("ip", get_adapter().get_client_ip(request)) |
| 97 | + elif rate.per == "user": |
| 98 | + if user is None: |
| 99 | + if not request.user.is_authenticated: |
| 100 | + raise ImproperlyConfigured( |
| 101 | + "ratelimit configured per user but used anonymously" |
| 102 | + ) |
| 103 | + user = request.user |
| 104 | + source = ("user", str(user.pk)) |
| 105 | + elif rate.per == "key": |
| 106 | + if key is None: |
| 107 | + raise ImproperlyConfigured( |
| 108 | + "ratelimit configured per key but no key specified" |
| 109 | + ) |
| 110 | + key_hash = hashlib.sha256(key.encode("utf8")).hexdigest() |
| 111 | + source = (key_hash,) |
| 112 | + else: |
| 113 | + raise ValueError(rate.per) |
| 114 | + keys = ["allauth", "rl", action, *source] |
| 115 | + return ":".join(keys) |
| 116 | + |
| 117 | + |
| 118 | +def _consume_single_rate( |
| 119 | + request, |
| 120 | + *, |
| 121 | + action: str, |
| 122 | + rate: Rate, |
| 123 | + key=None, |
| 124 | + user=None, |
| 125 | + dry_run: bool = False, |
| 126 | + raise_exception: bool = False |
| 127 | +) -> Optional[SingleRateLimitUsage]: |
| 128 | + cache_key = get_cache_key(request, action=action, rate=rate, key=key, user=user) |
| 129 | + history = cache.get(cache_key, []) |
| 130 | + now = time.time() |
| 131 | + while history and history[-1] <= now - rate.duration: |
| 132 | + history.pop() |
| 133 | + allowed = len(history) < rate.amount |
| 134 | + if allowed: |
| 135 | + usage = SingleRateLimitUsage( |
| 136 | + cache_key=cache_key, timestamp=now, cache_duration=rate.duration |
| 137 | + ) |
| 138 | + else: |
| 139 | + usage = None |
| 140 | + if allowed and not dry_run: |
| 141 | + history.insert(0, now) |
| 142 | + cache.set(cache_key, history, rate.duration) |
| 143 | + if not allowed and raise_exception: |
| 144 | + raise RateLimited |
| 145 | + return usage |
| 146 | + |
| 147 | + |
| 148 | +def consume( |
| 149 | + request: HttpRequest, |
| 150 | + *, |
| 151 | + action: str, |
| 152 | + config: Dict[str, str], |
| 153 | + key=None, |
| 154 | + user=None, |
| 155 | + dry_run: bool = False, |
| 156 | + raise_exception: bool = False |
| 157 | +) -> Optional[RateLimitUsage]: |
| 158 | + usage = RateLimitUsage(usage=[]) |
| 159 | + if request.method == "GET": |
| 160 | + return usage |
| 161 | + rates = parse_rates(config.get(action)) |
| 162 | + if not rates: |
| 163 | + return usage |
| 164 | + allowed = True |
| 165 | + for rate in rates: |
| 166 | + single_usage = _consume_single_rate( |
| 167 | + request, |
| 168 | + action=action, |
| 169 | + rate=rate, |
| 170 | + key=key, |
| 171 | + user=user, |
| 172 | + dry_run=dry_run, |
| 173 | + raise_exception=raise_exception, |
| 174 | + ) |
| 175 | + if not single_usage: |
| 176 | + allowed = False |
| 177 | + break |
| 178 | + usage.usage.append(single_usage) |
| 179 | + return usage if allowed else None |
| 180 | + |
| 181 | + |
| 182 | +def handler429(request) -> HttpResponse: |
| 183 | + from allauth.account import app_settings |
| 184 | + |
| 185 | + return render(request, "429." + app_settings.TEMPLATE_EXTENSION, status=429) |
| 186 | + |
| 187 | + |
| 188 | +def clear(request, *, config: dict, action: str, key=None, user=None): |
| 189 | + rates = parse_rates(config.get(action)) |
| 190 | + for rate in rates: |
| 191 | + cache_key = get_cache_key(request, action=action, rate=rate, key=key, user=user) |
| 192 | + cache.delete(cache_key) |
0 commit comments