askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
test_cross_origin_cookie_global_refusal.py · 3.2 KB · 77 lines
| 1 | """A cross-origin request that carries only the session cookie is refused by |
| 2 | app() for every route, before any handler runs. These handlers each had a |
| 3 | check of their own that could therefore never run; the tests pin the global |
| 4 | refusal (status and body) and that the handler is not reached.""" |
| 5 | |
| 6 | import json |
| 7 | |
| 8 | import pytest |
| 9 | |
| 10 | import object_server |
| 11 | |
| 12 | from test_object_server import create_identity_session, enable_admin_token, request |
| 13 | |
| 14 | |
| 15 | HOST = "desk.example.com" |
| 16 | REFUSAL = {"status": "error", "error": "Cross-origin cookie request rejected"} |
| 17 | JSON_BODY = json.dumps({"name": "laptop"}).encode() |
| 18 | |
| 19 | # (handler, method, path, body) |
| 20 | ROUTES = [ |
| 21 | ("_handle_identity_user_api_keys", "POST", "/identity/users/dan/api-keys", JSON_BODY), |
| 22 | ("_handle_identity_user_api_keys", "PUT", "/identity/users/dan/api-keys/k1", JSON_BODY), |
| 23 | ("_handle_identity_user_api_keys", "DELETE", "/identity/users/dan/api-keys/k1", b""), |
| 24 | ("_handle_identity_user_service_keys", "PUT", "/identity/users/dan/service-keys/openai", JSON_BODY), |
| 25 | ("_handle_identity_user_service_keys", "POST", "/identity/users/dan/service-keys/openai", JSON_BODY), |
| 26 | ("_handle_identity_user_service_keys", "DELETE", "/identity/users/dan/service-keys/openai", b""), |
| 27 | ("_handle_user_file_upload", "POST", "/api/files", b"not multipart"), |
| 28 | ("_handle_ai_chat", "POST", "/api/ai/chat", b"{}"), |
| 29 | ("_handle_tts", "POST", "/api/tts", b"{}"), |
| 30 | ("_handle_stt", "POST", "/api/stt", b""), |
| 31 | ("_handle_read", "POST", "/api/read", b"{}"), |
| 32 | ] |
| 33 | |
| 34 | |
| 35 | @pytest.mark.parametrize( |
| 36 | "handler, method, path, body", ROUTES, ids=[f"{method} {path}" for _, method, path, _ in ROUTES] |
| 37 | ) |
| 38 | def test_a_cross_origin_cookie_request_is_refused_by_the_global_check( |
| 39 | tmp_path, monkeypatch, handler, method, path, body |
| 40 | ): |
| 41 | monkeypatch.setenv(object_server.DATA_DIR_ENV, str(tmp_path / "data")) |
| 42 | # Every feature on, so nothing but the origin stands between the request |
| 43 | # and the handler's work. |
| 44 | for flag in ( |
| 45 | object_server.USER_FILES_ENABLED_ENV, |
| 46 | object_server.AI_CHAT_ENABLED_ENV, |
| 47 | object_server.TTS_ENABLED_ENV, |
| 48 | object_server.STT_ENABLED_ENV, |
| 49 | object_server.READER_ENABLED_ENV, |
| 50 | ): |
| 51 | monkeypatch.setenv(flag, "true") |
| 52 | enable_admin_token(monkeypatch) |
| 53 | token, _ = create_identity_session({"user_id": "dan"}) |
| 54 | cookie = ("cookie", f"dbbasic_session={token}") |
| 55 | |
| 56 | reached = [] |
| 57 | real = getattr(object_server, handler) |
| 58 | |
| 59 | async def spy(*args, **kwargs): |
| 60 | reached.append(handler) |
| 61 | return await real(*args, **kwargs) |
| 62 | |
| 63 | monkeypatch.setattr(object_server, handler, spy) |
| 64 | |
| 65 | for origin in (("origin", "https://evil.example.com"), ("referer", "https://evil.example.com/page")): |
| 66 | status, _, payload = request(path, method=method, body=body, headers=[cookie, ("host", HOST), origin]) |
| 67 | assert status == 403 |
| 68 | assert payload == REFUSAL |
| 69 | assert reached == [] |
| 70 | |
| 71 | # The same cookie from the box's own origin is the signed-in user at work: |
| 72 | # the handler runs and gives its own answer. |
| 73 | status, _, payload = request( |
| 74 | path, method=method, body=body, headers=[cookie, ("host", HOST), ("origin", f"https://{HOST}")] |
| 75 | ) |
| 76 | assert reached == [handler] |
| 77 | assert payload != REFUSAL |