fix: harden license server and web gui security
This commit is contained in:
1 parent
7c6189fd5e
commit
ee3424cd9e
9 files changed
+357
-11
No files matched your search
@@ -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
|
||||
|
||||
|
||||
@@ -280,3 +280,6 @@ class TestRequestParsing:
|
||||
|
||||
assert dummy.status == 200
|
||||
assert dummy.header_finished is True
|
||||
assert ("X-Content-Type-Options", "nosniff") in dummy.headers
|
||||
assert ("Referrer-Policy", "no-referrer") in dummy.headers
|
||||
assert ("X-Frame-Options", "DENY") in dummy.headers
|
||||
@@ -619,6 +619,27 @@ def test_rejects_non_local_get_api_requests(monkeypatch):
|
||||
assert obj.get("error") == "Local access only"
|
||||
|
||||
|
||||
def test_license_gate_html_has_security_headers(monkeypatch):
|
||||
monkeypatch.setattr(routes, "check_license", lambda: SimpleNamespace(ok=False))
|
||||
monkeypatch.setattr(
|
||||
routes,
|
||||
"render_license_gate_html",
|
||||
lambda token: "<!doctype html><html><body>gate</body></html>", # noqa: ARG005
|
||||
)
|
||||
handler = _DummyHandler()
|
||||
runner = _DummyRunner()
|
||||
u = SimpleNamespace(path="/", query="")
|
||||
|
||||
routes.handle_get(handler, u, runner, "token")
|
||||
|
||||
headers = dict(handler.response_headers)
|
||||
assert handler.response_status == 200
|
||||
assert headers["X-Content-Type-Options"] == "nosniff"
|
||||
assert headers["Referrer-Policy"] == "no-referrer"
|
||||
assert headers["X-Frame-Options"] == "DENY"
|
||||
assert "Content-Security-Policy" in headers
|
||||
|
||||
|
||||
def test_rejects_non_local_pic_requests(monkeypatch):
|
||||
handler = _DummyHandler()
|
||||
handler.client_address = ("192.168.1.50", 55555)
|
||||
|
||||
Reference in new issue
Block a user