|
| 1 | +# type: ignore |
| 2 | + |
| 3 | +import pytest |
| 4 | +from utils import Request |
| 5 | + |
| 6 | +from fastapi_sso.sso.base import DiscoveryDocument, OpenID, SSOBase, SSOLoginError |
| 7 | +from fastapi_sso.sso.google import GoogleSSO |
| 8 | + |
| 9 | + |
| 10 | +class FakeSSO(SSOBase): |
| 11 | + provider = "fake" |
| 12 | + |
| 13 | + async def get_discovery_document(self) -> DiscoveryDocument: |
| 14 | + return { |
| 15 | + "authorization_endpoint": "https://fake.com/authorize", |
| 16 | + "token_endpoint": "https://fake.com/token", |
| 17 | + "userinfo_endpoint": "https://fake.com/userinfo", |
| 18 | + } |
| 19 | + |
| 20 | + async def openid_from_response(self, response: dict, session=None) -> OpenID: |
| 21 | + return OpenID(id="fake-id", provider=self.provider) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture() |
| 25 | +def sso(monkeypatch: pytest.MonkeyPatch) -> FakeSSO: |
| 26 | + async def fake_process_login(self, code, request, **kwargs): |
| 27 | + return "logged-in" |
| 28 | + |
| 29 | + monkeypatch.setattr(SSOBase, "process_login", fake_process_login) |
| 30 | + return FakeSSO("client_id", "client_secret", redirect_uri="https://localhost/callback") |
| 31 | + |
| 32 | + |
| 33 | +def callback(state: str | None = None, cookie: str | None = None) -> Request: |
| 34 | + request = Request(cookies={"sso_state": cookie} if cookie is not None else None) |
| 35 | + request.query_params["code"] = "code" |
| 36 | + if state is not None: |
| 37 | + request.query_params["state"] = state |
| 38 | + return request |
| 39 | + |
| 40 | + |
| 41 | +@pytest.mark.parametrize("provider", [SSOBase, GoogleSSO]) |
| 42 | +def test_state_is_required_by_default(provider: type[SSOBase]): |
| 43 | + assert provider("client_id", "client_secret").requires_state is True |
| 44 | + |
| 45 | + |
| 46 | +async def test_callback_without_state_is_rejected(sso: FakeSSO): |
| 47 | + async with sso: |
| 48 | + with pytest.raises(SSOLoginError, match="'state' parameter was not found"): |
| 49 | + await sso.verify_and_process(callback()) |
| 50 | + |
| 51 | + |
| 52 | +async def test_callback_with_state_but_no_cookie_is_rejected(sso: FakeSSO): |
| 53 | + async with sso: |
| 54 | + with pytest.raises(SSOLoginError, match="State cookie not found"): |
| 55 | + await sso.verify_and_process(callback(state="attacker-state")) |
| 56 | + |
| 57 | + |
| 58 | +async def test_callback_with_mismatched_cookie_is_rejected(sso: FakeSSO): |
| 59 | + async with sso: |
| 60 | + with pytest.raises(SSOLoginError, match="Invalid state"): |
| 61 | + await sso.verify_and_process(callback(state="attacker-state", cookie="victim-state")) |
| 62 | + |
| 63 | + |
| 64 | +async def test_callback_with_non_ascii_state_is_rejected(sso: FakeSSO): |
| 65 | + async with sso: |
| 66 | + with pytest.raises(SSOLoginError, match="Invalid state"): |
| 67 | + await sso.verify_and_process(callback(state="státe", cookie="state")) |
| 68 | + |
| 69 | + |
| 70 | +async def test_callback_with_matching_cookie_is_accepted(sso: FakeSSO): |
| 71 | + async with sso: |
| 72 | + assert await sso.verify_and_process(callback(state="state", cookie="state")) == "logged-in" |
| 73 | + |
| 74 | + |
| 75 | +async def test_state_validation_can_be_opted_out_of(sso: FakeSSO): |
| 76 | + sso.requires_state = False |
| 77 | + async with sso: |
| 78 | + assert await sso.verify_and_process(callback()) == "logged-in" |
| 79 | + |
| 80 | + |
| 81 | +async def test_login_redirect_sets_hardened_state_cookie(sso: FakeSSO): |
| 82 | + async with sso: |
| 83 | + response = await sso.get_login_redirect() |
| 84 | + cookie = response.headers["set-cookie"] |
| 85 | + assert f"sso_state={sso._generated_state}" in cookie |
| 86 | + assert "HttpOnly" in cookie |
| 87 | + assert "Secure" in cookie |
| 88 | + assert "SameSite=lax" in cookie |
| 89 | + |
| 90 | + |
| 91 | +async def test_state_cookie_is_not_secure_over_insecure_http(monkeypatch: pytest.MonkeyPatch): |
| 92 | + monkeypatch.delenv("OAUTHLIB_INSECURE_TRANSPORT", raising=False) |
| 93 | + sso = FakeSSO("client_id", "client_secret", redirect_uri="http://localhost/callback", allow_insecure_http=True) |
| 94 | + async with sso: |
| 95 | + response = await sso.get_login_redirect() |
| 96 | + assert "Secure" not in response.headers["set-cookie"] |
0 commit comments