✨ feat: 整合 2.8.5 后端存储、安全与运行优化
* 禁用 OSS 非必需校验尾部,改用精确会话前缀逐对象清理 * 保护上传终态与分享事务,清理失败保留会话并支持安全重试 * 统一下载响应头,兼容旧下载器并防止文件名注入 * 限流改用单调时钟并限制 IP 记录,缓存模板与静态资源策略 * 统一 ORM 初始化与异常处理,新增查询索引及 SQLite 性能参数 * 固定 uvloop/httptools,统一 Docker keep-alive 与内存回收配置 * 同步版本与升级文档,补充 OSS、清理、缓存及数据库回归验证
This commit is contained in:
1 parent
b2a9aeddb3
commit
41519afb6a
29 files changed
+1033
-132
No files matched your search
@@ -0,0 +1,81 @@
|
||||
"""IPRateLimit is bounded and uses a monotonic clock."""
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from apps.base.dependencies import IPRateLimit
|
||||
|
||||
|
||||
class FakeClock:
|
||||
def __init__(self, now: float = 1000.0):
|
||||
self.now = now
|
||||
|
||||
def __call__(self) -> float:
|
||||
return self.now
|
||||
|
||||
|
||||
class IPRateLimitBoundsTests(unittest.TestCase):
|
||||
def test_first_add_returns_one(self):
|
||||
limiter = IPRateLimit(count=3, minutes=1)
|
||||
|
||||
self.assertEqual(limiter.add_ip("203.0.113.1"), 1)
|
||||
self.assertEqual(limiter.add_ip("203.0.113.1"), 2)
|
||||
|
||||
def test_capacity_evicts_least_recently_active_ip(self):
|
||||
limiter = IPRateLimit(count=10, minutes=1, max_ips=3)
|
||||
|
||||
for ip in ("a", "b", "c", "d"):
|
||||
limiter.add_ip(ip)
|
||||
|
||||
self.assertEqual(len(limiter.ips), 3)
|
||||
self.assertEqual(list(limiter.ips), ["b", "c", "d"])
|
||||
|
||||
def test_activity_refreshes_recency(self):
|
||||
limiter = IPRateLimit(count=10, minutes=1, max_ips=2)
|
||||
|
||||
limiter.add_ip("a")
|
||||
limiter.add_ip("b")
|
||||
limiter.add_ip("a")
|
||||
limiter.add_ip("c")
|
||||
|
||||
self.assertEqual(list(limiter.ips), ["a", "c"])
|
||||
|
||||
def test_window_blocks_then_resets(self):
|
||||
clock = FakeClock()
|
||||
with mock.patch("time.monotonic", clock):
|
||||
limiter = IPRateLimit(count=3, minutes=1)
|
||||
for _ in range(3):
|
||||
limiter.add_ip("198.51.100.9")
|
||||
|
||||
self.assertFalse(limiter.check_ip("198.51.100.9"))
|
||||
clock.now += 30
|
||||
self.assertFalse(limiter.check_ip("198.51.100.9"))
|
||||
clock.now += 31
|
||||
self.assertTrue(limiter.check_ip("198.51.100.9"))
|
||||
self.assertNotIn("198.51.100.9", limiter.ips)
|
||||
|
||||
def test_remove_expired_compacts_old_entries_only(self):
|
||||
clock = FakeClock()
|
||||
with mock.patch("time.monotonic", clock):
|
||||
limiter = IPRateLimit(count=10, minutes=1)
|
||||
limiter.add_ip("old")
|
||||
clock.now += 30
|
||||
limiter.add_ip("fresh")
|
||||
clock.now += 31
|
||||
|
||||
import asyncio
|
||||
|
||||
asyncio.run(limiter.remove_expired_ip())
|
||||
|
||||
self.assertEqual(list(limiter.ips), ["fresh"])
|
||||
|
||||
def test_record_count_is_bounded_under_ip_scan(self):
|
||||
limiter = IPRateLimit(count=10, minutes=1, max_ips=10_000)
|
||||
|
||||
for i in range(50_000):
|
||||
limiter.add_ip(f"10.0.{i // 256}.{i % 256}")
|
||||
|
||||
self.assertEqual(len(limiter.ips), 10_000)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user