|
| 1 | +""" |
| 2 | +Pins the search surface's route gates to READ_SEARCH so a read:search-scoped |
| 3 | +PAT can reach every endpoint the scope's description promises ("Use search and |
| 4 | +query endpoints"). Scoped PATs can never satisfy BASIC_ACCESS (implication |
| 5 | +only flows basic -> read:search), so a route in this surface regressing to |
| 6 | +BASIC_ACCESS silently locks out scoped tokens while unscoped ones still work. |
| 7 | +""" |
| 8 | + |
| 9 | +import inspect |
| 10 | +from collections.abc import Callable |
| 11 | +from typing import Any |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | +from ee.onyx.server.query_and_chat.search_backend import ( |
| 16 | + get_search_history, |
| 17 | + handle_send_search_message, |
| 18 | + search_flow_classification, |
| 19 | +) |
| 20 | +from onyx.db.enums import Permission |
| 21 | +from onyx.server.features.web_search.api import ( |
| 22 | + execute_open_urls, |
| 23 | + execute_web_search, |
| 24 | + execute_web_search_lite, |
| 25 | +) |
| 26 | +from onyx.server.query_and_chat.query_backend import get_tags |
| 27 | + |
| 28 | +_SEARCH_SURFACE_HANDLERS: list[Callable[..., Any]] = [ |
| 29 | + handle_send_search_message, |
| 30 | + get_search_history, |
| 31 | + search_flow_classification, |
| 32 | + get_tags, |
| 33 | + execute_web_search, |
| 34 | + execute_web_search_lite, |
| 35 | + execute_open_urls, |
| 36 | +] |
| 37 | + |
| 38 | + |
| 39 | +def _permission_gates(handler: Callable[..., Any]) -> list[Permission | None]: |
| 40 | + gates: list[Permission | None] = [] |
| 41 | + for param in inspect.signature(handler).parameters.values(): |
| 42 | + dependency = getattr(param.default, "dependency", None) |
| 43 | + if dependency is not None and getattr( |
| 44 | + dependency, "_is_require_permission", False |
| 45 | + ): |
| 46 | + gates.append(getattr(dependency, "_required_permission", None)) |
| 47 | + return gates |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize("handler", _SEARCH_SURFACE_HANDLERS, ids=lambda h: h.__name__) |
| 51 | +def test_search_surface_route_admits_read_search_scope( |
| 52 | + handler: Callable[..., Any], |
| 53 | +) -> None: |
| 54 | + gates = _permission_gates(handler) |
| 55 | + assert gates == [Permission.READ_SEARCH] |
0 commit comments