Skip to content

Scope API tokens to the queries shared with them - #724

Open
chrisclark wants to merge 1 commit into
masterfrom
fix/token-permission-scope
Open

chrisclark wants to merge 1 commit into
masterfrom
fix/token-permission-scope

Conversation

@chrisclark

Copy link
Copy Markdown
Collaborator

Summary

Fixes the API token bypassing the permission check. High.

view_permission() ORed the permission predicate together with the token check:

return app_settings.EXPLORER_PERMISSION_VIEW(request) \
    or user_can_see_query(request, **kwargs) \
    or (EXPLORER_TOKEN_AUTH_ENABLED() and request.headers.get("X-Api-Token") == EXPLORER_TOKEN)

DownloadQueryView and StreamQueryView both use view_permission, so a valid token satisfied the permission check on its own. Any token holder could read any saved query by walking ids (/explorer/1/download, /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 on these paths at all.

Worth noting explorer/permissions.py already documents "token auth does not give you permission to view the list" for view_permission_list — the per-query paths were missing that same treatment.

Fix

A token is a server-wide credential rather than a user, so it can only ever establish "this caller may use the API", never "and here is what they may read." 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.

Backwards compatibility was an explicit decision here. Installs that do not configure EXPLORER_USER_QUERY_VIEWS have no shareable-queries list to scope against, so they keep the documented behaviour of a token reaching any query id. That keeps existing token-as-data-API integrations working, while the bypass is closed for every install that actually uses the sharing feature. If you would prefer the strict version (token must pass the same per-query check as users, breaking any integration that relies on unscoped access), it is a one-line change in token_can_see_query — say the word.

Tests

New TestTokenAuthScope class:

  • token reaches a shared query;
  • token cannot download a query it was not given (asserts the results are absent, since denial renders the login page with a 200);
  • same for the streaming endpoint;
  • a wrong token is still rejected;
  • unscoped behaviour is preserved when EXPLORER_USER_QUERY_VIEWS is empty.

Full suite: 300 tests, OK. ruff check explorer/: clean.

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant