Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

dbbasic-object-server/object_rate_limit.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
object_rate_limit.py · 5.7 KB · 166 lines
1"""Filesystem-backed rate limiting for DBBASIC Object Server."""
2
3from __future__ import annotations
4
5import fcntl
6import hashlib
7import math
8import os
9import threading
10import time
11from dataclasses import dataclass
12from pathlib import Path
13
14
15@dataclass(frozen=True)
16class RateLimitResult:
17 """Result of one rate-limit check.
18
19 `degraded` is set when the limiter could not read/write its storage (an
20 OSError on the lock, bucket, or write path). The `allowed` value on a
21 degraded result reflects the caller's chosen failure posture (see
22 `check_rate_limit`'s `fail_closed`): the count is unknown, so the caller
23 decided whether "unknown" means allow or deny. Callers should log a
24 degraded result -- a silently degraded limiter is an invisible security
25 hole.
26 """
27
28 allowed: bool
29 retry_after: int
30 limit: int
31 window_seconds: int
32 remaining: int
33 degraded: bool = False
34
35
36def check_rate_limit(
37 *,
38 directory: str | Path,
39 identity: str,
40 limit: int,
41 window_seconds: int,
42 now: float | None = None,
43 fail_closed: bool = False,
44) -> RateLimitResult:
45 """Check and record a request for one identity.
46
47 `limit <= 0` disables the limiter. The storage format intentionally stays
48 simple: one timestamp per line under `data/ratelimit`.
49
50 Failure posture: if the limiter cannot read or write its storage (any
51 OSError -- permission denied, disk full, a broken directory), it returns a
52 `degraded` result whose `allowed` is set by `fail_closed`. A global
53 DoS-limiter on every request should stay `fail_closed=False` (a
54 rate-dir glitch must not take the whole site down) but MUST log the
55 degradation. A public-write surface (anonymous submissions, comments)
56 should pass `fail_closed=True`: when the abuse counter is unavailable,
57 denying the write is safer than admitting an unbounded flood. Either way
58 the failure is never silent -- it is surfaced via `degraded`, not
59 swallowed into a plain allow.
60 """
61
62 window_seconds = max(int(window_seconds), 1)
63 if limit <= 0:
64 return RateLimitResult(
65 allowed=True,
66 retry_after=0,
67 limit=limit,
68 window_seconds=window_seconds,
69 remaining=0,
70 )
71
72 now = time.time() if now is None else now
73
74 try:
75 rate_dir = Path(directory)
76 rate_dir.mkdir(parents=True, exist_ok=True)
77
78 key = hashlib.sha256(identity.encode("utf-8")).hexdigest()[:32]
79 bucket_path = rate_dir / f"{key}.txt"
80 lock_path = rate_dir / f"{key}.lock"
81
82 with open(lock_path, "a+", encoding="utf-8") as lock_file:
83 fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX)
84 try:
85 timestamps = _read_recent_timestamps(
86 bucket_path, cutoff=now - window_seconds
87 )
88 if len(timestamps) >= limit:
89 retry_after = _retry_after(
90 timestamps, now=now, window_seconds=window_seconds
91 )
92 _write_timestamps(bucket_path, timestamps[-limit:])
93 return RateLimitResult(
94 allowed=False,
95 retry_after=retry_after,
96 limit=limit,
97 window_seconds=window_seconds,
98 remaining=0,
99 )
100
101 timestamps.append(now)
102 timestamps = timestamps[-limit:]
103 _write_timestamps(bucket_path, timestamps)
104 return RateLimitResult(
105 allowed=True,
106 retry_after=0,
107 limit=limit,
108 window_seconds=window_seconds,
109 remaining=max(limit - len(timestamps), 0),
110 )
111 finally:
112 fcntl.flock(lock_file.fileno(), fcntl.LOCK_UN)
113 except OSError:
114 # Storage unavailable: the count is unknown. Never silently allow --
115 # return a degraded result and let the caller's posture decide, so a
116 # public-write surface (fail_closed=True) denies rather than admitting
117 # an unmetered flood, while a global limiter (fail_closed=False) stays
118 # up but flags the degradation for logging.
119 return RateLimitResult(
120 allowed=not fail_closed,
121 retry_after=window_seconds,
122 limit=limit,
123 window_seconds=window_seconds,
124 remaining=0,
125 degraded=True,
126 )
127
128
129def _read_recent_timestamps(path: Path, *, cutoff: float) -> list[float]:
130 # A missing bucket is normal (first request for this identity) -> empty.
131 # A genuine read error (OSError) is NOT swallowed here: it propagates to
132 # check_rate_limit's degraded handler so the failure posture (fail_closed)
133 # decides, instead of masquerading as "no recent requests" (a silent
134 # fail-open).
135 if not path.exists():
136 return []
137
138 timestamps: list[float] = []
139 lines = path.read_text(encoding="utf-8").splitlines()
140
141 for line in lines:
142 if not line:
143 continue
144 try:
145 timestamp = float(line)
146 except ValueError:
147 continue
148 if timestamp > cutoff:
149 timestamps.append(timestamp)
150
151 return timestamps
152
153
154def _write_timestamps(path: Path, timestamps: list[float]) -> None:
155 tmp_path = path.with_name(f"{path.name}.{os.getpid()}.{threading.get_ident()}.tmp")
156 tmp_path.write_text("\n".join(str(timestamp) for timestamp in timestamps), encoding="utf-8")
157 os.replace(tmp_path, path)
158
159
160def _retry_after(timestamps: list[float], *, now: float, window_seconds: int) -> int:
161 if not timestamps:
162 return window_seconds
163
164 oldest = min(timestamps)
165 retry_after = window_seconds - (now - oldest)
166 return max(math.ceil(retry_after), 1)