fix: harden license server security

This commit is contained in:
Your Name committed 2026-06-19 01:11:10 +08:00
1 parent ec51772cf0
commit a39545ed5d
10 files changed
+313 -88

No files matched your search

+21
View File
@@ -0,0 +1,21 @@
from __future__ import annotations
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
def _make_test_key_pair() -> tuple[bytes, bytes]:
private_key = Ed25519PrivateKey.generate()
private_pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.PKCS8,
encryption_algorithm=serialization.NoEncryption(),
)
public_pem = private_key.public_key().public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
return private_pem, public_pem
TEST_PRIVATE_KEY_PEM, TEST_PUBLIC_KEY_PEM = _make_test_key_pair()
+28 -18
View File
@@ -5,15 +5,24 @@ from datetime import datetime, timedelta, timezone
import pytest
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM, TEST_PUBLIC_KEY_PEM
def _create_test_license(*args, **kwargs):
from tools.license_dev import create_dev_license
kwargs.setdefault("private_key_pem", TEST_PRIVATE_KEY_PEM)
return create_dev_license(*args, **kwargs)
def test_signed_license_is_accepted_for_current_machine(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
license_path = tmp_path / "license.dat"
create_dev_license(
_create_test_license(
output_path=license_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
@@ -29,12 +38,12 @@ def test_signed_license_is_accepted_for_current_machine(tmp_path, monkeypatch):
def test_license_rejects_different_machine(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-b")
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
license_path = tmp_path / "license.dat"
create_dev_license(
_create_test_license(
output_path=license_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
@@ -48,12 +57,12 @@ def test_license_rejects_different_machine(tmp_path, monkeypatch):
def test_license_rejects_expired_file(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
license_path = tmp_path / "license.dat"
create_dev_license(
_create_test_license(
output_path=license_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) - timedelta(days=1),
@@ -67,12 +76,12 @@ def test_license_rejects_expired_file(tmp_path, monkeypatch):
def test_license_rejects_tampered_payload(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
license_path = tmp_path / "license.dat"
create_dev_license(
_create_test_license(
output_path=license_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
@@ -111,12 +120,12 @@ def test_missing_license_can_be_required(tmp_path, monkeypatch):
def test_install_license_accepts_json_text(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
source_path = tmp_path / "source_license.dat"
target_path = tmp_path / "license.dat"
create_dev_license(
_create_test_license(
output_path=source_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
@@ -136,12 +145,13 @@ def test_install_license_accepts_json_text(tmp_path, monkeypatch):
def test_install_license_accepts_activation_code(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license, encode_activation_code
from tools.license_dev import encode_activation_code
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
source_path = tmp_path / "source_license.dat"
target_path = tmp_path / "license.dat"
create_dev_license(
_create_test_license(
output_path=source_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
@@ -170,12 +180,12 @@ def test_install_license_rejects_invalid_text(tmp_path):
def test_activate_license_code_installs_server_license(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
source_path = tmp_path / "server_license.dat"
target_path = tmp_path / "license.dat"
create_dev_license(
_create_test_license(
output_path=source_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
@@ -208,9 +218,9 @@ def test_activate_license_code_installs_server_license(tmp_path, monkeypatch):
def test_plain_card_uses_default_activation_url(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
monkeypatch.setattr(
license_manager,
"get_default_activation_url",
@@ -218,7 +228,7 @@ def test_plain_card_uses_default_activation_url(tmp_path, monkeypatch):
)
source_path = tmp_path / "server_license.dat"
target_path = tmp_path / "license.dat"
create_dev_license(
_create_test_license(
output_path=source_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
@@ -245,11 +255,11 @@ def test_plain_card_uses_default_activation_url(tmp_path, monkeypatch):
def test_license_status_public_payload_hides_signature(tmp_path, monkeypatch):
from douyin.license import license_manager
from tools.license_dev import create_dev_license
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
license_path = tmp_path / "license.dat"
create_dev_license(
_create_test_license(
output_path=license_path,
machine_fingerprint="machine-a",
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
+87 -14
View File
@@ -5,10 +5,11 @@ def test_license_server_api_activates_card(tmp_path):
from fastapi.testclient import TestClient
from license_server import api
from license_server.core import DEV_PRIVATE_KEY_PEM, LicenseService
from license_server.core import LicenseService
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
db_path = tmp_path / "license.db"
service = LicenseService(db_path=db_path, private_key_pem=DEV_PRIVATE_KEY_PEM)
service = LicenseService(db_path=db_path, private_key_pem=TEST_PRIVATE_KEY_PEM)
card = service.create_cards(days=5, count=1, customer="api-user")[0]
client = TestClient(api.create_app(service=service))
@@ -31,9 +32,10 @@ def test_license_server_api_rejects_unknown_card(tmp_path):
from fastapi.testclient import TestClient
from license_server import api
from license_server.core import DEV_PRIVATE_KEY_PEM, LicenseService
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=DEV_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))
resp = client.post(
@@ -49,9 +51,10 @@ def test_admin_page_requires_token(tmp_path):
from fastapi.testclient import TestClient
from license_server import api
from license_server.core import DEV_PRIVATE_KEY_PEM, LicenseService
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=DEV_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, admin_token="secret"))
resp = client.get("/admin")
@@ -63,18 +66,26 @@ def test_admin_page_can_create_and_revoke_card(tmp_path):
from fastapi.testclient import TestClient
from license_server import api
from license_server.core import DEV_PRIVATE_KEY_PEM, LicenseService
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=DEV_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, admin_token="secret"))
created = client.post(
"/admin/create?token=secret",
data={"days": "8", "count": "1", "customer": "web-admin"},
"/admin/create",
auth=("admin", "secret"),
cookies={api.ADMIN_CSRF_COOKIE: "csrf-token"},
data={"csrf_token": "csrf-token", "days": "8", "count": "1", "customer": "web-admin"},
)
card = created.text.split("DG-", 1)[1].split("<", 1)[0]
card = "DG-" + card.strip()
revoked = client.post("/admin/revoke?token=secret", data={"card_key": card})
revoked = client.post(
"/admin/revoke",
auth=("admin", "secret"),
cookies={api.ADMIN_CSRF_COOKIE: "csrf-token"},
data={"csrf_token": "csrf-token", "card_key": card},
)
assert created.status_code == 200
assert "web-admin" in created.text
@@ -88,16 +99,17 @@ def test_admin_page_shows_stats_cards_and_audit_logs(tmp_path):
from fastapi.testclient import TestClient
from license_server import api
from license_server.core import DEV_PRIVATE_KEY_PEM, LicenseService
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=DEV_PRIVATE_KEY_PEM)
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
active = service.create_cards(days=5, count=1, customer="active-user")[0]
revoked = service.create_cards(days=2, count=1, customer="revoked-user")[0]
service.activate(active.code, machine_fingerprint="machine-a")
service.revoke(revoked.code)
client = TestClient(api.create_app(service=service, admin_token="secret"))
resp = client.get("/admin?token=secret")
resp = client.get("/admin", auth=("admin", "secret"))
assert resp.status_code == 200
body = resp.text
@@ -109,3 +121,64 @@ def test_admin_page_shows_stats_cards_and_audit_logs(tmp_path):
assert "最近审计日志" in body
assert "activate" in body
assert "revoke" in body
def test_admin_query_token_is_rejected(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, admin_token="secret"))
resp = client.get("/admin?token=secret")
assert resp.status_code == 403
def test_admin_create_requires_csrf(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, admin_token="secret"))
resp = client.post(
"/admin/create",
auth=("admin", "secret"),
data={"days": "8", "count": "1", "customer": "web-admin"},
)
assert resp.status_code == 200
assert "CSRF 校验失败" in resp.text
def test_api_rate_limits_repeated_failed_activation(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.setattr(api, "RATE_LIMIT_MAX_REQUESTS", 2)
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
client = TestClient(api.create_app(service=service))
for _ in range(2):
assert client.post(
"/api/v1/activate",
json={"card_key": "DG-NOPE-NOPE-NOPE", "machine_fingerprint": "machine-api"},
).status_code == 404
limited = client.post(
"/api/v1/activate",
json={"card_key": "DG-NOPE-NOPE-NOPE", "machine_fingerprint": "machine-api"},
)
assert limited.status_code == 429
assert limited.json()["reason"] == "rate_limited"
+9 -4
View File
@@ -6,10 +6,12 @@ from datetime import datetime, timezone
def test_license_server_creates_and_activates_card(tmp_path, monkeypatch):
from douyin.license import license_manager
from license_server import core
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM, TEST_PUBLIC_KEY_PEM
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
monkeypatch.setattr(license_manager, "PUBLIC_KEY_PEM", TEST_PUBLIC_KEY_PEM)
db_path = tmp_path / "license.db"
service = core.LicenseService(db_path=db_path, private_key_pem=core.DEV_PRIVATE_KEY_PEM)
service = core.LicenseService(db_path=db_path, private_key_pem=TEST_PRIVATE_KEY_PEM)
card = service.create_cards(days=30, count=1, customer="alice")[0]
result = service.activate(card.code, machine_fingerprint="machine-a")
@@ -24,8 +26,9 @@ def test_license_server_creates_and_activates_card(tmp_path, monkeypatch):
def test_license_server_rejects_revoked_card(tmp_path):
from license_server import core
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
service = core.LicenseService(db_path=tmp_path / "license.db", private_key_pem=core.DEV_PRIVATE_KEY_PEM)
service = core.LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
card = service.create_cards(days=7, count=1)[0]
service.revoke(card.code)
@@ -37,8 +40,9 @@ def test_license_server_rejects_revoked_card(tmp_path):
def test_license_server_verify_reports_bound_activation(tmp_path):
from license_server import core
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
service = core.LicenseService(db_path=tmp_path / "license.db", private_key_pem=core.DEV_PRIVATE_KEY_PEM)
service = core.LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
card = service.create_cards(days=3, count=1, customer="bob")[0]
service.activate(card.code, machine_fingerprint="machine-b")
@@ -52,8 +56,9 @@ def test_license_server_verify_reports_bound_activation(tmp_path):
def test_license_server_verify_rejects_other_machine(tmp_path):
from license_server import core
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
service = core.LicenseService(db_path=tmp_path / "license.db", private_key_pem=core.DEV_PRIVATE_KEY_PEM)
service = core.LicenseService(db_path=tmp_path / "license.db", private_key_pem=TEST_PRIVATE_KEY_PEM)
card = service.create_cards(days=3, count=1)[0]
service.activate(card.code, machine_fingerprint="machine-a")
+14 -2
View File
@@ -3,6 +3,12 @@ from __future__ import annotations
def test_manage_create_outputs_cards(tmp_path, capsys):
from license_server import manage
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
def fake_private_key() -> bytes:
return TEST_PRIVATE_KEY_PEM
manage.load_private_key_pem = fake_private_key
rc = manage.main(
[
@@ -27,10 +33,16 @@ def test_manage_create_outputs_cards(tmp_path, capsys):
def test_manage_revoke_disables_card(tmp_path, capsys):
from license_server import manage
from license_server.core import DEV_PRIVATE_KEY_PEM, LicenseService
from license_server.core import LicenseService
from tests.license_test_keys import TEST_PRIVATE_KEY_PEM
def fake_private_key() -> bytes:
return TEST_PRIVATE_KEY_PEM
manage.load_private_key_pem = fake_private_key
db_path = tmp_path / "license.db"
service = LicenseService(db_path=db_path, private_key_pem=DEV_PRIVATE_KEY_PEM)
service = LicenseService(db_path=db_path, private_key_pem=TEST_PRIVATE_KEY_PEM)
card = service.create_cards(days=1, count=1)[0]
rc = manage.main(["--db", str(db_path), "revoke", card.code])