diff --git a/HISTORY.rst b/HISTORY.rst index 75e7a307..1e3c096b 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -7,6 +7,12 @@ This project adheres to `Semantic Versioning `_. Unreleased =========================== +* **Security fix.** A valid API token bypassed ``EXPLORER_PERMISSION_VIEW`` entirely on + ``/download`` and ``/stream``, so any token holder could read *any* saved + query by walking ids, not just the ones shared with them. When ``EXPLORER_USER_QUERY_VIEWS`` is + configured, a token is now limited to the queries listed there. Installs that do not configure + it keep the previous behaviour, which is what the token-as-data-API documentation describes. + * `#722`_: The query list (home page) no longer loads the entire query log history on every view. Run counts and last runs are computed in SQL, so the page stays fast as logs accumulate (300k logs: ~9.7s -> ~0.9s). The query list and query log pages also no longer run an extra database query per row. diff --git a/explorer/permissions.py b/explorer/permissions.py index 02417a7e..970951f5 100644 --- a/explorer/permissions.py +++ b/explorer/permissions.py @@ -2,13 +2,42 @@ from explorer.utils import allowed_query_pks, user_can_see_query +def _token_supplied(request): + """ + Whether the request carries the configured API token. + + The token is a server-wide credential, not a user, so it says nothing about which + queries the caller is allowed to reach. That is decided by token_can_see_query. + """ + return app_settings.EXPLORER_TOKEN_AUTH_ENABLED() and ( + request.headers.get("X-Api-Token") == app_settings.EXPLORER_TOKEN + or request.GET.get("token") == app_settings.EXPLORER_TOKEN + ) + + +def token_can_see_query(query_id): + """ + Scope a valid token to the queries it is meant to reach. + + Token auth exists so a script can pull results for queries that have been shared + with it. When ``EXPLORER_USER_QUERY_VIEWS`` is configured, that is the list of + shareable queries, so the token is limited to it -- otherwise any token holder + could read every query by walking query ids. When it is not configured there is no + such list, and the token keeps the documented access to any query. + """ + if query_id is None: + return True + allowed = set(sum(app_settings.EXPLORER_GET_USER_QUERY_VIEWS().values(), [])) + if not allowed: + return True + return int(query_id) in allowed + + def view_permission(request, **kwargs): return app_settings.EXPLORER_PERMISSION_VIEW(request)\ or user_can_see_query(request, **kwargs)\ - or (app_settings.EXPLORER_TOKEN_AUTH_ENABLED() - and (request.headers.get("X-Api-Token") == - app_settings.EXPLORER_TOKEN - or request.GET.get("token") == app_settings.EXPLORER_TOKEN)) + or (_token_supplied(request) + and token_can_see_query(kwargs.get("query_id"))) # Users who can only see some queries can still see the list. # Different than the above because it's not checking for any specific diff --git a/explorer/tests/test_views.py b/explorer/tests/test_views.py index 87d94354..a82b5bc7 100644 --- a/explorer/tests/test_views.py +++ b/explorer/tests/test_views.py @@ -436,6 +436,67 @@ def test_multiple_connections_integration(self): self.assertContains(resp, "superchicken") +class TestTokenAuthScope(TestCase): + """ + A valid API token authenticates a caller, but must not widen which queries it can read. + """ + + def test_token_reaches_shared_queries(self): + shared = SimpleQueryFactory(sql="select 'shared';") + with self.settings(EXPLORER_TOKEN_AUTH_ENABLED=True, + EXPLORER_USER_QUERY_VIEWS={99: [shared.id]}): + resp = self.client.get( + reverse("download_query", kwargs={"query_id": shared.id}), + **{"HTTP_X_API_TOKEN": EXPLORER_TOKEN} + ) + self.assertEqual(200, resp.status_code) + self.assertIn("shared", resp.content.decode()) + + def test_token_cannot_reach_unshared_queries(self): + shared = SimpleQueryFactory(sql="select 'shared';") + private = SimpleQueryFactory(sql="select 'private';") + with self.settings(EXPLORER_TOKEN_AUTH_ENABLED=True, + EXPLORER_USER_QUERY_VIEWS={99: [shared.id]}): + resp = self.client.get( + reverse("download_query", kwargs={"query_id": private.id}), + **{"HTTP_X_API_TOKEN": EXPLORER_TOKEN} + ) + self.assertNotContains(resp, "private") + + def test_token_cannot_stream_unshared_queries(self): + shared = SimpleQueryFactory(sql="select 'shared';") + private = SimpleQueryFactory(sql="select 'private';") + with self.settings(EXPLORER_TOKEN_AUTH_ENABLED=True, + EXPLORER_USER_QUERY_VIEWS={99: [shared.id]}): + resp = self.client.get( + reverse("stream_query", kwargs={"query_id": private.id}), + HTTP_X_API_TOKEN=EXPLORER_TOKEN + ) + self.assertNotContains(resp, "private") + + def test_wrong_token_is_rejected(self): + q = SimpleQueryFactory(sql="select 'shared';") + with self.settings(EXPLORER_TOKEN_AUTH_ENABLED=True, + EXPLORER_USER_QUERY_VIEWS={99: [q.id]}): + resp = self.client.get( + reverse("download_query", kwargs={"query_id": q.id}), + HTTP_X_API_TOKEN="wrong-token" + ) + self.assertNotContains(resp, "shared") + + def test_token_is_unscoped_when_no_shared_queries_configured(self): + # Documented behaviour for installs using the token as a data API: with no + # EXPLORER_USER_QUERY_VIEWS there is nothing to scope the token to. + q = SimpleQueryFactory(sql="select 'anything';") + with self.settings(EXPLORER_TOKEN_AUTH_ENABLED=True, + EXPLORER_USER_QUERY_VIEWS={}): + resp = self.client.get( + reverse("stream_query", kwargs={"query_id": q.id}), + HTTP_X_API_TOKEN=EXPLORER_TOKEN + ) + self.assertEqual(200, resp.status_code) + + class TestDownloadView(TestCase): def setUp(self): self.query = SimpleQueryFactory(sql="select 1;")