From cd4ee814e5623f91ded3235d2723398630548509 Mon Sep 17 00:00:00 2001 From: Chris Clark Date: Thu, 29 Oct 2026 08:59:09 -0400 Subject: [PATCH] Scope API tokens to the queries shared with them view_permission() ORed the permission predicate together with the token check, and DownloadQueryView and StreamQueryView both use view_permission. A valid token therefore satisfied the permission check on its own, so any token holder could read any saved query -- /explorer/1/download, /explorer/2/download, ... -- rather than only the queries the token was issued for. EXPLORER_USER_QUERY_VIEWS, the documented mechanism for sharing selected queries, was not consulted. The token is a server-wide credential rather than a user, so it can only ever say "this caller is allowed to use the API", never "and nothing about which queries". Token access now goes through token_can_see_query, which limits it to the queries in EXPLORER_USER_QUERY_VIEWS when that setting is configured. Installs that do not configure EXPLORER_USER_QUERY_VIEWS have no shareable-queries list to scope against, so they keep the documented behaviour of tokens reaching any query id. That keeps existing token-as-data-API integrations working; the bypass above is closed for every install that does use the feature. --- HISTORY.rst | 6 ++++ explorer/permissions.py | 37 +++++++++++++++++++--- explorer/tests/test_views.py | 61 ++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 4 deletions(-) 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;")