fix: harden license server and web gui security

This commit is contained in:
Your Name committed 2026-06-28 09:24:22 +08:00
1 parent 7c6189fd5e
commit ee3424cd9e
9 files changed
+357 -11

No files matched your search

+66
View File
@@ -181,6 +181,37 @@ def test_admin_can_publish_update_and_download_installer(tmp_path):
assert download.content == payload
def test_admin_publish_update_rejects_oversized_upload(tmp_path, monkeypatch):
from fastapi.testclient import TestClient
from license_server import api
from license_server.core import LicenseService
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
monkeypatch.setenv("DYGJ_LICENSE_MAX_INSTALLER_UPLOAD_MB", "1")
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
client = TestClient(api.create_app(service=service, admin_token="secret"))
client.post("/admin/login", data={"admin_token": "secret"}, follow_redirects=False)
payload = b"x" * (1024 * 1024 + 1)
published = client.post(
"/admin/update/publish",
cookies={api.ADMIN_CSRF_COOKIE: "csrf-token"},
data={
"csrf_token": "csrf-token",
"version": "3.3.4",
"notes": "oversized",
"force": "1",
},
files={"installer": ("DouyinGuaji-3.3.4-Setup.exe", payload, "application/octet-stream")},
)
manifest = client.get("/api/v1/update/manifest").json()
assert published.status_code == 200
assert "超过大小限制" in published.text
assert manifest["enabled"] is False
def test_admin_can_publish_delta_update_and_download_zip(tmp_path):
from fastapi.testclient import TestClient
@@ -229,6 +260,41 @@ def test_admin_can_publish_delta_update_and_download_zip(tmp_path):
assert download.content == delta_payload
def test_license_server_rejects_untrusted_host(tmp_path):
from fastapi.testclient import TestClient
from license_server import api
from license_server.core import LicenseService
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
client = TestClient(api.create_app(service=service), base_url="https://evil.example")
resp = client.get("/api/v1/health")
assert resp.status_code == 400
def test_public_https_base_url_enables_secure_admin_cookies(tmp_path, monkeypatch):
from fastapi.testclient import TestClient
from license_server import api
from license_server.core import LicenseService
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
monkeypatch.setenv("DYGJ_LICENSE_PUBLIC_BASE_URL", "https://dy.absfan.cn")
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
client = TestClient(
api.create_app(service=service, admin_token="secret"),
base_url="https://dy.absfan.cn",
)
login = client.post("/admin/login", data={"admin_token": "secret"}, follow_redirects=False)
assert login.status_code == 303
assert "secure" in login.headers["set-cookie"].lower()
def test_admin_page_shows_stats_cards_and_audit_logs(tmp_path):
from fastapi.testclient import TestClient