Make entry autoban opt-in, bound PPTX unpacking, unify skin DB path, prune deploy examples
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
"""Shared IP is a signal, not a sanction: autoban is opt-in (IISHKO_ENTRY_AUTOBAN=1)."""
|
||||
from contextlib import closing
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sqlite3
|
||||
import sys
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "bot"))
|
||||
|
||||
import entry_autoban # noqa: E402
|
||||
import entry_telemetry # noqa: E402
|
||||
|
||||
NAT_IP = "203.0.113.7" # public_ip is stubbed: TEST-NET would be filtered
|
||||
|
||||
|
||||
class NatUsersTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = tempfile.TemporaryDirectory()
|
||||
root = Path(self.tmp.name)
|
||||
self.bot_db, self.gw_db = root / "bot.db", root / "one-api.db"
|
||||
with closing(sqlite3.connect(self.bot_db)) as con:
|
||||
con.execute("CREATE TABLE users(tg_id INTEGER PRIMARY KEY, username TEXT, token_name TEXT,"
|
||||
" admin INTEGER DEFAULT 0, banned INTEGER DEFAULT 0, privacy_ok INTEGER DEFAULT 1)")
|
||||
con.executemany("INSERT INTO users(tg_id,username,token_name) VALUES(?,?,?)",
|
||||
[(101, "alice", "tg101"), (102, "bob", "tg102")])
|
||||
con.commit()
|
||||
with closing(sqlite3.connect(self.gw_db)) as con:
|
||||
con.execute("CREATE TABLE tokens(id INTEGER PRIMARY KEY, name TEXT, status INTEGER,"
|
||||
" remain_quota INTEGER, unlimited_quota INTEGER DEFAULT 0, deleted_at TEXT)")
|
||||
con.execute("CREATE TABLE users(id INTEGER PRIMARY KEY, username TEXT, status INTEGER)")
|
||||
con.executemany("INSERT INTO tokens(id,name,status,remain_quota) VALUES(?,?,1,5000)",
|
||||
[(1, "tg101"), (2, "tg102")])
|
||||
con.executemany("INSERT INTO users(username,status) VALUES(?,1)", [("tg101",), ("tg102",)])
|
||||
con.commit()
|
||||
self.patches = [mock.patch.object(entry_autoban, "BOT_DB", str(self.bot_db)),
|
||||
mock.patch.object(entry_autoban, "GW_DB", str(self.gw_db)),
|
||||
mock.patch.object(entry_autoban, "SKIN_DB", str(root / "skin.db")),
|
||||
mock.patch.object(entry_telemetry, "public_ip", lambda ip: ip)]
|
||||
for p in self.patches:
|
||||
p.start()
|
||||
self.store = entry_telemetry.EntryTelemetry(self.bot_db, b"k", owners=[1])
|
||||
self.store.init_db()
|
||||
|
||||
def tearDown(self):
|
||||
for p in self.patches:
|
||||
p.stop()
|
||||
self.tmp.cleanup()
|
||||
|
||||
def _visit_both(self):
|
||||
self.store.record(101, NAT_IP)
|
||||
self.store.record(102, NAT_IP)
|
||||
with closing(sqlite3.connect(self.gw_db)) as gw, closing(sqlite3.connect(self.bot_db)) as bot:
|
||||
return (gw.execute("SELECT status,remain_quota FROM tokens ORDER BY id").fetchall(),
|
||||
bot.execute("SELECT banned FROM users ORDER BY tg_id").fetchall(),
|
||||
bot.execute("SELECT COUNT(*) FROM wa_entry_alerts").fetchone()[0])
|
||||
|
||||
def test_env_absent_keeps_access_and_quota(self):
|
||||
with mock.patch.dict(os.environ, {}, clear=False):
|
||||
os.environ.pop("IISHKO_ENTRY_AUTOBAN", None)
|
||||
tokens, banned, alerts = self._visit_both()
|
||||
self.assertEqual(tokens, [(1, 5000), (1, 5000)])
|
||||
self.assertEqual(banned, [(0,), (0,)])
|
||||
self.assertEqual(alerts, 1) # operator still gets the signal
|
||||
|
||||
def test_opt_in_enforces(self):
|
||||
with mock.patch.dict(os.environ, {"IISHKO_ENTRY_AUTOBAN": "1"}):
|
||||
tokens, banned, _ = self._visit_both()
|
||||
self.assertEqual(tokens, [(2, 0), (2, 0)])
|
||||
self.assertEqual(banned, [(1,), (1,)])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,39 @@
|
||||
"""PPTX: many highly-compressible slides must not be decompressed past the budgets."""
|
||||
import io
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import unittest
|
||||
from unittest import mock
|
||||
import zipfile
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "bot"))
|
||||
|
||||
import file_intake # noqa: E402
|
||||
|
||||
|
||||
def _pptx(slides: int, text: str) -> bytes:
|
||||
buf = io.BytesIO()
|
||||
with zipfile.ZipFile(buf, "w", zipfile.ZIP_DEFLATED) as zf:
|
||||
for i in range(1, slides + 1):
|
||||
zf.writestr(f"ppt/slides/slide{i}.xml", f"<p:sld><a:p>{text}</a:p></p:sld>")
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
class PptxBudgetTest(unittest.TestCase):
|
||||
def test_stops_reading_after_text_budget(self):
|
||||
data = _pptx(200, "x" * 5000)
|
||||
with mock.patch.object(file_intake, "_read_entry", wraps=file_intake._read_entry) as rd:
|
||||
res = file_intake.extract("deck.pptx", data)
|
||||
self.assertTrue(res.ok)
|
||||
self.assertTrue(res.truncated)
|
||||
self.assertLess(rd.call_count, 10)
|
||||
|
||||
def test_total_unpacked_budget_rejects(self):
|
||||
data = _pptx(20, "<b/>" * 262144) # 20 MiB of markup, no text
|
||||
with mock.patch.object(file_intake, "_MAX_UNPACKED_BYTES", 8 * 1024 * 1024):
|
||||
res = file_intake.extract("deck.pptx", data)
|
||||
self.assertFalse(res.ok)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user