security: harden license server and packaging checks
This commit is contained in:
1 parent
9062ee7204
commit
d0166d27d3
5 files changed
+160
-14
No files matched your search
@@ -46,6 +46,34 @@ def test_distribution_manifest_blocks_source_and_debug_artifacts():
|
||||
assert "_internal/cv2/__init__.py" not in blocked
|
||||
|
||||
|
||||
def test_distribution_manifest_blocks_project_native_binaries_outside_core_allowlist():
|
||||
from tools.distribution_manifest import find_distribution_issues
|
||||
|
||||
entries = [
|
||||
"DouyinGuajiGUI.exe",
|
||||
"web_gui.exe",
|
||||
"run_cli.exe",
|
||||
"_internal/base_library.zip",
|
||||
"_internal/python312.dll",
|
||||
"_internal/tests/evil.pyd",
|
||||
"_internal/tools/helper.dll",
|
||||
"_internal/douyin/fudai/core/compiled.cp312-win_amd64.pyd",
|
||||
"_internal/numpy/core/runtime.pyd",
|
||||
"webui/index.html",
|
||||
"config/README.md",
|
||||
"logs/.keep",
|
||||
"pic/.keep",
|
||||
"state/.keep",
|
||||
]
|
||||
|
||||
issues = find_distribution_issues(entries)
|
||||
|
||||
assert "_internal/tests/evil.pyd" in issues
|
||||
assert "_internal/tools/helper.dll" in issues
|
||||
assert "_internal/douyin/fudai/core/compiled.cp312-win_amd64.pyd" not in issues
|
||||
assert "_internal/numpy/core/runtime.pyd" not in issues
|
||||
|
||||
|
||||
def test_distribution_manifest_allows_expected_runtime_shape():
|
||||
from tools.distribution_manifest import find_distribution_issues
|
||||
|
||||
|
||||
@@ -261,6 +261,74 @@ def test_admin_login_rejects_wrong_token(tmp_path):
|
||||
assert "管理员 token 不正确" in resp.text
|
||||
|
||||
|
||||
def test_admin_login_is_rate_limited(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, "ADMIN_LOGIN_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, admin_token="secret"))
|
||||
|
||||
for _ in range(2):
|
||||
assert client.post("/admin/login", data={"admin_token": "wrong"}).status_code == 403
|
||||
|
||||
limited = client.post("/admin/login", data={"admin_token": "wrong"})
|
||||
|
||||
assert limited.status_code == 429
|
||||
assert "登录尝试过于频繁" in limited.text
|
||||
|
||||
|
||||
def test_license_server_secure_defaults_and_forwarded_for_policy(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", 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"))
|
||||
|
||||
assert client.get("/docs").status_code == 404
|
||||
first = client.post(
|
||||
"/api/v1/activate",
|
||||
headers={"x-forwarded-for": "198.51.100.1"},
|
||||
json={"card_key": "DG-NOPE-NOPE-NOPE", "machine_fingerprint": "machine-api"},
|
||||
)
|
||||
second = client.post(
|
||||
"/api/v1/activate",
|
||||
headers={"x-forwarded-for": "198.51.100.2"},
|
||||
json={"card_key": "DG-NOPE-NOPE-NOPE", "machine_fingerprint": "machine-api"},
|
||||
)
|
||||
|
||||
assert first.status_code == 404
|
||||
assert second.status_code == 429
|
||||
|
||||
|
||||
def test_license_server_can_enable_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_SECURE_COOKIES", "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"),
|
||||
base_url="https://testserver",
|
||||
)
|
||||
|
||||
login = client.post("/admin/login", data={"admin_token": "secret"}, follow_redirects=False)
|
||||
csrf = client.get("/admin")
|
||||
|
||||
assert "secure" in login.headers["set-cookie"].lower()
|
||||
assert "secure" in csrf.headers["set-cookie"].lower()
|
||||
|
||||
|
||||
def test_admin_logout_clears_session(tmp_path):
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
|
||||
Reference in new issue
Block a user