安全性更新
This commit is contained in:
1 parent
5bd77d2599
commit
e75f0c95e9
29 files changed
+726
-143
No files matched your search
@@ -4,9 +4,9 @@ import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from PIL import Image
|
||||
|
||||
from app import auth, checkin, clock, config, mailer, totp
|
||||
from app import auth, checkin, clock, config, db, limits, mailer, totp
|
||||
|
||||
ADMIN_LOGIN = "sthkm3"
|
||||
ADMIN_LOGIN = "[email protected]"
|
||||
ADMIN_TOTP = "JBSWY3DPEHPK3PXPJBSWY3DPEHPK3PXP" # 測試用密鑰
|
||||
|
||||
|
||||
@@ -45,7 +45,8 @@ def client(tmp_path, monkeypatch, fake_clock, sent):
|
||||
monkeypatch.setattr(config, "MEME_DIR", memes)
|
||||
monkeypatch.setattr(config, "ADMIN_LOGIN", ADMIN_LOGIN)
|
||||
monkeypatch.setattr(config, "ADMIN_TOTP", ADMIN_TOTP)
|
||||
auth._ip_hits.clear()
|
||||
for w in limits.Window._all:
|
||||
w.clear()
|
||||
for d in (checkin._next, checkin._rejects, checkin._cool):
|
||||
d.clear()
|
||||
from app.main import app
|
||||
@@ -61,6 +62,17 @@ def login(client, sent, email="[email protected]", lang="zh-TW"):
|
||||
return r.json()
|
||||
|
||||
|
||||
def admin_verify(client, code, email=ADMIN_LOGIN, headers=None):
|
||||
"""管理員登入:和一般人一樣要先要驗證碼(這裡略過 60 秒限制),再輸入 TOTP。"""
|
||||
conn = db.connect()
|
||||
conn.execute("DELETE FROM login_codes WHERE email = ?", (email,))
|
||||
conn.close()
|
||||
auth._ip_mail.clear()
|
||||
r = client.post("/api/auth/request-code", json={"email": email})
|
||||
assert r.status_code == 200, r.text
|
||||
return client.post("/api/auth/verify", json={"email": email, "code": code}, headers=headers)
|
||||
|
||||
|
||||
def admin_code(fake_clock, offset_steps=0):
|
||||
"""用假時鐘算出管理員的 TOTP。"""
|
||||
step = int(fake_clock.value.timestamp()) // totp.PERIOD + offset_steps
|
||||
|
||||
+14
-15
@@ -4,11 +4,11 @@ import pytest
|
||||
|
||||
from app import admin, config, db, totp
|
||||
|
||||
from .conftest import ADMIN_LOGIN, admin_code, login
|
||||
from .conftest import ADMIN_LOGIN, admin_code, admin_verify, login
|
||||
|
||||
|
||||
def admin_login(client, code, login_name=ADMIN_LOGIN):
|
||||
return client.post("/api/auth/verify", json={"email": login_name, "code": code})
|
||||
return admin_verify(client, code, login_name)
|
||||
|
||||
|
||||
def test_totp_rfc6238_vector():
|
||||
@@ -23,7 +23,7 @@ def test_totp_rfc6238_vector():
|
||||
|
||||
|
||||
def test_admin_request_code_does_not_send(client, sent):
|
||||
r = client.post("/api/auth/request-code", json={"email": "STHKM3"})
|
||||
r = client.post("/api/auth/request-code", json={"email": ADMIN_LOGIN.upper()})
|
||||
assert r.status_code == 200 and r.json()["status"] == "sent"
|
||||
assert sent == {}
|
||||
|
||||
@@ -64,10 +64,10 @@ def test_admin_lockout(client, fake_clock):
|
||||
wrong = "000000" if good != "000000" else "111111"
|
||||
assert admin_login(client, wrong).json()["detail"] == "wrong_code"
|
||||
assert admin_login(client, wrong).json()["detail"] == "wrong_code"
|
||||
r = admin_login(client, wrong) # 第 3 次就鎖
|
||||
assert r.status_code == 429
|
||||
assert admin_login(client, wrong).json()["detail"] == "wrong_code"
|
||||
assert db.get_meta(db.connect(), "admin_lock_level") == 1 # 第 3 次就鎖,但回應看不出來
|
||||
# 鎖定中,正確的碼也不行
|
||||
assert admin_login(client, good).status_code == 429
|
||||
assert admin_login(client, good).json()["detail"] == "wrong_code"
|
||||
|
||||
# 15 分鐘後解鎖
|
||||
fake_clock.value += timedelta(minutes=15)
|
||||
@@ -79,7 +79,7 @@ def test_admin_lock_doubles(client, fake_clock):
|
||||
for _ in range(3):
|
||||
admin_login(client, "000000" if admin_code(fake_clock) != "000000" else "111111")
|
||||
fake_clock.value += timedelta(minutes=minutes) - timedelta(seconds=1)
|
||||
assert admin_login(client, admin_code(fake_clock)).status_code == 429
|
||||
assert admin_login(client, admin_code(fake_clock)).status_code == 400
|
||||
fake_clock.value += timedelta(seconds=1)
|
||||
conn = db.connect()
|
||||
assert db.get_meta(conn, "admin_lock_level") == 3
|
||||
@@ -88,7 +88,7 @@ def test_admin_lock_doubles(client, fake_clock):
|
||||
def test_admin_unlock_command(client, fake_clock):
|
||||
for _ in range(3):
|
||||
admin_login(client, "000000" if admin_code(fake_clock) != "000000" else "111111")
|
||||
assert admin_login(client, admin_code(fake_clock)).status_code == 429
|
||||
assert admin_login(client, admin_code(fake_clock)).status_code == 400
|
||||
admin.main(["unlock"])
|
||||
assert admin_login(client, admin_code(fake_clock)).status_code == 200
|
||||
|
||||
@@ -96,16 +96,15 @@ def test_admin_unlock_command(client, fake_clock):
|
||||
def test_admin_disabled_without_secret(client, sent, fake_clock, monkeypatch):
|
||||
monkeypatch.setattr(config, "ADMIN_TOTP", "")
|
||||
db.init()
|
||||
r = client.post("/api/auth/request-code", json={"email": ADMIN_LOGIN})
|
||||
assert r.status_code == 400 and r.json()["detail"] == "invalid_email"
|
||||
assert admin_login(client, admin_code(fake_clock)).json()["detail"] == "invalid_email"
|
||||
assert admin_login(client, admin_code(fake_clock)).json()["detail"] == "wrong_code"
|
||||
assert sent == {}
|
||||
|
||||
|
||||
def test_admin_login_renamed(client, fake_clock, monkeypatch):
|
||||
monkeypatch.setattr(config, "ADMIN_LOGIN", "boss")
|
||||
monkeypatch.setattr(config, "ADMIN_LOGIN", "[email protected]")
|
||||
db.init()
|
||||
assert admin_login(client, admin_code(fake_clock), "sthkm3").json()["detail"] == "invalid_email"
|
||||
r = admin_login(client, admin_code(fake_clock), "boss")
|
||||
assert admin_login(client, admin_code(fake_clock)).json()["detail"] == "wrong_code" # 舊的變成一般信箱
|
||||
r = admin_login(client, admin_code(fake_clock), "[email protected]")
|
||||
assert r.status_code == 200 and r.json()["user"]["uid"] == 0
|
||||
|
||||
|
||||
@@ -119,7 +118,7 @@ def test_admin_login_clash_disables(client, sent, fake_clock, monkeypatch):
|
||||
def test_totp_uri(capsys, client):
|
||||
admin.main(["totp-uri"])
|
||||
last = capsys.readouterr().out.strip().splitlines()[-1]
|
||||
assert last.startswith("otpauth://totp/%E5%90%B1%E9%BC%AF%E8%A8%88%E7%95%AB%3Asthkm3?secret=")
|
||||
assert last.startswith("otpauth://totp/%E5%90%B1%E9%BC%AF%E8%A8%88%E7%95%AB%3Aboss-7f3a%40admin.invalid?secret=")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("uid", ["0", "-1"])
|
||||
|
||||
@@ -7,22 +7,26 @@ import pytest
|
||||
|
||||
from app import __version__, anticheat, card, checkin, config, db, mailer, notices, users
|
||||
|
||||
from .conftest import ADMIN_LOGIN, admin_code, login
|
||||
from .conftest import admin_code, admin_verify, login
|
||||
from .test_zhi import frozen # noqa: F401 (fixture)
|
||||
|
||||
|
||||
def as_admin(client, fake_clock):
|
||||
client.cookies.clear()
|
||||
r = client.post("/api/auth/verify", json={"email": ADMIN_LOGIN, "code": admin_code(fake_clock)})
|
||||
r = admin_verify(client, admin_code(fake_clock))
|
||||
assert r.status_code == 200, r.text
|
||||
forget_login()
|
||||
|
||||
|
||||
def zhi(client):
|
||||
return client.post("/api/zhi", json={"n": 1})
|
||||
|
||||
|
||||
def kinds(client):
|
||||
return [n["kind"] for n in client.get("/api/admin/notices").json()["items"]]
|
||||
def forget_login():
|
||||
"""管理員登入本身也會發一則通知,測其他通知時先拿掉。"""
|
||||
conn = db.connect()
|
||||
conn.execute("DELETE FROM notices WHERE kind = 'admin_login'")
|
||||
conn.close()
|
||||
|
||||
|
||||
# ---------- 冷靜時間 ----------
|
||||
@@ -151,7 +155,7 @@ def test_error_mail_and_lock_notices(client, sent, fake_clock, monkeypatch):
|
||||
monkeypatch.setattr(mailer, "send_code", boom)
|
||||
assert client.post("/api/auth/request-code", json={"email": "[email protected]"}).status_code == 502
|
||||
for _ in range(config.ADMIN_MAX_FAILS):
|
||||
client.post("/api/auth/verify", json={"email": ADMIN_LOGIN, "code": "abc"})
|
||||
admin_verify(client, "abc")
|
||||
monkeypatch.setattr(users, "render_md", boom)
|
||||
with pytest.raises(RuntimeError):
|
||||
client.get("/api/users/0")
|
||||
|
||||
@@ -0,0 +1,344 @@
|
||||
"""1.0.2 發版前的漏洞修補:寄信限制、管理員不可探測、圖片、請求大小、儲存權限。"""
|
||||
import io
|
||||
import os
|
||||
import struct
|
||||
import zlib
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from datetime import timedelta
|
||||
|
||||
import pytest
|
||||
from PIL import Image
|
||||
|
||||
from app import anticheat, auth, config, db, images, limits, main, users
|
||||
|
||||
from .conftest import ADMIN_LOGIN, admin_code, admin_verify, login
|
||||
|
||||
|
||||
def ask(client, email, ip="198.51.100.1"):
|
||||
return client.post("/api/auth/request-code", json={"email": email}, headers={"CF-Connecting-IP": ip})
|
||||
|
||||
|
||||
def verify(client, email, code):
|
||||
return client.post("/api/auth/verify", json={"email": email, "code": code})
|
||||
|
||||
|
||||
def bomb(w, h):
|
||||
"""檔頭寫很大、內容很小的 PNG。"""
|
||||
def chunk(t, d):
|
||||
return struct.pack(">I", len(d)) + t + d + struct.pack(">I", zlib.crc32(t + d))
|
||||
return (b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", struct.pack(">IIBBBBB", w, h, 8, 6, 0, 0, 0))
|
||||
+ chunk(b"IDAT", zlib.compress(b"\0" * 1000)) + chunk(b"IEND", b""))
|
||||
|
||||
|
||||
def img(fmt, size=(40, 30), **kw):
|
||||
buf = io.BytesIO()
|
||||
Image.new("RGB", size, (9, 99, 200)).save(buf, fmt, **kw)
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
def put_avatar(client, data):
|
||||
return client.put("/api/me/avatar", files={"file": ("a", data, "image/png")})
|
||||
|
||||
|
||||
# ---------- 寄信 ----------
|
||||
|
||||
@pytest.mark.parametrize("email", [
|
||||
"<[email protected]>", "a<[email protected]>", "z,[email protected]", "(c)[email protected]", '"x"<[email protected]>', "v@example",
|
||||
"[email protected]", "[email protected]", "v [email protected]", "[email protected]\nBcc: [email protected]", "v@example.com", "\ud800@a.co", "[email protected]", "",
|
||||
"a" * 65 + "@example.com", "v@" + "a." * 130 + "com",
|
||||
])
|
||||
def test_odd_emails_rejected(client, sent, email):
|
||||
import json
|
||||
r = client.post("/api/auth/request-code", content=json.dumps({"email": email}),
|
||||
headers={"Content-Type": "application/json"})
|
||||
assert r.status_code == 400 and r.json()["detail"] == "invalid_email" and sent == {}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("email", ["[email protected]", "[email protected]", "[email protected]", "o'[email protected]"])
|
||||
def test_normal_emails_accepted(email):
|
||||
assert limits.valid_email(email)
|
||||
|
||||
|
||||
def test_ip_key():
|
||||
assert limits.ip_key("2001:db8:1:1::5") == limits.ip_key("2001:db8:1:1:ffff::9")
|
||||
assert limits.ip_key("2001:db8:1:1::5") != limits.ip_key("2001:db8:1:2::5")
|
||||
assert limits.ip_key("::ffff:203.0.113.9") == limits.ip_key("203.0.113.9") == "203.0.113.9"
|
||||
assert limits.ip_key("203.0.113.9") != limits.ip_key("203.0.113.10")
|
||||
assert limits.ip_key("testclient") == "testclient"
|
||||
|
||||
|
||||
def test_ipv6_64_shares_limit(client, sent):
|
||||
codes = [ask(client, f"u{i}@example.com", f"2001:db8:1:1::{i:x}").status_code for i in range(12)]
|
||||
assert codes == [200] * config.IP_LIMIT + [429] * 2
|
||||
|
||||
|
||||
@pytest.mark.parametrize("name", ["MAIL_LIMIT", "MAIL_DAY_LIMIT"])
|
||||
def test_site_wide_mail_limit(client, sent, monkeypatch, name):
|
||||
monkeypatch.setattr(config, name, 3)
|
||||
codes = [ask(client, f"u{i}@example.com", f"203.0.113.{i}").status_code for i in range(5)]
|
||||
assert codes == [200, 200, 200, 429, 429] and len(sent) == 3
|
||||
conn = db.connect()
|
||||
assert [(r["kind"], r["count"]) for r in conn.execute("SELECT * FROM notices")] == [("mail_limit", 2)]
|
||||
conn.close()
|
||||
|
||||
|
||||
def test_sweep_drops_expired_only(monkeypatch):
|
||||
auth._ip_mail.clear()
|
||||
now = [1000.0]
|
||||
monkeypatch.setattr(limits.time, "monotonic", lambda: now[0])
|
||||
auth._ip_mail.allow("old")
|
||||
now[0] += config.IP_WINDOW - 1
|
||||
auth._ip_mail.allow("new")
|
||||
now[0] += 2
|
||||
limits.sweep_all()
|
||||
assert list(auth._ip_mail._hits) == ["new"]
|
||||
|
||||
|
||||
def test_hourly_loop_sweeps(monkeypatch):
|
||||
import asyncio
|
||||
called = []
|
||||
|
||||
async def fake_sleep(_):
|
||||
if called:
|
||||
raise asyncio.CancelledError
|
||||
monkeypatch.setattr(anticheat.asyncio, "sleep", fake_sleep)
|
||||
monkeypatch.setattr(anticheat, "check_safe", lambda: None)
|
||||
monkeypatch.setattr(limits, "sweep_all", lambda: called.append(1))
|
||||
with pytest.raises(asyncio.CancelledError):
|
||||
asyncio.run(anticheat.loop())
|
||||
assert called == [1]
|
||||
|
||||
|
||||
# ---------- 驗證碼:同時送也不能多猜、不能重複用 ----------
|
||||
|
||||
def test_parallel_guesses_are_capped(client, sent, monkeypatch):
|
||||
ask(client, "[email protected]")
|
||||
compared = []
|
||||
real = auth.secrets.compare_digest
|
||||
monkeypatch.setattr(auth.secrets, "compare_digest", lambda a, b: compared.append(1) or real(a, b))
|
||||
with ThreadPoolExecutor(30) as ex:
|
||||
codes = list(ex.map(lambda i: verify(client, "[email protected]", f"x{i}").json()["detail"], range(150)))
|
||||
assert len(compared) == config.CODE_MAX_ATTEMPTS
|
||||
assert codes.count("wrong_code") == config.CODE_MAX_ATTEMPTS - 1
|
||||
|
||||
|
||||
def test_parallel_right_code_works_once(client, sent):
|
||||
ask(client, "[email protected]")
|
||||
with ThreadPoolExecutor(10) as ex:
|
||||
codes = list(ex.map(lambda _: verify(client, "[email protected]", sent["[email protected]"]).status_code, range(30)))
|
||||
assert codes.count(200) == 1
|
||||
|
||||
|
||||
def test_fifth_try_can_still_succeed(client, sent):
|
||||
ask(client, "[email protected]")
|
||||
for _ in range(config.CODE_MAX_ATTEMPTS - 1):
|
||||
assert verify(client, "[email protected]", "x").json()["detail"] == "wrong_code"
|
||||
assert verify(client, "[email protected]", sent["[email protected]"]).status_code == 200
|
||||
|
||||
|
||||
def test_used_admin_code_does_not_burn_attempts(client, fake_clock):
|
||||
assert admin_verify(client, admin_code(fake_clock)).status_code == 200
|
||||
ask(client, ADMIN_LOGIN)
|
||||
for _ in range(config.CODE_MAX_ATTEMPTS + 2):
|
||||
assert verify(client, ADMIN_LOGIN, admin_code(fake_clock)).json()["detail"] == "code_used"
|
||||
|
||||
|
||||
# ---------- 管理員和一般信箱的回應要完全一樣 ----------
|
||||
|
||||
def walk(client, email, fake_clock):
|
||||
"""把外人能做的事走一遍,記下每一步的回應。"""
|
||||
out = []
|
||||
|
||||
def rec(r):
|
||||
out.append((r.status_code, r.text, sorted(k for k in r.headers if k not in ("date", "content-length"))))
|
||||
|
||||
rec(verify(client, email, "999999")) # 還沒要驗證碼
|
||||
rec(ask(client, email))
|
||||
rec(ask(client, email)) # 60 秒內重寄
|
||||
for code in ("999999", "123456", "", "abc", "é"):
|
||||
rec(verify(client, email, code)) # 第 5 次作廢(管理員第 3 次已鎖定)
|
||||
rec(verify(client, email, "999999"))
|
||||
fake_clock.value += timedelta(seconds=61)
|
||||
rec(ask(client, email))
|
||||
rec(verify(client, email, "999999")) # 管理員鎖定中
|
||||
fake_clock.value += timedelta(minutes=6)
|
||||
rec(verify(client, email, "999999")) # 過期
|
||||
return out
|
||||
|
||||
|
||||
def test_admin_is_indistinguishable(client, sent, fake_clock, monkeypatch):
|
||||
monkeypatch.setattr(auth.secrets, "randbelow", lambda n: 123457)
|
||||
start = fake_clock.value
|
||||
normal = walk(client, "[email protected]", fake_clock)
|
||||
fake_clock.value = start
|
||||
auth._ip_mail.clear()
|
||||
assert walk(client, ADMIN_LOGIN, fake_clock) == normal
|
||||
assert ADMIN_LOGIN not in sent
|
||||
conn = db.connect()
|
||||
assert db.get_meta(conn, "admin_lock_level") == 1 # 裡面還是有鎖
|
||||
conn.close()
|
||||
|
||||
|
||||
def test_surrogate_code_is_same_for_everyone(client, sent):
|
||||
for email in ("[email protected]", ADMIN_LOGIN):
|
||||
ask(client, email)
|
||||
r = client.post("/api/auth/verify", content='{"email":"%s","code":"\\ud800"}' % email,
|
||||
headers={"Content-Type": "application/json"})
|
||||
assert r.status_code == 400 and r.json()["detail"] == "wrong_code"
|
||||
|
||||
|
||||
def test_admin_waits_like_real_mail(client, sent, monkeypatch):
|
||||
slept = []
|
||||
monkeypatch.setattr(auth.time, "sleep", slept.append)
|
||||
ask(client, ADMIN_LOGIN)
|
||||
assert slept == [] # 沒設 SMTP:一般信箱也是立刻回
|
||||
db.connect().execute("DELETE FROM login_codes")
|
||||
monkeypatch.setattr(config, "SMTP_USER", "u")
|
||||
monkeypatch.setattr(config, "SMTP_PASSWORD", "p")
|
||||
auth._send_times.clear()
|
||||
auth._send_times.append(2.0)
|
||||
ask(client, ADMIN_LOGIN)
|
||||
assert len(slept) == 1 and 1.8 <= slept[0] <= 2.2
|
||||
|
||||
|
||||
@pytest.mark.parametrize("how", ["no_totp", "bad_totp", "not_email", "no_login"])
|
||||
def test_disabled_admin_cannot_be_taken_over_by_mail(client, sent, fake_clock, monkeypatch, how):
|
||||
"""管理員停用後,就算那個信箱真的存在,也不能用寄信的驗證碼登入成管理員。"""
|
||||
key, value = {"no_totp": ("ADMIN_TOTP", ""), "bad_totp": ("ADMIN_TOTP", "!!"),
|
||||
"not_email": ("ADMIN_LOGIN", "boss"), "no_login": ("ADMIN_LOGIN", "")}[how]
|
||||
monkeypatch.setattr(config, key, value)
|
||||
db.init()
|
||||
assert db.admin_ok is False
|
||||
monkeypatch.setattr(auth.secrets, "randbelow", lambda n: 123456)
|
||||
assert ask(client, ADMIN_LOGIN).status_code == 200 and sent == {}
|
||||
assert verify(client, ADMIN_LOGIN, "123456").json()["detail"] == "wrong_code"
|
||||
assert verify(client, ADMIN_LOGIN, admin_code(fake_clock)).json()["detail"] == "wrong_code"
|
||||
|
||||
|
||||
def test_admin_login_is_recorded(client, fake_clock):
|
||||
for _ in range(2):
|
||||
r = admin_verify(client, admin_code(fake_clock), headers={
|
||||
"CF-Connecting-IP": "203.0.113.7", "CF-IPCountry": "TW", "User-Agent": "Test/1.0 " + "x" * 300})
|
||||
assert r.status_code == 200
|
||||
fake_clock.value += timedelta(seconds=30)
|
||||
r = client.get("/api/admin/notices").json()
|
||||
assert [(n["kind"], n["count"], n["unread"]) for n in r["items"]] == [("admin_login", 1, True)] * 2
|
||||
assert r["items"][0]["detail"].startswith("203.0.113.7 · TW · Test/1.0 xxx") and len(r["items"][0]["detail"]) < 200
|
||||
|
||||
|
||||
def test_normal_login_is_not_recorded(client, sent):
|
||||
login(client, sent)
|
||||
assert db.connect().execute("SELECT COUNT(*) FROM notices").fetchone()[0] == 0
|
||||
|
||||
|
||||
# ---------- 圖片 ----------
|
||||
|
||||
def test_bomb_rejected_before_decode(client, sent, monkeypatch):
|
||||
login(client, sent)
|
||||
monkeypatch.setattr(Image.Image, "load", lambda self: pytest.fail("不該解碼"))
|
||||
r = put_avatar(client, bomb(8900, 8900))
|
||||
assert r.status_code == 400 and r.json()["detail"] == "invalid_image"
|
||||
|
||||
|
||||
@pytest.mark.parametrize("fmt,ok", [("PNG", 1), ("JPEG", 1), ("WEBP", 1), ("GIF", 1),
|
||||
("TIFF", 0), ("BMP", 0), ("ICO", 0), ("TGA", 0), ("PPM", 0), ("PCX", 0)])
|
||||
def test_only_four_formats(client, sent, fmt, ok):
|
||||
login(client, sent)
|
||||
assert put_avatar(client, img(fmt)).status_code == (200 if ok else 400)
|
||||
r = client.post("/api/submissions", files={"file": ("a.png", img(fmt), "image/png")})
|
||||
assert r.status_code == (200 if ok else 400)
|
||||
|
||||
|
||||
def test_psd_rejected(client, sent):
|
||||
login(client, sent)
|
||||
psd = b"8BPS" + struct.pack(">H6xHIIHH", 1, 3, 4, 4, 8, 3) + b"\0" * 14 + b"\0" * 48
|
||||
with Image.open(io.BytesIO(psd)) as im:
|
||||
assert im.format == "PSD" # 這個檔本身是讀得到的
|
||||
assert put_avatar(client, psd).status_code == 400
|
||||
|
||||
|
||||
def test_exif_rotation_kept(client, sent):
|
||||
exif = Image.Exif()
|
||||
exif[0x0112] = 6
|
||||
im, alpha = images.decode(img("JPEG", (40, 30), exif=exif))
|
||||
assert im.size == (30, 40) and alpha is False
|
||||
|
||||
|
||||
def test_avatar_three_per_minute(client, sent):
|
||||
login(client, sent)
|
||||
codes = [put_avatar(client, d).status_code for d in (img("PNG"), b"bad", img("PNG"), img("PNG"))]
|
||||
assert codes == [200, 400, 200, 429] # 失敗也算
|
||||
assert put_avatar(client, img("PNG")).json()["detail"] == "too_frequent"
|
||||
client.cookies.clear()
|
||||
login(client, sent, "[email protected]")
|
||||
assert put_avatar(client, img("PNG")).status_code == 200
|
||||
|
||||
|
||||
def test_one_image_at_a_time(client, sent, monkeypatch):
|
||||
login(client, sent)
|
||||
monkeypatch.setattr(config, "IMAGE_WAIT", 0.05)
|
||||
with images.slot():
|
||||
r = put_avatar(client, img("PNG"))
|
||||
assert r.status_code == 429 and r.json()["detail"] == "too_frequent"
|
||||
assert put_avatar(client, img("PNG")).status_code == 200 # 失敗後鎖有放開
|
||||
assert put_avatar(client, b"bad").status_code == 400
|
||||
assert images._slot.acquire(timeout=0) and images._slot.release() is None
|
||||
|
||||
|
||||
# ---------- 請求大小、路徑 ----------
|
||||
|
||||
def test_body_limit_by_length(client):
|
||||
r = client.post("/api/auth/request-code", json={"email": "a" * config.BODY_MAX})
|
||||
assert r.status_code == 413 and r.json()["detail"] == "file_too_large"
|
||||
r = client.post("/api/submissions", files={"file": ("a", b"\0" * config.UPLOAD_BODY_MAX)})
|
||||
assert r.status_code == 413
|
||||
|
||||
|
||||
@pytest.mark.parametrize("path,ctype,limit", [
|
||||
("/api/auth/request-code", "application/json", config.BODY_MAX),
|
||||
("/api/submissions", "multipart/form-data; boundary=b", config.UPLOAD_BODY_MAX),
|
||||
])
|
||||
def test_body_limit_chunked(client, path, ctype, limit):
|
||||
got = [0]
|
||||
|
||||
def body():
|
||||
yield b'--b\r\nContent-Disposition: form-data; name="file"; filename="a"\r\n\r\n' if "multi" in ctype else b'{"email":"'
|
||||
for _ in range(limit // 32768 + 40):
|
||||
got[0] += 1
|
||||
yield b"a" * 32768
|
||||
r = client.post(path, content=body(), headers={"Content-Type": ctype})
|
||||
assert r.status_code == 413 and r.json()["detail"] == "file_too_large"
|
||||
|
||||
|
||||
def test_five_mb_file_still_allowed(client, sent):
|
||||
login(client, sent)
|
||||
data = img("PNG")
|
||||
assert put_avatar(client, data + b"\0" * (config.AVATAR_MAX_BYTES - len(data))).status_code == 200
|
||||
|
||||
|
||||
def test_csrf_not_bypassed_by_host(client, sent):
|
||||
login(client, sent)
|
||||
r = client.post("/api/checkin", headers={"X-ZHIWU": "", "Host": "x/y?"})
|
||||
assert r.status_code in (400, 403)
|
||||
|
||||
|
||||
# ---------- 儲存權限 ----------
|
||||
|
||||
@pytest.mark.skipif(os.name == "nt" or os.geteuid() == 0, reason="要在非 root 的 Linux 上測")
|
||||
def test_storage_check(tmp_path, monkeypatch, capsys):
|
||||
data, memes = tmp_path / "d", tmp_path / "m"
|
||||
data.mkdir(), memes.mkdir()
|
||||
monkeypatch.setattr(config, "DATA_DIR", data)
|
||||
monkeypatch.setattr(config, "MEME_DIR", memes)
|
||||
main.check_storage()
|
||||
memes.chmod(0o500)
|
||||
main.check_storage()
|
||||
assert "[WARN]" in capsys.readouterr().out
|
||||
(data / "zhiwu.db").touch(0o400)
|
||||
with pytest.raises(RuntimeError, match="zhiwu.db"):
|
||||
main.check_storage()
|
||||
memes.chmod(0o700)
|
||||
|
||||
|
||||
def test_users_module_has_no_direct_open():
|
||||
assert not hasattr(users, "Image") or "Image.open" not in open(users.__file__, encoding="utf-8").read()
|
||||
@@ -4,7 +4,7 @@ from PIL import Image
|
||||
|
||||
from app import admin, checkin, config, db
|
||||
|
||||
from .conftest import ADMIN_LOGIN, admin_code, login
|
||||
from .conftest import admin_code, admin_verify, login
|
||||
|
||||
|
||||
def _img(fmt="PNG", color=(10, 200, 30), size=(300, 200), **kw):
|
||||
@@ -23,7 +23,7 @@ def upload(client, token, data=None, name="a.png"):
|
||||
|
||||
|
||||
def admin_token(client, fake_clock):
|
||||
r = client.post("/api/auth/verify", json={"email": ADMIN_LOGIN, "code": admin_code(fake_clock)})
|
||||
r = admin_verify(client, admin_code(fake_clock))
|
||||
assert r.status_code == 200, r.text
|
||||
return r.json()["token"]
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import sqlite3
|
||||
from app import admin, config, db
|
||||
from app.titles import text_on
|
||||
|
||||
from .conftest import ADMIN_LOGIN, admin_code, login
|
||||
from .conftest import admin_code, admin_verify, login
|
||||
|
||||
|
||||
def bearer(tok):
|
||||
@@ -12,8 +12,7 @@ def bearer(tok):
|
||||
|
||||
|
||||
def admin_token(client, fake_clock):
|
||||
client.post("/api/auth/request-code", json={"email": ADMIN_LOGIN, "lang": "zh-TW"})
|
||||
r = client.post("/api/auth/verify", json={"email": ADMIN_LOGIN, "code": admin_code(fake_clock), "lang": "zh-TW"})
|
||||
r = admin_verify(client, admin_code(fake_clock))
|
||||
assert r.status_code == 200, r.text
|
||||
client.cookies.clear()
|
||||
return r.json()["token"]
|
||||
|
||||
Reference in new issue
Block a user