Files
MoviePilot/tests/test_factory_cors.py
freeman 0cfb292cef fix(api): disable CORS credentials for wildcard origins
ALLOWED_HOSTS defaults to ["*"] and the CORS middleware always set
allow_credentials=True. Starlette echoes any request Origin with
Access-Control-Allow-Credentials in that combination, which lets every
site make credentialed cross-origin calls. resolve_cors_policy now
returns credentials=False for wildcard origins and keeps credentials
only when explicit origins are configured.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-09 11:39:12 +08:00

66 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""CORS 中间件在通配来源与显式来源下的凭据策略契约测试。"""
import pytest
from fastapi.testclient import TestClient
from app.factory import create_app, resolve_cors_policy
from app.runtime.config import settings
LOGIN_PATH = "/api/v1/login/access-token"
def _preflight(client: TestClient, origin: str):
"""发送一次登录接口的 CORS 预检请求。"""
return client.options(
LOGIN_PATH,
headers={
"Origin": origin,
"Access-Control-Request-Method": "POST",
"Access-Control-Request-Headers": "content-type",
},
)
@pytest.mark.parametrize(
("allowed_hosts", "expected"),
[
(None, (["*"], False)),
([], (["*"], False)),
(["*"], (["*"], False)),
(["*", "https://mp.example"], (["*"], False)),
(["https://mp.example"], (["https://mp.example"], True)),
([" https://mp.example ", "", "https://b.example"], (["https://mp.example", "https://b.example"], True)),
],
)
def test_resolve_cors_policy_disables_credentials_for_wildcard(allowed_hosts, expected):
"""通配来源一律关闭凭据,显式来源才允许携带凭据。"""
assert resolve_cors_policy(allowed_hosts) == expected
def test_wildcard_origins_do_not_allow_credentials(monkeypatch: pytest.MonkeyPatch) -> None:
"""默认通配配置下预检返回 *,且不声明允许凭据。"""
monkeypatch.setattr(settings, "ALLOWED_HOSTS", ["*"])
client = TestClient(create_app())
response = _preflight(client, "https://evil.example")
assert response.status_code == 200
assert response.headers["access-control-allow-origin"] == "*"
assert "access-control-allow-credentials" not in response.headers
def test_explicit_origins_echo_origin_with_credentials(monkeypatch: pytest.MonkeyPatch) -> None:
"""显式来源命中时回显 Origin 并允许携带凭据,未列出的来源不返回 CORS 头。"""
monkeypatch.setattr(settings, "ALLOWED_HOSTS", ["https://mp.example"])
client = TestClient(create_app())
allowed = _preflight(client, "https://mp.example")
assert allowed.status_code == 200
assert allowed.headers["access-control-allow-origin"] == "https://mp.example"
assert allowed.headers["access-control-allow-credentials"] == "true"
# Starlette 对未列出的来源拒绝预检:400 且不回显 allow-origin,浏览器据此阻止请求
denied = _preflight(client, "https://evil.example")
assert denied.status_code == 400
assert "access-control-allow-origin" not in denied.headers